source: josm/trunk/src/org/openstreetmap/josm/gui/history/HistoryBrowserDialogManager.java@ 8332

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

fix various issues:

  • performance - inefficient use of keySet iterator instead of entrySet iterator
  • squid:S2674: The value returned from a stream read should be checked
  • remove unused imports
  • improve javadoc
  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Dimension;
7import java.awt.Point;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.HashMap;
11import java.util.Iterator;
12import java.util.List;
13import java.util.Map;
14import java.util.Map.Entry;
15import java.util.Objects;
16
17import javax.swing.JOptionPane;
18import javax.swing.SwingUtilities;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.data.osm.PrimitiveId;
22import org.openstreetmap.josm.data.osm.history.History;
23import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
24import org.openstreetmap.josm.gui.MapView;
25import org.openstreetmap.josm.gui.layer.Layer;
26import org.openstreetmap.josm.tools.BugReportExceptionHandler;
27import org.openstreetmap.josm.tools.Predicate;
28import org.openstreetmap.josm.tools.Utils;
29import org.openstreetmap.josm.tools.WindowGeometry;
30
31/**
32 * Manager allowing to show/hide history dialogs.
33 * @since 2019
34 */
35public class HistoryBrowserDialogManager implements MapView.LayerChangeListener {
36
37 private static HistoryBrowserDialogManager instance;
38
39 /**
40 * Replies the unique instance.
41 * @return the unique instance
42 */
43 public static synchronized HistoryBrowserDialogManager getInstance() {
44 if (instance == null) {
45 instance = new HistoryBrowserDialogManager();
46 }
47 return instance;
48 }
49
50 private Map<Long, HistoryBrowserDialog> dialogs;
51
52 protected HistoryBrowserDialogManager() {
53 dialogs = new HashMap<>();
54 MapView.addLayerChangeListener(this);
55 }
56
57 /**
58 * Determines if an history dialog exists for the given object id.
59 * @param id the object id
60 * @return {@code true} if an history dialog exists for the given object id, {@code false} otherwise
61 */
62 public boolean existsDialog(long id) {
63 return dialogs.containsKey(id);
64 }
65
66 protected void show(long id, HistoryBrowserDialog dialog) {
67 if (dialogs.values().contains(dialog)) {
68 show(id);
69 } else {
70 placeOnScreen(dialog);
71 dialog.setVisible(true);
72 dialogs.put(id, dialog);
73 }
74 }
75
76 protected void show(long id) {
77 if (dialogs.keySet().contains(id)) {
78 dialogs.get(id).toFront();
79 }
80 }
81
82 protected boolean hasDialogWithCloseUpperLeftCorner(Point p) {
83 for (HistoryBrowserDialog dialog: dialogs.values()) {
84 Point corner = dialog.getLocation();
85 if (p.x >= corner.x -5 && corner.x + 5 >= p.x
86 && p.y >= corner.y -5 && corner.y + 5 >= p.y)
87 return true;
88 }
89 return false;
90 }
91
92 private final String WINDOW_GEOMETRY_PREF = getClass().getName() + ".geometry";
93
94 protected void placeOnScreen(HistoryBrowserDialog dialog) {
95 WindowGeometry geometry = new WindowGeometry(WINDOW_GEOMETRY_PREF, WindowGeometry.centerOnScreen(new Dimension(850, 500)));
96 geometry.applySafe(dialog);
97 Point p = dialog.getLocation();
98 while (hasDialogWithCloseUpperLeftCorner(p)) {
99 p.x += 20;
100 p.y += 20;
101 }
102 dialog.setLocation(p);
103 }
104
105 /**
106 * Hides the specified history dialog and cleans associated resources.
107 * @param dialog History dialog to hide
108 */
109 public void hide(HistoryBrowserDialog dialog) {
110 for (Iterator<Entry<Long, HistoryBrowserDialog>> it = dialogs.entrySet().iterator(); it.hasNext(); ) {
111 if (Objects.equals(it.next().getValue(), dialog)) {
112 it.remove();
113 if (dialogs.isEmpty()) {
114 new WindowGeometry(dialog).remember(WINDOW_GEOMETRY_PREF);
115 }
116 break;
117 }
118 }
119 dialog.setVisible(false);
120 dialog.dispose();
121 }
122
123 /**
124 * Hides and destroys all currently visible history browser dialogs
125 *
126 */
127 public void hideAll() {
128 List<HistoryBrowserDialog> dialogs = new ArrayList<>();
129 dialogs.addAll(this.dialogs.values());
130 for (HistoryBrowserDialog dialog: dialogs) {
131 dialog.unlinkAsListener();
132 hide(dialog);
133 }
134 }
135
136 /**
137 * Show history dialog for the given history.
138 * @param h History to show
139 */
140 public void show(History h) {
141 if (h == null)
142 return;
143 if (existsDialog(h.getId())) {
144 show(h.getId());
145 } else {
146 HistoryBrowserDialog dialog = new HistoryBrowserDialog(h);
147 show(h.getId(), dialog);
148 }
149 }
150
151 /* ----------------------------------------------------------------------------- */
152 /* LayerChangeListener */
153 /* ----------------------------------------------------------------------------- */
154 @Override
155 public void activeLayerChange(Layer oldLayer, Layer newLayer) {}
156 @Override
157 public void layerAdded(Layer newLayer) {}
158
159 @Override
160 public void layerRemoved(Layer oldLayer) {
161 // remove all history browsers if the number of layers drops to 0
162 if (Main.isDisplayingMapView() && Main.map.mapView.getNumLayers() == 0) {
163 hideAll();
164 }
165 }
166
167 /**
168 * Show history dialog(s) for the given primitive(s).
169 * @param primitives The primitive(s) for which history will be displayed
170 */
171 public void showHistory(final Collection<? extends PrimitiveId> primitives) {
172 final Collection<? extends PrimitiveId> notNewPrimitives = Utils.filter(primitives, notNewPredicate);
173 if (notNewPrimitives.isEmpty()) {
174 JOptionPane.showMessageDialog(
175 Main.parent,
176 tr("Please select at least one already uploaded node, way, or relation."),
177 tr("Warning"),
178 JOptionPane.WARNING_MESSAGE);
179 return;
180 }
181
182 Collection<PrimitiveId> toLoad = Utils.filter(primitives, unloadedHistoryPredicate);
183 if (!toLoad.isEmpty()) {
184 HistoryLoadTask task = new HistoryLoadTask();
185 for (PrimitiveId p : notNewPrimitives) {
186 task.add(p);
187 }
188 Main.worker.submit(task);
189 }
190
191 Runnable r = new Runnable() {
192
193 @Override
194 public void run() {
195 try {
196 for (PrimitiveId p : notNewPrimitives) {
197 final History h = HistoryDataSet.getInstance().getHistory(p);
198 if (h == null) {
199 continue;
200 }
201 SwingUtilities.invokeLater(new Runnable() {
202 @Override
203 public void run() {
204 show(h);
205 }
206 });
207 }
208 } catch (final Exception e) {
209 BugReportExceptionHandler.handleException(e);
210 }
211 }
212 };
213 Main.worker.submit(r);
214 }
215
216 private final Predicate<PrimitiveId> unloadedHistoryPredicate = new Predicate<PrimitiveId>() {
217
218 private HistoryDataSet hds = HistoryDataSet.getInstance();
219
220 @Override
221 public boolean evaluate(PrimitiveId p) {
222 History h = hds.getHistory(p);
223 if (h == null)
224 // reload if the history is not in the cache yet
225 return true;
226 else
227 // reload if the history object of the selected object is not in the cache yet
228 return (!p.isNew() && h.getByVersion(p.getUniqueId()) == null);
229 }
230 };
231
232 private final Predicate<PrimitiveId> notNewPredicate = new Predicate<PrimitiveId>() {
233
234 @Override
235 public boolean evaluate(PrimitiveId p) {
236 return !p.isNew();
237 }
238 };
239}
Note: See TracBrowser for help on using the repository browser.