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

Last change on this file since 2990 was 2990, checked in by jttt, 14 years ago

Fix some eclipse warnings

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