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

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

see #15182 - move WindowGeometry from tools to gui.util

  • Property svn:eol-style set to native
File size: 8.0 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 protected HistoryBrowserDialogManager() {
67 dialogs = new HashMap<>();
68 MainApplication.getLayerManager().addLayerChangeListener(this);
69 }
70
71 /**
72 * Replies the unique instance.
73 * @return the unique instance
74 */
75 public static synchronized HistoryBrowserDialogManager getInstance() {
76 if (instance == null) {
77 instance = new HistoryBrowserDialogManager();
78 }
79 return instance;
80 }
81
82 /**
83 * Determines if an history dialog exists for the given object id.
84 * @param id the object id
85 * @return {@code true} if an history dialog exists for the given object id, {@code false} otherwise
86 */
87 public boolean existsDialog(long id) {
88 return dialogs.containsKey(id);
89 }
90
91 private void show(long id, HistoryBrowserDialog dialog) {
92 if (dialogs.containsValue(dialog)) {
93 show(id);
94 } else {
95 placeOnScreen(dialog);
96 dialog.setVisible(true);
97 dialogs.put(id, dialog);
98 }
99 }
100
101 private void show(long id) {
102 if (dialogs.containsKey(id)) {
103 dialogs.get(id).toFront();
104 }
105 }
106
107 private boolean hasDialogWithCloseUpperLeftCorner(Point p) {
108 for (HistoryBrowserDialog dialog: dialogs.values()) {
109 Point corner = dialog.getLocation();
110 if (p.x >= corner.x -5 && corner.x + 5 >= p.x
111 && p.y >= corner.y -5 && corner.y + 5 >= p.y)
112 return true;
113 }
114 return false;
115 }
116
117 private void placeOnScreen(HistoryBrowserDialog dialog) {
118 WindowGeometry geometry = new WindowGeometry(WINDOW_GEOMETRY_PREF, WindowGeometry.centerOnScreen(new Dimension(850, 500)));
119 geometry.applySafe(dialog);
120 Point p = dialog.getLocation();
121 while (hasDialogWithCloseUpperLeftCorner(p)) {
122 p.x += 20;
123 p.y += 20;
124 }
125 dialog.setLocation(p);
126 }
127
128 /**
129 * Hides the specified history dialog and cleans associated resources.
130 * @param dialog History dialog to hide
131 */
132 public void hide(HistoryBrowserDialog dialog) {
133 for (Iterator<Entry<Long, HistoryBrowserDialog>> it = dialogs.entrySet().iterator(); it.hasNext();) {
134 if (Objects.equals(it.next().getValue(), dialog)) {
135 it.remove();
136 if (dialogs.isEmpty()) {
137 new WindowGeometry(dialog).remember(WINDOW_GEOMETRY_PREF);
138 }
139 break;
140 }
141 }
142 dialog.setVisible(false);
143 dialog.dispose();
144 }
145
146 /**
147 * Hides and destroys all currently visible history browser dialogs
148 *
149 */
150 public void hideAll() {
151 List<HistoryBrowserDialog> dialogs = new ArrayList<>();
152 dialogs.addAll(this.dialogs.values());
153 for (HistoryBrowserDialog dialog: dialogs) {
154 dialog.unlinkAsListener();
155 hide(dialog);
156 }
157 }
158
159 /**
160 * Show history dialog for the given history.
161 * @param h History to show
162 */
163 public void show(History h) {
164 if (h == null)
165 return;
166 if (existsDialog(h.getId())) {
167 show(h.getId());
168 } else {
169 HistoryBrowserDialog dialog = new HistoryBrowserDialog(h);
170 show(h.getId(), dialog);
171 }
172 }
173
174 /* ----------------------------------------------------------------------------- */
175 /* LayerChangeListener */
176 /* ----------------------------------------------------------------------------- */
177 @Override
178 public void layerAdded(LayerAddEvent e) {
179 // Do nothing
180 }
181
182 @Override
183 public void layerRemoving(LayerRemoveEvent e) {
184 // remove all history browsers if the number of layers drops to 0
185 if (e.getSource().getLayers().isEmpty()) {
186 hideAll();
187 }
188 }
189
190 @Override
191 public void layerOrderChanged(LayerOrderChangeEvent e) {
192 // Do nothing
193 }
194
195 /**
196 * Show history dialog(s) for the given primitive(s).
197 * @param primitives The primitive(s) for which history will be displayed
198 */
199 public void showHistory(final Collection<? extends PrimitiveId> primitives) {
200 final Collection<? extends PrimitiveId> notNewPrimitives = SubclassFilteredCollection.filter(primitives, notNewPredicate);
201 if (notNewPrimitives.isEmpty()) {
202 JOptionPane.showMessageDialog(
203 Main.parent,
204 tr("Please select at least one already uploaded node, way, or relation."),
205 tr("Warning"),
206 JOptionPane.WARNING_MESSAGE);
207 return;
208 }
209
210 Collection<? extends PrimitiveId> toLoad = SubclassFilteredCollection.filter(primitives, unloadedHistoryPredicate);
211 if (!toLoad.isEmpty()) {
212 HistoryLoadTask task = new HistoryLoadTask();
213 for (PrimitiveId p : notNewPrimitives) {
214 task.add(p);
215 }
216 MainApplication.worker.submit(task);
217 }
218
219 Runnable r = () -> {
220 try {
221 for (PrimitiveId p : notNewPrimitives) {
222 final History h = HistoryDataSet.getInstance().getHistory(p);
223 if (h == null) {
224 continue;
225 }
226 SwingUtilities.invokeLater(() -> show(h));
227 }
228 } catch (final JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
229 BugReportExceptionHandler.handleException(e);
230 }
231 };
232 MainApplication.worker.submit(r);
233 }
234}
Note: See TracBrowser for help on using the repository browser.