Bugs 5321, 7760 fixed: It was not easy to configure the tabulation 36/2236/2
Calixte DENIZET [Thu, 7 Oct 2010 15:37:36 +0000 (17:37 +0200)]
Change-Id: I15f17989355c909bc1d3b0438004f824b4cf6f0a

scilab/CHANGES_5.3.X
scilab/modules/scinotes/etc/scinotesGUIConfiguration.xml
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/SciNotes.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/actions/ConfigTabulationsAction.java [new file with mode: 0644]
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/utils/ConfigSciNotesManager.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/utils/SciNotesMessages.java

index 59c69c6..aea61e0 100644 (file)
@@ -19,7 +19,7 @@ Documentation:
 * Documentation of trigonometry functions moved in a dedicated documentation
   section.
 
-* bug 7048 fixed - Documentation of color managements in graphics functions 
+* bug 7048 fixed - Documentation of color managements in graphics functions
                    moved in a dedicated section.
 
 * bug 7052 fixed - Documentation of floating point functions moved in
@@ -96,6 +96,8 @@ SciNotes:
 
 * Add a completion on the fields name of a mlist or a tlist.
 
+* bugs 5321, 7760 fixed - It was not easy to configure the tabulation.
+
 * bug 7762 fixed - A keyword was not fully helpable depending on the caret
                    position.
 
index 32f212e..18a201d 100644 (file)
@@ -84,6 +84,7 @@
       <menuitem action="SetColorsAction" label="Set Colors ..."/>
       <menuitem action="SetFontAction" label="Set Font"/>
       <menuitem action="ResetFontAction" label="Reset default font"/>
+      <menuitem action="ConfigTabulationsAction" label="Configure Tabulation"/>
       <separator/>
       <menuitem action="RestoreOpenedFilesAction" label="Restore opened files on start-up"/>
     </menu>
index 33b8102..3cac8f2 100644 (file)
@@ -55,6 +55,7 @@ import javax.swing.event.ChangeListener;
 import javax.swing.text.BadLocationException;
 import javax.swing.text.ChangedCharSetException;
 import javax.swing.text.EditorKit;
+import javax.swing.text.View;
 import javax.swing.undo.UndoManager;
 
 import org.apache.commons.logging.LogFactory;
@@ -82,6 +83,7 @@ import org.scilab.modules.gui.utils.SciFileFilter;
 import org.scilab.modules.gui.utils.Size;
 import org.scilab.modules.gui.window.ScilabWindow;
 import org.scilab.modules.gui.window.Window;
+import org.scilab.modules.scinotes.actions.ConfigTabulationsAction;
 import org.scilab.modules.scinotes.actions.ExitAction;
 import org.scilab.modules.scinotes.actions.FindAction;
 import org.scilab.modules.scinotes.actions.SetColorsAction;
