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