|
| 1 | +/* |
| 2 | + DragSelectionCell.java |
| 3 | + Copyright (C) 2019 Sriram C. |
| 4 | +
|
| 5 | + This program is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | +
|
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | +package com.gems.table; |
| 20 | + |
| 21 | +import javafx.collections.ObservableList; |
| 22 | +import javafx.event.EventHandler; |
| 23 | +import javafx.scene.control.SelectionMode; |
| 24 | +import javafx.scene.control.TableCell; |
| 25 | +import javafx.scene.control.TableColumn; |
| 26 | +import javafx.scene.control.cell.TextFieldTableCell; |
| 27 | +import javafx.scene.input.MouseDragEvent; |
| 28 | +import javafx.scene.input.MouseEvent; |
| 29 | +import javafx.util.Callback; |
| 30 | +import javafx.util.StringConverter; |
| 31 | +import javafx.util.converter.DefaultStringConverter; |
| 32 | + |
| 33 | +public class DragSelectionCell<S,T> extends TextFieldTableCell<S, T> { |
| 34 | + |
| 35 | + static int startRow; |
| 36 | + static int startCol; |
| 37 | + |
| 38 | + static int lastIndex; |
| 39 | + |
| 40 | + public DragSelectionCell(final StringConverter<T> converter) { |
| 41 | + super(converter); |
| 42 | + |
| 43 | + dragEvent(); |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + public DragSelectionCell() { |
| 49 | + |
| 50 | + dragEvent(); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + public static <S> Callback<TableColumn<S, String>, TableCell<S, String>> forTableColumn() { |
| 55 | + return forTableColumn(new DefaultStringConverter()); |
| 56 | + } |
| 57 | + |
| 58 | + public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> forTableColumn( |
| 59 | + final StringConverter<T> converter) { |
| 60 | + return list -> new DragSelectionCell<S, T>(converter); |
| 61 | + } |
| 62 | + |
| 63 | + // FIXME select rectangle doesnt extend above on scrollviews |
| 64 | + |
| 65 | + public void selectRectangle() { |
| 66 | + |
| 67 | + ObservableList<TableColumn <S,?>> list = getTableColumn().getTableView().getColumns(); |
| 68 | + int endCol = list.indexOf(getTableColumn()); |
| 69 | + int endRow = getIndex(); |
| 70 | + |
| 71 | + System.out.println("On Drag Detected " +endRow + " " + endCol); |
| 72 | + |
| 73 | + // Clear old rectangle |
| 74 | + // FIXME - only the rectangle selection should be cleared and not the previous selection |
| 75 | + getTableColumn().getTableView().getSelectionModel().clearSelection(); |
| 76 | + |
| 77 | + // is the below two lines needed |
| 78 | + getTableColumn().getTableView().getSelectionModel().setCellSelectionEnabled(true);// |
| 79 | + getTableColumn().getTableView().getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); |
| 80 | + |
| 81 | + // Select Rectangle |
| 82 | + for (int i = (startRow <= endRow) ? startRow:endRow; i <= ((startRow < endRow)?endRow:startRow);i++) { |
| 83 | + for (int j = (startCol <= endCol) ? startCol:endCol; j <= ((startCol < endCol) ? endCol : startCol);j++) { |
| 84 | + |
| 85 | + getTableColumn().getTableView().getSelectionModel().select(i, list.get(j)); |
| 86 | + |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + public void dragEvent() { |
| 94 | + setOnDragDetected(new EventHandler<MouseEvent>() { |
| 95 | + @Override |
| 96 | + public void handle(MouseEvent event) { |
| 97 | + startFullDrag(); |
| 98 | + getTableColumn().getTableView().getSelectionModel().select(getIndex(), getTableColumn()); |
| 99 | + |
| 100 | + ObservableList<TableColumn <S,?>> columnList = getTableColumn().getTableView().getColumns(); |
| 101 | + startCol = columnList.indexOf(getTableColumn()); |
| 102 | + startRow = getIndex(); |
| 103 | + |
| 104 | + System.out.println("On Drag Entered " +startRow + " " + startCol); |
| 105 | + |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + setOnMouseDragEntered(new EventHandler<MouseDragEvent>() { |
| 110 | + |
| 111 | + @Override |
| 112 | + public void handle(MouseDragEvent event) { |
| 113 | + System.out.println("On Drag Entered " +getIndex() + " " + getTableColumn()); |
| 114 | + |
| 115 | + selectRectangle(); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + }); |
| 120 | + |
| 121 | + setOnMouseDragExited(new EventHandler<MouseDragEvent>() { |
| 122 | + |
| 123 | + @Override |
| 124 | + public void handle(MouseDragEvent event) { |
| 125 | + //getTableColumn().getTableView().getSelectionModel().select(getIndex(), getTableColumn()); |
| 126 | + System.out.println("On Drag exited " +getIndex() + " " + getTableColumn()); |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments