[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;
|
---|
| 7 | import java.awt.Dimension;
|
---|
| 8 | import java.awt.Rectangle;
|
---|
| 9 | import java.awt.Toolkit;
|
---|
[2876] | 10 | import java.awt.event.ComponentEvent;
|
---|
| 11 | import java.awt.event.ComponentListener;
|
---|
[301] | 12 | import java.awt.event.KeyEvent;
|
---|
[2876] | 13 | import java.awt.event.WindowAdapter;
|
---|
| 14 | import java.awt.event.WindowEvent;
|
---|
[301] | 15 | import java.io.File;
|
---|
| 16 | import java.net.URI;
|
---|
| 17 | import java.net.URISyntaxException;
|
---|
[2025] | 18 | import java.util.ArrayList;
|
---|
[301] | 19 | import java.util.Collection;
|
---|
[2025] | 20 | import java.util.List;
|
---|
[301] | 21 | import java.util.Map;
|
---|
| 22 | import java.util.StringTokenizer;
|
---|
[1465] | 23 | import java.util.concurrent.ExecutorService;
|
---|
[301] | 24 | import java.util.concurrent.Executors;
|
---|
[2322] | 25 | import java.util.concurrent.Future;
|
---|
[301] | 26 | import java.util.regex.Matcher;
|
---|
| 27 | import java.util.regex.Pattern;
|
---|
| 28 |
|
---|
| 29 | import javax.swing.JComponent;
|
---|
[1356] | 30 | import javax.swing.JFrame;
|
---|
[301] | 31 | import javax.swing.JOptionPane;
|
---|
| 32 | import javax.swing.JPanel;
|
---|
| 33 | import javax.swing.UIManager;
|
---|
| 34 |
|
---|
[2047] | 35 | import org.openstreetmap.josm.actions.OpenFileAction;
|
---|
[301] | 36 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
| 37 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
[2322] | 38 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
|
---|
| 39 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
[301] | 40 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
| 41 | import org.openstreetmap.josm.actions.search.SearchAction;
|
---|
| 42 | import org.openstreetmap.josm.data.Bounds;
|
---|
| 43 | import org.openstreetmap.josm.data.Preferences;
|
---|
| 44 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
[1990] | 45 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
---|
[2327] | 46 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[301] | 47 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[2305] | 48 | import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
|
---|
[301] | 49 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
| 50 | import org.openstreetmap.josm.gui.GettingStarted;
|
---|
| 51 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
| 52 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
[1895] | 53 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
[2025] | 54 | import org.openstreetmap.josm.gui.io.SaveLayersDialog;
|
---|
[301] | 55 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
| 56 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
| 57 | import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
|
---|
[486] | 58 | import org.openstreetmap.josm.gui.preferences.MapPaintPreference;
|
---|
[2272] | 59 | import org.openstreetmap.josm.gui.preferences.ProjectionPreference;
|
---|
[301] | 60 | import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference;
|
---|
| 61 | import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
---|
[1326] | 62 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
[301] | 63 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[1307] | 64 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
[1023] | 65 | import org.openstreetmap.josm.tools.PlatformHook;
|
---|
[1465] | 66 | import org.openstreetmap.josm.tools.PlatformHookOsx;
|
---|
[1023] | 67 | import org.openstreetmap.josm.tools.PlatformHookUnixoid;
|
---|
| 68 | import org.openstreetmap.josm.tools.PlatformHookWindows;
|
---|
[1084] | 69 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[626] | 70 |
|
---|
| 71 | abstract public class Main {
|
---|
[2485] | 72 |
|
---|
[1104] | 73 | /**
|
---|
[2343] | 74 | * Replies true if JOSM currently displays a map view. False, if it doesn't, i.e. if
|
---|
| 75 | * it only shows the MOTD panel.
|
---|
[2512] | 76 | *
|
---|
[2343] | 77 | * @return true if JOSM currently displays a map view
|
---|
| 78 | */
|
---|
| 79 | static public boolean isDisplayingMapView() {
|
---|
| 80 | if (map == null) return false;
|
---|
| 81 | if (map.mapView == null) return false;
|
---|
| 82 | return true;
|
---|
| 83 | }
|
---|
| 84 | /**
|
---|
[1104] | 85 | * Global parent component for all dialogs and message boxes
|
---|
| 86 | */
|
---|
| 87 | public static Component parent;
|
---|
| 88 | /**
|
---|
| 89 | * Global application.
|
---|
| 90 | */
|
---|
| 91 | public static Main main;
|
---|
| 92 | /**
|
---|
| 93 | * The worker thread slave. This is for executing all long and intensive
|
---|
| 94 | * calculations. The executed runnables are guaranteed to be executed separately
|
---|
| 95 | * and sequential.
|
---|
| 96 | */
|
---|
[1465] | 97 | public final static ExecutorService worker = Executors.newSingleThreadExecutor();
|
---|
[1104] | 98 | /**
|
---|
| 99 | * Global application preferences
|
---|
| 100 | */
|
---|
| 101 | public static Preferences pref = new Preferences();
|
---|
[1814] | 102 |
|
---|
[1104] | 103 | /**
|
---|
| 104 | * The global paste buffer.
|
---|
| 105 | */
|
---|
[2305] | 106 | public static PrimitiveDeepCopy pasteBuffer = new PrimitiveDeepCopy();
|
---|
[1592] | 107 | public static Layer pasteSource;
|
---|
[1104] | 108 | /**
|
---|
| 109 | * The projection method used.
|
---|
| 110 | */
|
---|
| 111 | public static Projection proj;
|
---|
| 112 | /**
|
---|
| 113 | * The MapFrame. Use setMapFrame to set or clear it.
|
---|
| 114 | */
|
---|
| 115 | public static MapFrame map;
|
---|
| 116 | /**
|
---|
| 117 | * True, when in applet mode
|
---|
| 118 | */
|
---|
| 119 | public static boolean applet = false;
|
---|
[626] | 120 |
|
---|
[1104] | 121 | /**
|
---|
| 122 | * The toolbar preference control to register new actions.
|
---|
| 123 | */
|
---|
| 124 | public static ToolbarPreferences toolbar;
|
---|
[626] | 125 |
|
---|
[1104] | 126 | public UndoRedoHandler undoRedo = new UndoRedoHandler();
|
---|
[626] | 127 |
|
---|
[1104] | 128 | /**
|
---|
| 129 | * The main menu bar at top of screen.
|
---|
| 130 | */
|
---|
| 131 | public final MainMenu menu;
|
---|
[626] | 132 |
|
---|
[1104] | 133 | /**
|
---|
[1356] | 134 | * The MOTD Layer.
|
---|
| 135 | */
|
---|
| 136 | private GettingStarted gettingStarted=new GettingStarted();
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
[1104] | 139 | * Print a debug message if debugging is on.
|
---|
| 140 | */
|
---|
| 141 | static public int debug_level = 1;
|
---|
| 142 | static public final void debug(String msg) {
|
---|
| 143 | if (debug_level <= 0)
|
---|
| 144 | return;
|
---|
| 145 | System.out.println(msg);
|
---|
| 146 | }
|
---|
[301] | 147 |
|
---|
[1104] | 148 | /**
|
---|
| 149 | * Platform specific code goes in here.
|
---|
| 150 | * Plugins may replace it, however, some hooks will be called before any plugins have been loeaded.
|
---|
| 151 | * So if you need to hook into those early ones, split your class and send the one with the early hooks
|
---|
| 152 | * to the JOSM team for inclusion.
|
---|
| 153 | */
|
---|
| 154 | public static PlatformHook platform;
|
---|
[1023] | 155 |
|
---|
[1104] | 156 | /**
|
---|
[2987] | 157 | * Wheather or not the java vm is openjdk
|
---|
| 158 | * We use this to work around openjdk bugs
|
---|
| 159 | */
|
---|
| 160 | public static boolean isOpenjdk;
|
---|
| 161 |
|
---|
| 162 | /**
|
---|
[1104] | 163 | * Set or clear (if passed <code>null</code>) the map.
|
---|
| 164 | */
|
---|
| 165 | public final void setMapFrame(final MapFrame map) {
|
---|
| 166 | MapFrame old = Main.map;
|
---|
| 167 | panel.setVisible(false);
|
---|
| 168 | panel.removeAll();
|
---|
[1750] | 169 | if (map != null) {
|
---|
[1104] | 170 | map.fillPanel(panel);
|
---|
[1750] | 171 | } else {
|
---|
[1104] | 172 | old.destroy();
|
---|
[1356] | 173 | panel.add(gettingStarted, BorderLayout.CENTER);
|
---|
[1104] | 174 | }
|
---|
| 175 | panel.setVisible(true);
|
---|
| 176 | redoUndoListener.commandChanged(0,0);
|
---|
[301] | 177 |
|
---|
[2485] | 178 | Main.map = map;
|
---|
| 179 |
|
---|
[2817] | 180 | PluginHandler.notifyMapFrameChanged(old, map);
|
---|
[1104] | 181 | }
|
---|
[626] | 182 |
|
---|
[1104] | 183 | /**
|
---|
[1169] | 184 | * Remove the specified layer from the map. If it is the last layer,
|
---|
[1096] | 185 | * remove the map as well.
|
---|
[1104] | 186 | */
|
---|
| 187 | public final void removeLayer(final Layer layer) {
|
---|
[1633] | 188 | if (map != null) {
|
---|
| 189 | map.mapView.removeLayer(layer);
|
---|
[1750] | 190 | if (map.mapView.getAllLayers().isEmpty()) {
|
---|
[1633] | 191 | setMapFrame(null);
|
---|
[1750] | 192 | }
|
---|
[1633] | 193 | }
|
---|
[1104] | 194 | }
|
---|
[626] | 195 |
|
---|
[1104] | 196 | public Main() {
|
---|
| 197 | main = this;
|
---|
[2987] | 198 | isOpenjdk = System.getProperty("java.vm.name").toUpperCase().indexOf("OPENJDK") != -1;
|
---|
[1104] | 199 | platform.startupHook();
|
---|
| 200 | contentPane.add(panel, BorderLayout.CENTER);
|
---|
[1356] | 201 | panel.add(gettingStarted, BorderLayout.CENTER);
|
---|
[1104] | 202 | menu = new MainMenu();
|
---|
[626] | 203 |
|
---|
[1104] | 204 | undoRedo.listenerCommands.add(redoUndoListener);
|
---|
[1023] | 205 |
|
---|
[1104] | 206 | // creating toolbar
|
---|
| 207 | contentPane.add(toolbar.control, BorderLayout.NORTH);
|
---|
[626] | 208 |
|
---|
[1326] | 209 | contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
---|
| 210 | .put(Shortcut.registerShortcut("system:help", tr("Help"),
|
---|
[1750] | 211 | KeyEvent.VK_F1, Shortcut.GROUP_DIRECT).getKeyStroke(), "Help");
|
---|
[1104] | 212 | contentPane.getActionMap().put("Help", menu.help);
|
---|
[626] | 213 |
|
---|
[1104] | 214 | TaggingPresetPreference.initialize();
|
---|
| 215 | MapPaintPreference.initialize();
|
---|
[626] | 216 |
|
---|
[1104] | 217 | toolbar.refreshToolbarControl();
|
---|
[626] | 218 |
|
---|
[1104] | 219 | toolbar.control.updateUI();
|
---|
| 220 | contentPane.updateUI();
|
---|
| 221 | }
|
---|
[626] | 222 |
|
---|
[1104] | 223 | /**
|
---|
| 224 | * Add a new layer to the map. If no map exists, create one.
|
---|
| 225 | */
|
---|
| 226 | public final void addLayer(final Layer layer) {
|
---|
| 227 | if (map == null) {
|
---|
| 228 | final MapFrame mapFrame = new MapFrame();
|
---|
| 229 | setMapFrame(mapFrame);
|
---|
| 230 | mapFrame.selectMapMode((MapMode)mapFrame.getDefaultButtonAction());
|
---|
| 231 | mapFrame.setVisible(true);
|
---|
[2224] | 232 | mapFrame.initializeDialogsPane();
|
---|
[1895] | 233 | // bootstrapping problem: make sure the layer list dialog is going to
|
---|
| 234 | // listen to change events of the very first layer
|
---|
| 235 | //
|
---|
| 236 | layer.addPropertyChangeListener(LayerListDialog.getInstance().getModel());
|
---|
[1104] | 237 | }
|
---|
| 238 | map.mapView.addLayer(layer);
|
---|
| 239 | }
|
---|
[1814] | 240 |
|
---|
[1104] | 241 | /**
|
---|
[1825] | 242 | * Replies true if there is an edit layer
|
---|
[1811] | 243 | *
|
---|
[1825] | 244 | * @return true if there is an edit layer
|
---|
[1104] | 245 | */
|
---|
[1825] | 246 | public boolean hasEditLayer() {
|
---|
[3071] | 247 | if (getEditLayer() == null) return false;
|
---|
[1814] | 248 | return true;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /**
|
---|
| 252 | * Replies the current edit layer
|
---|
[2224] | 253 | *
|
---|
[1814] | 254 | * @return the current edit layer. null, if no current edit layer exists
|
---|
| 255 | */
|
---|
| 256 | public OsmDataLayer getEditLayer() {
|
---|
| 257 | if (map == null) return null;
|
---|
| 258 | if (map.mapView == null) return null;
|
---|
[1750] | 259 | return map.mapView.getEditLayer();
|
---|
[1104] | 260 | }
|
---|
[626] | 261 |
|
---|
[1104] | 262 | /**
|
---|
[1814] | 263 | * Replies the current data set.
|
---|
[1811] | 264 | *
|
---|
[1814] | 265 | * @return the current data set. null, if no current data set exists
|
---|
[1750] | 266 | */
|
---|
[1814] | 267 | public DataSet getCurrentDataSet() {
|
---|
[1825] | 268 | if (!hasEditLayer()) return null;
|
---|
[1814] | 269 | return getEditLayer().data;
|
---|
[1750] | 270 | }
|
---|
| 271 |
|
---|
| 272 | /**
|
---|
[3071] | 273 | * Returns the currently active layer
|
---|
| 274 | *
|
---|
| 275 | * @return the currently active layer. null, if currently no active layer exists
|
---|
| 276 | */
|
---|
| 277 | public Layer getActiveLayer() {
|
---|
| 278 | if (map == null) return null;
|
---|
| 279 | if (map.mapView == null) return null;
|
---|
| 280 | return map.mapView.getActiveLayer();
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | /**
|
---|
[1104] | 284 | * Use this to register shortcuts to
|
---|
| 285 | */
|
---|
| 286 | public static final JPanel contentPane = new JPanel(new BorderLayout());
|
---|
[626] | 287 |
|
---|
[1104] | 288 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 289 | // Implementation part
|
---|
| 290 | ///////////////////////////////////////////////////////////////////////////
|
---|
[626] | 291 |
|
---|
[1104] | 292 | public static JPanel panel = new JPanel(new BorderLayout());
|
---|
[626] | 293 |
|
---|
[1104] | 294 | protected static Rectangle bounds;
|
---|
[2876] | 295 | protected static int windowState = JFrame.NORMAL;
|
---|
[626] | 296 |
|
---|
[1104] | 297 | private final CommandQueueListener redoUndoListener = new CommandQueueListener(){
|
---|
| 298 | public void commandChanged(final int queueSize, final int redoSize) {
|
---|
| 299 | menu.undo.setEnabled(queueSize > 0);
|
---|
| 300 | menu.redo.setEnabled(redoSize > 0);
|
---|
| 301 | }
|
---|
| 302 | };
|
---|
[1722] | 303 |
|
---|
[1104] | 304 | /**
|
---|
| 305 | * Should be called before the main constructor to setup some parameter stuff
|
---|
| 306 | * @param args The parsed argument list.
|
---|
| 307 | */
|
---|
| 308 | public static void preConstructorInit(Map<String, Collection<String>> args) {
|
---|
[2272] | 309 | ProjectionPreference.setProjection();
|
---|
[626] | 310 |
|
---|
[1104] | 311 | try {
|
---|
[2708] | 312 | String defaultlaf = platform.getDefaultStyle();
|
---|
[2709] | 313 | String laf = Main.pref.get("laf", defaultlaf);
|
---|
[1188] | 314 | try {
|
---|
[2709] | 315 | UIManager.setLookAndFeel(laf);
|
---|
[1188] | 316 | }
|
---|
[2708] | 317 | catch (final java.lang.ClassNotFoundException e) {
|
---|
[2709] | 318 | System.out.println("Look and Feel not found: " + laf);
|
---|
[2708] | 319 | Main.pref.put("laf", defaultlaf);
|
---|
| 320 | }
|
---|
[1188] | 321 | catch (final javax.swing.UnsupportedLookAndFeelException e) {
|
---|
[2709] | 322 | System.out.println("Look and Feel not supported: " + laf);
|
---|
[2708] | 323 | Main.pref.put("laf", defaultlaf);
|
---|
[1188] | 324 | }
|
---|
[1104] | 325 | toolbar = new ToolbarPreferences();
|
---|
| 326 | contentPane.updateUI();
|
---|
| 327 | panel.updateUI();
|
---|
| 328 | } catch (final Exception e) {
|
---|
| 329 | e.printStackTrace();
|
---|
| 330 | }
|
---|
| 331 | UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
|
---|
| 332 | UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
|
---|
| 333 | UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
|
---|
| 334 | UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
|
---|
[626] | 335 |
|
---|
[1990] | 336 | // init default coordinate format
|
---|
| 337 | //
|
---|
| 338 | try {
|
---|
[2039] | 339 | //CoordinateFormat format = CoordinateFormat.valueOf(Main.pref.get("coordinates"));
|
---|
[1990] | 340 | CoordinateFormat.setCoordinateFormat(CoordinateFormat.valueOf(Main.pref.get("coordinates")));
|
---|
| 341 | } catch (IllegalArgumentException iae) {
|
---|
| 342 | CoordinateFormat.setCoordinateFormat(CoordinateFormat.DECIMAL_DEGREES);
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[1104] | 345 | Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
[2876] | 346 | String geometry = null;
|
---|
[1104] | 347 | if (args.containsKey("geometry")) {
|
---|
[1286] | 348 | geometry = args.get("geometry").iterator().next();
|
---|
[2876] | 349 | // Main.debug("Main window geometry from args: \""+geometry+"\"");
|
---|
| 350 | } else {
|
---|
| 351 | geometry = Main.pref.get("gui.geometry");
|
---|
| 352 | // Main.debug("Main window geometry from preferences: \""+geometry+"\"");
|
---|
[1356] | 353 | }
|
---|
| 354 | if (geometry.length() != 0) {
|
---|
[1104] | 355 | final Matcher m = Pattern.compile("(\\d+)x(\\d+)(([+-])(\\d+)([+-])(\\d+))?").matcher(geometry);
|
---|
| 356 | if (m.matches()) {
|
---|
| 357 | int w = Integer.valueOf(m.group(1));
|
---|
| 358 | int h = Integer.valueOf(m.group(2));
|
---|
| 359 | int x = 0, y = 0;
|
---|
| 360 | if (m.group(3) != null) {
|
---|
| 361 | x = Integer.valueOf(m.group(5));
|
---|
| 362 | y = Integer.valueOf(m.group(7));
|
---|
[1750] | 363 | if (m.group(4).equals("-")) {
|
---|
[1104] | 364 | x = screenDimension.width - x - w;
|
---|
[1750] | 365 | }
|
---|
| 366 | if (m.group(6).equals("-")) {
|
---|
[1104] | 367 | y = screenDimension.height - y - h;
|
---|
[1750] | 368 | }
|
---|
[1104] | 369 | }
|
---|
[2876] | 370 | // copied from WindowsGeometry.applySafe()
|
---|
| 371 | if (x > Toolkit.getDefaultToolkit().getScreenSize().width - 10) {
|
---|
| 372 | x = 0;
|
---|
| 373 | }
|
---|
| 374 | if (y > Toolkit.getDefaultToolkit().getScreenSize().height - 10) {
|
---|
| 375 | y = 0;
|
---|
| 376 | }
|
---|
[1104] | 377 | bounds = new Rectangle(x,y,w,h);
|
---|
[1356] | 378 | if(!Main.pref.get("gui.geometry").equals(geometry)) {
|
---|
| 379 | // remember this geometry
|
---|
[2876] | 380 | // Main.debug("Main window: saving geometry \"" + geometry + "\"");
|
---|
[1356] | 381 | Main.pref.put("gui.geometry", geometry);
|
---|
| 382 | }
|
---|
[1523] | 383 | } else {
|
---|
[1104] | 384 | System.out.println("Ignoring malformed geometry: "+geometry);
|
---|
[1523] | 385 | }
|
---|
[1104] | 386 | }
|
---|
[1750] | 387 | if (bounds == null) {
|
---|
[1496] | 388 | bounds = !args.containsKey("no-maximize") ? new Rectangle(0,0,screenDimension.width,screenDimension.height) : new Rectangle(1000,740);
|
---|
[1750] | 389 | }
|
---|
[2876] | 390 | // Main.debug("window geometry: "+bounds);
|
---|
[1104] | 391 | }
|
---|
[626] | 392 |
|
---|
[1104] | 393 | public void postConstructorProcessCmdLine(Map<String, Collection<String>> args) {
|
---|
[1750] | 394 | if (args.containsKey("download")) {
|
---|
[2798] | 395 | List<File> fileList = new ArrayList<File>();
|
---|
[1750] | 396 | for (String s : args.get("download")) {
|
---|
[2798] | 397 | File f = null;
|
---|
| 398 | switch(paramType(s)) {
|
---|
| 399 | case httpUrl:
|
---|
| 400 | downloadFromParamHttp(false, s);
|
---|
| 401 | break;
|
---|
| 402 | case bounds:
|
---|
| 403 | downloadFromParamBounds(false, s);
|
---|
| 404 | break;
|
---|
| 405 | case fileUrl:
|
---|
| 406 | try {
|
---|
| 407 | f = new File(new URI(s));
|
---|
| 408 | } catch (URISyntaxException e) {
|
---|
| 409 | JOptionPane.showMessageDialog(
|
---|
| 410 | Main.parent,
|
---|
| 411 | tr("Ignoring malformed file URL: \"{0}\"", s),
|
---|
| 412 | tr("Warning"),
|
---|
| 413 | JOptionPane.WARNING_MESSAGE
|
---|
| 414 | );
|
---|
| 415 | }
|
---|
| 416 | if (f!=null) {
|
---|
| 417 | fileList.add(f);
|
---|
| 418 | }
|
---|
| 419 | break;
|
---|
| 420 | case fileName:
|
---|
| 421 | f = new File(s);
|
---|
| 422 | fileList.add(f);
|
---|
| 423 | break;
|
---|
| 424 | }
|
---|
[1750] | 425 | }
|
---|
[2798] | 426 | if(!fileList.isEmpty())
|
---|
| 427 | {
|
---|
| 428 | OpenFileAction.openFiles(fileList);
|
---|
| 429 | }
|
---|
[1750] | 430 | }
|
---|
| 431 | if (args.containsKey("downloadgps")) {
|
---|
| 432 | for (String s : args.get("downloadgps")) {
|
---|
[2798] | 433 | switch(paramType(s)) {
|
---|
| 434 | case httpUrl:
|
---|
| 435 | downloadFromParamHttp(true, s);
|
---|
| 436 | break;
|
---|
| 437 | case bounds:
|
---|
| 438 | downloadFromParamBounds(true, s);
|
---|
| 439 | break;
|
---|
| 440 | case fileUrl:
|
---|
| 441 | case fileName:
|
---|
| 442 | JOptionPane.showMessageDialog(
|
---|
| 443 | Main.parent,
|
---|
| 444 | tr("Parameter \"downloadgps\" does not accept file names or file URLs"),
|
---|
| 445 | tr("Warning"),
|
---|
| 446 | JOptionPane.WARNING_MESSAGE
|
---|
| 447 | );
|
---|
| 448 | }
|
---|
[1750] | 449 | }
|
---|
| 450 | }
|
---|
| 451 | if (args.containsKey("selection")) {
|
---|
| 452 | for (String s : args.get("selection")) {
|
---|
[1213] | 453 | SearchAction.search(s, SearchAction.SearchMode.add, false, false);
|
---|
[1750] | 454 | }
|
---|
| 455 | }
|
---|
[1104] | 456 | }
|
---|
[626] | 457 |
|
---|
[2025] | 458 | public static boolean saveUnsavedModifications() {
|
---|
| 459 | if (map == null) return true;
|
---|
| 460 | SaveLayersDialog dialog = new SaveLayersDialog(Main.parent);
|
---|
| 461 | List<OsmDataLayer> layersWithUnmodifiedChanges = new ArrayList<OsmDataLayer>();
|
---|
| 462 | for (OsmDataLayer l: Main.map.mapView.getLayersOfType(OsmDataLayer.class)) {
|
---|
| 463 | if (l.requiresSaveToFile() || l.requiresUploadToServer()) {
|
---|
| 464 | layersWithUnmodifiedChanges.add(l);
|
---|
| 465 | }
|
---|
| 466 | }
|
---|
| 467 | dialog.prepareForSavingAndUpdatingLayersBeforeExit();
|
---|
| 468 | if (!layersWithUnmodifiedChanges.isEmpty()) {
|
---|
| 469 | dialog.getModel().populate(layersWithUnmodifiedChanges);
|
---|
| 470 | dialog.setVisible(true);
|
---|
| 471 | switch(dialog.getUserAction()) {
|
---|
[2485] | 472 | case CANCEL: return false;
|
---|
| 473 | case PROCEED: return true;
|
---|
| 474 | default: return false;
|
---|
[2025] | 475 | }
|
---|
| 476 | }
|
---|
[2274] | 477 |
|
---|
[2025] | 478 | return true;
|
---|
| 479 | }
|
---|
| 480 |
|
---|
[2798] | 481 | /**
|
---|
| 482 | * The type of a command line parameter, to be used in switch statements.
|
---|
| 483 | * @see paramType
|
---|
| 484 | */
|
---|
| 485 | private enum DownloadParamType { httpUrl, fileUrl, bounds, fileName }
|
---|
| 486 |
|
---|
| 487 | /**
|
---|
| 488 | * Guess the type of a parameter string specified on the command line with --download= or --downloadgps.
|
---|
| 489 | * @param s A parameter string
|
---|
| 490 | * @return The guessed parameter type
|
---|
| 491 | */
|
---|
| 492 | private DownloadParamType paramType(String s) {
|
---|
| 493 | if(s.startsWith("http:")) return DownloadParamType.httpUrl;
|
---|
| 494 | if(s.startsWith("file:")) return DownloadParamType.fileUrl;
|
---|
| 495 | final StringTokenizer st = new StringTokenizer(s, ",");
|
---|
| 496 | // we assume a string with exactly 3 commas is a bounds parameter
|
---|
| 497 | if (st.countTokens() == 4) return DownloadParamType.bounds;
|
---|
| 498 | // everything else must be a file name
|
---|
| 499 | return DownloadParamType.fileName;
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | /**
|
---|
| 503 | * Download area specified on the command line as OSM URL.
|
---|
| 504 | * @param rawGps Flag to download raw GPS tracks
|
---|
| 505 | * @param s The URL parameter
|
---|
| 506 | */
|
---|
| 507 | private static void downloadFromParamHttp(final boolean rawGps, String s) {
|
---|
| 508 | final Bounds b = OsmUrlToBounds.parse(s);
|
---|
| 509 | if (b == null) {
|
---|
| 510 | JOptionPane.showMessageDialog(
|
---|
| 511 | Main.parent,
|
---|
| 512 | tr("Ignoring malformed URL: \"{0}\"", s),
|
---|
| 513 | tr("Warning"),
|
---|
| 514 | JOptionPane.WARNING_MESSAGE
|
---|
| 515 | );
|
---|
| 516 | } else {
|
---|
| 517 | downloadFromParamBounds(rawGps, b);
|
---|
[1104] | 518 | }
|
---|
[2798] | 519 | }
|
---|
[626] | 520 |
|
---|
[2798] | 521 | /**
|
---|
| 522 | * Download area specified on the command line as bounds string.
|
---|
| 523 | * @param rawGps Flag to download raw GPS tracks
|
---|
| 524 | * @param s The bounds parameter
|
---|
| 525 | */
|
---|
| 526 | private static void downloadFromParamBounds(final boolean rawGps, String s) {
|
---|
[1104] | 527 | final StringTokenizer st = new StringTokenizer(s, ",");
|
---|
| 528 | if (st.countTokens() == 4) {
|
---|
[2327] | 529 | Bounds b = new Bounds(
|
---|
[2485] | 530 | new LatLon(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken())),
|
---|
| 531 | new LatLon(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()))
|
---|
| 532 | );
|
---|
[2798] | 533 | downloadFromParamBounds(rawGps, b);
|
---|
[1104] | 534 | }
|
---|
| 535 | }
|
---|
[1023] | 536 |
|
---|
[2798] | 537 | /**
|
---|
| 538 | * Download area specified as Bounds value.
|
---|
| 539 | * @param rawGps Flag to download raw GPS tracks
|
---|
| 540 | * @param b The bounds value
|
---|
| 541 | * @see downloadFromParamBounds(final boolean rawGps, String s)
|
---|
| 542 | * @see downloadFromParamHttp
|
---|
| 543 | */
|
---|
| 544 | private static void downloadFromParamBounds(final boolean rawGps, Bounds b) {
|
---|
| 545 | DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
|
---|
| 546 | // asynchronously launch the download task ...
|
---|
| 547 | Future<?> future = task.download(true, b, null);
|
---|
| 548 | // ... and the continuation when the download is finished (this will wait for the download to finish)
|
---|
| 549 | Main.worker.execute(new PostDownloadHandler(task, future));
|
---|
| 550 | }
|
---|
| 551 |
|
---|
[1849] | 552 | public static void determinePlatformHook() {
|
---|
[1104] | 553 | String os = System.getProperty("os.name");
|
---|
| 554 | if (os == null) {
|
---|
| 555 | System.err.println("Your operating system has no name, so I'm guessing its some kind of *nix.");
|
---|
| 556 | platform = new PlatformHookUnixoid();
|
---|
| 557 | } else if (os.toLowerCase().startsWith("windows")) {
|
---|
| 558 | platform = new PlatformHookWindows();
|
---|
[1169] | 559 | } else if (os.equals("Linux") || os.equals("Solaris") ||
|
---|
[1750] | 560 | os.equals("SunOS") || os.equals("AIX") ||
|
---|
| 561 | os.equals("FreeBSD") || os.equals("NetBSD") || os.equals("OpenBSD")) {
|
---|
[1104] | 562 | platform = new PlatformHookUnixoid();
|
---|
| 563 | } else if (os.toLowerCase().startsWith("mac os x")) {
|
---|
| 564 | platform = new PlatformHookOsx();
|
---|
| 565 | } else {
|
---|
| 566 | System.err.println("I don't know your operating system '"+os+"', so I'm guessing its some kind of *nix.");
|
---|
| 567 | platform = new PlatformHookUnixoid();
|
---|
| 568 | }
|
---|
| 569 | }
|
---|
[1023] | 570 |
|
---|
[1286] | 571 | static public void saveGuiGeometry() {
|
---|
[2162] | 572 | // save the current window geometry and the width of the toggle dialog area
|
---|
[1356] | 573 | String newGeometry = "";
|
---|
[2185] | 574 | String newToggleDlgWidth = null;
|
---|
[1356] | 575 | try {
|
---|
[2876] | 576 | Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
| 577 | int width = (int)bounds.getWidth();
|
---|
| 578 | int height = (int)bounds.getHeight();
|
---|
| 579 | int x = (int)bounds.getX();
|
---|
| 580 | int y = (int)bounds.getY();
|
---|
| 581 | if (width > screenDimension.width) {
|
---|
| 582 | width = screenDimension.width;
|
---|
[1356] | 583 | }
|
---|
[2876] | 584 | if (height > screenDimension.height) {
|
---|
| 585 | width = screenDimension.height;
|
---|
| 586 | }
|
---|
| 587 | if (x < 0) {
|
---|
| 588 | x = 0;
|
---|
| 589 | }
|
---|
| 590 | if (y < 0) {
|
---|
| 591 | y = 0;
|
---|
| 592 | }
|
---|
| 593 | newGeometry = width + "x" + height + "+" + x + "+" + y;
|
---|
[2224] | 594 |
|
---|
[2185] | 595 | if (map != null) {
|
---|
| 596 | newToggleDlgWidth = Integer.toString(map.getToggleDlgWidth());
|
---|
| 597 | if (newToggleDlgWidth.equals(Integer.toString(map.DEF_TOGGLE_DLG_WIDTH))) {
|
---|
| 598 | newToggleDlgWidth = "";
|
---|
| 599 | }
|
---|
[2162] | 600 | }
|
---|
[1356] | 601 | }
|
---|
| 602 | catch (Exception e) {
|
---|
[2876] | 603 | System.out.println("Failed to get GUI geometry: " + e);
|
---|
[2185] | 604 | e.printStackTrace();
|
---|
[1356] | 605 | }
|
---|
[2876] | 606 | boolean maximized = (windowState & JFrame.MAXIMIZED_BOTH) != 0;
|
---|
| 607 | // Main.debug("Main window: saving geometry \"" + newGeometry + "\" " + (maximized?"maximized":"normal"));
|
---|
| 608 | pref.put("gui.maximized", maximized);
|
---|
[1356] | 609 | pref.put("gui.geometry", newGeometry);
|
---|
[2185] | 610 | if (newToggleDlgWidth != null) {
|
---|
| 611 | pref.put("toggleDialogs.width", newToggleDlgWidth);
|
---|
| 612 | }
|
---|
[1286] | 613 | }
|
---|
[2876] | 614 | private static class WindowPositionSizeListener extends WindowAdapter implements
|
---|
| 615 | ComponentListener {
|
---|
| 616 |
|
---|
| 617 | @Override
|
---|
| 618 | public void windowStateChanged(WindowEvent e) {
|
---|
| 619 | Main.windowState = e.getNewState();
|
---|
| 620 | // Main.debug("Main window state changed to " + Main.windowState);
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | public void componentHidden(ComponentEvent e) {
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | public void componentMoved(ComponentEvent e) {
|
---|
| 627 | handleComponentEvent(e);
|
---|
| 628 | }
|
---|
| 629 |
|
---|
| 630 | public void componentResized(ComponentEvent e) {
|
---|
| 631 | handleComponentEvent(e);
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | public void componentShown(ComponentEvent e) {
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | private void handleComponentEvent(ComponentEvent e) {
|
---|
| 638 | Component c = e.getComponent();
|
---|
| 639 | if (c instanceof JFrame) {
|
---|
| 640 | if (Main.windowState == JFrame.NORMAL) {
|
---|
| 641 | Main.bounds = ((JFrame) c).getBounds();
|
---|
| 642 | // Main.debug("Main window: new geometry " + Main.bounds);
|
---|
| 643 | } else {
|
---|
| 644 | // Main.debug("Main window state is " + Main.windowState);
|
---|
| 645 | }
|
---|
| 646 | }
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | }
|
---|
| 650 | public static void addListener() {
|
---|
| 651 | parent.addComponentListener(new WindowPositionSizeListener());
|
---|
| 652 | ((JFrame)parent).addWindowStateListener(new WindowPositionSizeListener());
|
---|
| 653 | }
|
---|
[626] | 654 | }
|
---|