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