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

Last change on this file since 3562 was 3542, checked in by stoecker, 14 years ago

hopefully fix some applet issues

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