Bug 8135 fixed: An exception was thrown when restoring files on MacOS X
[scilab.git] / scilab / modules / scinotes / src / java / org / scilab / modules / scinotes / actions / HelpOnKeywordAction.java
index 4a8558e..4133c56 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 javax.swing.text.BadLocationException;
 
@@ -28,35 +33,87 @@ import org.scilab.modules.action_binding.InterpreterManagement;
  * @author Calixte DENIZET
  */
 public class HelpOnKeywordAction extends DefaultAction {
-    
+
+    protected boolean isPopup;
+
     /**
      * Constructor
+     * @param name the name of the action
      * @param editor SciNotes
      */
-    private HelpOnKeywordAction(SciNotes editor) {
-       super(SciNotesMessages.HELP_ON_KEYWORD, editor);
+    public HelpOnKeywordAction(String name, SciNotes editor) {
+        super(name, editor);
     }
-    
+
     /**
      * doAction
      */
     public void doAction() {
-       KeywordEvent kwe = ((ScilabEditorPane) getEditor().getTextPane()).getKeywordEvent();
-       if (ScilabLexerConstants.isHelpable(kwe.getType())) {
-           try {
-               String kw = getEditor().getTextPane().getDocument().getText(kwe.getStart(), kwe.getLength());
-           InterpreterManagement.requestScilabExec("help('" + kw + "')");    
-           } catch (BadLocationException e) { }
-       }
+        String selection = "";
+        int start = getEditor().getTextPane().getSelectionStart();
+        int end = getEditor().getTextPane().getSelectionEnd();
+        try {
+            if (start == end) {
+                KeywordEvent kwe = ((ScilabEditorPane) getEditor().getTextPane()).getKeywordEvent(!isPopup, true);
+                if (ScilabLexerConstants.isHelpable(kwe.getType())) {
+                    selection = getEditor().getTextPane().getDocument().getText(kwe.getStart(), kwe.getLength());
+                }
+            } else {
+                selection = getEditor().getTextPane().getDocument().getText(start, end - start);
+            }
+        } catch (BadLocationException e) { }
+
+        InterpreterManagement.requestScilabExec("help('" + selection + "')");
+    }
+
+    /**
+     * 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();
+        return createMenu(label1, label2, editor, key, new HelpOnKeywordAction(label1 + SciNotesMessages.DOTS, editor));
     }
-    
+
     /**
      * createMenu
+     * @param label label of the menu
      * @param editor SciNotes
      * @param key Keystroke
+     * @param hoka the HelpOnKeyword action
      * @return MenuItem
      */
-    public static MenuItem createMenu(SciNotes editor, KeyStroke key) {
-       return createMenu(SciNotesMessages.HELP_ON_KEYWORD, null, new HelpOnKeywordAction(editor), key);
+    protected static MenuItem createMenu(final String label1, final String label2, final SciNotes editor, KeyStroke key, final HelpOnKeywordAction hoka) {
+        final MenuItem menuitem = createMenu(label1, null, hoka, key);
+        ((JMenuItem) menuitem.getAsSimpleMenuItem()).addPropertyChangeListener(new PropertyChangeListener() {
+                public void propertyChange(PropertyChangeEvent e) {
+                    if (editor.getTextPane() != null) {
+                        String select = editor.getTextPane().getSelectedText();
+                        if (select == null) {
+                            KeywordEvent kwe = ((ScilabEditorPane) editor.getTextPane()).getKeywordEvent(!hoka.isPopup, true);
+                            if (ScilabLexerConstants.isHelpable(kwe.getType())) {
+                                try {
+                                    String kw = editor.getTextPane().getDocument().getText(kwe.getStart(), kwe.getLength());
+                                    menuitem.setText(label1 + SciNotesMessages.QUOTE + kw + SciNotesMessages.QUOTE);
+                                    menuitem.setEnabled(true);
+                                } catch (BadLocationException ex) { }
+                            } else {
+                                menuitem.setText(label1 + SciNotesMessages.DOTS);
+                                menuitem.setEnabled(false);
+                            }
+                        } else {
+                            menuitem.setText(label2);
+                            menuitem.setEnabled(true);
+                        }
+                    }
+                }
+            });
+
+        return menuitem;
     }
 }