GUI: save the windows dimension & location before they have been maximized 53/10553/1
Calixte DENIZET [Wed, 20 Feb 2013 17:46:25 +0000 (18:46 +0100)]
Change-Id: I3014f4e1aa6493cb21e103523dcdcd226c9a8d5a

scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/window/SwingScilabWindow.java
scilab/modules/gui/src/java/org/scilab/modules/gui/utils/WindowsConfigurationManager.java

index 5f68e86..2a26045 100644 (file)
@@ -52,8 +52,11 @@ import javax.swing.SwingUtilities;
 import javax.swing.WindowConstants;
 import java.awt.Dimension;
 import java.awt.Frame;
+import java.awt.Point;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.ComponentAdapter;
+import java.awt.event.ComponentEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.lang.reflect.InvocationTargetException;
@@ -97,6 +100,8 @@ public class SwingScilabWindow extends JFrame implements SimpleWindow {
     private int elementId; // the id of the Window which contains this SimpleWindow
     private String windowUID;
     private final boolean MAC_OS_X = (System.getProperty("os.name").toLowerCase().startsWith("mac os x"));
+    private Dimension lastDimension;
+    private Point lastPosition;
 
     /**
      * Constructor
@@ -109,11 +114,11 @@ public class SwingScilabWindow extends JFrame implements SimpleWindow {
 
         // By default ctrl+w close the window
         ActionListener listener = new ActionListener() {
-            @Override
-            public void actionPerformed(ActionEvent e) {
-                processWindowEvent(new WindowEvent(SwingScilabWindow.this, WindowEvent.WINDOW_CLOSING));
-            }
-        };
+                @Override
+                public void actionPerformed(ActionEvent e) {
+                    processWindowEvent(new WindowEvent(SwingScilabWindow.this, WindowEvent.WINDOW_CLOSING));
+                }
+            };
         getRootPane().registerKeyboardAction(listener, ScilabKeyStroke.getKeyStroke("OSSCKEY W"), JComponent.WHEN_IN_FOCUSED_WINDOW);
 
         // TODO : Only for testing : Must be removed
@@ -150,11 +155,27 @@ public class SwingScilabWindow extends JFrame implements SimpleWindow {
         setLocationByPlatform(true);
 
         addWindowListener(new WindowAdapter() {
-            @Override
-            public void windowClosing(WindowEvent e) {
-                ClosingOperationsManager.startClosingOperation(SwingScilabWindow.this);
-            }
-        });
+                @Override
+                public void windowClosing(WindowEvent e) {
+                    ClosingOperationsManager.startClosingOperation(SwingScilabWindow.this);
+                }
+            });
+
+        addComponentListener(new ComponentAdapter() {
+                @Override
+                public void componentResized(ComponentEvent e) {
+                    if (getExtendedState() == NORMAL) {
+                        lastDimension = getSize();
+                    }
+                }
+
+                @Override
+                public void componentMoved(ComponentEvent e) {
+                    if (getExtendedState() == NORMAL) {
+                        lastPosition = getLocation();
+                    }
+                }
+            });
 
         if (MAC_OS_X) {
             registerForMacOSXEvents();
@@ -168,6 +189,30 @@ public class SwingScilabWindow extends JFrame implements SimpleWindow {
     }
 
     /**
+     * Get the last dimension of the window before MAXIMIZED or MINIMIZED
+     * @return the last dimension
+     */
+    public Dimension getLastDimension() {
+        if (lastDimension == null) {
+            return getSize();
+        }
+
+        return lastDimension;
+    }
+
+    /**
+     * Get the last position of the window before MAXIMIZED or MINIMIZED
+     * @return the last position
+     */
+    public Point getLastPosition() {
+        if (lastPosition == null) {
+            return getLocation();
+        }
+
+        return lastPosition;
+    }
+
+    /**
      * This method registers some methods against the specific Mac OS X API
      * (in order to set the "special" mac os x menus)
      */
@@ -268,11 +313,11 @@ public class SwingScilabWindow extends JFrame implements SimpleWindow {
             /* javasci bug: See bug 9544 why we are doing this check */
             try {
                 SwingUtilities.invokeAndWait(new Runnable() {
-                    @Override
-                    public void run() {
-                        raiseToFront();
-                    }
-                });
+                        @Override
+                        public void run() {
+                            raiseToFront();
+                        }
+                    });
             } catch (InterruptedException e) {
                 e.printStackTrace();
             } catch (InvocationTargetException e) {
index 3b18f73..76cfb31 100644 (file)
@@ -235,10 +235,10 @@ public class WindowsConfigurationManager implements XConfigurationListener {
 
         Element root = doc.getDocumentElement();
         Element win = createNode(root, "Window", new Object[] {"uuid", window.getUUID(),
-                                                               "x", (int) window.getLocation().getX(),
-                                                               "y", (int) window.getLocation().getY(),
-                                                               "width", (int) window.getSize().getWidth(),
-                                                               "height", (int) window.getSize().getHeight(),
+                                                               "x", (int) window.getLastPosition().getX(),
+                                                               "y", (int) window.getLastPosition().getY(),
+                                                               "width", (int) window.getLastDimension().getWidth(),
+                                                               "height", (int) window.getLastDimension().getHeight(),
                                                                "state", window.getExtendedState()
             });
         LayoutNode layoutNode = window.getDockingPort().exportLayout();