2 * Copyright (c) 2008-2009, Matthias Mann
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Matthias Mann nor the names of its contributors may
15 * be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 package de.matthiasmann.twl;
32 import de.matthiasmann.twl.model.DefaultTableSelectionModel;
33 import de.matthiasmann.twl.model.TableSelectionModel;
37 * @author Matthias Mann
39 public class TableRowSelectionManager implements TableSelectionManager {
41 protected final ActionMap actionMap;
42 protected final TableSelectionModel selectionModel;
44 protected TableBase tableBase;
46 public TableRowSelectionManager(TableSelectionModel selectionModel) {
47 if(selectionModel == null) {
48 throw new NullPointerException("selectionModel");
50 this.selectionModel = selectionModel;
51 this.actionMap = new ActionMap();
53 actionMap.addMapping(this);
56 public TableRowSelectionManager() {
57 this(new DefaultTableSelectionModel());
60 public void setAssociatedTable(TableBase base) {
61 if(tableBase != base) {
62 if(tableBase != null && base != null) {
63 throw new IllegalStateException("selection manager still in use");
65 this.tableBase = base;
70 public SelectionGranularity getSelectionGranularity() {
71 return SelectionGranularity.ROWS;
74 public boolean handleKeyStrokeAction(String action, Event event) {
75 return actionMap.invoke(action, event);
78 public boolean handleMouseEvent(int row, int column, Event event) {
79 if(event.getType() == Event.Type.MOUSE_BTNDOWN && event.getMouseButton() == Event.MOUSE_LBUTTON) {
80 boolean isShift = (event.getModifiers() & Event.MODIFIER_SHIFT) != 0;
81 boolean isCtrl = (event.getModifiers() & Event.MODIFIER_CTRL) != 0;
82 handleMouseClick(row, column, isShift, isCtrl);
88 public boolean isRowSelected(int row) {
89 return selectionModel.isSelected(row);
92 public boolean isCellSelected(int row, int column) {
96 public int getLeadRow() {
97 return selectionModel.getLeadIndex();
100 public int getLeadColumn() {
104 public void modelChanged() {
105 selectionModel.clearSelection();
106 selectionModel.setAnchorIndex(-1);
107 selectionModel.setLeadIndex(-1);
110 public void rowsInserted(int index, int count) {
111 selectionModel.rowsInserted(index, count);
114 public void rowsDeleted(int index, int count) {
115 selectionModel.rowsDeleted(index, count);
118 public void columnInserted(int index, int count) {
121 public void columnsDeleted(int index, int count) {
125 public void selectNextRow() {
126 handleRelativeAction(1, SET);
130 public void selectPreviousRow() {
131 handleRelativeAction(-1, SET);
135 public void selectNextPage() {
136 handleRelativeAction(getPageSize(), SET);
140 public void selectPreviousPage() {
141 handleRelativeAction(-getPageSize(), SET);
145 public void selectFirstRow() {
146 int numRows = getNumRows();
148 handleAbsoluteAction(0, SET);
153 public void selectLastRow() {
154 int numRows = getNumRows();
156 handleRelativeAction(numRows-1, SET);
161 public void extendSelectionToNextRow() {
162 handleRelativeAction(1, EXTEND);
166 public void extendSelectionToPreviousRow() {
167 handleRelativeAction(-1, EXTEND);
171 public void extendSelectionToNextPage() {
172 handleRelativeAction(getPageSize(), EXTEND);
176 public void extendSelectionToPreviousPage() {
177 handleRelativeAction(-getPageSize(), EXTEND);
181 public void extendSelectionToFirstRow() {
182 int numRows = getNumRows();
184 handleAbsoluteAction(0, EXTEND);
189 public void extendSelectionToLastRow() {
190 int numRows = getNumRows();
192 handleRelativeAction(numRows-1, EXTEND);
197 public void moveLeadToNextRow() {
198 handleRelativeAction(1, MOVE);
202 public void moveLeadToPreviousRow() {
203 handleRelativeAction(-1, MOVE);
207 public void moveLeadToNextPage() {
208 handleRelativeAction(getPageSize(), MOVE);
212 public void moveLeadToPreviousPage() {
213 handleRelativeAction(-getPageSize(), MOVE);
217 public void moveLeadToFirstRow() {
218 int numRows = getNumRows();
220 handleAbsoluteAction(0, MOVE);
225 public void moveLeadToLastRow() {
226 int numRows = getNumRows();
228 handleAbsoluteAction(numRows-1, MOVE);
233 public void toggleSelectionOnLeadRow() {
234 int leadIndex = selectionModel.getLeadIndex();
236 selectionModel.invertSelection(leadIndex, leadIndex);
241 public void selectAll() {
242 int numRows = getNumRows();
244 selectionModel.setSelection(0, numRows-1);
249 public void selectNone() {
250 selectionModel.clearSelection();
253 protected static final int TOGGLE = 0;
254 protected static final int EXTEND = 1;
255 protected static final int SET = 2;
256 protected static final int MOVE = 3;
258 protected void handleRelativeAction(int delta, int mode) {
259 int numRows = getNumRows();
261 int leadIndex = Math.max(0, selectionModel.getLeadIndex());
262 int index = Math.max(0, Math.min(numRows-1, leadIndex + delta));
264 handleAbsoluteAction(index, mode);
268 protected void handleAbsoluteAction(int index, int mode) {
269 if(tableBase != null) {
270 tableBase.adjustScrollPosition(index);
275 selectionModel.setLeadIndex(index);
278 int anchorIndex = Math.max(0, selectionModel.getAnchorIndex());
279 selectionModel.setSelection(anchorIndex, index);
282 selectionModel.invertSelection(index, index);
285 selectionModel.setSelection(index, index);
290 protected void handleMouseClick(int row, int column, boolean isShift, boolean isCtrl) {
293 selectionModel.clearSelection();
296 tableBase.adjustScrollPosition(row);
297 int anchorIndex = selectionModel.getAnchorIndex();
298 boolean anchorSelected;
299 if(anchorIndex == -1) {
301 anchorSelected = false;
303 anchorSelected = selectionModel.isSelected(anchorIndex);
309 selectionModel.addSelection(anchorIndex, row);
311 selectionModel.removeSelection(anchorIndex, row);
313 } else if(selectionModel.isSelected(row)) {
314 selectionModel.removeSelection(row, row);
316 selectionModel.addSelection(row, row);
319 selectionModel.setSelection(anchorIndex, row);
321 selectionModel.setSelection(row, row);
326 protected int getNumRows() {
327 if(tableBase != null) {
328 return tableBase.getNumRows();
333 protected int getPageSize() {
334 if(tableBase != null) {
335 return Math.max(1, tableBase.getNumVisibleRows());