Prefs: add a way to backup preferences file 31/7631/3
Calixte DENIZET [Wed, 20 Jun 2012 19:39:20 +0000 (21:39 +0200)]
Change-Id: I02b83e7fa50ec172c6465e76b70eb6a14f907290

scilab/modules/commons/src/java/org/scilab/modules/commons/ScilabCommonsUtils.java
scilab/modules/preferences/Makefile.am
scilab/modules/preferences/Makefile.in
scilab/modules/preferences/etc/XConfiguration-preference.xml [new file with mode: 0644]
scilab/modules/preferences/etc/XConfiguration-preference.xsl [new file with mode: 0644]
scilab/modules/preferences/etc/XConfiguration.xml
scilab/modules/preferences/src/java/org/scilab/modules/preferences/Component/FileSelector.java
scilab/modules/preferences/src/java/org/scilab/modules/preferences/Component/Select.java
scilab/modules/preferences/src/java/org/scilab/modules/preferences/XCommonManager.java
scilab/modules/preferences/src/java/org/scilab/modules/preferences/XConfigManager.java

index e1ec2d9..ab26e15 100644 (file)
@@ -100,6 +100,32 @@ public final class ScilabCommonsUtils {
     }
 
     /**
+     * Replace ~ by user home, SCI by Scilab main directory, ...
+     * @param baseDir the base directory
+     * @return correct base directory
+     */
+    public static String getCorrectedPath(String path) {
+        path = path.trim();
+        if (path != null && !path.isEmpty()) {
+            if (path.startsWith("~" + File.separator) || path.equals("~")) {
+                return path.replaceFirst("~", ScilabConstants.USERHOME);
+            } else if (path.startsWith("SCI" + File.separator) || path.equals("SCI")) {
+                try {
+                    return path.replaceFirst("SCI", ScilabConstants.SCI.getCanonicalPath());
+                } catch (IOException e) {
+                    return path.replaceFirst("SCI", ScilabConstants.SCI.getAbsolutePath());
+                }
+            } else if (path.startsWith("SCIHOME" + File.separator) || path.equals("SCIHOME")) {
+                return path.replaceFirst("SCIHOME", ScilabCommons.getSCIHOME());
+            } else if (path.startsWith("TMPDIR" + File.separator) || path.equals("TMPDIR")) {
+                return path.replaceFirst("TMPDIR", ScilabCommons.getTMPDIR());
+            }
+        }
+
+        return path;
+    }
+
+    /**
      * Load on use
      * @param str the action
      */
index c82f0b5..9bfcff5 100644 (file)
@@ -24,6 +24,8 @@ libscipreferences_la_etc_DATA = \
        etc/preferences.quit \
        etc/preferences.start \
        etc/XConfiguration.xml \
+       etc/XConfiguration-preference.xml \
+       etc/XConfiguration-preference.xsl \
        etc/XWizard-expression.xml \
        etc/XWizard-function.xml \
        etc/XWizard-xcos.xml \
index fa03a44..771a768 100644 (file)
@@ -456,6 +456,8 @@ libscipreferences_la_etc_DATA = \
        etc/preferences.quit \
        etc/preferences.start \
        etc/XConfiguration.xml \
+       etc/XConfiguration-preference.xml \
+       etc/XConfiguration-preference.xsl \
        etc/XWizard-expression.xml \
        etc/XWizard-function.xml \
        etc/XWizard-xcos.xml \
