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 9adcdc8..4133c56 100644 (file)
@@ -14,6 +14,7 @@ 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;
@@ -24,6 +25,7 @@ import org.scilab.modules.scinotes.SciNotes;
 import org.scilab.modules.scinotes.ScilabLexerConstants;
 import org.scilab.modules.scinotes.ScilabEditorPane;
 import org.scilab.modules.scinotes.KeywordEvent;
+import org.scilab.modules.scinotes.utils.SciNotesMessages;
 import org.scilab.modules.action_binding.InterpreterManagement;
 
 /**
@@ -32,6 +34,8 @@ import org.scilab.modules.action_binding.InterpreterManagement;
  */
 public class HelpOnKeywordAction extends DefaultAction {
 
+    protected boolean isPopup;
+
     /**
      * Constructor
      * @param name the name of the action
@@ -45,28 +49,68 @@ public class HelpOnKeywordAction extends DefaultAction {
      * 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
+     * @param key KeyStroke
      * @return MenuItem
      */
     public static MenuItem createMenu(String label, final SciNotes editor, KeyStroke key) {
-        final MenuItem menuitem = createMenu(label, null, new HelpOnKeywordAction(label, editor), 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
+     */
+    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) {
-                    KeywordEvent kwe = ((ScilabEditorPane) editor.getTextPane()).getKeywordEvent();
-                    menuitem.setEnabled(ScilabLexerConstants.isHelpable(kwe.getType()));
+                    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);
+                        }
+                    }
                 }
             });