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