Prefs: Warn before clearing the console with clc() 61/7661/3
Calixte DENIZET [Thu, 21 Jun 2012 12:31:01 +0000 (14:31 +0200)]
Change-Id: Ic3f0f6712cdef937a919137c1150bbc7c818ab65

scilab/modules/console/etc/XConfiguration-general.xml
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/console/SwingScilabConsole.java

index 7f2a408..cd1f0a5 100644 (file)
@@ -13,7 +13,7 @@
       <tools>
         <tool state="checked" id="console-exit" name="Console" description="Warn before exiting Scilab"/>
         <tool state="checked" id="delete-history-item" name="Command History" description="Warn before deleting Command History items"/>
-        <tool state="unchecked" id="console-clear" name="Console" description="Warn before clearing the Console"/>
+        <tool state="checked" id="console-clear" name="Console" description="Warn before clearing the Console"/>
       </tools>
     </body>
   </confirmation-dialogs>
index d764fd5..92525f1 100644 (file)
@@ -18,6 +18,7 @@ import java.awt.datatransfer.Clipboard;
 import java.awt.datatransfer.DataFlavor;
 import java.awt.datatransfer.StringSelection;
 import java.awt.datatransfer.UnsupportedFlavorException;
+import java.awt.event.ActionEvent;
 import java.awt.event.FocusEvent;
 import java.awt.event.FocusListener;
 import java.beans.PropertyChangeEvent;
@@ -25,6 +26,9 @@ import java.beans.PropertyChangeListener;
 import java.io.IOException;
 import java.util.StringTokenizer;
 
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.JCheckBox;
 import javax.swing.JEditorPane;
 import javax.swing.JPanel;
 import javax.swing.JTextPane;
@@ -43,11 +47,18 @@ import org.scilab.modules.gui.bridge.contextmenu.SwingScilabContextMenu;
 import org.scilab.modules.gui.bridge.menuitem.SwingScilabMenuItem;
 import org.scilab.modules.gui.console.SimpleConsole;
 import org.scilab.modules.gui.events.callback.ScilabCallBack;
+import org.scilab.modules.gui.messagebox.ScilabModalDialog;
+import org.scilab.modules.gui.messagebox.ScilabModalDialog.AnswerOption;
+import org.scilab.modules.gui.messagebox.ScilabModalDialog.ButtonType;
+import org.scilab.modules.gui.messagebox.ScilabModalDialog.IconType;
 import org.scilab.modules.gui.utils.ConfigManager;
 import org.scilab.modules.gui.utils.Position;
 import org.scilab.modules.gui.utils.Size;
 import org.scilab.modules.history_manager.HistoryManagement;
 import org.scilab.modules.localization.Messages;
+import org.scilab.modules.commons.xml.XConfiguration;
+import static org.scilab.modules.commons.xml.XConfiguration.XConfAttribute;
+import org.scilab.modules.localization.Messages;
 
 import com.artenum.rosetta.interfaces.ui.InputCommandView;
 import com.artenum.rosetta.util.StringConstants;
