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

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

Add TableHelper.setSelectedIndices

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