diff --git a/scilab/modules/preferences/etc/XConfiguration-preference.xml b/scilab/modules/preferences/etc/XConfiguration-preference.xml
new file mode 100644 (file)
index 0000000..6d30f11
--- /dev/null
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<preference title="Preferences">
+  <body>
+    <backups restore-path="" save-path=""/>
+  </body>
+</preference>
diff --git a/scilab/modules/preferences/etc/XConfiguration-preference.xsl b/scilab/modules/preferences/etc/XConfiguration-preference.xsl
new file mode 100644 (file)
index 0000000..b82a108
--- /dev/null
@@ -0,0 +1,54 @@
+<?xml version='1.0' encoding='utf-8'?>
+<xsl:stylesheet version ="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+  <xsl:template match="preference" mode="tooltip">Preferences</xsl:template>
+  <xsl:template match="backups">
+    <xsl:variable name="name" select="@name"/>
+    <Title text="Preferences backup">
+    <Grid>
+      <Label gridx="1" gridy="1" weightx="0" text="Select a backup file "/>
+      <FileSelector gridx="2" gridy="1" weightx="1" anchor="above_baseline"
+                    listener="EntryListener"
+                    href="{@restore-path}"
+                    mask="*.xml"
+                    desc="Scilab Configuration files"
+                    dir-selection="false"
+                   check-entry="false">
+        <entryChanged choose="restore-path">
+          <xsl:call-template name="context"/>
+        </entryChanged>
+      </FileSelector>
+      <Panel gridx="3" gridy="1" weightx="1" fill="both"/>
+      <Button gridx="4" gridy="1" text="Restore" listener="ActionListener">
+        <actionPerformed callback="Restore Backup">
+          <xsl:attribute name="path">
+            <xsl:value-of select="@restore-path"/>
+          </xsl:attribute>
+         <xsl:call-template name="context"/>
+        </actionPerformed>
+      </Button>
+      <Label gridx="1" gridy="2" weightx="0" text="Create a backup file "/>
+      <FileSelector gridx="2" gridy="2" weightx="1" anchor="above_baseline"
+                    listener="EntryListener"
+                    href="{@save-path}"
+                    mask="*.xml"
+                    desc="Scilab Configuration files"
+                    dir-selection="false"
+                   check-entry="false">
+        <entryChanged choose="save-path">
+          <xsl:call-template name="context"/>
+        </entryChanged>
+      </FileSelector>
+      <Panel gridx="3" gridy="2" weightx="1" fill="both"/>
+      <Button gridx="4" gridy="2" text="Save" listener="ActionListener">
+        <actionPerformed callback="Save Backup">
+         <xsl:attribute name="path">
+            <xsl:value-of select="@save-path"/>
+          </xsl:attribute>
+         <xsl:call-template name="context"/>
+        </actionPerformed>
+      </Button>
+    </Grid>
+    </Title>
+  </xsl:template>
+</xsl:stylesheet>
\ No newline at end of file
index 279acf1..31af516 100644 (file)
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <interface path="1/" width="800" height="550">
   <general/>
+  <preference/>
   <fonts/>
   <colors/>
   <console/>
index d5e3bc6..623a8a8 100644 (file)
@@ -32,6 +32,7 @@ import javax.swing.SwingUtilities;
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentListener;
 
+import org.scilab.modules.commons.ScilabCommonsUtils;
 import org.scilab.modules.preferences.XCommonManager;
 import org.scilab.modules.preferences.XComponent;
 import org.scilab.modules.preferences.XChooser;
@@ -60,6 +61,7 @@ public class FileSelector extends Panel implements XComponent, XChooser, Documen
     private JButton button;
     private String currentDir = System.getProperty("user.home");
     private boolean dirSelection;
+    private boolean checkEntry;
     private String previousPath;
 
     /** Constructor.
@@ -72,6 +74,7 @@ public class FileSelector extends Panel implements XComponent, XChooser, Documen
 
         textField = new JTextField();
         textField.setEditable(true);
+        textField.setColumns(10);
         textField.getDocument().addDocumentListener(this);
 
         button = new JButton("...");
@@ -94,15 +97,13 @@ public class FileSelector extends Panel implements XComponent, XChooser, Documen
                 String[] paths = fileChooser.getSelection();
                 if (paths != null && paths.length != 0 && !paths[0].isEmpty()) {
                     File file = new File(paths[0]);
-                    if (file.exists()) {
-                        if (dirSelection) {
-                            currentDir = file.getAbsolutePath();
-                        } else {
-                            currentDir = file.getParentFile().getAbsolutePath();
-                        }
-
-                        textField.setText(file.getAbsolutePath());
+                    if (dirSelection) {
+                        currentDir = file.getAbsolutePath();
+                    } else {
+                        currentDir = file.getParentFile().getAbsolutePath();
                     }
+
+                    textField.setText(file.getAbsolutePath());
                 }
             }
         });
@@ -127,6 +128,9 @@ public class FileSelector extends Panel implements XComponent, XChooser, Documen
 
         String dirsel = XCommonManager.getAttribute(peer, "dir-selection", "false");
         dirSelection = dirsel.equals("true");
+
+        String checkentry = XConfigManager.getAttribute(peer, "check-entry", "true");
+        checkEntry = checkentry.equals("true");
     }
 
     public void addDocumentListener(DocumentListener listener) {
@@ -138,7 +142,7 @@ public class FileSelector extends Panel implements XComponent, XChooser, Documen
      * @return array of actuator names.
      */
     public final String[] actuators() {
-        String[] actuators = {"enable", "href", "desc", "mask", "dir-selection"};
+        String[] actuators = {"enable", "href", "desc", "mask", "dir-selection", "check-entry"};
         return actuators;
     }
 
