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

Last change on this file since 3872 was 3815, checked in by stoecker, 13 years ago

fixes for applet

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