Refactorization of the management of the actions in menus, button, shortcuts
[scilab.git] / scilab / modules / scinotes / src / java / org / scilab / modules / scinotes / actions / EvaluateSelectionAction.java
index 7dc0b5b..9c44b1a 100644 (file)
 
 package org.scilab.modules.scinotes.actions;
 
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.StringTokenizer;
+import javax.swing.JMenuItem;
 import javax.swing.KeyStroke;
 
 import org.scilab.modules.gui.console.ScilabConsole;
 import org.scilab.modules.gui.menuitem.MenuItem;
 import org.scilab.modules.scinotes.SciNotes;
-import org.scilab.modules.scinotes.utils.SciNotesMessages;
 import org.scilab.modules.scinotes.ScilabEditorPane;
 
 /**
@@ -26,39 +29,59 @@ import org.scilab.modules.scinotes.ScilabEditorPane;
  *
  */
 public final class EvaluateSelectionAction extends DefaultAction {
-       
-       /**
-        * serialVersionUID
-        */
-       private static final long serialVersionUID = 320938663765236236L;
 
-       /**
-        * Constructor
-        * @param editor SciNotes
-        */
-       private EvaluateSelectionAction(SciNotes editor) {
-               super(SciNotesMessages.EVALUATE_SELECTION, editor);
-       }
+    /**
+     * serialVersionUID
+     */
+    private static final long serialVersionUID = 320938663765236236L;
 
-       /**
-        * doAction
-        */
-       public void doAction() {
-               /* Will do the job as if it was copy / paste in scilab Console */
-               ScilabEditorPane sep = (ScilabEditorPane) getEditor().getTextPane();
-               String selection = sep.getCodeToExecute();
-               if (selection.compareTo("") != 0) {
-                       ScilabConsole.getConsole().getAsSimpleConsole().sendCommandsToScilab(selection, true, false);   
-               }
-       }
+    /**
+     * Constructor
+     * @param name the name of the action
+     * @param editor SciNotes
+     */
+    public EvaluateSelectionAction(String name, SciNotes editor) {
+        super(name, editor);
+    }
 
-        /**
-        * createMenu
-        * @param editor scinotes
-        * @param key KeyStroke
-        * @return MenuItem
-        */
-        public static MenuItem createMenu(SciNotes editor, KeyStroke key) {
-               return createMenu(SciNotesMessages.EVALUATE_SELECTION, null, new EvaluateSelectionAction(editor), key);
-       }
+    /**
+     * doAction
+     */
+    public void doAction() {
+        /* Will do the job as if it was copy / paste in scilab Console */
+        ScilabEditorPane sep = (ScilabEditorPane) getEditor().getTextPane();
+        String selection = sep.getCodeToExecute();
+        if (selection.compareTo("") != 0) {
+            ScilabConsole.getConsole().getAsSimpleConsole().sendCommandsToScilab(selection, true, false);
+        }
+    }
+
+    /**
+     * 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) {
+        StringTokenizer token = new StringTokenizer(label, ";");
+        final String label1 = token.nextToken();
+        final String label2 = token.nextToken();
+        final MenuItem menuitem = createMenu(label1, null, new EvaluateSelectionAction(label1, editor), key);
+        if (!ScilabConsole.isExistingConsole()) { // Only available in STD mode
+            ((JMenuItem) menuitem.getAsSimpleMenuItem()).setEnabled(false);
+        }
+        ((JMenuItem) menuitem.getAsSimpleMenuItem()).addPropertyChangeListener(new PropertyChangeListener() {
+                public void propertyChange(PropertyChangeEvent e) {
+                    String select = editor.getTextPane().getSelectedText();
+                    if (select == null) {
+                        menuitem.setText(label1);
+                    } else {
+                        menuitem.setText(label2);
+                    }
+                }
+            });
+
+        return menuitem;
+    }
 }