source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManagerModel.java@ 12495

Last change on this file since 12495 was 12495, checked in by Don-vip, 7 years ago

fix #5869 - Download dialog, bookmarks: add "home location" bookmark (if set in OSM user settings) + last 15 changesets bookmarks

  • Property svn:eol-style set to native
File size: 6.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.changeset;
3
4import java.beans.PropertyChangeListener;
5import java.beans.PropertyChangeSupport;
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.Comparator;
9import java.util.HashSet;
10import java.util.List;
11import java.util.Set;
12
13import javax.swing.DefaultListSelectionModel;
14import javax.swing.table.AbstractTableModel;
15
16import org.openstreetmap.josm.data.osm.Changeset;
17import org.openstreetmap.josm.data.osm.ChangesetCache;
18import org.openstreetmap.josm.data.osm.ChangesetCacheEvent;
19import org.openstreetmap.josm.data.osm.ChangesetCacheListener;
20import org.openstreetmap.josm.gui.util.GuiHelper;
21
22/**
23 * This is the model for the changeset cache manager dialog.
24 */
25public class ChangesetCacheManagerModel extends AbstractTableModel implements ChangesetCacheListener {
26
27 /** the name of the property for the currently selected changeset in the detail view */
28 public static final String CHANGESET_IN_DETAIL_VIEW_PROP = ChangesetCacheManagerModel.class.getName() + ".changesetInDetailView";
29
30 private final transient List<Changeset> data = new ArrayList<>();
31 private final DefaultListSelectionModel selectionModel;
32 private transient Changeset changesetInDetailView;
33 private final PropertyChangeSupport support = new PropertyChangeSupport(this);
34
35 /**
36 * Creates a new ChangesetCacheManagerModel that is based on the selectionModel
37 * @param selectionModel A new selection model that should be used.
38 */
39 public ChangesetCacheManagerModel(DefaultListSelectionModel selectionModel) {
40 this.selectionModel = selectionModel;
41 }
42
43 /**
44 * Adds a property change listener to this model.
45 * @param listener The listener
46 */
47 public void addPropertyChangeListener(PropertyChangeListener listener) {
48 support.addPropertyChangeListener(listener);
49 }
50
51 /**
52 * Removes a property change listener from this model.
53 * @param listener The listener
54 */
55 public void removePropertyChangeListener(PropertyChangeListener listener) {
56 support.removePropertyChangeListener(listener);
57 }
58
59 /**
60 * Sets the changeset currently displayed in the detail view. Fires a property change event
61 * for the property {@link #CHANGESET_IN_DETAIL_VIEW_PROP} if necessary.
62 *
63 * @param cs the changeset currently displayed in the detail view.
64 */
65 public void setChangesetInDetailView(Changeset cs) {
66 Changeset oldValue = changesetInDetailView;
67 changesetInDetailView = cs;
68 if (oldValue != cs) {
69 support.firePropertyChange(CHANGESET_IN_DETAIL_VIEW_PROP, oldValue, changesetInDetailView);
70 }
71 }
72
73 /**
74 * Replies true if there is at least one selected changeset
75 *
76 * @return true if there is at least one selected changeset
77 */
78 public boolean hasSelectedChangesets() {
79 return selectionModel.getMinSelectionIndex() >= 0;
80 }
81
82 /**
83 * Replies the list of selected changesets
84 *
85 * @return the list of selected changesets
86 */
87 public List<Changeset> getSelectedChangesets() {
88 List<Changeset> ret = new ArrayList<>();
89 for (int i = 0; i < data.size(); i++) {
90 Changeset cs = data.get(i);
91 if (selectionModel.isSelectedIndex(i)) {
92 ret.add(cs);
93 }
94 }
95 return ret;
96 }
97
98 /**
99 * Replies a set of ids of the selected changesets
100 *
101 * @return a set of ids of the selected changesets
102 */
103 public Set<Integer> getSelectedChangesetIds() {
104 Set<Integer> ret = new HashSet<>();
105 for (Changeset cs: getSelectedChangesets()) {
106 ret.add(cs.getId());
107 }
108 return ret;
109 }
110
111 /**
112 * Selects the changesets in <code>selected</code>.
113 *
114 * @param selected the collection of changesets to select. Ignored if empty.
115 */
116 public void setSelectedChangesets(Collection<Changeset> selected) {
117 selectionModel.setValueIsAdjusting(true);
118 selectionModel.clearSelection();
119 if (selected != null) {
120 for (Changeset cs: selected) {
121 final int idx = data.indexOf(cs);
122 if (idx >= 0) {
123 selectionModel.addSelectionInterval(idx, idx);
124 }
125 }
126 }
127 GuiHelper.runInEDTAndWait(() -> selectionModel.setValueIsAdjusting(false));
128 }
129
130 @Override
131 public int getColumnCount() {
132 return 5;
133 }
134
135 @Override
136 public int getRowCount() {
137 return data.size();
138 }
139
140 @Override
141 public Changeset getValueAt(int row, int column) {
142 return data.get(row);
143 }
144
145 /**
146 * Initializes the data that is displayed using the changeset cache.
147 */
148 public void init() {
149 ChangesetCache cc = ChangesetCache.getInstance();
150 List<Changeset> selected = getSelectedChangesets();
151 data.clear();
152 data.addAll(cc.getChangesets());
153 sort();
154 fireTableDataChanged();
155 setSelectedChangesets(selected);
156
157 cc.addChangesetCacheListener(this);
158 }
159
160 /**
161 * Destroys and unregisters this model.
162 */
163 public void tearDown() {
164 ChangesetCache.getInstance().removeChangesetCacheListener(this);
165 }
166
167 /**
168 * Gets the selection model this table is based on.
169 * @return The selection model.
170 */
171 public DefaultListSelectionModel getSelectionModel() {
172 return selectionModel;
173 }
174
175 protected void sort() {
176 data.sort(Comparator.comparingInt(Changeset::getId).reversed());
177 }
178
179 /* ------------------------------------------------------------------------------ */
180 /* interface ChangesetCacheListener */
181 /* ------------------------------------------------------------------------------ */
182 @Override
183 public void changesetCacheUpdated(ChangesetCacheEvent event) {
184 List<Changeset> selected = getSelectedChangesets();
185 for (Changeset cs: event.getAddedChangesets()) {
186 data.add(cs);
187 }
188 for (Changeset cs: event.getRemovedChangesets()) {
189 data.remove(cs);
190 }
191 for (Changeset cs: event.getUpdatedChangesets()) {
192 int idx = data.indexOf(cs);
193 if (idx >= 0) {
194 Changeset mine = data.get(idx);
195 if (mine != cs) {
196 mine.mergeFrom(cs);
197 }
198 }
199 }
200 sort();
201 fireTableDataChanged();
202 setSelectedChangesets(selected);
203 }
204}
Note: See TracBrowser for help on using the repository browser.