Bug 9884 fixed: Add in prefs the possibility to add a default header on new files... 69/7769/4
Calixte DENIZET [Wed, 4 Jul 2012 12:13:14 +0000 (14:13 +0200)]
Change-Id: Iaf6cb9c5c1bc8c234a2bc129277aa310a8de8e28

scilab/CHANGES_5.4.X
scilab/modules/preferences/src/java/org/scilab/modules/preferences/Component/FontSelector.java
scilab/modules/preferences/src/java/org/scilab/modules/preferences/Component/TextArea.java
scilab/modules/preferences/src/java/org/scilab/modules/preferences/XCommonManager.java
scilab/modules/preferences/src/java/org/scilab/modules/preferences/XUpdateVisitor.java
scilab/modules/scinotes/etc/XConfiguration-scinotes.xml
scilab/modules/scinotes/etc/XConfiguration-scinotes.xsl
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/SciNotes.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/SciNotesConfiguration.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/SciNotesOptions.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/ScilabEditorPane.java

index 47a92e5..0a056ae 100644 (file)
@@ -28,6 +28,9 @@ Bug Fixes
 
 * Bug #9830 fixed - leastsq did not match the information of optim.
 
+* Bug #9884 fixed - Add in prefs the possibility to add a default header on
+                   new files in SciNotes.
+
 * Bug #10150 fixed - Completion help page moved to the console chapter.
 
 * Bug #10595 fixed - In SciNotes, <TAB> didn't go to the next tab position.
index ca55a75..ef85aa9 100644 (file)
@@ -228,7 +228,6 @@ public class FontSelector extends Panel implements XComponent, XChooser {
         return new String[] {fontname(), fontface(), fontsize()};
     }
 
-
     public void addActionListener(ActionListener actionListener) {
         this.actionListener = actionListener;
     }
index 67d4418..171f5d0 100644 (file)
 package org.scilab.modules.preferences.Component;
 
 import java.awt.Color;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.regex.Pattern;
 
 import javax.swing.BorderFactory;
+import javax.swing.JScrollBar;
+import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
+import javax.swing.ScrollPaneConstants;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
 
+import org.scilab.modules.preferences.XChooser;
 import org.scilab.modules.preferences.XCommonManager;
 import org.scilab.modules.preferences.XComponent;
 import org.scilab.modules.preferences.XConfigManager;
 
 import org.w3c.dom.Node;
 
-/** 
+/**
  * Implementation of a TextArea.
  *
  * @author Calixte DENIZET
  *
  */
