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