source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetContentTableModel.java@ 6340

Last change on this file since 6340 was 6316, checked in by Don-vip, 11 years ago

Sonar/FindBugs - Loose coupling

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.changeset;
3
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.Comparator;
7import java.util.HashSet;
8import java.util.Iterator;
9import java.util.List;
10import java.util.Set;
11
12import javax.swing.DefaultListSelectionModel;
13import javax.swing.table.AbstractTableModel;
14
15import org.openstreetmap.josm.data.osm.ChangesetDataSet;
16import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetDataSetEntry;
17import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType;
18import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
19
20/**
21 * This is the table model for the content of a changeset.
22 *
23 */
24public 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 static private 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}
Note: See TracBrowser for help on using the repository browser.