@@ -147,17 +151,20 @@ public class FileSelector extends Panel implements XComponent, XChooser, Documen
      * @param peer the corresponding view DOM node
      */
     public final void refresh(final Node peer) {
-        String href = XCommonManager.getAttribute(peer , "href");
+        String href = XCommonManager.getAttribute(peer, "href");
         if (!href.equals(href())) {
             href(href);
         }
 
-        String dirsel = XCommonManager.getAttribute(peer , "dir-selection", "false");
+        String dirsel = XCommonManager.getAttribute(peer, "dir-selection", "false");
         dirSelection = dirsel.equals("true");
 
-        String enable = XConfigManager.getAttribute(peer , "enable", "true");
+        String enable = XConfigManager.getAttribute(peer, "enable", "true");
         textField.setEnabled(enable.equals("true"));
         button.setEnabled(enable.equals("true"));
+
+        String checkentry = XConfigManager.getAttribute(peer, "check-entry", "true");
+        checkEntry = checkentry.equals("true");
     }
 
     /** Sensor for 'href' attribute.
@@ -214,6 +221,8 @@ public class FileSelector extends Panel implements XComponent, XChooser, Documen
             return false;
         }
 
+        path = ScilabCommonsUtils.getCorrectedPath(path);
+
         File file = new File(path);
         boolean ok = false;
         if (file.exists()) {
@@ -224,13 +233,13 @@ public class FileSelector extends Panel implements XComponent, XChooser, Documen
             }
         }
 
-        if (ok) {
+        if (!checkEntry || ok) {
             textField.setForeground(NORMALCOLOR);
         } else {
             textField.setForeground(ERRORCOLOR);
         }
 
-        return ok;
+        return checkEntry ? ok : true;
     }
 
     /** Actual response read by the listener.
index 3bd89a7..5c07704 100644 (file)
@@ -67,8 +67,12 @@ public class Select extends JComboBox implements XComponent, XChooser {
                 index += 1;
             }
         }
-        setSelectedIndex(select);
+
+        if (select < getItemCount()) {
+            setSelectedIndex(select);
+        }
     }
+
     /** Refresh the component by the use of actuators.
     *
     * @param peer the corresponding view DOM node
@@ -87,7 +91,7 @@ public class Select extends JComboBox implements XComponent, XChooser {
                 index += 1;
             }
         }
-        if (select != getSelectedIndex()) {
+        if (select != getSelectedIndex() && select < getItemCount()) {
             setEnabled(false);
             setSelectedIndex(select);
         }
index 82b134b..442aec1 100644 (file)
@@ -23,7 +23,6 @@ import java.io.File;
 import java.io.FilenameFilter;
 import java.io.FileFilter;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.StringReader;
@@ -753,7 +752,7 @@ public abstract class XCommonManager {
         /* Create a local copy of the configuration file */
         try {
             copyFile(new File(original), new File(copy));
-        } catch (FileNotFoundException e) {
+        } catch (IOException e) {
             System.err.println(ERROR_READ + copy);
         }
     }
@@ -764,7 +763,7 @@ public abstract class XCommonManager {
      * @param out dest file
      * @throws FileNotFoundException
      */
-    private static void copyFile(final File in, final File out) throws FileNotFoundException {
+    protected static void copyFile(final File in, final File out) throws IOException {
         FileInputStream fis = new FileInputStream(in);
         FileOutputStream fos = new FileOutputStream(out);
 
index 8ae85c3..8beb468 100644 (file)
@@ -27,6 +27,7 @@ import java.awt.event.ComponentListener;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.io.File;
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -40,6 +41,7 @@ import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 import org.scilab.modules.commons.ScilabCommons;
+import org.scilab.modules.commons.ScilabCommonsUtils;
 import org.scilab.modules.commons.xml.XConfiguration;
 import org.scilab.modules.gui.utils.Position;
 import org.scilab.modules.gui.utils.Size;
@@ -272,6 +274,39 @@ public final class XConfigManager extends XCommonManager {
             return true;
             }
         **/
+        if (callback.equals("Save Backup")) {
+            String path = getAttribute(action, "path");
+            writeDocument(ScilabCommonsUtils.getCorrectedPath(path), document);
+
+            return true;
+        }
+
+        if (callback.equals("Restore Backup")) {
+            String path = getAttribute(action, "path");
+            try {
+                copyFile(new File(ScilabCommonsUtils.getCorrectedPath(path)), new File(USER_CONFIG_FILE));
+            } catch (IOException e) {
+                System.err.println(e);
+            }
+
+            XConfiguration.invalidate();
+            XConfiguration.addModifiedPath("ALL");
+            reloadTransformer(SCILAB_CONFIG_XSL);
+            document = XConfiguration.createDocument();
+
+            List<ToolboxInfos> infos = ScilabPreferences.getToolboxesInfos();
+            for (ToolboxInfos info : infos) {
+                String filename = new File(info.getPrefFile()).getName();
+                String tbxFile = ScilabCommons.getSCIHOME() + "/" + filename;
+                refreshUserCopy(info.getPrefFile(), tbxFile);
+            }
+            readUserDocuments();
+            updated = false;
+            refreshDisplay();
+
+            return true;
+        }
+
         if (callback.equals("Ok")) {
             WriteUserDocuments();
             dialog.dispose();