Xpad.java: Add a method to the xpad object to open a new window with a String as...
Sylvestre Ledru [Wed, 11 Nov 2009 00:41:43 +0000 (01:41 +0100)]
sci_helpbrowser.c: We load the text editor when calling javahelp because we have no way to know
to load it when using Javahelp because it can call text editor directly

SwingScilabHelpBrowserViewer.java: Provides a popup menu on the help (\o/)

checkstyle update (minor)

scilab/checkstyle/scilab_checkstyle_convention.xml
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/helpbrowser/SwingScilabHelpBrowserViewer.java [new file with mode: 0644]

index eecd327..9e82062 100644 (file)
@@ -1,21 +1,22 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!--
-       This configuration file was written by the eclipse-cs plugin configuration editor
+    This configuration file was written by the eclipse-cs plugin configuration editor
 -->
 <!--
-Checkstyle-Configuration: scilab checkstyle
-Description:
-
+    Checkstyle-Configuration: Scilab checkstyle
+    Description: none
 -->
 <!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
 <module name="Checker">
     <property name="severity" value="warning"/>
     <module name="TreeWalker">
         <module name="JavadocMethod">
-            <property name="severity" value="error"/>
+            <metadata name="com.atlassw.tools.eclipse.checkstyle.lastEnabledSeverity" value="error"/>
+            <property name="severity" value="ignore"/>
         </module>
         <module name="JavadocType">
-            <property name="severity" value="error"/>
+            <metadata name="com.atlassw.tools.eclipse.checkstyle.lastEnabledSeverity" value="error"/>
+            <property name="severity" value="ignore"/>
         </module>
         <module name="JavadocVariable">
             <property name="severity" value="info"/>
@@ -126,9 +127,4 @@ Description:
     <module name="PackageHtml"/>
     <module name="NewlineAtEndOfFile"/>
     <module name="Translation"/>
-<!--    
-Commented because we do need it (copy and paste for create bridges)
-<module name="StrictDuplicateCode">
-        <property name="severity" value="info"/>
-    </module>-->
 </module>