@@ -60,6 +71,11 @@ public class SwingScilabConsole extends SciConsole implements SimpleConsole {
 
     private static final long serialVersionUID = 1L;
 
+    private static final String CONFIRMATION_PATH = "//general/confirmation-dialogs/body/tools/tool[@id='console-clear']";
+    private static final String CLEAR = Messages.gettext("Clear Console");
+    private static final String CLEAR_CONFIRM = Messages.gettext("Are you sure you want to clear the console ?");
+    private static final String DONT_SHOW = Messages.gettext("Do not show this message again");
+
     /**
      * Constructor
      */
@@ -71,51 +87,51 @@ public class SwingScilabConsole extends SciConsole implements SimpleConsole {
         SwingScilabMenuItem cutMenu = new SwingScilabMenuItem();
         cutMenu.setText(Messages.gettext("Cut"));
         cutMenu.setCallback(ScilabCallBack.createCallback(
-                "org.scilab.modules.gui.bridge.CallScilabBridge.cutConsoleSelection",
-                CallBack.JAVA));
+                                "org.scilab.modules.gui.bridge.CallScilabBridge.cutConsoleSelection",
+                                CallBack.JAVA));
         cutMenu.setMnemonic('U');
 
         SwingScilabMenuItem copyMenu = new SwingScilabMenuItem();
         copyMenu.setText(Messages.gettext("Copy"));
         copyMenu.setCallback(ScilabCallBack.createCallback(
-                "org.scilab.modules.gui.bridge.CallScilabBridge.copyConsoleSelection",
-                CallBack.JAVA));
+                                 "org.scilab.modules.gui.bridge.CallScilabBridge.copyConsoleSelection",
+                                 CallBack.JAVA));
         copyMenu.setMnemonic('C');
 
         SwingScilabMenuItem pasteMenu = new SwingScilabMenuItem();
         pasteMenu.setText(Messages.gettext("Paste"));
         pasteMenu.setCallback(ScilabCallBack.createCallback(
-                "org.scilab.modules.gui.bridge.CallScilabBridge.pasteClipboardIntoConsole",
-                CallBack.JAVA));
+                                  "org.scilab.modules.gui.bridge.CallScilabBridge.pasteClipboardIntoConsole",
+                                  CallBack.JAVA));
         pasteMenu.setMnemonic('P');
 
         SwingScilabMenuItem clearHistoryMenu = new SwingScilabMenuItem();
         clearHistoryMenu.setText(Messages.gettext("Clear History"));
         clearHistoryMenu.setCallback(ScilabCallBack.createCallback(
-                "org.scilab.modules.gui.bridge.CallScilabBridge.clearHistory",
-                CallBack.JAVA));
+                                         "org.scilab.modules.gui.bridge.CallScilabBridge.clearHistory",
+                                         CallBack.JAVA));
         clearHistoryMenu.setMnemonic('H');
 
         SwingScilabMenuItem clearMenu = new SwingScilabMenuItem();
         clearMenu.setText(Messages.gettext("Clear Console"));
         clearMenu.setCallback(ScilabCallBack.createCallback(
-                "org.scilab.modules.gui.bridge.CallScilabBridge.clear",
-                CallBack.JAVA));
+                                  "org.scilab.modules.gui.bridge.CallScilabBridge.clear",
+                                  CallBack.JAVA));
         clearMenu.setMnemonic('O');
 
         SwingScilabMenuItem selectMenu = new SwingScilabMenuItem();
         selectMenu.setText(Messages.gettext("Select All"));
         selectMenu.setCallback(ScilabCallBack.createCallback(
-                "org.scilab.modules.gui.bridge.CallScilabBridge.selectAllConsoleContents",
-                CallBack.JAVA));
+                                   "org.scilab.modules.gui.bridge.CallScilabBridge.selectAllConsoleContents",
+                                   CallBack.JAVA));
         selectMenu.setMnemonic('S');
 
 
         final SwingScilabMenuItem helpMenu = new SwingScilabMenuItem();
         helpMenu.setText(Messages.gettext("Help on a selected keyword"));
         helpMenu.setCallback(ScilabCallBack.createCallback(
-                "org.scilab.modules.gui.bridge.CallScilabBridge.helpOnTheKeyword",
-                CallBack.JAVA));
+                                 "org.scilab.modules.gui.bridge.CallScilabBridge.helpOnTheKeyword",
+                                 CallBack.JAVA));
         helpMenu.setMnemonic('M');
         PropertyChangeListener listener = new PropertyChangeListener() {
             public void propertyChange(PropertyChangeEvent arg0) {
@@ -136,7 +152,7 @@ public class SwingScilabConsole extends SciConsole implements SimpleConsole {
         final SwingScilabMenuItem evalWithEchoMenu = new SwingScilabMenuItem();
         evalWithEchoMenu.setText(Messages.gettext("Evaluate selection with echo"));
         evalWithEchoMenu.setCallback(ScilabCallBack.createCallback("org.scilab.modules.gui.bridge.CallScilabBridge.evaluateSelectionWithEcho",
-                CallBack.JAVA));
+                                     CallBack.JAVA));
         evalWithEchoMenu.setMnemonic('E');
         listener = new PropertyChangeListener() {
             public void propertyChange(PropertyChangeEvent arg0) {
@@ -149,7 +165,7 @@ public class SwingScilabConsole extends SciConsole implements SimpleConsole {
         final SwingScilabMenuItem evalWithNoEchoMenu = new SwingScilabMenuItem();
         evalWithNoEchoMenu.setText(Messages.gettext("Evaluate selection with no echo"));
         evalWithNoEchoMenu.setCallback(ScilabCallBack.createCallback("org.scilab.modules.gui.bridge.CallScilabBridge.evaluateSelectionWithNoEcho",
-                CallBack.JAVA));
+                                       CallBack.JAVA));
         evalWithNoEchoMenu.setMnemonic('N');
         listener = new PropertyChangeListener() {
             public void propertyChange(PropertyChangeEvent arg0) {
@@ -350,6 +366,27 @@ public class SwingScilabConsole extends SciConsole implements SimpleConsole {
      * Clears the Console
      */
     public void clear() {
+        CheckClearConfirmation ccc = XConfiguration.get(CheckClearConfirmation.class, XConfiguration.getXConfigurationDocument(), CONFIRMATION_PATH)[0];
+        if (ccc.checked) {
+            final boolean[] checked = new boolean[1];
+            final Action action = new AbstractAction() {
+                public void actionPerformed(ActionEvent e) {
+                    checked[0] = ((JCheckBox) e.getSource()).isSelected();
+                }
+            };
+
+            if (ScilabModalDialog.show(this, new String[] { CLEAR_CONFIRM }, CLEAR, IconType.WARNING_ICON, ButtonType.YES_NO, DONT_SHOW, action) == AnswerOption.NO_OPTION) {
+                if (checked[0]) {
+                    XConfiguration.set(XConfiguration.getXConfigurationDocument(), CONFIRMATION_PATH + "/@state", "unchecked");
+                }
+                return;
+            }
+
+            if (checked[0]) {
+                XConfiguration.set(XConfiguration.getXConfigurationDocument(), CONFIRMATION_PATH + "/@state", "unchecked");
+            }
+        }
+
         super.clear();
     }
 
@@ -415,7 +452,7 @@ public class SwingScilabConsole extends SciConsole implements SimpleConsole {
         // Insert clipboard contents
         try {
             doc.insertString(((JTextPane) this.getConfiguration().getInputCommandView()).getCaretPosition(),
-                    clipboardContents, doc.getStyle(StyleContext.DEFAULT_STYLE));
+                             clipboardContents, doc.getStyle(StyleContext.DEFAULT_STYLE));
         } catch (BadLocationException e) {
             e.printStackTrace();
         }
@@ -459,15 +496,15 @@ public class SwingScilabConsole extends SciConsole implements SimpleConsole {
         /* Double the quote/double quote in order to avoid
          * and error with the call of help()
          */
-        keyword=keyword.replaceAll("'", "''");
-        keyword=keyword.replaceAll("\"", "\"\"");
+        keyword = keyword.replaceAll("'", "''");
+        keyword = keyword.replaceAll("\"", "\"\"");
 
         /* @TODO: Check if it is possible to call directly
          * from the Java engine the help
          * Last time I check, we needed some information
          * provided by the Scilab native engine
          */
-        InterpreterManagement.requestScilabExec("help('"+keyword+"')");
+        InterpreterManagement.requestScilabExec("help('" + keyword + "')");
     }
 
     /**
@@ -529,4 +566,17 @@ public class SwingScilabConsole extends SciConsole implements SimpleConsole {
     public void setMaxOutputSize(int nbLines) {
         ((SciOutputView) this.getConfiguration().getOutputView()).setMaxSize(nbLines);
     }
+
+    @XConfAttribute
+    private static class CheckClearConfirmation {
+
+        public boolean checked;
+
+        private CheckClearConfirmation() { }
+
+        @XConfAttribute(attributes = {"state"})
+        private void set(String checked) {
+            this.checked = checked.equals("checked");
+        }
+    }
 }