modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/CellBehaviorBase.java
Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization
@@ -29,11 +29,11 @@
import javafx.scene.control.Control;
import javafx.scene.control.FocusModel;
import javafx.scene.control.IndexedCell;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.control.SelectionMode;
-import javafx.scene.input.ContextMenuEvent;
+import com.sun.javafx.scene.control.inputmap.InputMap;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import java.util.ArrayList;
import java.util.List;
@@ -99,10 +99,12 @@
* *
* Private fields *
* *
**************************************************************************/
+ private final InputMap<T> cellInputMap;
+
// To support touch devices, we have to slightly modify this behavior, such
// that selection only happens on mouse release, if only minimal dragging
// has occurred.
private boolean latePress = false;
@@ -112,12 +114,25 @@
* *
* Constructors *
* *
**************************************************************************/
- public CellBehaviorBase(T control, List<KeyBinding> bindings) {
- super(control, bindings);
+ public CellBehaviorBase(T control) {
+ super(control);
+
+ // create a map for cell-specific mappings (this reuses the default
+ // InputMap installed on the control, if it is non-null, allowing us to pick up any user-specified mappings)
+ cellInputMap = createInputMap();
+
+ // TODO add focus traversal mappings (?)
+ // addDefaultMapping(cellInputMap, FocusTraversalInputMap.getFocusTraversalMappings());
+
+ addDefaultMapping(
+ new InputMap.MouseMapping(MouseEvent.MOUSE_PRESSED, this::mousePressed),
+ new InputMap.MouseMapping(MouseEvent.MOUSE_RELEASED, this::mouseReleased),
+ new InputMap.MouseMapping(MouseEvent.MOUSE_DRAGGED, this::mouseDragged)
+ );
}
protected abstract Control getCellContainer(); // e.g. ListView
protected abstract MultipleSelectionModel<?> getSelectionModel();
@@ -129,21 +144,27 @@
protected boolean isClickPositionValid(final double x, final double y) {
return true;
}
+
/***************************************************************************
* *
* Public API *
* *
**************************************************************************/
+ /** {@inheritDoc} */
+ @Override public InputMap<T> getInputMap() {
+ return cellInputMap;
+ }
+
protected int getIndex() {
- return getControl() instanceof IndexedCell ? ((IndexedCell<?>)getControl()).getIndex() : -1;
+ return getNode() instanceof IndexedCell ? ((IndexedCell<?>)getNode()).getIndex() : -1;
}
- @Override public void mousePressed(MouseEvent e) {
+ public void mousePressed(MouseEvent e) {
if (e.isSynthesized()) {
latePress = true;
} else {
latePress = isSelected();
if (!latePress) {
@@ -151,19 +172,19 @@
e.isShiftDown(), e.isShortcutDown());
}
}
}
- @Override public void mouseReleased(MouseEvent e) {
+ public void mouseReleased(MouseEvent e) {
if (latePress) {
latePress = false;
doSelect(e.getX(), e.getY(), e.getButton(), e.getClickCount(),
e.isShiftDown(), e.isShortcutDown());
}
}
- @Override public void mouseDragged(MouseEvent e) {
+ public void mouseDragged(MouseEvent e) {
latePress = false;
}
@@ -174,11 +195,11 @@
**************************************************************************/
protected void doSelect(final double x, final double y, final MouseButton button,
final int clickCount, final boolean shiftDown, final boolean shortcutDown) {
// we update the cell to point to the new tree node
- final T cell = getControl();
+ final T cell = getNode();
final Control cellContainer = getCellContainer();
// If the mouse event is not contained within this TreeCell, then
// we don't want to react to it.
@@ -261,16 +282,16 @@
protected void handleClicks(MouseButton button, int clickCount, boolean isAlreadySelected) {
// handle editing, which only occurs with the primary mouse button
if (button == MouseButton.PRIMARY) {
if (clickCount == 1 && isAlreadySelected) {
- edit(getControl());
+ edit(getNode());
} else if (clickCount == 1) {
// cancel editing
edit(null);
- } else if (clickCount == 2 && getControl().isEditable()) {
- edit(getControl());
+ } else if (clickCount == 2 && getNode().isEditable()) {
+ edit(getNode());
}
}
}
void selectRows(int focusedIndex, int index) {
@@ -309,8 +330,8 @@
}
}
}
protected boolean isSelected() {
- return getControl().isSelected();
+ return getNode().isSelected();
}
}