source: josm/trunk/src/org/openstreetmap/josm/gui/util/ReorderableTableModel.java@ 16416

Last change on this file since 16416 was 16416, checked in by simon04, 4 years ago

fix #19234 - ToolbarPreferences: move up/down all selected entries

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.util;
3
4import javax.swing.JList;
5import javax.swing.JTable;
6import javax.swing.ListModel;
7import javax.swing.ListSelectionModel;
8import javax.swing.table.TableModel;
9
10import org.openstreetmap.josm.data.ReorderableModel;
11
12/**
13 * Defines a list/table model that can be reordered.
14 * @param <T> item type
15 * @since 15226
16 */
17public interface ReorderableTableModel<T> extends ReorderableModel<T> {
18
19 /**
20 * Returns the selection model.
21 * @return the selection model (never null)
22 * @see JList#getSelectionModel()
23 * @see JTable#getSelectionModel()
24 */
25 ListSelectionModel getSelectionModel();
26
27 /**
28 * Returns the number of rows in the list/table.
29 * @return the number of rows in the list/table
30 * @see ListModel#getSize()
31 * @see TableModel#getRowCount()
32 */
33 int getRowCount();
34
35 /**
36 * Returns an array of all of the selected indices in the selection model, in increasing order.
37 * @return an array of all of the selected indices in the selection model, in increasing order
38 */
39 default int[] getSelectedIndices() {
40 return TableHelper.getSelectedIndices(getSelectionModel());
41 }
42
43 /**
44 * Checks that the currently selected range of rows can be moved by a number of positions.
45 * @param delta negative or positive delta
46 * @return {@code true} if rows can be moved
47 */
48 default boolean canMove(int delta) {
49 return canMove(delta, this::getRowCount, getSelectedIndices());
50 }
51
52 /**
53 * Checks that the currently selected range of rows can be moved up.
54 * @return {@code true} if rows can be moved up
55 */
56 default boolean canMoveUp() {
57 return canMoveUp(getSelectedIndices());
58 }
59
60 /**
61 * Checks that a range of rows can be moved up.
62 * @param rows indexes of rows to move up
63 * @return {@code true} if rows can be moved up
64 */
65 default boolean canMoveUp(int... rows) {
66 return canMoveUp(this::getRowCount, rows);
67 }
68
69 /**
70 * Checks that the currently selected range of rows can be moved down.
71 * @return {@code true} if rows can be moved down
72 */
73 default boolean canMoveDown() {
74 return canMoveDown(getSelectedIndices());
75 }
76
77 /**
78 * Checks that a range of rows can be moved down.
79 * @param rows indexes of rows to move down
80 * @return {@code true} if rows can be moved down
81 */
82 default boolean canMoveDown(int... rows) {
83 return canMoveDown(this::getRowCount, rows);
84 }
85
86 /**
87 * Move up selected rows, if possible.
88 * @return {@code true} if the move was performed
89 * @see #canMoveUp
90 */
91 default boolean moveUp() {
92 return moveUp(getSelectedIndices());
93 }
94
95 /**
96 * Move up selected rows, if possible.
97 * @param selectedRows rows to move up
98 * @return {@code true} if the move was performed
99 * @see #canMoveUp
100 */
101 default boolean moveUp(int... selectedRows) {
102 return move(-1, selectedRows);
103 }
104
105 /**
106 * Move down selected rows, if possible.
107 * @return {@code true} if the move was performed
108 * @see #canMoveDown
109 */
110 default boolean moveDown() {
111 return moveDown(getSelectedIndices());
112 }
113
114 /**
115 * Move down selected rows by 1 position, if possible.
116 * @param selectedRows rows to move down
117 * @return {@code true} if the move was performed
118 * @see #canMoveDown
119 */
120 default boolean moveDown(int... selectedRows) {
121 return move(1, selectedRows);
122 }
123
124 /**
125 * Move selected rows by any number of positions, if possible.
126 * @param delta negative or positive delta
127 * @param selectedRows rows to move
128 * @return {@code true} if the move was performed
129 * @see #canMove
130 */
131 default boolean move(int delta, int... selectedRows) {
132 if (!canMove(delta, this::getRowCount, selectedRows))
133 return false;
134 if (!doMove(delta, selectedRows))
135 return false;
136 final ListSelectionModel selectionModel = getSelectionModel();
137 selectionModel.setValueIsAdjusting(true);
138 selectionModel.clearSelection();
139 for (int row: selectedRows) {
140 selectionModel.addSelectionInterval(row + delta, row + delta);
141 }
142 selectionModel.setValueIsAdjusting(false);
143 return true;
144 }
145}
Note: See TracBrowser for help on using the repository browser.