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