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