@@ -468,6 +470,7 @@ public class SciNotes extends SwingScilabTab implements Tab {
         IncrementalSearchAction.close(this);
         SetColorsAction.closeSetColorsWindow();
         OpenSourceFileOnKeywordAction.closeOpenSourceWindow();
+        ConfigTabulationsAction.closeConfTabWindow();
 
         while (getTabPane().getTabCount() > 0) {
             closeTabAt(0, true);
@@ -1445,6 +1448,30 @@ public class SciNotes extends SwingScilabTab implements Tab {
      * Set the color of the highlighted line in this editor
      * @param c Color
      */
+    public static void setDefaultTabulation(TabManager.Tabulation tab) {
+        for (SciNotes ed : scinotesList) {
+            int n = ed.getTabPane().getTabCount();
+            for (int i = 0; i < n; i++) {
+                ScilabEditorPane sep = (ScilabEditorPane) ed.getTextPane(i);
+                sep.getTabManager().setTabulation(tab);
+                View view = ((ScilabDocument) sep.getDocument()).getView();
+                if (view != null) {
+                    if (view instanceof ScilabView) {
+                        ((ScilabView) view).setTabRepresentation(tab);
+                        ((ScilabView) view).reinitialize();
+                    } else {
+                        ((ScilabPlainView) view).setTabRepresentation(tab);
+                        ((ScilabPlainView) view).reinitialize();
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * Set the color of the highlighted line in this editor
+     * @param c Color
+     */
     public static void setHighlightedLineColor(Color c) {
         for (SciNotes ed : scinotesList) {
             int n = ed.getTabPane().getTabCount();
diff --git a/scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/actions/ConfigTabulationsAction.java b/scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/actions/ConfigTabulationsAction.java
new file mode 100644 (file)
index 0000000..c486fce
--- /dev/null
@@ -0,0 +1,285 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2010 - Calixte DENIZET
+ *
+ * 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
+ *
+ */
+
+package org.scilab.modules.scinotes.actions;
+
+import java.awt.Component;
+import java.awt.FlowLayout;
+import java.awt.GridLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.KeyEvent;
+import java.awt.event.WindowEvent;
+import java.awt.event.WindowListener;
+
+import javax.swing.AbstractAction;
+import javax.swing.BorderFactory;
+import javax.swing.BoxLayout;
+import javax.swing.ButtonGroup;
+import javax.swing.ImageIcon;
+import javax.swing.JButton;
+import javax.swing.JCheckBox;
+import javax.swing.JComponent;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JRadioButton;
+import javax.swing.JScrollBar;
+import javax.swing.JSpinner;
+import javax.swing.KeyStroke;
+import javax.swing.SpinnerNumberModel;
+import javax.swing.text.View;
+
+import org.scilab.modules.commons.ScilabConstants;
+import org.scilab.modules.gui.menuitem.MenuItem;
+import org.scilab.modules.scinotes.IndentManager;
+import org.scilab.modules.scinotes.SciNotes;
+import org.scilab.modules.scinotes.ScilabDocument;
+import org.scilab.modules.scinotes.ScilabEditorPane;
+import org.scilab.modules.scinotes.ScilabView;
+import org.scilab.modules.scinotes.ScilabPlainView;
+import org.scilab.modules.scinotes.TabManager;
+import org.scilab.modules.scinotes.utils.SciNotesMessages;
+import org.scilab.modules.scinotes.utils.ConfigSciNotesManager;
+
+/**
+ * ConfigTabulationsAction Class
+ * @author Calixte DENIZET
+ */
+public class ConfigTabulationsAction extends DefaultAction {
+
+    private static final long serialVersionUID = 1L;
+    private static final int GAP = 5;
+    private static final String ESCAPE = "escape";
+    private static final String SCI = ScilabConstants.SCI.getPath();
+
+    private static boolean windowAlreadyExist;
+    private static JFrame mainFrame;
+
+    private JButton okButton;
+    private JButton cancelButton;
+    private JButton applyButton;
+    private JRadioButton tabHRule;
+    private JRadioButton tabVRule;
+    private JRadioButton tabDbleChev;
+    private JCheckBox useWhitesCheckBox;
+    private JSpinner tabLengthSpinner;
+
+    /**
+     * Constructor
+     * @param name the name of the action
+     * @param editor SciNotes
+     */
+    public ConfigTabulationsAction(String name, SciNotes editor) {
+        super(name, editor);
+    }
+
+    /**
+     * doAction
+     */
+    public void doAction() {
+        ScilabEditorPane sep = (ScilabEditorPane) getEditor().getTextPane();
+        openConfTabWindow();
+    }
+
+    /**
+     * createMenu
+     * @param label label of the menu
+     * @param editor SciNotes
+     * @param key Keystroke
+     * @return MenuItem
+     */
+    public static MenuItem createMenu(String label, final SciNotes editor, KeyStroke key) {
+        return createMenu(label, null, new ConfigTabulationsAction(label, editor), key);
+    }
+
+    /**
+     * Close the window
+     */
+    public static void closeConfTabWindow() {
+        if (windowAlreadyExist) {
+            mainFrame.dispose();
+            mainFrame = null;
+            windowAlreadyExist = false;
+        }
+    }
+
+    /**
+     * Open a window
+     */
+    public void openConfTabWindow() {
+        if (windowAlreadyExist) {
+            mainFrame.setVisible(true);
+            return;
+        }
+
+        TabManager.Tabulation tabProps = ConfigSciNotesManager.getDefaultTabulation();
+
+        mainFrame = new JFrame();
+        mainFrame.setIconImage(new ImageIcon(SCI + "/modules/gui/images/icons/scilab.png").getImage());
+        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        mainFrame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE , 0), ESCAPE);
+        mainFrame.getRootPane().getActionMap().put(ESCAPE, new AbstractAction() {
+                public void actionPerformed(ActionEvent e) {
+                    closeConfTabWindow();
+                }
+            });
+
+        windowAlreadyExist = true;
+
+        cancelButton = new JButton(SciNotesMessages.CANCEL);
+        okButton = new JButton(SciNotesMessages.OK);
+        okButton.setPreferredSize(cancelButton.getPreferredSize());
+        applyButton = new JButton(SciNotesMessages.APPLYREINDENT);
+
+        JPanel panelButton = new JPanel();
+        panelButton.setLayout(new GridLayout(1, 4, GAP, GAP));
+        panelButton.setBorder(BorderFactory.createEmptyBorder(GAP, 0, 0, 0));
+        panelButton.add(new JLabel());
+        panelButton.add(okButton);
+        panelButton.add(applyButton);
+        panelButton.add(cancelButton);
+
+        /* Main content pane */
+        JPanel contentPanel = new JPanel(new GridLayout(1, 2, GAP, GAP));
+        contentPanel.setBorder(BorderFactory.createEmptyBorder(2 * GAP, 2 * GAP, 2 * GAP, 2 * GAP));
+
+        /* Style */
+        JPanel stylePanel = new JPanel(new GridLayout(2, 1, GAP, GAP));
+        stylePanel.setBorder(BorderFactory.createTitledBorder(SciNotesMessages.TABSTYLE));
+
+        useWhitesCheckBox = new JCheckBox(SciNotesMessages.TABUSEWHITES, tabProps.tab == ' ');
+        stylePanel.add(useWhitesCheckBox);
+
+        JPanel tabLengthPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
+        tabLengthSpinner = new JSpinner(new SpinnerNumberModel(tabProps.number, 0, 16, 1));
+
+        tabLengthPanel.add(new JLabel(SciNotesMessages.TABLENGTH));
+        tabLengthPanel.add(tabLengthSpinner);
+
+        stylePanel.add(tabLengthPanel);
+
+        contentPanel.add(stylePanel);
+
+        /* Representation */
+        final JPanel panelRepresentation = new JPanel(new GridLayout(3, 1, GAP, GAP));
+        panelRepresentation.setBorder(BorderFactory.createTitledBorder(SciNotesMessages.TABREPRESENTATION));
+        tabHRule = new JRadioButton(SciNotesMessages.TABHRULE, tabProps.type == ScilabView.TABHORIZONTAL);
+        panelRepresentation.add(tabHRule);
+        tabVRule = new JRadioButton(SciNotesMessages.TABVRULE, tabProps.type == ScilabView.TABVERTICAL);
+        panelRepresentation.add(tabVRule);
+        tabDbleChev = new JRadioButton(SciNotesMessages.TABDOUBLECHEVRONS, tabProps.type == ScilabView.TABDOUBLECHEVRONS);
+        panelRepresentation.add(tabDbleChev);
+        ButtonGroup repGroup = new ButtonGroup();
+        repGroup.add(tabHRule);
+        repGroup.add(tabVRule);
+        repGroup.add(tabDbleChev);
+
+        contentPanel.add(panelRepresentation);
+        useWhitesCheckBox.addActionListener(new ActionListener() {
+                public void actionPerformed(ActionEvent e) {
+                    boolean b = !useWhitesCheckBox.isSelected();
+                    for (Component c : panelRepresentation.getComponents()) {
+                        c.setEnabled(b);
+                    }
+                }
+            });
+
+        boolean b = !useWhitesCheckBox.isSelected();
+        for (Component c : panelRepresentation.getComponents()) {
+            c.setEnabled(b);
+        }
+
+        JPanel framePanel = new JPanel();
+        framePanel.setLayout(new BoxLayout(framePanel, BoxLayout.PAGE_AXIS));
+        framePanel.add(contentPanel);
+        framePanel.add(panelButton);
+
+        mainFrame.setContentPane(framePanel);
+
+        cancelButton.addActionListener(new ActionListener() {
+                public void actionPerformed(ActionEvent e) {
+                    ConfigTabulationsAction.windowAlreadyExist = false;
+                    mainFrame.dispose();
+                }
+            });
+
+        okButton.addActionListener(new ActionListener() {
+                public void actionPerformed(ActionEvent e) {
+                    ConfigTabulationsAction.windowAlreadyExist = false;
+                    refreshPane();
+                    mainFrame.dispose();
+                }
+            });
+
+        applyButton.addActionListener(new ActionListener() {
+                public void actionPerformed(ActionEvent e) {
+                    ScilabEditorPane sep = getEditor().getTextPane();
+                    IndentManager indent = sep.getIndentManager();
+                    ScilabDocument doc = (ScilabDocument) sep.getDocument();
+                    int currentLine = doc.getDefaultRootElement().getElementIndex(sep.getCaretPosition());
+                    JScrollBar sb = sep.getScrollPane().getHorizontalScrollBar();
+                    int val = -1;
+                    if (sb != null) {
+                        val = sb.getValue();
+                    }
+                    refreshPane();
+                    indent.indentDoc(0, doc.getLength() - 1);
+                    if (val != -1) {
+                        sb.setValue(val);
+                    }
+                }
+            });
+
+        mainFrame.addWindowListener(new WindowListener() {
+                public void windowClosed(WindowEvent arg0) { }
+                public void windowDeiconified(WindowEvent arg0) { }
+                public void windowActivated(WindowEvent arg0) { }
+
+                public void windowClosing(WindowEvent arg0) {
+                    closeConfTabWindow();
+                }
+
+                public void windowDeactivated(WindowEvent arg0) { }
+                public void windowIconified(WindowEvent arg0) { };
+                public void windowOpened(WindowEvent arg0) { }
+            });
+
+        mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
+        mainFrame.setTitle(SciNotesMessages.CONFIGTAB);
+        mainFrame.pack();
+        mainFrame.setLocationRelativeTo(getEditor());
+        mainFrame.setVisible(true);
+    }
+
+    /**
+     * Refresh the pane
+     */
+    private void refreshPane() {
+        char tab = ' ';
+        if (!useWhitesCheckBox.isSelected()) {
+            tab = '\t';
+        }
+        int len = ((Integer) tabLengthSpinner.getValue()).intValue();
+        int type = ScilabView.TABNOTHING;
+        if (tab == '\t' && tabVRule.isSelected()) {
+            type = ScilabView.TABVERTICAL;
+        } else if (tabHRule.isSelected()) {
+            type = ScilabView.TABHORIZONTAL;
+        } else if (tabDbleChev.isSelected()) {
+            type = ScilabView.TABDOUBLECHEVRONS;
+        }
+        TabManager.Tabulation cfg = new TabManager.Tabulation(tab, len, type, ' ');
+        ConfigSciNotesManager.saveDefaultTabulation(cfg);
+        SciNotes.setDefaultTabulation(cfg);
+    }
+}
index 876b440..32e0487 100644 (file)
@@ -718,6 +718,44 @@ public final class ConfigSciNotesManager {
     }
 
     /**
+     * Retrieve from scinotesConfiguration.xml the infos about a tabulation
+     * @return a Tabulation containing infos
+     */
+    public static void saveDefaultTabulation(TabManager.Tabulation cfg) {
+        /* <style name="Tabulation" rep="vertical" value="4" white="false"> */
+        readDocument();
+
+        Element root = document.getDocumentElement();
+        NodeList styles = root.getElementsByTagName(STYLE);
+
+        for (int i = 0; i < styles.getLength(); ++i) {
+            Element style = (Element) styles.item(i);
+            if ("Tabulation".equals(style.getAttribute(NAME))) {
+               String type = "none";
+               switch (cfg.type) {
+               case ScilabView.TABVERTICAL:
+                   type = "vertical";
+                   break;
+               case ScilabView.TABHORIZONTAL:
+                   type = "horizontal";
+                   break;
+               case ScilabView.TABDOUBLECHEVRONS:
+                   type = "doublechevrons";
+                   break;
+               default:
+                   break;
+               }
+
+               style.setAttribute("rep", type);
+               style.setAttribute(VALUE, Integer.toString(cfg.number));
+               style.setAttribute("white", Boolean.toString(cfg.tab == ' '));
+               writeDocument();
+               return;
+            }
+        }
+    }
+
+    /**
      * Retrieve form scinotesConfiguration.xml the infos the matchers
      * @param kind should be "KeywordsHighlighter" or "OpenCloseHighlighter"
      * @return an Object containing infos
index 16e56d4..2f1967a 100644 (file)
@@ -172,6 +172,7 @@ public class SciNotesMessages {
     public static final String FILE_ALREADY_EXIST = Messages.gettext("File already exists");
     public static final String UNTITLED = Messages.gettext("Untitled ");
     public static final String OK = Messages.gettext("Ok");
+    public static final String APPLYREINDENT = Messages.gettext("Apply & Reindent");
     public static final String CANCEL = Messages.gettext("Cancel");
     public static final String DEFAULT = Messages.gettext("Default");
     public static final String NEXT_TAB = Messages.gettext("Next tab");
@@ -270,4 +271,16 @@ public class SciNotesMessages {
      */
     public static final String RESTORE_FILES_QUERY = Messages.gettext("Restore files from your last SciNotes session ?");
     public static final String RESTORE_FILES_TITLE = Messages.gettext("Restore the last sessions");
+
+    /**
+     * Tabulations configuration
+     */
+    public static final String CONFIGTAB = Messages.gettext("Configure Tabulation");
+    public static final String TABSTYLE = Messages.gettext("Tab style");
+    public static final String TABUSEWHITES = Messages.gettext("Use spaces");
+    public static final String TABLENGTH = Messages.gettext("Length");
+    public static final String TABREPRESENTATION = Messages.gettext("Tab representation");
+    public static final String TABHRULE = Messages.gettext("Horizontal rule");
+    public static final String TABVRULE = Messages.gettext("Vertical rule");
+    public static final String TABDOUBLECHEVRONS = Messages.gettext("Double chevrons");
 }