1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.function.IntSupplier;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.tools.Utils;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Defines a model that can be reordered.
|
---|
11 | * @param <T> item type
|
---|
12 | * @since 15226
|
---|
13 | */
|
---|
14 | public interface ReorderableModel<T> {
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Get object value at given index.
|
---|
18 | * @param index index
|
---|
19 | * @return object value at given index
|
---|
20 | */
|
---|
21 | T getValue(int index);
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Set object value at given index.
|
---|
25 | * @param index index
|
---|
26 | * @param value new value
|
---|
27 | * @return the value previously at the specified position
|
---|
28 | */
|
---|
29 | T setValue(int index, T value);
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Checks that a range of rows can be moved by a given number of positions.
|
---|
33 | * @param delta negative or positive delta
|
---|
34 | * @param rowCount row count supplier
|
---|
35 | * @param rows indexes of rows to move
|
---|
36 | * @return {@code true} if rows can be moved
|
---|
37 | */
|
---|
38 | default boolean canMove(int delta, IntSupplier rowCount, int... rows) {
|
---|
39 | if (rows == null || rows.length == 0)
|
---|
40 | return false;
|
---|
41 | int[] sortedRows = Utils.copyArray(rows);
|
---|
42 | Arrays.sort(sortedRows);
|
---|
43 | if (delta < 0)
|
---|
44 | return sortedRows[0] >= -delta;
|
---|
45 | else if (delta > 0)
|
---|
46 | return sortedRows[sortedRows.length-1] <= rowCount.getAsInt()-1 - delta;
|
---|
47 | else
|
---|
48 | return true;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Checks that a range of rows can be moved up (by 1 position).
|
---|
53 | * @param rowCount row count supplier
|
---|
54 | * @param rows indexes of rows to move up
|
---|
55 | * @return {@code true} if rows can be moved up
|
---|
56 | */
|
---|
57 | default boolean canMoveUp(IntSupplier rowCount, int... rows) {
|
---|
58 | return canMove(-1, rowCount, rows);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Checks that a range of rows can be moved down (by 1 position).
|
---|
63 | * @param rowCount row count supplier
|
---|
64 | * @param rows indexes of rows to move down
|
---|
65 | * @return {@code true} if rows can be moved down
|
---|
66 | */
|
---|
67 | default boolean canMoveDown(IntSupplier rowCount, int... rows) {
|
---|
68 | return canMove(1, rowCount, rows);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Performs the move operation, without any check nor selection handling.
|
---|
73 | * @param delta negative or positive delta
|
---|
74 | * @param selectedRows rows to move
|
---|
75 | * @return {@code true} if rows have been moved
|
---|
76 | */
|
---|
77 | default boolean doMove(int delta, int... selectedRows) {
|
---|
78 | if (delta != 0) {
|
---|
79 | if (delta < 0) {
|
---|
80 | for (int row: selectedRows) {
|
---|
81 | setValue(row + delta, setValue(row, getValue(row + delta)));
|
---|
82 | }
|
---|
83 | } else {
|
---|
84 | for (int i = selectedRows.length - 1; i >= 0; i--) {
|
---|
85 | int row = selectedRows[i];
|
---|
86 | setValue(row + delta, setValue(row, getValue(row + delta)));
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | return delta != 0 && selectedRows.length > 0;
|
---|
91 | }
|
---|
92 | }
|
---|