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