1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.changeset;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Arrays;
|
---|
6 | import java.util.Comparator;
|
---|
7 | import java.util.Iterator;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Set;
|
---|
10 | import java.util.stream.Collectors;
|
---|
11 |
|
---|
12 | import javax.swing.DefaultListSelectionModel;
|
---|
13 | import javax.swing.JTable;
|
---|
14 | import javax.swing.table.AbstractTableModel;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.osm.ChangesetDataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetDataSetEntry;
|
---|
18 | import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType;
|
---|
19 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * This is the table model for the content of a changeset.
|
---|
23 | * @since 2689
|
---|
24 | */
|
---|
25 | public class ChangesetContentTableModel extends AbstractTableModel {
|
---|
26 |
|
---|
27 | private final transient List<ChangesetContentEntry> data = new ArrayList<>();
|
---|
28 | private final DefaultListSelectionModel selectionModel;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Constructs a new {@code ChangesetContentTableModel}.
|
---|
32 | * @param selectionModel selection model
|
---|
33 | */
|
---|
34 | public ChangesetContentTableModel(DefaultListSelectionModel selectionModel) {
|
---|
35 | this.selectionModel = selectionModel;
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Replies true if there is at least one selected primitive in the table model
|
---|
40 | *
|
---|
41 | * @return true if there is at least one selected primitive in the table model
|
---|
42 | */
|
---|
43 | public boolean hasSelectedPrimitives() {
|
---|
44 | return selectionModel.getMinSelectionIndex() >= 0;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Selects a single item by its index.
|
---|
49 | * @param row index
|
---|
50 | */
|
---|
51 | public void setSelectedByIdx(int row) {
|
---|
52 | selectionModel.setSelectionInterval(row, row);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Replies the selection model
|
---|
57 | * @return the selection model
|
---|
58 | */
|
---|
59 | public DefaultListSelectionModel getSelectionModel() {
|
---|
60 | return selectionModel;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Returns the selected history primitives.
|
---|
65 | * @param table the JTable used with this model
|
---|
66 | * @return the selected history primitives
|
---|
67 | */
|
---|
68 | public Set<HistoryOsmPrimitive> getSelectedPrimitives(JTable table) {
|
---|
69 | int[] selection = table.getSelectedRows();
|
---|
70 | return Arrays.stream(selection)
|
---|
71 | .mapToObj(i -> data.get(table.convertRowIndexToModel(i)).getPrimitive())
|
---|
72 | .collect(Collectors.toSet());
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Populates the model with the content of a changeset. If ds is null, the table is cleared.
|
---|
77 | *
|
---|
78 | * @param ds the changeset content.
|
---|
79 | */
|
---|
80 | public void populate(ChangesetDataSet ds) {
|
---|
81 | this.data.clear();
|
---|
82 | if (ds == null) {
|
---|
83 | fireTableDataChanged();
|
---|
84 | return;
|
---|
85 | }
|
---|
86 | for (Iterator<ChangesetDataSetEntry> it = ds.iterator(); it.hasNext();) {
|
---|
87 | data.add(new ChangesetContentEntry(it.next()));
|
---|
88 | }
|
---|
89 | sort();
|
---|
90 | fireTableDataChanged();
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Sort data.
|
---|
95 | */
|
---|
96 | protected void sort() {
|
---|
97 | data.sort(Comparator.comparing(ChangesetContentEntry::getModificationType).thenComparingLong(c -> c.getPrimitive().getId())
|
---|
98 | );
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* -------------------------------------------------------------- */
|
---|
102 | /* interface TableModel */
|
---|
103 | /* -------------------------------------------------------------- */
|
---|
104 | @Override
|
---|
105 | public int getColumnCount() {
|
---|
106 | return 3;
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public int getRowCount() {
|
---|
111 | return data.size();
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public Object getValueAt(int row, int col) {
|
---|
116 | switch(col) {
|
---|
117 | case 0: return data.get(row).getModificationType();
|
---|
118 | default: return data.get(row).getPrimitive();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * The type used internally to keep information about {@link HistoryOsmPrimitive}
|
---|
124 | * with their {@link ChangesetModificationType}.
|
---|
125 | */
|
---|
126 | private static class ChangesetContentEntry implements ChangesetDataSetEntry {
|
---|
127 | private final ChangesetModificationType modificationType;
|
---|
128 | private final HistoryOsmPrimitive primitive;
|
---|
129 |
|
---|
130 | ChangesetContentEntry(ChangesetModificationType modificationType, HistoryOsmPrimitive primitive) {
|
---|
131 | this.modificationType = modificationType;
|
---|
132 | this.primitive = primitive;
|
---|
133 | }
|
---|
134 |
|
---|
135 | ChangesetContentEntry(ChangesetDataSetEntry entry) {
|
---|
136 | this(entry.getModificationType(), entry.getPrimitive());
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | public ChangesetModificationType getModificationType() {
|
---|
141 | return modificationType;
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Override
|
---|
145 | public HistoryOsmPrimitive getPrimitive() {
|
---|
146 | return primitive;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|