24 */ 25 26 package com.sun.javafx.scene.control.behavior; 27 28 import javafx.scene.control.*; 29 import javafx.scene.input.MouseButton; 30 import javafx.scene.input.MouseEvent; 31 32 import java.util.ArrayList; 33 import java.util.Collections; 34 import java.util.List; 35 36 public class TableRowBehavior<T> extends CellBehaviorBase<TableRow<T>> { 37 38 /*************************************************************************** 39 * * 40 * Private fields * 41 * * 42 **************************************************************************/ 43 44 // For RT-17456: have selection occur as fast as possible with mouse input. 45 // The idea is (consistently with some native applications we've tested) to 46 // do the action as soon as you can. It takes a bit more coding but provides 47 // the best feel: 48 // - when you click on a not-selected item, you can select immediately on press 49 // - when you click on a selected item, you need to wait whether DragDetected or Release comes first 50 // 51 // To support touch devices, we have to slightly modify this behavior, such 52 // that selection only happens on mouse release, if only minimal dragging 53 // has occurred. 54 private boolean latePress = false; 55 private boolean wasSelected = false; 56 57 58 59 /*************************************************************************** 60 * * 61 * Constructors * 62 * * 63 **************************************************************************/ 64 65 public TableRowBehavior(TableRow<T> control) { 66 super(control, Collections.EMPTY_LIST); 67 } 68 69 70 71 /*************************************************************************** 72 * * 73 * Public API * 74 * * 75 **************************************************************************/ 76 77 @Override public void mousePressed(MouseEvent event) { 78 // we only care about clicks to the right of the right-most column 79 if (! isClickOutsideCellBounds(event.getX())) return; 80 81 boolean selectedBefore = getControl().isSelected(); 82 83 if (getControl().isSelected()) { 84 latePress = true; 85 return; 86 } 87 88 doSelect(event); 89 90 if (IS_TOUCH_SUPPORTED && selectedBefore) { 91 wasSelected = getControl().isSelected(); 92 } 93 } 94 95 @Override public void mouseReleased(MouseEvent event) { 96 // we only care about clicks to the right of the right-most column 97 if (! isClickOutsideCellBounds(event.getX())) return; 98 99 if (latePress) { 100 latePress = false; 101 doSelect(event); 102 } 103 104 wasSelected = false; 105 } 106 107 @Override public void mouseDragged(MouseEvent event) { 108 // we only care about clicks to the right of the right-most column 109 if (! isClickOutsideCellBounds(event.getX())) return; 110 111 latePress = false; 112 113 // the mouse has now been dragged on a touch device, we should 114 // remove the selection if we just added it in the last mouse press 115 // event 116 if (IS_TOUCH_SUPPORTED && ! wasSelected && getControl().isSelected()) { 117 getControl().getTableView().getSelectionModel().clearSelection(getControl().getIndex()); 118 } 119 } 120 121 122 123 124 /*************************************************************************** 125 * * 126 * Private implementation * 127 * * 128 **************************************************************************/ 129 130 private void doSelect(MouseEvent e) { 131 super.mouseReleased(e); 132 133 if (e.getButton() != MouseButton.PRIMARY) return; 134 135 final TableRow<T> tableRow = getControl(); 136 final TableView<T> table = tableRow.getTableView(); 137 if (table == null) return; 138 final TableSelectionModel<T> sm = table.getSelectionModel(); | 24 */ 25 26 package com.sun.javafx.scene.control.behavior; 27 28 import javafx.scene.control.*; 29 import javafx.scene.input.MouseButton; 30 import javafx.scene.input.MouseEvent; 31 32 import java.util.ArrayList; 33 import java.util.Collections; 34 import java.util.List; 35 36 public class TableRowBehavior<T> extends CellBehaviorBase<TableRow<T>> { 37 38 /*************************************************************************** 39 * * 40 * Private fields * 41 * * 42 **************************************************************************/ 43 44 // To support touch devices, we have to slightly modify this behavior, such 45 // that selection only happens on mouse release, if only minimal dragging 46 // has occurred. 47 private boolean latePress = false; 48 49 50 51 /*************************************************************************** 52 * * 53 * Constructors * 54 * * 55 **************************************************************************/ 56 57 public TableRowBehavior(TableRow<T> control) { 58 super(control, Collections.EMPTY_LIST); 59 } 60 61 62 63 /*************************************************************************** 64 * * 65 * Public API * 66 * * 67 **************************************************************************/ 68 69 @Override public void mousePressed(MouseEvent event) { 70 // we only care about clicks to the right of the right-most column 71 if (! isClickOutsideCellBounds(event.getX())) return; 72 73 if (event.isSynthesized()) { 74 latePress = true; 75 } else { 76 latePress = getControl().isSelected(); 77 if (!latePress) { 78 doSelect(event); 79 } 80 } 81 } 82 83 @Override public void mouseReleased(MouseEvent event) { 84 if (latePress) { 85 latePress = false; 86 doSelect(event); 87 } 88 } 89 90 @Override public void mouseDragged(MouseEvent event) { 91 latePress = false; 92 } 93 94 95 96 97 /*************************************************************************** 98 * * 99 * Private implementation * 100 * * 101 **************************************************************************/ 102 103 private void doSelect(MouseEvent e) { 104 super.mouseReleased(e); 105 106 if (e.getButton() != MouseButton.PRIMARY) return; 107 108 final TableRow<T> tableRow = getControl(); 109 final TableView<T> table = tableRow.getTableView(); 110 if (table == null) return; 111 final TableSelectionModel<T> sm = table.getSelectionModel(); |