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.HashSet;
|
---|
6 | import java.util.Iterator;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Set;
|
---|
9 |
|
---|
10 | import javax.swing.DefaultListSelectionModel;
|
---|
11 | import javax.swing.table.AbstractTableModel;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.ChangesetDataSet;
|
---|
14 | import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetDataSetEntry;
|
---|
15 | import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType;
|
---|
16 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * This is the table model for the content of a changeset.
|
---|
20 | *
|
---|
21 | */
|
---|
22 | public class ChangesetContentTableModel extends AbstractTableModel {
|
---|
23 |
|
---|
24 | private final transient List<ChangesetContentEntry> data = new ArrayList<>();
|
---|
25 | private final DefaultListSelectionModel selectionModel;
|
---|
26 |
|
---|
27 | public ChangesetContentTableModel(DefaultListSelectionModel selectionModel) {
|
---|
28 | this.selectionModel = selectionModel;
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Replies true if there is at least one selected primitive in the table model
|
---|
33 | *
|
---|
34 | * @return true if there is at least one selected primitive in the table model
|
---|
35 | */
|
---|
36 | public boolean hasSelectedPrimitives() {
|
---|
37 | return selectionModel.getMinSelectionIndex() >= 0;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void setSelectedByIdx(int row) {
|
---|
41 | selectionModel.setSelectionInterval(row, row);
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Replies the selection model
|
---|
46 | * @return the selection model
|
---|
47 | */
|
---|
48 | public DefaultListSelectionModel getSelectionModel() {
|
---|
49 | return selectionModel;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public Set<HistoryOsmPrimitive> getSelectedPrimitives() {
|
---|
53 | Set<HistoryOsmPrimitive> ret = new HashSet<>();
|
---|
54 | for (int i = 0; i < data.size(); i++) {
|
---|
55 | if (selectionModel.isSelectedIndex(i)) {
|
---|
56 | ret.add(data.get(i).getPrimitive());
|
---|
57 | }
|
---|
58 | }
|
---|
59 | return ret;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Populates the model with the content of a changeset. If ds is null, the table is cleared.
|
---|
64 | *
|
---|
65 | * @param ds the changeset content.
|
---|
66 | */
|
---|
67 | public void populate(ChangesetDataSet ds) {
|
---|
68 | this.data.clear();
|
---|
69 | if (ds == null) {
|
---|
70 | fireTableDataChanged();
|
---|
71 | return;
|
---|
72 | }
|
---|
73 | for (Iterator<ChangesetDataSetEntry> it = ds.iterator(); it.hasNext();) {
|
---|
74 | data.add(new ChangesetContentEntry(it.next()));
|
---|
75 | }
|
---|
76 | sort();
|
---|
77 | fireTableDataChanged();
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected void sort() {
|
---|
81 | data.sort((c1, c2) -> {
|
---|
82 | if (c1.getModificationType().equals(c2.getModificationType())) {
|
---|
83 | long id1 = c1.getPrimitive().getId();
|
---|
84 | long id2 = c2.getPrimitive().getId();
|
---|
85 |
|
---|
86 | if (id1 == id2)
|
---|
87 | return 0;
|
---|
88 | else if (id1 < id2)
|
---|
89 | return -1;
|
---|
90 | return 1;
|
---|
91 | }
|
---|
92 | switch(c1.getModificationType()) {
|
---|
93 | case CREATED: return -1;
|
---|
94 | case UPDATED:
|
---|
95 | switch(c2.getModificationType()) {
|
---|
96 | case CREATED: return 1;
|
---|
97 | default: return -1;
|
---|
98 | }
|
---|
99 | case DELETED:
|
---|
100 | return 1;
|
---|
101 | }
|
---|
102 | // should not happen
|
---|
103 | return 0;
|
---|
104 | }
|
---|
105 | );
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* -------------------------------------------------------------- */
|
---|
109 | /* interface TableModel */
|
---|
110 | /* -------------------------------------------------------------- */
|
---|
111 | @Override
|
---|
112 | public int getColumnCount() {
|
---|
113 | return 3;
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public int getRowCount() {
|
---|
118 | return data.size();
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override
|
---|
122 | public Object getValueAt(int row, int col) {
|
---|
123 | switch(col) {
|
---|
124 | case 0: return data.get(row).getModificationType();
|
---|
125 | default: return data.get(row).getPrimitive();
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * The type used internally to keep information about {@link HistoryOsmPrimitive}
|
---|
131 | * with their {@link ChangesetModificationType}.
|
---|
132 | *
|
---|
133 | */
|
---|
134 | private static class ChangesetContentEntry implements ChangesetDataSetEntry {
|
---|
135 | private final ChangesetModificationType modificationType;
|
---|
136 | private final HistoryOsmPrimitive primitive;
|
---|
137 |
|
---|
138 | ChangesetContentEntry(ChangesetModificationType modificationType, HistoryOsmPrimitive primitive) {
|
---|
139 | this.modificationType = modificationType;
|
---|
140 | this.primitive = primitive;
|
---|
141 | }
|
---|
142 |
|
---|
143 | ChangesetContentEntry(ChangesetDataSetEntry entry) {
|
---|
144 | this(entry.getModificationType(), entry.getPrimitive());
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Override
|
---|
148 | public ChangesetModificationType getModificationType() {
|
---|
149 | return modificationType;
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | public HistoryOsmPrimitive getPrimitive() {
|
---|
154 | return primitive;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|