2017-07-31 22:53:42 -07:00
|
|
|
package org.nwapw.abacus.fx;
|
|
|
|
|
|
|
|
import javafx.scene.control.TableCell;
|
|
|
|
import javafx.scene.input.MouseEvent;
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.datatransfer.StringSelection;
|
|
|
|
|
2017-08-01 10:47:31 -07:00
|
|
|
/**
|
|
|
|
* A cell that copies its value to the clipboard
|
|
|
|
* when double clicked.
|
2017-08-04 13:20:57 -07:00
|
|
|
*
|
2017-08-01 10:47:31 -07:00
|
|
|
* @param <S> The type of the table view generic type.
|
|
|
|
* @param <T> The type of the value contained in the cell.
|
|
|
|
*/
|
2017-07-31 22:53:42 -07:00
|
|
|
public class CopyableCell<S, T> extends TableCell<S, T> {
|
|
|
|
|
2017-08-01 10:47:31 -07:00
|
|
|
/**
|
|
|
|
* Creates a new copyable cell.
|
|
|
|
*/
|
2017-08-04 13:20:57 -07:00
|
|
|
public CopyableCell() {
|
2017-07-31 22:53:42 -07:00
|
|
|
addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
|
2017-08-04 13:20:57 -07:00
|
|
|
if (event.getClickCount() == 2) {
|
2017-07-31 22:53:42 -07:00
|
|
|
Toolkit.getDefaultToolkit().getSystemClipboard()
|
|
|
|
.setContents(new StringSelection(getText()), null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void updateItem(T item, boolean empty) {
|
|
|
|
super.updateItem(item, empty);
|
|
|
|
setText((empty || item == null) ? null : item.toString());
|
|
|
|
setGraphic(null);
|
|
|
|
}
|
|
|
|
}
|