8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.javafx.scene.control.behavior; 27 28 import javafx.scene.Node; 29 import javafx.scene.control.*; 30 import javafx.scene.input.MouseButton; 31 import javafx.scene.input.MouseEvent; 32 import java.util.ArrayList; 33 import java.util.Collections; 34 import java.util.List; 35 36 /** 37 */ 38 public abstract class TableCellBehaviorBase<S, T, TC extends TableColumnBase<S, ?>, C extends IndexedCell<T>> extends CellBehaviorBase<C> { 39 40 /*************************************************************************** 41 * * 42 * Private static implementation * 43 * * 44 **************************************************************************/ 45 46 private static final String ANCHOR_PROPERTY_KEY = "table.anchor"; 47 48 static TablePositionBase getAnchor(Control table, TablePositionBase focusedCell) { 49 return hasAnchor(table) ? 50 (TablePositionBase) table.getProperties().get(ANCHOR_PROPERTY_KEY) : 51 focusedCell; 58 table.getProperties().put(ANCHOR_PROPERTY_KEY, anchor); 59 } 60 } 61 62 static boolean hasAnchor(Control table) { 63 return table.getProperties().get(ANCHOR_PROPERTY_KEY) != null; 64 } 65 66 static void removeAnchor(Control table) { 67 table.getProperties().remove(ANCHOR_PROPERTY_KEY); 68 } 69 70 71 72 /*************************************************************************** 73 * * 74 * Private fields * 75 * * 76 **************************************************************************/ 77 78 // For RT-17456: have selection occur as fast as possible with mouse input. 79 // The idea is (consistently with some native applications we've tested) to 80 // do the action as soon as you can. It takes a bit more coding but provides 81 // the best feel: 82 // - when you click on a not-selected item, you can select immediately on press 83 // - when you click on a selected item, you need to wait whether DragDetected or Release comes first 84 // To support touch devices, we have to slightly modify this behavior, such 85 // that selection only happens on mouse release, if only minimal dragging 86 // has occurred. 87 private boolean latePress = false; 88 private boolean wasSelected = false; 89 90 91 92 /*************************************************************************** 93 * * 94 * Constructors * 95 * * 96 **************************************************************************/ 97 98 public TableCellBehaviorBase(C control) { 99 super(control, Collections.EMPTY_LIST); 100 } 101 102 103 104 /************************************************************************** 105 * * 106 * Abstract API * 107 * * 108 *************************************************************************/ 109 110 abstract Control getTableControl(); // tableCell.getTreeTableView() 117 abstract TableColumnBase<S,T> getVisibleLeafColumn(int index); 118 119 /** 120 * Returns the position of the given table column in the visible leaf columns 121 * list of the underlying control. 122 */ 123 protected abstract int getVisibleLeafIndex(TableColumnBase<S,T> tc); 124 125 abstract void focus(int row, TableColumnBase<S,T> tc); //fm.focus(new TreeTablePosition(tableView, row, tableColumn)); 126 abstract void edit(int row, TableColumnBase<S,T> tc); 127 128 129 130 /*************************************************************************** 131 * * 132 * Public API * 133 * * 134 **************************************************************************/ 135 136 @Override public void mousePressed(MouseEvent event) { 137 boolean selectedBefore = isSelected(); 138 139 if (isSelected()) { 140 latePress = true; 141 return; 142 } 143 144 doSelect(event); 145 146 if (IS_TOUCH_SUPPORTED && selectedBefore) { 147 wasSelected = isSelected(); 148 } 149 } 150 151 @Override public void mouseReleased(MouseEvent event) { 152 if (latePress) { 153 latePress = false; 154 doSelect(event); 155 } 156 157 wasSelected = false; 158 } 159 160 @Override public void mouseDragged(MouseEvent event) { 161 latePress = false; 162 163 // the mouse has now been dragged on a touch device, we should 164 // remove the selection if we just added it in the last mouse press 165 // event 166 if (IS_TOUCH_SUPPORTED && ! wasSelected && isSelected()) { 167 getSelectionModel().clearSelection(getControl().getIndex()); 168 } 169 } 170 171 172 173 /*************************************************************************** 174 * * 175 * Private implementation * 176 * * 177 **************************************************************************/ 178 179 private void doSelect(MouseEvent e) { 180 // Note that table.select will reset selection 181 // for out of bounds indexes. So, need to check 182 final C tableCell = getControl(); 183 184 // If the mouse event is not contained within this tableCell, then 185 // we don't want to react to it. 186 if (! tableCell.contains(e.getX(), e.getY())) return; 187 188 final Control tableView = getTableControl(); | 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.javafx.scene.control.behavior; 27 28 import javafx.scene.control.Control; 29 import javafx.scene.control.IndexedCell; 30 import javafx.scene.control.SelectionMode; 31 import javafx.scene.control.TableColumnBase; 32 import javafx.scene.control.TableFocusModel; 33 import javafx.scene.control.TablePositionBase; 34 import javafx.scene.control.TableSelectionModel; 35 import javafx.scene.input.MouseButton; 36 import javafx.scene.input.MouseEvent; 37 38 import java.util.ArrayList; 39 import java.util.Collections; 40 import java.util.List; 41 42 /** 43 */ 44 public abstract class TableCellBehaviorBase<S, T, TC extends TableColumnBase<S, ?>, C extends IndexedCell<T>> extends CellBehaviorBase<C> { 45 46 /*************************************************************************** 47 * * 48 * Private static implementation * 49 * * 50 **************************************************************************/ 51 52 private static final String ANCHOR_PROPERTY_KEY = "table.anchor"; 53 54 static TablePositionBase getAnchor(Control table, TablePositionBase focusedCell) { 55 return hasAnchor(table) ? 56 (TablePositionBase) table.getProperties().get(ANCHOR_PROPERTY_KEY) : 57 focusedCell; 64 table.getProperties().put(ANCHOR_PROPERTY_KEY, anchor); 65 } 66 } 67 68 static boolean hasAnchor(Control table) { 69 return table.getProperties().get(ANCHOR_PROPERTY_KEY) != null; 70 } 71 72 static void removeAnchor(Control table) { 73 table.getProperties().remove(ANCHOR_PROPERTY_KEY); 74 } 75 76 77 78 /*************************************************************************** 79 * * 80 * Private fields * 81 * * 82 **************************************************************************/ 83 84 // To support touch devices, we have to slightly modify this behavior, such 85 // that selection only happens on mouse release, if only minimal dragging 86 // has occurred. 87 private boolean latePress = false; 88 89 /*************************************************************************** 90 * * 91 * Constructors * 92 * * 93 **************************************************************************/ 94 95 public TableCellBehaviorBase(C control) { 96 super(control, Collections.EMPTY_LIST); 97 } 98 99 100 101 /************************************************************************** 102 * * 103 * Abstract API * 104 * * 105 *************************************************************************/ 106 107 abstract Control getTableControl(); // tableCell.getTreeTableView() 114 abstract TableColumnBase<S,T> getVisibleLeafColumn(int index); 115 116 /** 117 * Returns the position of the given table column in the visible leaf columns 118 * list of the underlying control. 119 */ 120 protected abstract int getVisibleLeafIndex(TableColumnBase<S,T> tc); 121 122 abstract void focus(int row, TableColumnBase<S,T> tc); //fm.focus(new TreeTablePosition(tableView, row, tableColumn)); 123 abstract void edit(int row, TableColumnBase<S,T> tc); 124 125 126 127 /*************************************************************************** 128 * * 129 * Public API * 130 * * 131 **************************************************************************/ 132 133 @Override public void mousePressed(MouseEvent event) { 134 if (event.isSynthesized()) { 135 latePress = true; 136 } else { 137 latePress = getControl().isSelected(); 138 if (!latePress) { 139 doSelect(event); 140 } 141 } 142 } 143 144 @Override public void mouseReleased(MouseEvent event) { 145 if (latePress) { 146 latePress = false; 147 doSelect(event); 148 } 149 } 150 151 @Override public void mouseDragged(MouseEvent event) { 152 latePress = false; 153 } 154 155 156 157 /*************************************************************************** 158 * * 159 * Private implementation * 160 * * 161 **************************************************************************/ 162 163 private void doSelect(MouseEvent e) { 164 // Note that table.select will reset selection 165 // for out of bounds indexes. So, need to check 166 final C tableCell = getControl(); 167 168 // If the mouse event is not contained within this tableCell, then 169 // we don't want to react to it. 170 if (! tableCell.contains(e.getX(), e.getY())) return; 171 172 final Control tableView = getTableControl(); |