1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 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.*; 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(); 112 if (sm == null || sm.isCellSelectionEnabled()) return; 113 114 final int index = getControl().getIndex(); 115 final boolean isAlreadySelected = sm.isSelected(index); 116 int clickCount = e.getClickCount(); 117 if (clickCount == 1) { 118 // we only care about clicks to the right of the right-most column 119 if (! isClickOutsideCellBounds(e.getX())) return; 120 121 // In the case of clicking to the right of the rightmost 122 // TreeTableCell, we should still support selection, so that 123 // is what we are doing here. 124 if (isAlreadySelected && e.isShortcutDown()) { 125 sm.clearSelection(index); 126 } else { 127 if (e.isShortcutDown()) { 128 sm.select(tableRow.getIndex()); 129 } else if (e.isShiftDown()) { 130 // we add all rows between the current focus and 131 // this row (inclusive) to the current selection. 132 TablePositionBase anchor = TableCellBehavior.getAnchor(table, table.getFocusModel().getFocusedCell()); 133 final int anchorRow = anchor.getRow(); 134 final boolean asc = anchorRow < index; 135 136 // and then determine all row and columns which must be selected 137 int minRow = Math.min(anchorRow, index); 138 int maxRow = Math.max(anchorRow, index); 139 140 // To prevent RT-32119, we make a copy of the selected indices 141 // list first, so that we are not iterating and modifying it 142 // concurrently. 143 List<Integer> selectedIndices = new ArrayList<>(sm.getSelectedIndices()); 144 for (int i = 0, max = selectedIndices.size(); i < max; i++) { 145 int selectedIndex = selectedIndices.get(i); 146 if (selectedIndex < minRow || selectedIndex > maxRow) { 147 sm.clearSelection(selectedIndex); 148 } 149 } 150 151 if (minRow == maxRow) { 152 // RT-32560: This prevents the anchor 'sticking' in 153 // the wrong place when a range is selected and then 154 // selection goes back to the anchor position. 155 // (Refer to the video in RT-32560 for more detail). 156 sm.select(minRow); 157 } else { 158 // RT-21444: We need to put the range in the correct 159 // order or else the last selected row will not be the 160 // last item in the selectedItems list of the selection 161 // model, 162 if (asc) { 163 sm.selectRange(minRow, maxRow + 1); 164 } else { 165 sm.selectRange(maxRow, minRow - 1); 166 } 167 } 168 } else { 169 sm.clearAndSelect(tableRow.getIndex()); 170 } 171 } 172 } 173 } 174 175 private boolean isClickOutsideCellBounds(final double x) { 176 // get width of all visible columns (we only care about clicks to the 177 // right of the right-most column) 178 final TableRow<T> tableRow = getControl(); 179 final TableView<T> table = tableRow.getTableView(); 180 if (table == null) return false; 181 List<TableColumn<T, ?>> columns = table.getVisibleLeafColumns(); 182 double width = 0.0; 183 for (int i = 0; i < columns.size(); i++) { 184 width += columns.get(i).getWidth(); 185 } 186 187 return x > width; 188 } 189 }