source: josm/trunk/src/org/openstreetmap/josm/Main.java@ 4719

Last change on this file since 4719 was 4719, checked in by stoecker, 12 years ago

make patching locale loader easier, update i18n

  • Property svn:eol-style set to native
File size: 35.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm;
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.BorderLayout;
6import java.awt.Component;
7import java.awt.Dimension;
8import java.awt.GridBagConstraints;
9import java.awt.GridBagLayout;
10import java.awt.Rectangle;
11import java.awt.Toolkit;
12import java.awt.event.ComponentEvent;
13import java.awt.event.ComponentListener;
14import java.awt.event.KeyEvent;
15import java.awt.event.WindowAdapter;
16import java.awt.event.WindowEvent;
17import java.io.File;
18import java.lang.ref.WeakReference;
19import java.net.URI;
20import java.net.URISyntaxException;
21import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Iterator;
24import java.util.List;
25import java.util.Map;
26import java.util.StringTokenizer;
27import java.util.concurrent.ExecutorService;
28import java.util.concurrent.Future;
29import java.util.regex.Matcher;
30import java.util.regex.Pattern;
31
32import javax.swing.Action;
33import javax.swing.InputMap;
34import javax.swing.JComponent;
35import javax.swing.JFrame;
36import javax.swing.JLabel;
37import javax.swing.JOptionPane;
38import javax.swing.JPanel;
39import javax.swing.JTextArea;
40import javax.swing.KeyStroke;
41import javax.swing.RepaintManager;
42import javax.swing.UIManager;
43
44import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
45import org.openstreetmap.josm.actions.JosmAction;
46import org.openstreetmap.josm.actions.OpenFileAction;
47import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
48import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
49import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
50import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
51import org.openstreetmap.josm.actions.mapmode.MapMode;
52import org.openstreetmap.josm.actions.search.SearchAction;
53import org.openstreetmap.josm.data.Bounds;
54import org.openstreetmap.josm.data.Preferences;
55import org.openstreetmap.josm.data.UndoRedoHandler;
56import org.openstreetmap.josm.data.coor.CoordinateFormat;
57import org.openstreetmap.josm.data.coor.LatLon;
58import org.openstreetmap.josm.data.osm.DataSet;
59import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
60import org.openstreetmap.josm.data.projection.Projection;
61import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
62import org.openstreetmap.josm.data.validation.OsmValidator;
63import org.openstreetmap.josm.gui.GettingStarted;
64import org.openstreetmap.josm.gui.MainMenu;
65import org.openstreetmap.josm.gui.MapFrame;
66import org.openstreetmap.josm.gui.MapView;
67import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
68import org.openstreetmap.josm.gui.io.SaveLayersDialog;
69import org.openstreetmap.josm.gui.layer.Layer;
70import org.openstreetmap.josm.gui.layer.OsmDataLayer;
71import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
72import org.openstreetmap.josm.gui.preferences.ImageryPreference;
73import org.openstreetmap.josm.gui.preferences.MapPaintPreference;
74import org.openstreetmap.josm.gui.preferences.ProjectionPreference;
75import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference;
76import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
77import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
78import org.openstreetmap.josm.gui.progress.ProgressMonitorExecutor;
79import org.openstreetmap.josm.io.OsmApi;
80import org.openstreetmap.josm.plugins.PluginHandler;
81import org.openstreetmap.josm.tools.CheckParameterUtil;
82import org.openstreetmap.josm.tools.I18n;
83import org.openstreetmap.josm.tools.ImageProvider;
84import org.openstreetmap.josm.tools.OpenBrowser;
85import org.openstreetmap.josm.tools.OsmUrlToBounds;
86import org.openstreetmap.josm.tools.PlatformHook;
87import org.openstreetmap.josm.tools.PlatformHookOsx;
88import org.openstreetmap.josm.tools.PlatformHookUnixoid;
89import org.openstreetmap.josm.tools.PlatformHookWindows;
90import org.openstreetmap.josm.tools.Shortcut;
91import org.openstreetmap.josm.tools.Utils;
92
93abstract 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 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 JPanel contentPanePrivate = new JPanel(new BorderLayout());
380
381 /**
382 * @deprecated If you just need to register shortcut for action, use registerActionShortcut instead of accessing InputMap directly
383 */
384 @Deprecated
385 public static final JPanel contentPane = contentPanePrivate;
386
387 public static void registerActionShortcut(Action action, Shortcut shortcut) {
388 registerActionShortcut(action, shortcut.getKeyStroke());
389 }
390
391 public static void registerActionShortcut(Action action, KeyStroke keyStroke) {
392 if (keyStroke == null)
393 return;
394
395 InputMap inputMap = contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
396 Object existing = inputMap.get(keyStroke);
397 if (existing != null && !existing.equals(action)) {
398 System.out.println(String.format("Keystroke %s is already assigned to %s, will be overridden by %s", keyStroke, existing, action));
399 }
400 inputMap.put(keyStroke, action);
401
402 contentPanePrivate.getActionMap().put(action, action);
403 }
404
405 public static void unregisterActionShortcut(Shortcut shortcut) {
406 contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(shortcut.getKeyStroke());
407 }
408
409 public static void unregisterActionShortcut(JosmAction action) {
410 unregisterActionShortcut(action, action.getShortcut());
411 }
412
413 public static void unregisterActionShortcut(Action action, Shortcut shortcut) {
414 contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(shortcut.getKeyStroke());
415 contentPanePrivate.getActionMap().remove(action);
416 }
417
418
419 ///////////////////////////////////////////////////////////////////////////
420 // Implementation part
421 ///////////////////////////////////////////////////////////////////////////
422
423 public static JPanel panel = new JPanel(new BorderLayout());
424
425 protected static Rectangle bounds;
426 protected static int windowState = JFrame.NORMAL;
427
428 private final CommandQueueListener redoUndoListener = new CommandQueueListener(){
429 public void commandChanged(final int queueSize, final int redoSize) {
430 menu.undo.setEnabled(queueSize > 0);
431 menu.redo.setEnabled(redoSize > 0);
432 }
433 };
434
435 /**
436 * Should be called before the main constructor to setup some parameter stuff
437 * @param args The parsed argument list.
438 */
439 public static void preConstructorInit(Map<String, Collection<String>> args) {
440 ProjectionPreference.setProjection();
441
442 try {
443 String defaultlaf = platform.getDefaultStyle();
444 String laf = Main.pref.get("laf", defaultlaf);
445 try {
446 UIManager.setLookAndFeel(laf);
447 }
448 catch (final java.lang.ClassNotFoundException e) {
449 System.out.println("Look and Feel not found: " + laf);
450 Main.pref.put("laf", defaultlaf);
451 }
452 catch (final javax.swing.UnsupportedLookAndFeelException e) {
453 System.out.println("Look and Feel not supported: " + laf);
454 Main.pref.put("laf", defaultlaf);
455 }
456 toolbar = new ToolbarPreferences();
457 contentPanePrivate.updateUI();
458 panel.updateUI();
459 } catch (final Exception e) {
460 e.printStackTrace();
461 }
462 UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
463 UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
464 UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
465 UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
466
467 I18n.translateJavaInternalMessages();
468
469 // init default coordinate format
470 //
471 try {
472 //CoordinateFormat format = CoordinateFormat.valueOf(Main.pref.get("coordinates"));
473 CoordinateFormat.setCoordinateFormat(CoordinateFormat.valueOf(Main.pref.get("coordinates")));
474 } catch (IllegalArgumentException iae) {
475 CoordinateFormat.setCoordinateFormat(CoordinateFormat.DECIMAL_DEGREES);
476 }
477
478 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
479 String geometry = null;
480 if (args.containsKey("geometry")) {
481 geometry = args.get("geometry").iterator().next();
482 } else {
483 geometry = Main.pref.get("gui.geometry");
484 }
485 if (geometry.length() != 0) {
486 final Matcher m = Pattern.compile("(\\d+)x(\\d+)(([+-])(\\d+)([+-])(\\d+))?").matcher(geometry);
487 if (m.matches()) {
488 int w = Integer.valueOf(m.group(1));
489 int h = Integer.valueOf(m.group(2));
490 int x = 0, y = 0;
491 if (m.group(3) != null) {
492 x = Integer.valueOf(m.group(5));
493 y = Integer.valueOf(m.group(7));
494 if (m.group(4).equals("-")) {
495 x = screenDimension.width - x - w;
496 }
497 if (m.group(6).equals("-")) {
498 y = screenDimension.height - y - h;
499 }
500 }
501 // copied from WindowsGeometry.applySafe()
502 if (x > Toolkit.getDefaultToolkit().getScreenSize().width - 10) {
503 x = 0;
504 }
505 if (y > Toolkit.getDefaultToolkit().getScreenSize().height - 10) {
506 y = 0;
507 }
508 bounds = new Rectangle(x,y,w,h);
509 if(!Main.pref.get("gui.geometry").equals(geometry)) {
510 // remember this geometry
511 Main.pref.put("gui.geometry", geometry);
512 }
513 } else {
514 System.out.println("Ignoring malformed geometry: "+geometry);
515 }
516 }
517 if (bounds == null) {
518 bounds = !args.containsKey("no-maximize") ? new Rectangle(0,0,screenDimension.width,screenDimension.height) : new Rectangle(1000,740);
519 }
520 }
521
522 public void postConstructorProcessCmdLine(Map<String, Collection<String>> args) {
523 if (args.containsKey("download")) {
524 List<File> fileList = new ArrayList<File>();
525 for (String s : args.get("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("downloadgps")) {
561 for (String s : args.get("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("selection")) {
581 for (String s : args.get("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()) {
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 Main.saveGuiGeometry();
613 // Remove all layers because somebody may rely on layerRemoved events (like AutosaveTask)
614 if (Main.isDisplayingMapView()) {
615 Collection<Layer> layers = new ArrayList<Layer>(Main.map.mapView.getAllLayers());
616 for (Layer l: layers) {
617 Main.map.mapView.removeLayer(l);
618 }
619 }
620 if (exit) {
621 System.exit(0);
622 return true;
623 } else
624 return true;
625 } else
626 return false;
627 }
628
629 /**
630 * The type of a command line parameter, to be used in switch statements.
631 * @see paramType
632 */
633 private enum DownloadParamType { httpUrl, fileUrl, bounds, fileName }
634
635 /**
636 * Guess the type of a parameter string specified on the command line with --download= or --downloadgps.
637 * @param s A parameter string
638 * @return The guessed parameter type
639 */
640 private DownloadParamType paramType(String s) {
641 if(s.startsWith("http:")) return DownloadParamType.httpUrl;
642 if(s.startsWith("file:")) return DownloadParamType.fileUrl;
643 final StringTokenizer st = new StringTokenizer(s, ",");
644 // we assume a string with exactly 3 commas is a bounds parameter
645 if (st.countTokens() == 4) 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 static public void saveGuiGeometry() {
720 // save the current window geometry and the width of the toggle dialog area
721 String newGeometry = "";
722 String newToggleDlgWidth = null;
723 try {
724 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
725 int width = (int)bounds.getWidth();
726 int height = (int)bounds.getHeight();
727 int x = (int)bounds.getX();
728 int y = (int)bounds.getY();
729 if (width > screenDimension.width) {
730 width = screenDimension.width;
731 }
732 if (height > screenDimension.height) {
733 width = screenDimension.height;
734 }
735 if (x < 0) {
736 x = 0;
737 }
738 if (y < 0) {
739 y = 0;
740 }
741 newGeometry = width + "x" + height + "+" + x + "+" + y;
742
743 if (map != null) {
744 newToggleDlgWidth = Integer.toString(map.getToggleDlgWidth());
745 if (newToggleDlgWidth.equals(Integer.toString(MapFrame.DEF_TOGGLE_DLG_WIDTH))) {
746 newToggleDlgWidth = "";
747 }
748 }
749 }
750 catch (Exception e) {
751 System.out.println("Failed to get GUI geometry: " + e);
752 e.printStackTrace();
753 }
754 boolean maximized = (windowState & JFrame.MAXIMIZED_BOTH) != 0;
755 // Main.debug("Main window: saving geometry \"" + newGeometry + "\" " + (maximized?"maximized":"normal"));
756 pref.put("gui.maximized", maximized);
757 pref.put("gui.geometry", newGeometry);
758 if (newToggleDlgWidth != null) {
759 pref.put("toggleDialogs.width", newToggleDlgWidth);
760 }
761 }
762 private static class WindowPositionSizeListener extends WindowAdapter implements
763 ComponentListener {
764
765 @Override
766 public void windowStateChanged(WindowEvent e) {
767 Main.windowState = e.getNewState();
768 // Main.debug("Main window state changed to " + Main.windowState);
769 }
770
771 public void componentHidden(ComponentEvent e) {
772 }
773
774 public void componentMoved(ComponentEvent e) {
775 handleComponentEvent(e);
776 }
777
778 public void componentResized(ComponentEvent e) {
779 handleComponentEvent(e);
780 }
781
782 public void componentShown(ComponentEvent e) {
783 }
784
785 private void handleComponentEvent(ComponentEvent e) {
786 Component c = e.getComponent();
787 if (c instanceof JFrame) {
788 if (Main.windowState == JFrame.NORMAL) {
789 Main.bounds = ((JFrame) c).getBounds();
790 // Main.debug("Main window: new geometry " + Main.bounds);
791 } else {
792 // Main.debug("Main window state is " + Main.windowState);
793 }
794 }
795 }
796
797 }
798 public static void addListener() {
799 parent.addComponentListener(new WindowPositionSizeListener());
800 ((JFrame)parent).addWindowStateListener(new WindowPositionSizeListener());
801 }
802
803 public static void checkJava6() {
804 String version = System.getProperty("java.version");
805 if (version != null) {
806 if (version.startsWith("1.6") || version.startsWith("6") ||
807 version.startsWith("1.7") || version.startsWith("7"))
808 return;
809 if (version.startsWith("1.5") || version.startsWith("5")) {
810 JLabel ho = new JLabel("<html>"+
811 tr("<h2>JOSM requires Java version 6.</h2>"+
812 "Detected Java version: {0}.<br>"+
813 "You can <ul><li>update your Java (JRE) or</li>"+
814 "<li>use an earlier (Java 5 compatible) version of JOSM.</li></ul>"+
815 "More Info:", version)+"</html>");
816 JTextArea link = new JTextArea("http://josm.openstreetmap.de/wiki/Help/SystemRequirements");
817 link.setEditable(false);
818 link.setBackground(panel.getBackground());
819 JPanel panel = new JPanel(new GridBagLayout());
820 GridBagConstraints gbc = new GridBagConstraints();
821 gbc.gridwidth = GridBagConstraints.REMAINDER;
822 gbc.anchor = GridBagConstraints.WEST;
823 gbc.weightx = 1.0;
824 panel.add(ho, gbc);
825 panel.add(link, gbc);
826 final String EXIT = tr("Exit JOSM");
827 final String CONTINUE = tr("Continue, try anyway");
828 int ret = JOptionPane.showOptionDialog(null, panel, tr("Error"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, new String[] {EXIT, CONTINUE}, EXIT);
829 if (ret == 0) {
830 System.exit(0);
831 }
832 return;
833 }
834 }
835 System.err.println("Error: Could not recognize Java Version: "+version);
836 }
837
838 /* ----------------------------------------------------------------------------------------- */
839 /* projection handling - Main is a registry for a single, global projection instance */
840 /* */
841 /* TODO: For historical reasons the registry is implemented by Main. An alternative approach */
842 /* would be a singleton org.openstreetmap.josm.data.projection.ProjectionRegistry class. */
843 /* ----------------------------------------------------------------------------------------- */
844 /**
845 * The projection method used.
846 * @deprecated use {@link #getProjection()} and {@link #setProjection(Projection)} instead.
847 * For the time being still publicly available, but avoid/migrate write access to it. Use
848 * {@link #setProjection(Projection)} in order to trigger a projection change event.
849 */
850 @Deprecated
851 public static Projection proj;
852
853 /**
854 * Replies the current projection.
855 *
856 * @return
857 */
858 public static Projection getProjection() {
859 return proj;
860 }
861
862 /**
863 * Sets the current projection
864 *
865 * @param p the projection
866 */
867 public static void setProjection(Projection p) {
868 CheckParameterUtil.ensureParameterNotNull(p);
869 Projection oldValue = proj;
870 proj = p;
871 fireProjectionChanged(oldValue, proj);
872 }
873
874 /*
875 * Keep WeakReferences to the listeners. This relieves clients from the burden of
876 * explicitly removing the listeners and allows us to transparently register every
877 * created dataset as projection change listener.
878 */
879 private static final ArrayList<WeakReference<ProjectionChangeListener>> listeners = new ArrayList<WeakReference<ProjectionChangeListener>>();
880
881 private static void fireProjectionChanged(Projection oldValue, Projection newValue) {
882 if (newValue == null ^ oldValue == null
883 || (newValue != null && oldValue != null && !Utils.equal(newValue.toCode(), oldValue.toCode()))) {
884
885 synchronized(Main.class) {
886 Iterator<WeakReference<ProjectionChangeListener>> it = listeners.iterator();
887 while(it.hasNext()){
888 WeakReference<ProjectionChangeListener> wr = it.next();
889 if (wr.get() == null) {
890 it.remove();
891 continue;
892 }
893 wr.get().projectionChanged(oldValue, newValue);
894 }
895 }
896 if (newValue != null) {
897 Bounds b = (Main.map != null && Main.map.mapView != null) ? Main.map.mapView.getRealBounds() : null;
898 if (b != null){
899 Main.map.mapView.zoomTo(b);
900 }
901 }
902 /* TODO - remove layers with fixed projection */
903 }
904 }
905
906 /**
907 * Register a projection change listener
908 *
909 * @param listener the listener. Ignored if null.
910 */
911 public static void addProjectionChangeListener(ProjectionChangeListener listener) {
912 if (listener == null) return;
913 synchronized (Main.class) {
914 for (WeakReference<ProjectionChangeListener> wr : listeners) {
915 // already registered ? => abort
916 if (wr.get() == listener) return;
917 }
918 }
919 listeners.add(new WeakReference<ProjectionChangeListener>(listener));
920 }
921
922 /**
923 * Removes a projection change listener
924 *
925 * @param listener the listener. Ignored if null.
926 */
927 public static void removeProjectionChangeListener(ProjectionChangeListener listener) {
928 if (listener == null) return;
929 synchronized(Main.class){
930 Iterator<WeakReference<ProjectionChangeListener>> it = listeners.iterator();
931 while(it.hasNext()){
932 WeakReference<ProjectionChangeListener> wr = it.next();
933 // remove the listener - and any other listener which god garbage
934 // collected in the meantime
935 if (wr.get() == null || wr.get() == listener) {
936 it.remove();
937 }
938 }
939 }
940 }
941}
Note: See TracBrowser for help on using the repository browser.