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

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

fix HistoryHook signature

  • Property svn:eol-style set to native
File size: 8.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;
16import java.util.function.Predicate;
17
18import javax.swing.JOptionPane;
19import javax.swing.SwingUtilities;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.data.osm.PrimitiveId;
23import org.openstreetmap.josm.data.osm.history.History;
24import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
25import org.openstreetmap.josm.gui.MainApplication;
26import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
27import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
28import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
29import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
30import org.openstreetmap.josm.gui.util.WindowGeometry;
31import org.openstreetmap.josm.tools.JosmRuntimeException;
32import org.openstreetmap.josm.tools.SubclassFilteredCollection;
33import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
34
35/**
36 * Manager allowing to show/hide history dialogs.
37 * @since 2019
38 */
39public final class HistoryBrowserDialogManager implements LayerChangeListener {
40
41 static final class UnloadedHistoryPredicate implements Predicate<PrimitiveId> {
42 private final HistoryDataSet hds = HistoryDataSet.getInstance();
43
44 @Override
45 public boolean test(PrimitiveId p) {
46 History h = hds.getHistory(p);
47 if (h == null)
48 // reload if the history is not in the cache yet
49 return true;
50 else
51 // reload if the history object of the selected object is not in the cache yet
52 return !p.isNew() && h.getByVersion(p.getUniqueId()) == null;
53 }
54 }
55
56 private static final String WINDOW_GEOMETRY_PREF = HistoryBrowserDialogManager.class.getName() + ".geometry";
57
58 private static HistoryBrowserDialogManager instance;
59
60 private final Map<Long, HistoryBrowserDialog> dialogs;
61
62 private final Predicate<PrimitiveId> unloadedHistoryPredicate = new UnloadedHistoryPredicate();
63
64 private final Predicate<PrimitiveId> notNewPredicate = p -> !p.isNew();
65
66 private static final List<HistoryHook> hooks = new ArrayList<>();
67
68 protected HistoryBrowserDialogManager() {
69 dialogs = new HashMap<>();
70 MainApplication.getLayerManager().addLayerChangeListener(this);
71 }
72
73 /**
74 * Replies the unique instance.
75 * @return the unique instance
76 */
77 public static synchronized HistoryBrowserDialogManager getInstance() {
78 if (instance == null) {
79 instance = new HistoryBrowserDialogManager();
80 }
81 return instance;
82 }
83
84 /**
85 * Determines if an history dialog exists for the given object id.
86 * @param id the object id
87 * @return {@code true} if an history dialog exists for the given object id, {@code false} otherwise
88 */
89 public boolean existsDialog(long id) {
90 return dialogs.containsKey(id);
91 }
92
93 private void show(long id, HistoryBrowserDialog dialog) {
94 if (dialogs.containsValue(dialog)) {
95 show(id);
96 } else {
97 placeOnScreen(dialog);
98 dialog.setVisible(true);
99 dialogs.put(id, dialog);
100 }
101 }
102
103 private void show(long id) {
104 if (dialogs.containsKey(id)) {
105 dialogs.get(id).toFront();
106 }
107 }
108
109 private boolean hasDialogWithCloseUpperLeftCorner(Point p) {
110 for (HistoryBrowserDialog dialog: dialogs.values()) {
111 Point corner = dialog.getLocation();
112 if (p.x >= corner.x -5 && corner.x + 5 >= p.x
113 && p.y >= corner.y -5 && corner.y + 5 >= p.y)
114 return true;
115 }
116 return false;
117 }
118
119 private void placeOnScreen(HistoryBrowserDialog dialog) {
120 WindowGeometry geometry = new WindowGeometry(WINDOW_GEOMETRY_PREF, WindowGeometry.centerOnScreen(new Dimension(850, 500)));
121 geometry.applySafe(dialog);
122 Point p = dialog.getLocation();
123 while (hasDialogWithCloseUpperLeftCorner(p)) {
124 p.x += 20;
125 p.y += 20;
126 }
127 dialog.setLocation(p);
128 }
129
130 /**
131 * Hides the specified history dialog and cleans associated resources.
132 * @param dialog History dialog to hide
133 */
134 public void hide(HistoryBrowserDialog dialog) {
135 for (Iterator<Entry<Long, HistoryBrowserDialog>> it = dialogs.entrySet().iterator(); it.hasNext();) {
136 if (Objects.equals(it.next().getValue(), dialog)) {
137 it.remove();
138 if (dialogs.isEmpty()) {
139 new WindowGeometry(dialog).remember(WINDOW_GEOMETRY_PREF);
140 }
141 break;
142 }
143 }
144 dialog.setVisible(false);
145 dialog.dispose();
146 }
147
148 /**
149 * Hides and destroys all currently visible history browser dialogs
150 *
151 */
152 public void hideAll() {
153 List<HistoryBrowserDialog> dialogs = new ArrayList<>();
154 dialogs.addAll(this.dialogs.values());
155 for (HistoryBrowserDialog dialog: dialogs) {
156 dialog.unlinkAsListener();
157 hide(dialog);
158 }
159 }
160
161 /**
162 * Show history dialog for the given history.
163 * @param h History to show
164 */
165 public void show(History h) {
166 if (h == null)
167 return;
168 if (existsDialog(h.getId())) {
169 show(h.getId());
170 } else {
171 HistoryBrowserDialog dialog = new HistoryBrowserDialog(h);
172 show(h.getId(), dialog);
173 }
174 }
175
176 /* ----------------------------------------------------------------------------- */
177 /* LayerChangeListener */
178 /* ----------------------------------------------------------------------------- */
179 @Override
180 public void layerAdded(LayerAddEvent e) {
181 // Do nothing
182 }
183
184 @Override
185 public void layerRemoving(LayerRemoveEvent e) {
186 // remove all history browsers if the number of layers drops to 0
187 if (e.getSource().getLayers().isEmpty()) {
188 hideAll();
189 }
190 }
191
192 @Override
193 public void layerOrderChanged(LayerOrderChangeEvent e) {
194 // Do nothing
195 }
196
197 /**
198 * Adds a new {@code HistoryHook}.
199 * @param hook hook to add
200 * @return <tt>true</tt> (as specified by {@link Collection#add})
201 * @since 13947
202 */
203 public static boolean addHistoryHook(HistoryHook hook) {
204 return hooks.add(Objects.requireNonNull(hook));
205 }
206
207 /**
208 * Removes an existing {@code HistoryHook}.
209 * @param hook hook to remove
210 * @return <tt>true</tt> if this list contained the specified element
211 * @since 13947
212 */
213 public static boolean removeHistoryHook(HistoryHook hook) {
214 return hooks.remove(Objects.requireNonNull(hook));
215 }
216
217 /**
218 * Show history dialog(s) for the given primitive(s).
219 * @param primitives The primitive(s) for which history will be displayed
220 */
221 public void showHistory(final Collection<? extends PrimitiveId> primitives) {
222 final List<PrimitiveId> realPrimitives = new ArrayList<>(primitives);
223 hooks.forEach(h -> h.modifyRequestedIds(realPrimitives));
224 final Collection<? extends PrimitiveId> notNewPrimitives = SubclassFilteredCollection.filter(realPrimitives, notNewPredicate);
225 if (notNewPrimitives.isEmpty()) {
226 JOptionPane.showMessageDialog(
227 Main.parent,
228 tr("Please select at least one already uploaded node, way, or relation."),
229 tr("Warning"),
230 JOptionPane.WARNING_MESSAGE);
231 return;
232 }
233
234 Collection<? extends PrimitiveId> toLoad = SubclassFilteredCollection.filter(realPrimitives, unloadedHistoryPredicate);
235 if (!toLoad.isEmpty()) {
236 HistoryLoadTask task = new HistoryLoadTask();
237 for (PrimitiveId p : notNewPrimitives) {
238 task.add(p);
239 }
240 MainApplication.worker.submit(task);
241 }
242
243 Runnable r = () -> {
244 try {
245 for (PrimitiveId p : notNewPrimitives) {
246 final History h = HistoryDataSet.getInstance().getHistory(p);
247 if (h == null) {
248 continue;
249 }
250 SwingUtilities.invokeLater(() -> show(h));
251 }
252 } catch (final JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
253 BugReportExceptionHandler.handleException(e);
254 }
255 };
256 MainApplication.worker.submit(r);
257 }
258}
Note: See TracBrowser for help on using the repository browser.