[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.history;
|
---|
| 3 |
|
---|
[4408] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[2512] | 6 | import java.awt.Dimension;
|
---|
| 7 | import java.awt.Point;
|
---|
| 8 | import java.util.ArrayList;
|
---|
[4408] | 9 | import java.util.Collection;
|
---|
[2512] | 10 | import java.util.HashMap;
|
---|
| 11 | import java.util.Map;
|
---|
[4760] | 12 |
|
---|
[4408] | 13 | import javax.swing.JOptionPane;
|
---|
| 14 | import javax.swing.SwingUtilities;
|
---|
[2512] | 15 |
|
---|
| 16 | import org.openstreetmap.josm.Main;
|
---|
[4408] | 17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[2512] | 18 | import org.openstreetmap.josm.data.osm.history.History;
|
---|
[4408] | 19 | import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
|
---|
[2621] | 20 | import org.openstreetmap.josm.gui.MapView;
|
---|
[2512] | 21 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[4408] | 22 | import org.openstreetmap.josm.tools.BugReportExceptionHandler;
|
---|
| 23 | import org.openstreetmap.josm.tools.Predicate;
|
---|
| 24 | import org.openstreetmap.josm.tools.Utils;
|
---|
[2512] | 25 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
| 26 |
|
---|
[2621] | 27 | public class HistoryBrowserDialogManager implements MapView.LayerChangeListener {
|
---|
[2512] | 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>();
|
---|
[2621] | 40 | MapView.addLayerChangeListener(this);
|
---|
[2512] | 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));
|
---|
[4932] | 75 | geometry.applySafe(dialog);
|
---|
[2512] | 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 | //
|
---|
[5460] | 132 | if (Main.isDisplayingMapView() && Main.map.mapView.getNumLayers() == 0) {
|
---|
[2512] | 133 | hideAll();
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
[4408] | 136 |
|
---|
[4760] | 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 | }
|
---|
[4674] | 147 |
|
---|
[4760] | 148 | Collection<OsmPrimitive> toLoad = Utils.filter(primitives, unloadedHistoryPredicate);
|
---|
| 149 | if (!toLoad.isEmpty()) {
|
---|
| 150 | HistoryLoadTask task = new HistoryLoadTask();
|
---|
| 151 | task.add(notNewPrimitives);
|
---|
| 152 | Main.worker.submit(task);
|
---|
| 153 | }
|
---|
[4408] | 154 |
|
---|
[4760] | 155 | Runnable r = new Runnable() {
|
---|
[4408] | 156 |
|
---|
[4760] | 157 | @Override
|
---|
| 158 | public void run() {
|
---|
| 159 | try {
|
---|
| 160 | for (OsmPrimitive p : notNewPrimitives) {
|
---|
| 161 | final History h = HistoryDataSet.getInstance().getHistory(p.getPrimitiveId());
|
---|
| 162 | if (h == null) {
|
---|
| 163 | continue;
|
---|
| 164 | }
|
---|
| 165 | SwingUtilities.invokeLater(new Runnable() {
|
---|
| 166 | @Override
|
---|
| 167 | public void run() {
|
---|
| 168 | show(h);
|
---|
| 169 | }
|
---|
| 170 | });
|
---|
| 171 | }
|
---|
| 172 | } catch (final Exception e) {
|
---|
| 173 | BugReportExceptionHandler.handleException(e);
|
---|
| 174 | }
|
---|
[4408] | 175 |
|
---|
[4760] | 176 | }
|
---|
| 177 | };
|
---|
| 178 | Main.worker.submit(r);
|
---|
| 179 | }
|
---|
[4408] | 180 |
|
---|
[4760] | 181 | private final Predicate<OsmPrimitive> unloadedHistoryPredicate = new Predicate<OsmPrimitive>() {
|
---|
[4408] | 182 |
|
---|
[4760] | 183 | HistoryDataSet hds = HistoryDataSet.getInstance();
|
---|
[4408] | 184 |
|
---|
[4760] | 185 | @Override
|
---|
| 186 | public boolean evaluate(OsmPrimitive p) {
|
---|
| 187 | if (hds.getHistory(p.getPrimitiveId()) == null)
|
---|
| 188 | // reload if the history is not in the cache yet
|
---|
| 189 | return true;
|
---|
| 190 | else if (!p.isNew() && hds.getHistory(p.getPrimitiveId()).getByVersion(p.getUniqueId()) == null)
|
---|
| 191 | // reload if the history object of the selected object is not in the cache yet
|
---|
| 192 | return true;
|
---|
| 193 | else
|
---|
| 194 | return false;
|
---|
| 195 | }
|
---|
| 196 | };
|
---|
[4408] | 197 |
|
---|
[4760] | 198 | private final Predicate<OsmPrimitive> notNewPredicate = new Predicate<OsmPrimitive>() {
|
---|
[4408] | 199 |
|
---|
[4760] | 200 | @Override
|
---|
| 201 | public boolean evaluate(OsmPrimitive p) {
|
---|
| 202 | return !p.isNew();
|
---|
| 203 | }
|
---|
| 204 | };
|
---|
[4408] | 205 |
|
---|
[2512] | 206 | }
|
---|