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