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