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

Last change on this file since 3671 was 3669, checked in by bastiK, 13 years ago

add validator plugin to josm core. Original author: Francisco R. Santos (frsantos); major contributions by bilbo, daeron, delta_foxtrot, imi, jttt, jrreid, gabriel, guggis, pieren, rrankin, skela, stoecker, stotz and others

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