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