diff --git a/scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/helpbrowser/SwingScilabHelpBrowserViewer.java b/scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/helpbrowser/SwingScilabHelpBrowserViewer.java
new file mode 100644 (file)
index 0000000..f4ca4f5
--- /dev/null
@@ -0,0 +1,228 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2009 - DIGITEO - Sylvestre Ledru
+ *
+ * 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.gui.bridge.helpbrowser;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Locale;
+
+import javax.help.HelpUtilities;
+import javax.help.JHelpContentViewer;
+import javax.help.plaf.basic.BasicContentViewerUI;
+import javax.swing.JComponent;
+import javax.swing.JMenuItem;
+import javax.swing.JPopupMenu;
+import javax.swing.text.DefaultEditorKit;
+
+import org.scilab.modules.gui.console.ScilabConsole;
+import org.scilab.modules.localization.Messages;
+
+
+/**
+ * This class inherits from BasicContentViewerUI from Javahelp.
+ * Through this class, we are adding some features on the javahelp browser
+ * We are adding a popup menu on the right click of the mouse
+ * In this menu, we are providing:
+ *     - Execute in Scilab
+ *  - Edit in the text editor
+ *  - Copy
+ *  - Select all
+ */
+public class SwingScilabHelpBrowserViewer extends BasicContentViewerUI {
+
+       /**
+        * 
+        */
+       private static final long serialVersionUID = -2593697956426596790L;
+       /* This field is a copy of BasicContentViewerUI which is privated.
+        * Therefor, I am changing the permission here to make it available
+        * to the methods of this object
+        */
+       private javax.swing.JEditorPane accessibleHtml;
+       
+       public SwingScilabHelpBrowserViewer(JHelpContentViewer x) {
+        super(x);
+    }
+
+      public static javax.swing.plaf.ComponentUI createUI(JComponent x) {
+        return new SwingScilabHelpBrowserViewer((JHelpContentViewer) x);
+    }
+
+    /**
+     * Create the UI interface
+     * @see javax.help.plaf.basic.BasicContentViewerUI#installUI(javax.swing.JComponent)
+     * @param c The component
+     */
+    public void installUI(JComponent c) {
+               super.installUI(c);
+               this.retrievePrivateFieldFromBasicContentViewerUI();
+               this.createPopupMenu(c);
+       }
+
+    
+       /**
+        * Retrieve the field "html" from BasicContentViewerUI and change
+        * permission (it is private by default) 
+        */
+    private void retrievePrivateFieldFromBasicContentViewerUI() {
+               Field privateField = null;
+               try {
+                       privateField = BasicContentViewerUI.class.getDeclaredField("html");
+                       privateField.setAccessible(true);
+               } catch (SecurityException e) {
+                       System.err.println("Security error: Could not change the accessibility on the html component of the help browser.");
+                       System.err.println("Please submit a bug report: http://bugzilla.scilab.org");
+                       e.printStackTrace();
+               } catch (NoSuchFieldException e) {
+                       System.err.println("Could not find the field of the html component of the help browser.");
+                       System.err.println("Please submit a bug report: http://bugzilla.scilab.org");
+                       e.printStackTrace();
+               } 
+               
+               try {
+                       this.accessibleHtml = (javax.swing.JEditorPane) privateField.get(this);
+               } catch (IllegalArgumentException e) {
+                       System.err.println("Illegal argument in the retrieval of the html component of Javahelp");
+                       e.printStackTrace();
+               } catch (IllegalAccessException e) {
+                       System.err.println("Illegal access in the retrieval of the html component of Javahelp");
+                       e.printStackTrace();
+               }
+    }    
+    
+    /**
+     * Create the popup menu on the help
+     * @param c The graphic component
+     */
+       private void createPopupMenu(JComponent c) {
+               Locale locale = HelpUtilities.getLocale(c);
+               final JPopupMenu popup = new JPopupMenu();
+               
+               /* Execute into Scilab */
+               ActionListener actionListenerExecuteIntoScilab = new ActionListener() {
+                               public void actionPerformed(ActionEvent actionEvent) {
+                                       String selection = accessibleHtml.getSelectedText();
+                                       if (selection == null) {
+                                               /* @TODO: check if we can instead write in the info bar of the help */
+                                               ScilabConsole.getConsole().getInfoBar().setText(Messages.gettext("No text selected"));
+                                       } else {
+                                               ScilabConsole.getConsole().getAsSimpleConsole().sendCommandsToScilab(selection, true, false);
+                                       }
+                               }
+                       };
+
+               JMenuItem menuItem = new JMenuItem(Messages.gettext("Execute into Scilab"));
+               menuItem.addActionListener(actionListenerExecuteIntoScilab);
+               popup.add(menuItem);
+               
+               /* Edit in the Scilab Text Editor */
+               ActionListener actionListenerLoadIntoTextEditor = new ActionListener() {
+                               public void actionPerformed(ActionEvent actionEvent) {
+                                       String selection = accessibleHtml.getSelectedText();
+                                       if (selection == null) {
+                                               /* @TODO: check if we can instead write in the info bar of the help */
+                                               ScilabConsole.getConsole().getInfoBar().setText(Messages.gettext("No text selected"));
+                                       } else {
+                                   try {
+                                       /* Dynamic load of the Xpad class. 
+                                        * This is done to avoid a cyclic dependency on gui <=> xpad
+                                        */
+                                                       Class xpadClass = Class.forName("org.scilab.modules.xpad.Xpad");
+                                                   Class arguments[] = new Class[] { String.class };
+                                                       Method method = xpadClass.getMethod("xpadWithText",arguments);
+                                                   method.invoke(xpadClass, new Object[]{selection});
+                                                       
+                                               } catch (ClassNotFoundException e) {
+                                                       System.err.println("Could not find Xpad class");
+                                                       e.printStackTrace();
+                                               } catch (SecurityException e) {
+                                                       System.err.println("Security error: Could not access to Xpad class");
+                                                       e.printStackTrace();
+                                               } catch (NoSuchMethodException e) {
+                                                       System.err.println("Could not access to xpathWithText method from object Xpad");
+                                                       // TODO Auto-generated catch block
+                                                       e.printStackTrace();
+                                               } catch (IllegalArgumentException e) {
+                                                       System.err.println("Wrong argument used with xpathWithText method from object Xpad");
+                                                       e.printStackTrace();
+                                               } catch (IllegalAccessException e) {
+                                                       System.err.println("Illegal access with xpathWithText method from object Xpad");
+                                                       e.printStackTrace();
+                                               } catch (InvocationTargetException e) {
+                                                       System.err.println("Error of invocation with xpathWithText method from object Xpad");
+                                                       e.printStackTrace();
+                                               }
+                                       }
+                               }
+                       };
+
+               menuItem = new JMenuItem(Messages.gettext("Edit in the Scilab Text Editor"));
+               menuItem.addActionListener(actionListenerLoadIntoTextEditor);
+               popup.add(menuItem);
+               popup.addSeparator();
+
+               /*
+                * History disable (hard to access to the main object 
+                
+               ActionListener actionListenerBack = new ActionListener() {
+                               public void actionPerformed(ActionEvent actionEvent) {
+                                       DefaultHelpHistoryModel history = new DefaultHelpHistoryModel(null);
+                                       history.goBack();
+//               help.getHistoryModel().goBack();
+//               ScilabHelpBrowser.getHelpBrowser().getHistoryModel().goBack();
+//               SwingScilabHelpBrowser()
+//               SwingScilabHelpBrowser.getHistoryModel().goBack();
+                                       System.out.println("Selected: " + actionEvent.getActionCommand());
+                               }
+                       };
+               
+
+               menuItem = new JMenuItem(HelpUtilities.getString(locale, "tooltip.BackAction"));
+               menuItem.addActionListener(actionListenerBack);
+
+//new BackAction(control));
+//menuItem.addActionListener(actionListener);
+//             menuItem.setActionCommand("BackAction");
+//                                     menuItem.addActionListener(this);
+               popup.add(menuItem);
+
+               menuItem = new JMenuItem(HelpUtilities.getString(locale, "tooltip.ForwardAction"));
+//             menuItem.setActionCommand("javax.help.ForwardAction");
+               popup.add(menuItem);
+               popup.addSeparator();
+//                                     popup.setMnemonic('C');
+               */
+               
+               /* Copy */
+        menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
+        menuItem.setText(Messages.gettext("Copy"));
+               popup.add(menuItem); 
+               popup.addSeparator();
+
+
+               /* Select all */
+               ActionListener actionListenerSelectAll = new ActionListener() {
+                               public void actionPerformed(ActionEvent actionEvent) {
+                                       accessibleHtml.selectAll();
+                               }
+                       };
+               menuItem = new JMenuItem("Select All");
+               menuItem.addActionListener(actionListenerSelectAll);
+               popup.add(menuItem);
+
+               /* Creates the Popupmenu on the component */
+               accessibleHtml.setComponentPopupMenu(popup);
+       }
+}