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

Last change on this file since 10680 was 10657, checked in by Don-vip, 8 years ago

see #11390, see #12890 - use Java 8 Predicates

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