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

Last change on this file since 16436 was 16436, checked in by simon04, 4 years ago

see #19251 - Java 8: use Stream

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