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

Last change on this file since 3380 was 3378, checked in by jttt, 14 years ago

Fix #2662 Auto-save

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