[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[626] | 2 | package org.openstreetmap.josm;
|
---|
[301] | 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 4 |
|
---|
| 5 | import java.awt.BorderLayout;
|
---|
| 6 | import java.awt.Component;
|
---|
[3815] | 7 | import java.awt.GridBagConstraints;
|
---|
| 8 | import java.awt.GridBagLayout;
|
---|
[2876] | 9 | import java.awt.event.ComponentEvent;
|
---|
| 10 | import java.awt.event.ComponentListener;
|
---|
[301] | 11 | import java.awt.event.KeyEvent;
|
---|
[2876] | 12 | import java.awt.event.WindowAdapter;
|
---|
| 13 | import java.awt.event.WindowEvent;
|
---|
[301] | 14 | import java.io.File;
|
---|
[4126] | 15 | import java.lang.ref.WeakReference;
|
---|
[301] | 16 | import java.net.URI;
|
---|
| 17 | import java.net.URISyntaxException;
|
---|
[2025] | 18 | import java.util.ArrayList;
|
---|
[301] | 19 | import java.util.Collection;
|
---|
[4126] | 20 | import java.util.Iterator;
|
---|
[2025] | 21 | import java.util.List;
|
---|
[301] | 22 | import java.util.Map;
|
---|
| 23 | import java.util.StringTokenizer;
|
---|
[5134] | 24 | import java.util.concurrent.Callable;
|
---|
[1465] | 25 | import java.util.concurrent.ExecutorService;
|
---|
[5134] | 26 | import java.util.concurrent.Executors;
|
---|
[2322] | 27 | import java.util.concurrent.Future;
|
---|
[301] | 28 |
|
---|
[3252] | 29 | import javax.swing.Action;
|
---|
| 30 | import javax.swing.InputMap;
|
---|
[301] | 31 | import javax.swing.JComponent;
|
---|
[1356] | 32 | import javax.swing.JFrame;
|
---|
[3815] | 33 | import javax.swing.JLabel;
|
---|
[301] | 34 | import javax.swing.JOptionPane;
|
---|
| 35 | import javax.swing.JPanel;
|
---|
[3815] | 36 | import javax.swing.JTextArea;
|
---|
[3252] | 37 | import javax.swing.KeyStroke;
|
---|
[301] | 38 | import javax.swing.UIManager;
|
---|
| 39 |
|
---|
[4489] | 40 | import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
|
---|
[3444] | 41 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
[2047] | 42 | import org.openstreetmap.josm.actions.OpenFileAction;
|
---|
[301] | 43 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
| 44 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
[2322] | 45 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
|
---|
| 46 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
[301] | 47 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
| 48 | import org.openstreetmap.josm.actions.search.SearchAction;
|
---|
| 49 | import org.openstreetmap.josm.data.Bounds;
|
---|
| 50 | import org.openstreetmap.josm.data.Preferences;
|
---|
| 51 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
[1990] | 52 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
---|
[2327] | 53 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[301] | 54 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[2305] | 55 | import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
|
---|
[301] | 56 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
[4126] | 57 | import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
|
---|
[3669] | 58 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
[301] | 59 | import org.openstreetmap.josm.gui.GettingStarted;
|
---|
[5279] | 60 | import org.openstreetmap.josm.gui.MainApplication.Option;
|
---|
[301] | 61 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
| 62 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
[3669] | 63 | import org.openstreetmap.josm.gui.MapView;
|
---|
[1895] | 64 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
[2025] | 65 | import org.openstreetmap.josm.gui.io.SaveLayersDialog;
|
---|
[301] | 66 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
| 67 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
| 68 | import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
|
---|
| 69 | import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
---|
[4968] | 70 | import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
|
---|
| 71 | import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
|
---|
| 72 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
|
---|
[5226] | 73 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
|
---|
[4718] | 74 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
| 75 | import org.openstreetmap.josm.gui.progress.ProgressMonitorExecutor;
|
---|
[4897] | 76 | import org.openstreetmap.josm.gui.util.RedirectInputMap;
|
---|
[3934] | 77 | import org.openstreetmap.josm.io.OsmApi;
|
---|
[1326] | 78 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
[4126] | 79 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[3340] | 80 | import org.openstreetmap.josm.tools.I18n;
|
---|
[301] | 81 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[4489] | 82 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
[1307] | 83 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
[1023] | 84 | import org.openstreetmap.josm.tools.PlatformHook;
|
---|
[1465] | 85 | import org.openstreetmap.josm.tools.PlatformHookOsx;
|
---|
[1023] | 86 | import org.openstreetmap.josm.tools.PlatformHookUnixoid;
|
---|
| 87 | import org.openstreetmap.josm.tools.PlatformHookWindows;
|
---|
[1084] | 88 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[4288] | 89 | import org.openstreetmap.josm.tools.Utils;
|
---|
[4932] | 90 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
[626] | 91 |
|
---|
| 92 | abstract public class Main {
|
---|
[2485] | 93 |
|
---|
[1104] | 94 | /**
|
---|
[2343] | 95 | * Replies true if JOSM currently displays a map view. False, if it doesn't, i.e. if
|
---|
| 96 | * it only shows the MOTD panel.
|
---|
[2512] | 97 | *
|
---|
[5357] | 98 | * @return <code>true</code> if JOSM currently displays a map view
|
---|
[2343] | 99 | */
|
---|
| 100 | static public boolean isDisplayingMapView() {
|
---|
| 101 | if (map == null) return false;
|
---|
| 102 | if (map.mapView == null) return false;
|
---|
| 103 | return true;
|
---|
| 104 | }
|
---|
| 105 | /**
|
---|
[1104] | 106 | * Global parent component for all dialogs and message boxes
|
---|
| 107 | */
|
---|
| 108 | public static Component parent;
|
---|
| 109 | /**
|
---|
| 110 | * Global application.
|
---|
| 111 | */
|
---|
| 112 | public static Main main;
|
---|
| 113 | /**
|
---|
| 114 | * The worker thread slave. This is for executing all long and intensive
|
---|
| 115 | * calculations. The executed runnables are guaranteed to be executed separately
|
---|
| 116 | * and sequential.
|
---|
| 117 | */
|
---|
[4718] | 118 | public final static ExecutorService worker = new ProgressMonitorExecutor();
|
---|
[1104] | 119 | /**
|
---|
| 120 | * Global application preferences
|
---|
| 121 | */
|
---|
[3542] | 122 | public static Preferences pref;
|
---|
[1814] | 123 |
|
---|
[1104] | 124 | /**
|
---|
| 125 | * The global paste buffer.
|
---|
| 126 | */
|
---|
[4869] | 127 | public static final PrimitiveDeepCopy pasteBuffer = new PrimitiveDeepCopy();
|
---|
[1592] | 128 | public static Layer pasteSource;
|
---|
[4126] | 129 |
|
---|
[1104] | 130 | /**
|
---|
| 131 | * The MapFrame. Use setMapFrame to set or clear it.
|
---|
| 132 | */
|
---|
| 133 | public static MapFrame map;
|
---|
| 134 | /**
|
---|
[5357] | 135 | * Set to <code>true</code>, when in applet mode
|
---|
[1104] | 136 | */
|
---|
| 137 | public static boolean applet = false;
|
---|
[626] | 138 |
|
---|
[1104] | 139 | /**
|
---|
| 140 | * The toolbar preference control to register new actions.
|
---|
| 141 | */
|
---|
| 142 | public static ToolbarPreferences toolbar;
|
---|
[626] | 143 |
|
---|
[1104] | 144 | public UndoRedoHandler undoRedo = new UndoRedoHandler();
|
---|
[626] | 145 |
|
---|
[4718] | 146 | public static PleaseWaitProgressMonitor currentProgressMonitor;
|
---|
| 147 |
|
---|
[1104] | 148 | /**
|
---|
| 149 | * The main menu bar at top of screen.
|
---|
| 150 | */
|
---|
[5134] | 151 | public MainMenu menu;
|
---|
[626] | 152 |
|
---|
[5134] | 153 | public OsmValidator validator;
|
---|
[1104] | 154 | /**
|
---|
[1356] | 155 | * The MOTD Layer.
|
---|
| 156 | */
|
---|
[3669] | 157 | private GettingStarted gettingStarted = new GettingStarted();
|
---|
[1356] | 158 |
|
---|
| 159 | /**
|
---|
[4190] | 160 | * Print a message if logging is on.
|
---|
[1104] | 161 | */
|
---|
[4190] | 162 | static public int log_level = 2;
|
---|
| 163 | static public void warn(String msg) {
|
---|
| 164 | if (log_level < 1)
|
---|
| 165 | return;
|
---|
| 166 | System.out.println(msg);
|
---|
| 167 | }
|
---|
| 168 | static public void info(String msg) {
|
---|
| 169 | if (log_level < 2)
|
---|
| 170 | return;
|
---|
| 171 | System.out.println(msg);
|
---|
| 172 | }
|
---|
[4126] | 173 | static public void debug(String msg) {
|
---|
[4190] | 174 | if (log_level < 3)
|
---|
[1104] | 175 | return;
|
---|
| 176 | System.out.println(msg);
|
---|
| 177 | }
|
---|
[301] | 178 |
|
---|
[1104] | 179 | /**
|
---|
| 180 | * Platform specific code goes in here.
|
---|
| 181 | * Plugins may replace it, however, some hooks will be called before any plugins have been loeaded.
|
---|
| 182 | * So if you need to hook into those early ones, split your class and send the one with the early hooks
|
---|
| 183 | * to the JOSM team for inclusion.
|
---|
| 184 | */
|
---|
| 185 | public static PlatformHook platform;
|
---|
[1023] | 186 |
|
---|
[1104] | 187 | /**
|
---|
[5262] | 188 | * Whether or not the java vm is openjdk
|
---|
[2987] | 189 | * We use this to work around openjdk bugs
|
---|
| 190 | */
|
---|
| 191 | public static boolean isOpenjdk;
|
---|
| 192 |
|
---|
| 193 | /**
|
---|
[1104] | 194 | * Set or clear (if passed <code>null</code>) the map.
|
---|
| 195 | */
|
---|
| 196 | public final void setMapFrame(final MapFrame map) {
|
---|
| 197 | MapFrame old = Main.map;
|
---|
| 198 | panel.setVisible(false);
|
---|
| 199 | panel.removeAll();
|
---|
[1750] | 200 | if (map != null) {
|
---|
[1104] | 201 | map.fillPanel(panel);
|
---|
[1750] | 202 | } else {
|
---|
[1104] | 203 | old.destroy();
|
---|
[1356] | 204 | panel.add(gettingStarted, BorderLayout.CENTER);
|
---|
[1104] | 205 | }
|
---|
| 206 | panel.setVisible(true);
|
---|
| 207 | redoUndoListener.commandChanged(0,0);
|
---|
[301] | 208 |
|
---|
[2485] | 209 | Main.map = map;
|
---|
| 210 |
|
---|
[2817] | 211 | PluginHandler.notifyMapFrameChanged(old, map);
|
---|
[4718] | 212 | if (map == null && currentProgressMonitor != null) {
|
---|
| 213 | currentProgressMonitor.showForegroundDialog();
|
---|
| 214 | }
|
---|
[1104] | 215 | }
|
---|
[626] | 216 |
|
---|
[1104] | 217 | /**
|
---|
[1169] | 218 | * Remove the specified layer from the map. If it is the last layer,
|
---|
[1096] | 219 | * remove the map as well.
|
---|
[1104] | 220 | */
|
---|
| 221 | public final void removeLayer(final Layer layer) {
|
---|
[1633] | 222 | if (map != null) {
|
---|
| 223 | map.mapView.removeLayer(layer);
|
---|
[4582] | 224 | if (map != null && map.mapView.getAllLayers().isEmpty()) {
|
---|
[1633] | 225 | setMapFrame(null);
|
---|
[1750] | 226 | }
|
---|
[1633] | 227 | }
|
---|
[1104] | 228 | }
|
---|
[626] | 229 |
|
---|
[4681] | 230 | private static InitStatusListener initListener = null;
|
---|
| 231 |
|
---|
| 232 | public static interface InitStatusListener {
|
---|
| 233 |
|
---|
| 234 | void updateStatus(String event);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | public static void setInitStatusListener(InitStatusListener listener) {
|
---|
| 238 | initListener = listener;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[1104] | 241 | public Main() {
|
---|
| 242 | main = this;
|
---|
[2987] | 243 | isOpenjdk = System.getProperty("java.vm.name").toUpperCase().indexOf("OPENJDK") != -1;
|
---|
[4681] | 244 |
|
---|
[4718] | 245 | if (initListener != null) {
|
---|
[4681] | 246 | initListener.updateStatus(tr("Executing platform startup hook"));
|
---|
[4718] | 247 | }
|
---|
[1104] | 248 | platform.startupHook();
|
---|
[3934] | 249 |
|
---|
[5138] | 250 | if (initListener != null) {
|
---|
| 251 | initListener.updateStatus(tr("Building main menu"));
|
---|
| 252 | }
|
---|
| 253 | contentPanePrivate.add(panel, BorderLayout.CENTER);
|
---|
| 254 | panel.add(gettingStarted, BorderLayout.CENTER);
|
---|
| 255 | menu = new MainMenu();
|
---|
| 256 |
|
---|
| 257 | undoRedo.addCommandQueueListener(redoUndoListener);
|
---|
| 258 |
|
---|
| 259 | // creating toolbar
|
---|
| 260 | contentPanePrivate.add(toolbar.control, BorderLayout.NORTH);
|
---|
| 261 |
|
---|
| 262 | registerActionShortcut(menu.help, Shortcut.registerShortcut("system:help", tr("Help"),
|
---|
| 263 | KeyEvent.VK_F1, Shortcut.DIRECT));
|
---|
| 264 |
|
---|
[5134] | 265 | // contains several initialization tasks to be executed (in parallel) by a ExecutorService
|
---|
| 266 | List<Callable<Void>> tasks = new ArrayList<Callable<Void>>();
|
---|
[3934] | 267 |
|
---|
[5134] | 268 | tasks.add(new Callable<Void>() {
|
---|
[626] | 269 |
|
---|
[5134] | 270 | @Override
|
---|
| 271 | public Void call() throws Exception {
|
---|
| 272 | // We try to establish an API connection early, so that any API
|
---|
| 273 | // capabilities are already known to the editor instance. However
|
---|
| 274 | // if it goes wrong that's not critical at this stage.
|
---|
| 275 | if (initListener != null) {
|
---|
| 276 | initListener.updateStatus(tr("Initializing OSM API"));
|
---|
| 277 | }
|
---|
| 278 | try {
|
---|
| 279 | OsmApi.getOsmApi().initialize(null, true);
|
---|
| 280 | } catch (Exception x) {
|
---|
| 281 | // ignore any exception here.
|
---|
| 282 | }
|
---|
| 283 | return null;
|
---|
| 284 | }
|
---|
| 285 | });
|
---|
[1023] | 286 |
|
---|
[5134] | 287 | tasks.add(new Callable<Void>() {
|
---|
[626] | 288 |
|
---|
[5134] | 289 | @Override
|
---|
| 290 | public Void call() throws Exception {
|
---|
| 291 | if (initListener != null) {
|
---|
| 292 | initListener.updateStatus(tr("Initializing presets"));
|
---|
| 293 | }
|
---|
| 294 | TaggingPresetPreference.initialize();
|
---|
[5177] | 295 | // some validator tests require the presets to be initialized
|
---|
| 296 | // TODO remove this dependency for parallel initialization
|
---|
| 297 | if (initListener != null) {
|
---|
| 298 | initListener.updateStatus(tr("Initializing validator"));
|
---|
| 299 | }
|
---|
| 300 | validator = new OsmValidator();
|
---|
| 301 | MapView.addLayerChangeListener(validator);
|
---|
[5134] | 302 | return null;
|
---|
| 303 | }
|
---|
| 304 | });
|
---|
| 305 |
|
---|
| 306 | tasks.add(new Callable<Void>() {
|
---|
| 307 |
|
---|
| 308 | @Override
|
---|
| 309 | public Void call() throws Exception {
|
---|
| 310 | if (initListener != null) {
|
---|
| 311 | initListener.updateStatus(tr("Initializing map styles"));
|
---|
| 312 | }
|
---|
| 313 | MapPaintPreference.initialize();
|
---|
| 314 | return null;
|
---|
| 315 | }
|
---|
| 316 | });
|
---|
| 317 |
|
---|
| 318 | tasks.add(new Callable<Void>() {
|
---|
| 319 |
|
---|
| 320 | @Override
|
---|
| 321 | public Void call() throws Exception {
|
---|
| 322 | if (initListener != null) {
|
---|
| 323 | initListener.updateStatus(tr("Loading imagery preferences"));
|
---|
| 324 | }
|
---|
| 325 | ImageryPreference.initialize();
|
---|
| 326 | return null;
|
---|
| 327 | }
|
---|
| 328 | });
|
---|
| 329 |
|
---|
| 330 | try {
|
---|
[5195] | 331 | for (Future<Void> i : Executors.newFixedThreadPool(
|
---|
| 332 | Runtime.getRuntime().availableProcessors()).invokeAll(tasks)) {
|
---|
| 333 | i.get();
|
---|
| 334 | }
|
---|
| 335 | } catch (Exception ex) {
|
---|
[5134] | 336 | throw new RuntimeException(ex);
|
---|
[4718] | 337 | }
|
---|
[3669] | 338 |
|
---|
[4489] | 339 | // hooks for the jmapviewer component
|
---|
| 340 | FeatureAdapter.registerBrowserAdapter(new FeatureAdapter.BrowserAdapter() {
|
---|
| 341 | @Override
|
---|
| 342 | public void openLink(String url) {
|
---|
| 343 | OpenBrowser.displayUrl(url);
|
---|
| 344 | }
|
---|
| 345 | });
|
---|
| 346 | FeatureAdapter.registerTranslationAdapter(I18n.getTranslationAdapter());
|
---|
| 347 |
|
---|
[4718] | 348 | if (initListener != null) {
|
---|
[4681] | 349 | initListener.updateStatus(tr("Updating user interface"));
|
---|
[4718] | 350 | }
|
---|
[4681] | 351 |
|
---|
[1104] | 352 | toolbar.refreshToolbarControl();
|
---|
[626] | 353 |
|
---|
[1104] | 354 | toolbar.control.updateUI();
|
---|
[3252] | 355 | contentPanePrivate.updateUI();
|
---|
[3934] | 356 |
|
---|
[1104] | 357 | }
|
---|
[626] | 358 |
|
---|
[1104] | 359 | /**
|
---|
| 360 | * Add a new layer to the map. If no map exists, create one.
|
---|
| 361 | */
|
---|
[5262] | 362 | public final synchronized void addLayer(final Layer layer) {
|
---|
[1104] | 363 | if (map == null) {
|
---|
[3252] | 364 | final MapFrame mapFrame = new MapFrame(contentPanePrivate);
|
---|
[1104] | 365 | setMapFrame(mapFrame);
|
---|
[4567] | 366 | mapFrame.selectMapMode((MapMode)mapFrame.getDefaultButtonAction(), layer);
|
---|
[1104] | 367 | mapFrame.setVisible(true);
|
---|
[2224] | 368 | mapFrame.initializeDialogsPane();
|
---|
[1895] | 369 | // bootstrapping problem: make sure the layer list dialog is going to
|
---|
| 370 | // listen to change events of the very first layer
|
---|
| 371 | //
|
---|
| 372 | layer.addPropertyChangeListener(LayerListDialog.getInstance().getModel());
|
---|
[1104] | 373 | }
|
---|
[5391] | 374 | layer.hookUpMapView();
|
---|
[1104] | 375 | map.mapView.addLayer(layer);
|
---|
| 376 | }
|
---|
[1814] | 377 |
|
---|
[1104] | 378 | /**
|
---|
[5357] | 379 | * Replies <code>true</code> if there is an edit layer
|
---|
[1811] | 380 | *
|
---|
[5357] | 381 | * @return <code>true</code> if there is an edit layer
|
---|
[1104] | 382 | */
|
---|
[1825] | 383 | public boolean hasEditLayer() {
|
---|
[3071] | 384 | if (getEditLayer() == null) return false;
|
---|
[1814] | 385 | return true;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | /**
|
---|
| 389 | * Replies the current edit layer
|
---|
[2224] | 390 | *
|
---|
[5357] | 391 | * @return the current edit layer. <code>null</code>, if no current edit layer exists
|
---|
[1814] | 392 | */
|
---|
| 393 | public OsmDataLayer getEditLayer() {
|
---|
| 394 | if (map == null) return null;
|
---|
| 395 | if (map.mapView == null) return null;
|
---|
[1750] | 396 | return map.mapView.getEditLayer();
|
---|
[1104] | 397 | }
|
---|
[626] | 398 |
|
---|
[1104] | 399 | /**
|
---|
[1814] | 400 | * Replies the current data set.
|
---|
[1811] | 401 | *
|
---|
[5357] | 402 | * @return the current data set. <code>null</code>, if no current data set exists
|
---|
[1750] | 403 | */
|
---|
[1814] | 404 | public DataSet getCurrentDataSet() {
|
---|
[1825] | 405 | if (!hasEditLayer()) return null;
|
---|
[1814] | 406 | return getEditLayer().data;
|
---|
[1750] | 407 | }
|
---|
| 408 |
|
---|
| 409 | /**
|
---|
[3071] | 410 | * Returns the currently active layer
|
---|
| 411 | *
|
---|
[5357] | 412 | * @return the currently active layer. <code>null</code>, if currently no active layer exists
|
---|
[3071] | 413 | */
|
---|
| 414 | public Layer getActiveLayer() {
|
---|
| 415 | if (map == null) return null;
|
---|
| 416 | if (map.mapView == null) return null;
|
---|
| 417 | return map.mapView.getActiveLayer();
|
---|
| 418 | }
|
---|
| 419 |
|
---|
[4869] | 420 | protected static final JPanel contentPanePrivate = new JPanel(new BorderLayout());
|
---|
[3252] | 421 |
|
---|
[4897] | 422 | public static void redirectToMainContentPane(JComponent source) {
|
---|
| 423 | RedirectInputMap.redirect(source, contentPanePrivate);
|
---|
| 424 | }
|
---|
[626] | 425 |
|
---|
[5278] | 426 | public static void registerActionShortcut(JosmAction action) {
|
---|
| 427 | registerActionShortcut(action, action.getShortcut());
|
---|
| 428 | }
|
---|
| 429 |
|
---|
[3252] | 430 | public static void registerActionShortcut(Action action, Shortcut shortcut) {
|
---|
[5002] | 431 | KeyStroke keyStroke = shortcut.getKeyStroke();
|
---|
[3252] | 432 | if (keyStroke == null)
|
---|
| 433 | return;
|
---|
| 434 |
|
---|
| 435 | InputMap inputMap = contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
|
---|
| 436 | Object existing = inputMap.get(keyStroke);
|
---|
| 437 | if (existing != null && !existing.equals(action)) {
|
---|
[3327] | 438 | System.out.println(String.format("Keystroke %s is already assigned to %s, will be overridden by %s", keyStroke, existing, action));
|
---|
[3252] | 439 | }
|
---|
| 440 | inputMap.put(keyStroke, action);
|
---|
| 441 |
|
---|
| 442 | contentPanePrivate.getActionMap().put(action, action);
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[5382] | 445 | public static void unregisterShortcut(Shortcut shortcut) {
|
---|
[3327] | 446 | contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(shortcut.getKeyStroke());
|
---|
[3252] | 447 | }
|
---|
| 448 |
|
---|
[3444] | 449 | public static void unregisterActionShortcut(JosmAction action) {
|
---|
| 450 | unregisterActionShortcut(action, action.getShortcut());
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | public static void unregisterActionShortcut(Action action, Shortcut shortcut) {
|
---|
[5382] | 454 | unregisterShortcut(shortcut);
|
---|
[3444] | 455 | contentPanePrivate.getActionMap().remove(action);
|
---|
| 456 | }
|
---|
| 457 |
|
---|
[1104] | 458 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 459 | // Implementation part
|
---|
| 460 | ///////////////////////////////////////////////////////////////////////////
|
---|
[626] | 461 |
|
---|
[4869] | 462 | public static final JPanel panel = new JPanel(new BorderLayout());
|
---|
[626] | 463 |
|
---|
[4932] | 464 | protected static WindowGeometry geometry;
|
---|
[2876] | 465 | protected static int windowState = JFrame.NORMAL;
|
---|
[626] | 466 |
|
---|
[1104] | 467 | private final CommandQueueListener redoUndoListener = new CommandQueueListener(){
|
---|
| 468 | public void commandChanged(final int queueSize, final int redoSize) {
|
---|
| 469 | menu.undo.setEnabled(queueSize > 0);
|
---|
| 470 | menu.redo.setEnabled(redoSize > 0);
|
---|
| 471 | }
|
---|
| 472 | };
|
---|
[1722] | 473 |
|
---|
[1104] | 474 | /**
|
---|
| 475 | * Should be called before the main constructor to setup some parameter stuff
|
---|
| 476 | * @param args The parsed argument list.
|
---|
| 477 | */
|
---|
[5279] | 478 | public static void preConstructorInit(Map<Option, Collection<String>> args) {
|
---|
[2272] | 479 | ProjectionPreference.setProjection();
|
---|
[626] | 480 |
|
---|
[1104] | 481 | try {
|
---|
[2708] | 482 | String defaultlaf = platform.getDefaultStyle();
|
---|
[2709] | 483 | String laf = Main.pref.get("laf", defaultlaf);
|
---|
[1188] | 484 | try {
|
---|
[2709] | 485 | UIManager.setLookAndFeel(laf);
|
---|
[1188] | 486 | }
|
---|
[2708] | 487 | catch (final java.lang.ClassNotFoundException e) {
|
---|
[2709] | 488 | System.out.println("Look and Feel not found: " + laf);
|
---|
[2708] | 489 | Main.pref.put("laf", defaultlaf);
|
---|
| 490 | }
|
---|
[1188] | 491 | catch (final javax.swing.UnsupportedLookAndFeelException e) {
|
---|
[2709] | 492 | System.out.println("Look and Feel not supported: " + laf);
|
---|
[2708] | 493 | Main.pref.put("laf", defaultlaf);
|
---|
[1188] | 494 | }
|
---|
[1104] | 495 | toolbar = new ToolbarPreferences();
|
---|
[3252] | 496 | contentPanePrivate.updateUI();
|
---|
[1104] | 497 | panel.updateUI();
|
---|
| 498 | } catch (final Exception e) {
|
---|
| 499 | e.printStackTrace();
|
---|
| 500 | }
|
---|
| 501 | UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
|
---|
| 502 | UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
|
---|
| 503 | UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
|
---|
| 504 | UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
|
---|
[626] | 505 |
|
---|
[3433] | 506 | I18n.translateJavaInternalMessages();
|
---|
[3378] | 507 |
|
---|
[1990] | 508 | // init default coordinate format
|
---|
| 509 | //
|
---|
| 510 | try {
|
---|
[2039] | 511 | //CoordinateFormat format = CoordinateFormat.valueOf(Main.pref.get("coordinates"));
|
---|
[1990] | 512 | CoordinateFormat.setCoordinateFormat(CoordinateFormat.valueOf(Main.pref.get("coordinates")));
|
---|
| 513 | } catch (IllegalArgumentException iae) {
|
---|
| 514 | CoordinateFormat.setCoordinateFormat(CoordinateFormat.DECIMAL_DEGREES);
|
---|
| 515 | }
|
---|
| 516 |
|
---|
[4932] | 517 | geometry = WindowGeometry.mainWindow("gui.geometry",
|
---|
[5279] | 518 | (args.containsKey(Option.GEOMETRY) ? args.get(Option.GEOMETRY).iterator().next() : null),
|
---|
| 519 | !args.containsKey(Option.NO_MAXIMIZE) && Main.pref.getBoolean("gui.maximized", false));
|
---|
[1104] | 520 | }
|
---|
[626] | 521 |
|
---|
[5279] | 522 | public void postConstructorProcessCmdLine(Map<Option, Collection<String>> args) {
|
---|
| 523 | if (args.containsKey(Option.DOWNLOAD)) {
|
---|
[2798] | 524 | List<File> fileList = new ArrayList<File>();
|
---|
[5279] | 525 | for (String s : args.get(Option.DOWNLOAD)) {
|
---|
[2798] | 526 | File f = null;
|
---|
| 527 | switch(paramType(s)) {
|
---|
| 528 | case httpUrl:
|
---|
| 529 | downloadFromParamHttp(false, s);
|
---|
| 530 | break;
|
---|
| 531 | case bounds:
|
---|
| 532 | downloadFromParamBounds(false, s);
|
---|
| 533 | break;
|
---|
| 534 | case fileUrl:
|
---|
| 535 | try {
|
---|
| 536 | f = new File(new URI(s));
|
---|
| 537 | } catch (URISyntaxException e) {
|
---|
| 538 | JOptionPane.showMessageDialog(
|
---|
| 539 | Main.parent,
|
---|
| 540 | tr("Ignoring malformed file URL: \"{0}\"", s),
|
---|
| 541 | tr("Warning"),
|
---|
| 542 | JOptionPane.WARNING_MESSAGE
|
---|
[4718] | 543 | );
|
---|
[2798] | 544 | }
|
---|
| 545 | if (f!=null) {
|
---|
| 546 | fileList.add(f);
|
---|
| 547 | }
|
---|
| 548 | break;
|
---|
| 549 | case fileName:
|
---|
| 550 | f = new File(s);
|
---|
| 551 | fileList.add(f);
|
---|
| 552 | break;
|
---|
| 553 | }
|
---|
[1750] | 554 | }
|
---|
[2798] | 555 | if(!fileList.isEmpty())
|
---|
| 556 | {
|
---|
[3924] | 557 | OpenFileAction.openFiles(fileList, true);
|
---|
[2798] | 558 | }
|
---|
[1750] | 559 | }
|
---|
[5279] | 560 | if (args.containsKey(Option.DOWNLOADGPS)) {
|
---|
| 561 | for (String s : args.get(Option.DOWNLOADGPS)) {
|
---|
[2798] | 562 | switch(paramType(s)) {
|
---|
| 563 | case httpUrl:
|
---|
| 564 | downloadFromParamHttp(true, s);
|
---|
| 565 | break;
|
---|
| 566 | case bounds:
|
---|
| 567 | downloadFromParamBounds(true, s);
|
---|
| 568 | break;
|
---|
| 569 | case fileUrl:
|
---|
| 570 | case fileName:
|
---|
| 571 | JOptionPane.showMessageDialog(
|
---|
| 572 | Main.parent,
|
---|
| 573 | tr("Parameter \"downloadgps\" does not accept file names or file URLs"),
|
---|
| 574 | tr("Warning"),
|
---|
| 575 | JOptionPane.WARNING_MESSAGE
|
---|
[4718] | 576 | );
|
---|
[2798] | 577 | }
|
---|
[1750] | 578 | }
|
---|
| 579 | }
|
---|
[5279] | 580 | if (args.containsKey(Option.SELECTION)) {
|
---|
| 581 | for (String s : args.get(Option.SELECTION)) {
|
---|
[3317] | 582 | SearchAction.search(s, SearchAction.SearchMode.add);
|
---|
[1750] | 583 | }
|
---|
| 584 | }
|
---|
[1104] | 585 | }
|
---|
[626] | 586 |
|
---|
[2025] | 587 | public static boolean saveUnsavedModifications() {
|
---|
| 588 | if (map == null) return true;
|
---|
| 589 | SaveLayersDialog dialog = new SaveLayersDialog(Main.parent);
|
---|
| 590 | List<OsmDataLayer> layersWithUnmodifiedChanges = new ArrayList<OsmDataLayer>();
|
---|
| 591 | for (OsmDataLayer l: Main.map.mapView.getLayersOfType(OsmDataLayer.class)) {
|
---|
[5112] | 592 | if ((l.requiresSaveToFile() || l.requiresUploadToServer()) && l.data.isModified()) {
|
---|
[2025] | 593 | layersWithUnmodifiedChanges.add(l);
|
---|
| 594 | }
|
---|
| 595 | }
|
---|
| 596 | dialog.prepareForSavingAndUpdatingLayersBeforeExit();
|
---|
| 597 | if (!layersWithUnmodifiedChanges.isEmpty()) {
|
---|
| 598 | dialog.getModel().populate(layersWithUnmodifiedChanges);
|
---|
| 599 | dialog.setVisible(true);
|
---|
| 600 | switch(dialog.getUserAction()) {
|
---|
[2485] | 601 | case CANCEL: return false;
|
---|
| 602 | case PROCEED: return true;
|
---|
| 603 | default: return false;
|
---|
[2025] | 604 | }
|
---|
| 605 | }
|
---|
[2274] | 606 |
|
---|
[2025] | 607 | return true;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
[3378] | 610 | public static boolean exitJosm(boolean exit) {
|
---|
| 611 | if (Main.saveUnsavedModifications()) {
|
---|
[4932] | 612 | geometry.remember("gui.geometry");
|
---|
| 613 | if (map != null) {
|
---|
| 614 | map.rememberToggleDialogWidth();
|
---|
| 615 | }
|
---|
| 616 | pref.put("gui.maximized", (windowState & JFrame.MAXIMIZED_BOTH) != 0);
|
---|
[3378] | 617 | // Remove all layers because somebody may rely on layerRemoved events (like AutosaveTask)
|
---|
| 618 | if (Main.isDisplayingMapView()) {
|
---|
| 619 | Collection<Layer> layers = new ArrayList<Layer>(Main.map.mapView.getAllLayers());
|
---|
| 620 | for (Layer l: layers) {
|
---|
| 621 | Main.map.mapView.removeLayer(l);
|
---|
| 622 | }
|
---|
| 623 | }
|
---|
| 624 | if (exit) {
|
---|
| 625 | System.exit(0);
|
---|
| 626 | return true;
|
---|
| 627 | } else
|
---|
| 628 | return true;
|
---|
| 629 | } else
|
---|
| 630 | return false;
|
---|
| 631 | }
|
---|
| 632 |
|
---|
[2798] | 633 | /**
|
---|
| 634 | * The type of a command line parameter, to be used in switch statements.
|
---|
[5195] | 635 | * @see #paramType
|
---|
[2798] | 636 | */
|
---|
| 637 | private enum DownloadParamType { httpUrl, fileUrl, bounds, fileName }
|
---|
| 638 |
|
---|
| 639 | /**
|
---|
| 640 | * Guess the type of a parameter string specified on the command line with --download= or --downloadgps.
|
---|
| 641 | * @param s A parameter string
|
---|
| 642 | * @return The guessed parameter type
|
---|
| 643 | */
|
---|
| 644 | private DownloadParamType paramType(String s) {
|
---|
| 645 | if(s.startsWith("http:")) return DownloadParamType.httpUrl;
|
---|
| 646 | if(s.startsWith("file:")) return DownloadParamType.fileUrl;
|
---|
[5240] | 647 | String coorPattern = "\\s*[+-]?[0-9]+(\\.[0-9]+)?\\s*";
|
---|
[4901] | 648 | if(s.matches(coorPattern+"(,"+coorPattern+"){3}")) return DownloadParamType.bounds;
|
---|
[2798] | 649 | // everything else must be a file name
|
---|
| 650 | return DownloadParamType.fileName;
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | /**
|
---|
| 654 | * Download area specified on the command line as OSM URL.
|
---|
| 655 | * @param rawGps Flag to download raw GPS tracks
|
---|
| 656 | * @param s The URL parameter
|
---|
| 657 | */
|
---|
| 658 | private static void downloadFromParamHttp(final boolean rawGps, String s) {
|
---|
| 659 | final Bounds b = OsmUrlToBounds.parse(s);
|
---|
| 660 | if (b == null) {
|
---|
| 661 | JOptionPane.showMessageDialog(
|
---|
| 662 | Main.parent,
|
---|
| 663 | tr("Ignoring malformed URL: \"{0}\"", s),
|
---|
| 664 | tr("Warning"),
|
---|
| 665 | JOptionPane.WARNING_MESSAGE
|
---|
[4718] | 666 | );
|
---|
[2798] | 667 | } else {
|
---|
| 668 | downloadFromParamBounds(rawGps, b);
|
---|
[1104] | 669 | }
|
---|
[2798] | 670 | }
|
---|
[626] | 671 |
|
---|
[2798] | 672 | /**
|
---|
| 673 | * Download area specified on the command line as bounds string.
|
---|
| 674 | * @param rawGps Flag to download raw GPS tracks
|
---|
| 675 | * @param s The bounds parameter
|
---|
| 676 | */
|
---|
| 677 | private static void downloadFromParamBounds(final boolean rawGps, String s) {
|
---|
[1104] | 678 | final StringTokenizer st = new StringTokenizer(s, ",");
|
---|
| 679 | if (st.countTokens() == 4) {
|
---|
[2327] | 680 | Bounds b = new Bounds(
|
---|
[2485] | 681 | new LatLon(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken())),
|
---|
| 682 | new LatLon(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()))
|
---|
[4718] | 683 | );
|
---|
[2798] | 684 | downloadFromParamBounds(rawGps, b);
|
---|
[1104] | 685 | }
|
---|
| 686 | }
|
---|
[1023] | 687 |
|
---|
[2798] | 688 | /**
|
---|
| 689 | * Download area specified as Bounds value.
|
---|
| 690 | * @param rawGps Flag to download raw GPS tracks
|
---|
| 691 | * @param b The bounds value
|
---|
[5195] | 692 | * @see #downloadFromParamBounds(boolean, String)
|
---|
| 693 | * @see #downloadFromParamHttp
|
---|
[2798] | 694 | */
|
---|
| 695 | private static void downloadFromParamBounds(final boolean rawGps, Bounds b) {
|
---|
| 696 | DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
|
---|
| 697 | // asynchronously launch the download task ...
|
---|
| 698 | Future<?> future = task.download(true, b, null);
|
---|
| 699 | // ... and the continuation when the download is finished (this will wait for the download to finish)
|
---|
| 700 | Main.worker.execute(new PostDownloadHandler(task, future));
|
---|
| 701 | }
|
---|
| 702 |
|
---|
[1849] | 703 | public static void determinePlatformHook() {
|
---|
[1104] | 704 | String os = System.getProperty("os.name");
|
---|
| 705 | if (os == null) {
|
---|
| 706 | System.err.println("Your operating system has no name, so I'm guessing its some kind of *nix.");
|
---|
| 707 | platform = new PlatformHookUnixoid();
|
---|
| 708 | } else if (os.toLowerCase().startsWith("windows")) {
|
---|
| 709 | platform = new PlatformHookWindows();
|
---|
[1169] | 710 | } else if (os.equals("Linux") || os.equals("Solaris") ||
|
---|
[1750] | 711 | os.equals("SunOS") || os.equals("AIX") ||
|
---|
| 712 | os.equals("FreeBSD") || os.equals("NetBSD") || os.equals("OpenBSD")) {
|
---|
[1104] | 713 | platform = new PlatformHookUnixoid();
|
---|
| 714 | } else if (os.toLowerCase().startsWith("mac os x")) {
|
---|
| 715 | platform = new PlatformHookOsx();
|
---|
| 716 | } else {
|
---|
| 717 | System.err.println("I don't know your operating system '"+os+"', so I'm guessing its some kind of *nix.");
|
---|
| 718 | platform = new PlatformHookUnixoid();
|
---|
| 719 | }
|
---|
| 720 | }
|
---|
[1023] | 721 |
|
---|
[2876] | 722 | private static class WindowPositionSizeListener extends WindowAdapter implements
|
---|
| 723 | ComponentListener {
|
---|
| 724 | @Override
|
---|
| 725 | public void windowStateChanged(WindowEvent e) {
|
---|
| 726 | Main.windowState = e.getNewState();
|
---|
| 727 | }
|
---|
| 728 |
|
---|
[4932] | 729 | @Override
|
---|
[2876] | 730 | public void componentHidden(ComponentEvent e) {
|
---|
| 731 | }
|
---|
| 732 |
|
---|
[4932] | 733 | @Override
|
---|
[2876] | 734 | public void componentMoved(ComponentEvent e) {
|
---|
| 735 | handleComponentEvent(e);
|
---|
| 736 | }
|
---|
| 737 |
|
---|
[4932] | 738 | @Override
|
---|
[2876] | 739 | public void componentResized(ComponentEvent e) {
|
---|
| 740 | handleComponentEvent(e);
|
---|
| 741 | }
|
---|
| 742 |
|
---|
[4932] | 743 | @Override
|
---|
[2876] | 744 | public void componentShown(ComponentEvent e) {
|
---|
| 745 | }
|
---|
| 746 |
|
---|
| 747 | private void handleComponentEvent(ComponentEvent e) {
|
---|
| 748 | Component c = e.getComponent();
|
---|
[4932] | 749 | if (c instanceof JFrame && c.isVisible() && Main.windowState == JFrame.NORMAL) {
|
---|
| 750 | Main.geometry = new WindowGeometry((JFrame) c);
|
---|
[2876] | 751 | }
|
---|
| 752 | }
|
---|
| 753 | }
|
---|
| 754 | public static void addListener() {
|
---|
| 755 | parent.addComponentListener(new WindowPositionSizeListener());
|
---|
| 756 | ((JFrame)parent).addWindowStateListener(new WindowPositionSizeListener());
|
---|
| 757 | }
|
---|
[3815] | 758 |
|
---|
| 759 | public static void checkJava6() {
|
---|
| 760 | String version = System.getProperty("java.version");
|
---|
| 761 | if (version != null) {
|
---|
| 762 | if (version.startsWith("1.6") || version.startsWith("6") ||
|
---|
| 763 | version.startsWith("1.7") || version.startsWith("7"))
|
---|
| 764 | return;
|
---|
| 765 | if (version.startsWith("1.5") || version.startsWith("5")) {
|
---|
| 766 | JLabel ho = new JLabel("<html>"+
|
---|
| 767 | tr("<h2>JOSM requires Java version 6.</h2>"+
|
---|
| 768 | "Detected Java version: {0}.<br>"+
|
---|
| 769 | "You can <ul><li>update your Java (JRE) or</li>"+
|
---|
| 770 | "<li>use an earlier (Java 5 compatible) version of JOSM.</li></ul>"+
|
---|
| 771 | "More Info:", version)+"</html>");
|
---|
| 772 | JTextArea link = new JTextArea("http://josm.openstreetmap.de/wiki/Help/SystemRequirements");
|
---|
| 773 | link.setEditable(false);
|
---|
| 774 | link.setBackground(panel.getBackground());
|
---|
| 775 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
| 776 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
| 777 | gbc.gridwidth = GridBagConstraints.REMAINDER;
|
---|
| 778 | gbc.anchor = GridBagConstraints.WEST;
|
---|
| 779 | gbc.weightx = 1.0;
|
---|
| 780 | panel.add(ho, gbc);
|
---|
| 781 | panel.add(link, gbc);
|
---|
| 782 | final String EXIT = tr("Exit JOSM");
|
---|
| 783 | final String CONTINUE = tr("Continue, try anyway");
|
---|
| 784 | int ret = JOptionPane.showOptionDialog(null, panel, tr("Error"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, new String[] {EXIT, CONTINUE}, EXIT);
|
---|
| 785 | if (ret == 0) {
|
---|
| 786 | System.exit(0);
|
---|
| 787 | }
|
---|
| 788 | return;
|
---|
| 789 | }
|
---|
| 790 | }
|
---|
| 791 | System.err.println("Error: Could not recognize Java Version: "+version);
|
---|
| 792 | }
|
---|
[4126] | 793 |
|
---|
| 794 | /* ----------------------------------------------------------------------------------------- */
|
---|
| 795 | /* projection handling - Main is a registry for a single, global projection instance */
|
---|
| 796 | /* */
|
---|
| 797 | /* TODO: For historical reasons the registry is implemented by Main. An alternative approach */
|
---|
| 798 | /* would be a singleton org.openstreetmap.josm.data.projection.ProjectionRegistry class. */
|
---|
| 799 | /* ----------------------------------------------------------------------------------------- */
|
---|
| 800 | /**
|
---|
| 801 | * The projection method used.
|
---|
[4897] | 802 | * use {@link #getProjection()} and {@link #setProjection(Projection)} for access.
|
---|
| 803 | * Use {@link #setProjection(Projection)} in order to trigger a projection change event.
|
---|
[4126] | 804 | */
|
---|
[4897] | 805 | private static Projection proj;
|
---|
[4126] | 806 |
|
---|
| 807 | /**
|
---|
| 808 | * Replies the current projection.
|
---|
[4489] | 809 | *
|
---|
[5357] | 810 | * @return the currently active projection
|
---|
[4126] | 811 | */
|
---|
| 812 | public static Projection getProjection() {
|
---|
| 813 | return proj;
|
---|
| 814 | }
|
---|
| 815 |
|
---|
| 816 | /**
|
---|
| 817 | * Sets the current projection
|
---|
[4489] | 818 | *
|
---|
[4126] | 819 | * @param p the projection
|
---|
| 820 | */
|
---|
| 821 | public static void setProjection(Projection p) {
|
---|
| 822 | CheckParameterUtil.ensureParameterNotNull(p);
|
---|
| 823 | Projection oldValue = proj;
|
---|
[5234] | 824 | Bounds b = (Main.map != null && Main.map.mapView != null) ? Main.map.mapView.getRealBounds() : null;
|
---|
[4126] | 825 | proj = p;
|
---|
[5234] | 826 | fireProjectionChanged(oldValue, proj, b);
|
---|
[4126] | 827 | }
|
---|
| 828 |
|
---|
| 829 | /*
|
---|
| 830 | * Keep WeakReferences to the listeners. This relieves clients from the burden of
|
---|
| 831 | * explicitly removing the listeners and allows us to transparently register every
|
---|
| 832 | * created dataset as projection change listener.
|
---|
| 833 | */
|
---|
| 834 | private static final ArrayList<WeakReference<ProjectionChangeListener>> listeners = new ArrayList<WeakReference<ProjectionChangeListener>>();
|
---|
| 835 |
|
---|
[5234] | 836 | private static void fireProjectionChanged(Projection oldValue, Projection newValue, Bounds oldBounds) {
|
---|
[4126] | 837 | if (newValue == null ^ oldValue == null
|
---|
[4288] | 838 | || (newValue != null && oldValue != null && !Utils.equal(newValue.toCode(), oldValue.toCode()))) {
|
---|
[4126] | 839 |
|
---|
| 840 | synchronized(Main.class) {
|
---|
| 841 | Iterator<WeakReference<ProjectionChangeListener>> it = listeners.iterator();
|
---|
| 842 | while(it.hasNext()){
|
---|
| 843 | WeakReference<ProjectionChangeListener> wr = it.next();
|
---|
[4724] | 844 | ProjectionChangeListener listener = wr.get();
|
---|
| 845 | if (listener == null) {
|
---|
[4126] | 846 | it.remove();
|
---|
| 847 | continue;
|
---|
| 848 | }
|
---|
[4724] | 849 | listener.projectionChanged(oldValue, newValue);
|
---|
[4126] | 850 | }
|
---|
| 851 | }
|
---|
[5234] | 852 | if (newValue != null && oldBounds != null) {
|
---|
| 853 | Main.map.mapView.zoomTo(oldBounds);
|
---|
[4126] | 854 | }
|
---|
| 855 | /* TODO - remove layers with fixed projection */
|
---|
| 856 | }
|
---|
| 857 | }
|
---|
| 858 |
|
---|
| 859 | /**
|
---|
| 860 | * Register a projection change listener
|
---|
[4489] | 861 | *
|
---|
[5357] | 862 | * @param listener the listener. Ignored if <code>null</code>.
|
---|
[4126] | 863 | */
|
---|
| 864 | public static void addProjectionChangeListener(ProjectionChangeListener listener) {
|
---|
| 865 | if (listener == null) return;
|
---|
| 866 | synchronized (Main.class) {
|
---|
| 867 | for (WeakReference<ProjectionChangeListener> wr : listeners) {
|
---|
| 868 | // already registered ? => abort
|
---|
| 869 | if (wr.get() == listener) return;
|
---|
| 870 | }
|
---|
[4774] | 871 | listeners.add(new WeakReference<ProjectionChangeListener>(listener));
|
---|
[4126] | 872 | }
|
---|
| 873 | }
|
---|
| 874 |
|
---|
| 875 | /**
|
---|
| 876 | * Removes a projection change listener
|
---|
[4489] | 877 | *
|
---|
[5357] | 878 | * @param listener the listener. Ignored if <code>null</code>.
|
---|
[4126] | 879 | */
|
---|
| 880 | public static void removeProjectionChangeListener(ProjectionChangeListener listener) {
|
---|
| 881 | if (listener == null) return;
|
---|
| 882 | synchronized(Main.class){
|
---|
| 883 | Iterator<WeakReference<ProjectionChangeListener>> it = listeners.iterator();
|
---|
| 884 | while(it.hasNext()){
|
---|
| 885 | WeakReference<ProjectionChangeListener> wr = it.next();
|
---|
| 886 | // remove the listener - and any other listener which god garbage
|
---|
| 887 | // collected in the meantime
|
---|
| 888 | if (wr.get() == null || wr.get() == listener) {
|
---|
| 889 | it.remove();
|
---|
| 890 | }
|
---|
| 891 | }
|
---|
| 892 | }
|
---|
| 893 | }
|
---|
[626] | 894 | }
|
---|