-public class TextArea extends Panel implements XComponent {
+public class TextArea extends Panel implements XComponent, XChooser, DocumentListener {
 
     /** Universal identifier for serialization.
      *
@@ -37,13 +46,15 @@ public class TextArea extends Panel implements XComponent {
     private static final long serialVersionUID = -7007541669965737408L;
 
     private JTextArea textarea;
+    private JScrollPane scrollPane;
+    private ActionListener actionListener;
 
     /** Define the set of actuators.
      *
      * @return array of actuator names.
      */
     public final String [] actuators() {
-        String [] actuators = {"enable", "text", "columns", "rows", "editable"};
+        String [] actuators = {"enable", "text", "columns", "rows", "editable", "scroll"};
         return actuators;
     }
 
@@ -53,9 +64,12 @@ public class TextArea extends Panel implements XComponent {
      */
     public TextArea(final Node peer) {
         super(peer);
-       textarea = new JTextArea();
-       textarea.setBorder(BorderFactory.createLineBorder(Color.GRAY));
-       add(textarea);
+        textarea = new JTextArea();
+        textarea.setBorder(BorderFactory.createLineBorder(Color.GRAY));
+        scrollPane = new JScrollPane(textarea);
+        add(scrollPane);
+        textarea.setFocusable(true);
+        textarea.setColumns(10);
         refresh(peer);
     }
 
@@ -66,8 +80,9 @@ public class TextArea extends Panel implements XComponent {
     public final void refresh(final Node peer) {
         String text = XCommonManager.getAttribute(peer, "text");
         String columns = XCommonManager.getAttribute(peer, "columns");
-       String rows = XCommonManager.getAttribute(peer, "rows");
-       String editable = XCommonManager.getAttribute(peer, "editable");
+        String rows = XCommonManager.getAttribute(peer, "rows");
+        String editable = XCommonManager.getAttribute(peer, "editable");
+        String scroll = XCommonManager.getAttribute(peer, "scroll");
 
         if (!text.equals(text())) {
             text(text);
@@ -85,8 +100,47 @@ public class TextArea extends Panel implements XComponent {
             editable(editable);
         }
 
+        if (!scroll.equals(scroll())) {
+            scroll(scroll);
+        }
+
         boolean enable = XConfigManager.getBoolean(peer, "enable", true);
         textarea.setEnabled(enable);
+        JScrollBar bar = scrollPane.getVerticalScrollBar();
+        if (bar != null) {
+            bar.setEnabled(enable);
+        }
+
+        bar = scrollPane.getHorizontalScrollBar();
+        if (bar != null) {
+            bar.setEnabled(enable);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void changedUpdate(DocumentEvent e) { }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void insertUpdate(DocumentEvent e) {
+        update();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void removeUpdate(DocumentEvent e) {
+        update();
+    }
+
+    private void update() {
+        if (actionListener != null) {
+            ActionEvent transmit = new ActionEvent(this, 0, "TextArea change", System.currentTimeMillis(), 0);
+            actionListener.actionPerformed(transmit);
+        }
     }
 
     /** Sensor for 'text' attribute.
@@ -140,7 +194,7 @@ public class TextArea extends Panel implements XComponent {
      * @param text : the attribute value.
      */
     public final void editable(final String editable) {
-       textarea.setEditable(editable.equalsIgnoreCase("true"));
+        textarea.setEditable(editable.equalsIgnoreCase("true"));
     }
 
     /** Sensor for 'columns' attribute.
@@ -155,10 +209,35 @@ public class TextArea extends Panel implements XComponent {
      *
      * @param text : the attribute value.
      */
+    public final void scroll(final String scroll) {
+        scrollPane.setVerticalScrollBarPolicy(scroll.equalsIgnoreCase("true") ? ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED : ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
+        scrollPane.setHorizontalScrollBarPolicy(scroll.equalsIgnoreCase("true") ? ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED : ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
+    }
+
+    /** Sensor for 'columns' attribute.
+     *
+     * @return the attribute value.
+     */
+    public final String scroll() {
+        return Boolean.toString(scrollPane.getVerticalScrollBarPolicy() == ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
+    }
+
+    /** Actuator for 'text' attribute.
+     *
+     * @param text : the attribute value.
+     */
     public final void text(final String text) {
         textarea.setText(text);
     }
 
+    public void addActionListener(ActionListener actionListener) {
+        this.actionListener = actionListener;
+    }
+
+    public void addDocumentListener(DocumentListener listener) {
+        textarea.getDocument().addDocumentListener(listener);
+    }
+
     /** Actual response read by the listener.
      *
      * @return response read by the listener.
index e95f195..99257c1 100644 (file)
@@ -528,12 +528,13 @@ public abstract class XCommonManager {
         }
 
         if (!getAttribute(action, "choose").equals(NAV) && enable) {
-            String context = getAttribute(action, "context");
-            Element element = getElementByContext(context);
-
             if (source == null) {
                 return false;
             }
+
+            String context = getAttribute(action, "context");
+            Element element = getElementByContext(context);
+
             if (source instanceof XChooser) {
                 XChooser chooser = (XChooser) source;
                 Object value = chooser.choose();
@@ -561,6 +562,37 @@ public abstract class XCommonManager {
             }
             return true;
         }
+
+        if (!getAttribute(action, "choose-child").equals(NAV) && enable) {
+            if (source == null) {
+                return false;
+            }
+
+            int childNb = Integer.parseInt(getAttribute(action, "choose-child")) - 1;
+            String context = getAttribute(action, "context");
+            Element element = getElementByContext(context);
+            NodeList list = element.getChildNodes();
+            if (element == null || childNb < 0 || childNb >= list.getLength()) {
+                return false;
+            }
+            Node child = list.item(childNb);
+
+            if (source instanceof XChooser) {
+                XChooser chooser = (XChooser) source;
+                Object value = chooser.choose();
+                if (value != null) {
+                    child.setTextContent(value.toString());
+                    XConfiguration.addModifiedPath(getNodePath(element));
+                    refreshDisplay();
+                    updated = true;
+                }
+            } else {
+                System.err.println("@choose-child attribute only valid on choosers " + "(SELECT, COLOR, FILE, ENTRY,...)");
+            }
+
+            return true;
+        }
+
         return false;
     }
 
index b085d2b..d1cd398 100644 (file)
@@ -33,8 +33,9 @@ import javax.swing.border.TitledBorder;
 import org.scilab.modules.preferences.Component.Entry;
 import org.scilab.modules.preferences.Component.FileSelector;
 import org.scilab.modules.preferences.Component.Scroll;
-import org.scilab.modules.preferences.Component.Table;
 import org.scilab.modules.preferences.Component.Select;
+import org.scilab.modules.preferences.Component.Table;
+import org.scilab.modules.preferences.Component.TextArea;
 
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -262,6 +263,11 @@ public class XUpdateVisitor {
                 ((FileSelector) component).addDocumentListener(sentinel);
                 return;
             }
+
+            if (component instanceof TextArea) {
+                ((TextArea) component).addDocumentListener(sentinel);
+                return;
+            }
         }
 
         if (listener.equals("TableListener")) {
index c8803d4..0ce44c5 100644 (file)
@@ -8,6 +8,22 @@
       <scinotes-display highlight-current-line="false" current-line-color="#FACADE" show-line-numbers="true" wrap-lines="true" keywords-colorization="true" highlight-brackets="true" brackets-color="#E40087" brackets-highlightment="Framed" brackets-onmouseover="true" highlight-keywords="true" keywords-color="#FFF3D2" keywords-highlightment="Filled" keywords-onmouseover="true" whereami="false" tab-size="4" tab-representation="chevrons" use-spaces="true" indent-size="4" automatic-indent="true" auto-complete-openers="true" auto-complete-keywords="true"/>
     </body>
   </display>
+  <header title="Header">
+    <body>
+      <scinotes-header enable="true"><![CDATA[// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) {$current-year} - Scilab Enterprises - Mr Puffin
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+//
+// Date of creation: {$current-date}
+//
+]]></scinotes-header>
+    </body>
+  </header>
   <autosave title="Autosave">
     <body>
       <scinotes-autosave enable="true" save-every="5" automatic-delete="true" append-filename="true" append-with="~" replace-with="tmp" source-flag="true" single-directory=""/>
index ff6f538..a160ceb 100644 (file)
       </Grid>\r
     </Title>\r
   </xsl:template>\r
+\r
+  <xsl:template match="scinotes-header">\r
+    <Title text="Default header">\r
+      <Grid>\r
+        <Checkbox checked="{@enable}" selected-value="true" unselected-value="false" listener="ActionListener" text="Add a default header to new file" gridx="1" gridy="1" fill="none" weightx="0" anchor="west">\r
+          <actionPerformed choose="enable">\r
+            <xsl:call-template name="context"/>\r
+          </actionPerformed>\r
+        </Checkbox>\r
+        <Panel gridx="2" gridy="1" gridheight="1" fill="both"/>\r
+       <TextArea gridx="1" gridy="2" weightx="1" gridwidth="2" weighty="0" anchor="west"\r
+                 editable="true"\r
+                 rows="15"\r
+                 scroll="true"\r
+                 listener="EntryListener"\r
+                 enable="{@enable}">\r
+         <xsl:attribute name="text">\r
+           <xsl:value-of select="string(child::node()[1])" disable-output-escaping="yes"/>\r
+         </xsl:attribute>\r
+         <entryChanged choose-child="1">\r
+            <xsl:call-template name="context"/>\r
+          </entryChanged>\r
+       </TextArea>\r
+      </Grid>\r
+      </Title>\r
+    </xsl:template> \r
 </xsl:stylesheet>\r
index dc533be..a56210c 100644 (file)
@@ -1353,6 +1353,19 @@ public class SciNotes extends SwingScilabTab {
      * @return the text component inside the tab
      */
     public ScilabEditorPane addTab(String title, int index) {
+        return addTab(title, index, 0);
+    }
+
+    /**
+     * Create a new tab in SciNotes.
+     *
+     * @param title
+     *            the title of the tab
+     * @param index
+     *            the index where to put the new tab
+     * @return the text component inside the tab
+     */
+    public ScilabEditorPane addTab(String title, int index, int caretPos) {
         ScilabEditorPane sep = new ScilabEditorPane(this);
         initPane(sep);
         int ind = Math.min(Math.max(0, index), tabPane.getTabCount());
@@ -1362,7 +1375,7 @@ public class SciNotes extends SwingScilabTab {
         updateTabTitle();
         getInfoBar().setText(sep.getInfoBarText());
         repaint();
-        sep.init();
+        sep.init(caretPos);
         return sep;
     }
 
@@ -1519,6 +1532,16 @@ public class SciNotes extends SwingScilabTab {
         sep.setShortName(SciNotesMessages.UNTITLED + n);
         sep.setTitle(SciNotesMessages.UNTITLED + n);
         setTitle(sep.getTitle());
+        SciNotesOptions.Header header = SciNotesOptions.getSciNotesHeader();
+        if (header.header != null) {
+            ((ScilabDocument) sep.getDocument()).disableUndoManager();
+            try {
+                sep.getDocument().insertString(0, header.header, null);
+            } catch (BadLocationException e) { }
+            sep.init(header.header.length());
+            ((ScilabDocument) sep.getDocument()).setContentModified(false);
+            ((ScilabDocument) sep.getDocument()).enableUndoManager();
+        }
 
         return sep;
     }
index fd4a8a7..85cf297 100644 (file)
@@ -43,6 +43,7 @@ public class SciNotesConfiguration implements XConfigurationListener {
         public boolean systemfont;
         public boolean font;
         public boolean keymap;
+        public boolean header;
 
         public Conf(Set<String> path) {
             if (path.contains("ALL")) {
@@ -53,6 +54,7 @@ public class SciNotesConfiguration implements XConfigurationListener {
                 systemfont = true;
                 font = true;
                 keymap = true;
+                header = true;
             } else {
                 preferences = path.contains(SciNotesOptions.PREFERENCESPATH);
                 display = path.contains(SciNotesOptions.DISPLAYPATH);
@@ -61,11 +63,12 @@ public class SciNotesConfiguration implements XConfigurationListener {
                 systemfont = path.contains(ScilabContext.SYSTEMFONTPATH);
                 font = path.contains(ScilabContext.FONTPATH);
                 keymap = path.contains(SciNotesOptions.KEYMAPPATH);
+                header = path.contains(SciNotesOptions.HEADERPATH);
             }
         }
 
         public boolean changed() {
-            return preferences || display || autosave || colors || systemfont || font || keymap;
+            return preferences || display || autosave || colors || systemfont || font || keymap || header;
         }
     }
 }
index 29ffb69..913b32e 100644 (file)
 package org.scilab.modules.scinotes;
 
 import java.awt.Color;
+import java.text.DateFormat;
+import java.util.Calendar;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
 
 import org.w3c.dom.Document;
 
@@ -28,11 +35,13 @@ public class SciNotesOptions {
     public static final String PREFERENCESPATH = "//scinotes/body/scinotes-preferences";
     public static final String DISPLAYPATH = "//scinotes/display/body/scinotes-display";
     public static final String AUTOSAVEPATH = "//scinotes/autosave/body/scinotes-autosave";
+    public static final String HEADERPATH = "//scinotes/header/body/scinotes-header";
     public static final String KEYMAPPATH = "//general/shortcuts/body/actions/action-folder[@xconf-uid=\"scinotes\"]/action";
 
     private static SciNotesOptions.Preferences prefs;
     private static SciNotesOptions.Display display;
     private static SciNotesOptions.Autosave autosave;
+    private static SciNotesOptions.Header header;
 
     private static Document doc;
 
@@ -178,6 +187,35 @@ public class SciNotesOptions {
         }
     }
 
+    /* scinotes-header */
+    @XConfAttribute
+    public static class Header {
+
+        public String header;
+        public boolean enable;
+
+        @XConfAttribute(tag = "scinotes-header", attributes = {"enable"})
+        private void set(boolean enable) {
+            this.enable = enable;
+            if (enable) {
+                XPathFactory xpathFactory = XPathFactory.newInstance();
+                XPath xp = xpathFactory.newXPath();
+                try {
+                    header = (String) xp.compile("string(" + HEADERPATH + ")").evaluate(doc, XPathConstants.STRING);
+                } catch (XPathExpressionException e) {
+                    System.err.println(e);
+                }
+
+                if (header != null) {
+                    Calendar cal = Calendar.getInstance();
+                    DateFormat dateFormat = DateFormat.getDateInstance();
+                    header = header.replaceAll("\\{\\$current-year\\}", Integer.toString(cal.get(Calendar.YEAR)));
+                    header = header.replaceAll("\\{\\$current-date\\}", dateFormat.format(cal.getTime()));
+                }
+            }
+        }
+    }
+
     public static void invalidate(SciNotesConfiguration.Conf conf) {
         if (conf.preferences) {
             prefs = null;
@@ -191,6 +229,10 @@ public class SciNotesOptions {
             autosave = null;
             doc = null;
         }
+        if (conf.header) {
+            header = null;
+            doc = null;
+        }
     }
 
     public static final SciNotesOptions.Preferences getSciNotesPreferences() {
@@ -225,4 +267,17 @@ public class SciNotesOptions {
 
         return autosave;
     }
+
+    public static final SciNotesOptions.Header getSciNotesHeader() {
+        if (header == null) {
+            if (doc == null) {
+                doc = XConfiguration.getXConfigurationDocument();
+            }
+
+            header = XConfiguration.get(SciNotesOptions.Header.class, doc, HEADERPATH)[0];
+        }
+
+
+        return header;
+    }
 }
index 2a6d867..fb55ad9 100644 (file)
@@ -491,11 +491,11 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
     /**
      * Init the pane
      */
-    public void init() {
+    public void init(final int pos) {
         SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 requestFocus();
-                setCaretPosition(0);
+                setCaretPosition(pos);
             }
         });
     }