Don't save win properties in NW mode ... 96/12296/1
Calixte DENIZET [Mon, 19 Aug 2013 13:55:26 +0000 (15:55 +0200)]
Change-Id: Ia653e9ae4fdd855cd4f43c49e441822e66b5e2db

scilab/modules/commons/src/java/org/scilab/modules/commons/ScilabConstants.java
scilab/modules/core/src/java/org/scilab/modules/core/Scilab.java
scilab/modules/gui/src/java/org/scilab/modules/gui/utils/WindowsConfigurationManager.java

index 4f04a43..f659fe1 100644 (file)
@@ -52,6 +52,9 @@ public class ScilabConstants {
     /** Escape double quote symbol */
     public static final char QUOTE = '\"';
 
+    /** 2 is for GUI **/
+    public static int mode;
+
     /*
      * Static constructor
      */
@@ -79,4 +82,12 @@ public class ScilabConstants {
     /** This class is a static singleton, thus it must not be instantiated */
     protected ScilabConstants() { }
 
+    public static void setMode(int mode) {
+        ScilabConstants.mode = mode;
+    }
+
+    public static boolean isGUI() {
+        return ScilabConstants.mode == 2;
+    }
+
 }
index bf4d574..f722875 100644 (file)
@@ -92,6 +92,8 @@ public class Scilab {
      */
     public Scilab(int mode) {
         Scilab.mode = mode;
+        ScilabConstants.setMode(mode);
+
         DockingManager.setDockableFactory(ScilabTabFactory.getInstance());
 
         /*
index 3170170..b7a29fe 100644 (file)
@@ -266,25 +266,27 @@ public class WindowsConfigurationManager implements XConfigurationListener {
      * @param window the window
      */
     public static void saveWindowProperties(SwingScilabWindow window) {
-        readDocument();
+        if (ScilabConstants.isGUI()) {
+            readDocument();
 
-        Element root = doc.getDocumentElement();
-        Element win = createNode(root, "Window", new Object[] {"uuid", window.getUUID(),
-                                 "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();
-        LayoutNodeSerializer serializer = new LayoutNodeSerializer();
-        win.appendChild(serializer.serialize(doc, layoutNode));
-
-        for (Dockable dockable : (Set<Dockable>) window.getDockingPort().getDockables()) {
-            saveTabProperties((SwingScilabTab) dockable, false);
-        }
+            Element root = doc.getDocumentElement();
+            Element win = createNode(root, "Window", new Object[] {"uuid", window.getUUID(),
+                                     "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();
+            LayoutNodeSerializer serializer = new LayoutNodeSerializer();
+            win.appendChild(serializer.serialize(doc, layoutNode));
+
+            for (Dockable dockable : (Set<Dockable>) window.getDockingPort().getDockables()) {
+                saveTabProperties((SwingScilabTab) dockable, false);
+            }
 
-        writeDocument();
+            writeDocument();
+        }
     }
 
     /**
@@ -368,6 +370,32 @@ public class WindowsConfigurationManager implements XConfigurationListener {
         return window;
     }
 
+    public static SwingScilabWindow restoreWindow(String uuid) {
+        String winuuid = UUID.randomUUID().toString();
+        SwingScilabWindow win = new SwingScilabWindow();
+        win.setUUID(winuuid);
+        win.setIsRestoring(true);
+
+        final SwingScilabTab tab = ScilabTabFactory.getInstance().getTab(uuid);
+        win.addTab(tab);
+        BarUpdater.forceUpdateBars(tab.getParentWindowId(), tab.getMenuBar(), tab.getToolBar(), tab.getInfoBar(), tab.getName(), tab.getWindowIcon());
+
+        win.pack();
+        win.setVisible(true);
+        win.requestFocus();
+        win.toFront();
+        win.setIsRestoring(false);
+
+        SwingUtilities.invokeLater(new Runnable() {
+            @Override
+            public void run() {
+                ActiveDockableTracker.requestDockableActivation(tab);
+            }
+        });
+
+        return win;
+    }
+
     /**
      * Restore a window with a given uuid
      *
@@ -513,7 +541,9 @@ public class WindowsConfigurationManager implements XConfigurationListener {
      * @param uuid the win uuid
      */
     public static final void removeWin(String uuid) {
-        alreadyRestoredWindows.remove(uuid);
+        if (ScilabConstants.isGUI()) {
+            alreadyRestoredWindows.remove(uuid);
+        }
     }
 
     /**
@@ -624,8 +654,27 @@ public class WindowsConfigurationManager implements XConfigurationListener {
      * @param uuid the tab uuid to restore
      */
     private static final void startRestoration(String uuid) {
+        if (!ScilabConstants.isGUI()) {
+            readDocument();
+            Element root = doc.getDocumentElement();
+            Element e = ScilabXMLUtilities.getElementsWithAttributeEquals(root, "uuid", uuid).get(0);
+            if (e != null) {
+                String winuuid = e.getAttribute("winuuid");
+                if (!winuuid.isEmpty() && !winuuid.equals(NULLUUID)) {
+                    List<Element> elements = ScilabXMLUtilities.getElementsWithAttributeEquals(root, "winuuid", winuuid);
+                    for (Element ee : elements) {
+                        if (ee.getNodeName().equals("Console")) {
+                            restoreWindow(uuid);
+                            return;
+                        }
+                    }
+                }
+            }
+        }
+
         Set<Element> list = createDescendantTabs(uuid);
         list = createAdjacentTabs(list);
+
         List<String> wins = new ArrayList<String>();
         List<String> tabsWithoutWin = new ArrayList<String>();
         for (Element e : list) {
@@ -911,34 +960,36 @@ public class WindowsConfigurationManager implements XConfigurationListener {
      * @param nullWin if true, the winuuid will be set to 0 (the tab is not docked)
      */
     public static void saveTabProperties(SwingScilabTab tab, boolean nullWin) {
-        readDocument();
+        if (ScilabConstants.isGUI()) {
+            readDocument();
 
-        ScilabTabFactory factory = ScilabTabFactory.getInstance();
-        String uuid = tab.getPersistentId();
-        Element root = doc.getDocumentElement();
+            ScilabTabFactory factory = ScilabTabFactory.getInstance();
+            String uuid = tab.getPersistentId();
+            Element root = doc.getDocumentElement();
 
-        String app = factory.getApplication(uuid);
-        if (app.isEmpty()) {
-            return;
-        }
+            String app = factory.getApplication(uuid);
+            if (app.isEmpty()) {
+                return;
+            }
 
-        String winuuid;
-        if (nullWin) {
-            winuuid = NULLUUID;
-        } else {
-            winuuid = tab.getParentWindowUUID();
-        }
+            String winuuid;
+            if (nullWin) {
+                winuuid = NULLUUID;
+            } else {
+                winuuid = tab.getParentWindowUUID();
+            }
 
-        Dimension dim = tab.getSize();
+            Dimension dim = tab.getSize();
 
-        createNode(root, app, new Object[] {"winuuid", winuuid,
-                                            "uuid", uuid,
-                                            "load", factory.getPackage(uuid),
-                                            "factory", factory.getClassName(uuid),
-                                            "width", (int) dim.getWidth(),
-                                            "height", (int) dim.getHeight()
-                                           });
-        writeDocument();
+            createNode(root, app, new Object[] {"winuuid", winuuid,
+                                                "uuid", uuid,
+                                                "load", factory.getPackage(uuid),
+                                                "factory", factory.getClassName(uuid),
+                                                "width", (int) dim.getWidth(),
+                                                "height", (int) dim.getHeight()
+                                               });
+            writeDocument();
+        }
     }
 
     /**
@@ -946,24 +997,26 @@ public class WindowsConfigurationManager implements XConfigurationListener {
      * and validate the different windows.
      */
     public static void clean() {
-        readDocument();
-
-        validateWindows();
+        if (ScilabConstants.isGUI()) {
+            readDocument();
 
-        Element root = doc.getDocumentElement();
-        NodeList list = root.getElementsByTagName("Window");
-        int len = getNodeListLength(list);
-        for (int i = 0; i < len; i++) {
-            if (list.item(i) instanceof Element) {
-                String uuid = ((Element) list.item(i)).getAttribute("uuid");
-                List<Element> elements = ScilabXMLUtilities.getElementsWithAttributeEquals(root, "winuuid", uuid);
-                if (elements == null || elements.size() == 0) {
-                    root.removeChild(list.item(i));
-                    removeWin(uuid);
+            validateWindows();
+
+            Element root = doc.getDocumentElement();
+            NodeList list = root.getElementsByTagName("Window");
+            int len = getNodeListLength(list);
+            for (int i = 0; i < len; i++) {
+                if (list.item(i) instanceof Element) {
+                    String uuid = ((Element) list.item(i)).getAttribute("uuid");
+                    List<Element> elements = ScilabXMLUtilities.getElementsWithAttributeEquals(root, "winuuid", uuid);
+                    if (elements == null || elements.size() == 0) {
+                        root.removeChild(list.item(i));
+                        removeWin(uuid);
+                    }
                 }
             }
+            writeDocument();
         }
-        writeDocument();
     }
 
     /**
@@ -972,13 +1025,15 @@ public class WindowsConfigurationManager implements XConfigurationListener {
      * @param childUUID the child tab uuid
      */
     public static final void makeDependency(String parentUUID, String childUUID) {
-        readDocument();
+        if (ScilabConstants.isGUI()) {
+            readDocument();
 
-        Element e = getElementWithUUID(doc.getDocumentElement(), childUUID);
-        if (e != null) {
-            e.setAttribute("depends", parentUUID);
+            Element e = getElementWithUUID(doc.getDocumentElement(), childUUID);
+            if (e != null) {
+                e.setAttribute("depends", parentUUID);
+            }
+            writeDocument();
         }
-        writeDocument();
     }
 
     /**
@@ -986,13 +1041,15 @@ public class WindowsConfigurationManager implements XConfigurationListener {
      * @param childUUID the child tab uuid
      */
     public static void removeDependency(String childUUID) {
-        readDocument();
+        if (ScilabConstants.isGUI()) {
+            readDocument();
 
-        Element e = getElementWithUUID(doc.getDocumentElement(), childUUID);
-        if (e != null) {
-            e.removeAttribute("depends");
+            Element e = getElementWithUUID(doc.getDocumentElement(), childUUID);
+            if (e != null) {
+                e.removeAttribute("depends");
+            }
+            writeDocument();
         }
-        writeDocument();
     }
 
     /**
@@ -1025,7 +1082,10 @@ public class WindowsConfigurationManager implements XConfigurationListener {
             }
         }
 
-        writeDocument();
+        if (ScilabConstants.isGUI()) {
+            writeDocument();
+        }
+
         doc = null;
 
         return true;