Enable event_handler on Frame component 70/15370/4
Bruno JOFRET [Mon, 13 Oct 2014 08:56:16 +0000 (10:56 +0200)]
Change-Id: I1361573553ee9120bbd3576ec82661d993045116

scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/event/EventHandler.java [new file with mode: 0644]
scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/figure/Figure.java
scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/uicontrol/Uicontrol.java
scilab/modules/graphics/help/en_US/interaction/event_handler_functions.xml
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/frame/SwingScilabFrame.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/tab/SwingScilabDockablePanel.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/tab/SwingScilabStaticPanel.java
scilab/modules/gui/src/java/org/scilab/modules/gui/events/ScilabEventListener.java

diff --git a/scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/event/EventHandler.java b/scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/event/EventHandler.java
new file mode 100644 (file)
index 0000000..021246c
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2014 - Scilab Enterprises - Bruno JOFRET
+ *
+ * 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.1-en.txt
+ *
+ */
+
+package org.scilab.modules.graphic_objects.event;
+
+import org.scilab.modules.graphic_objects.graphicObject.GraphicObject.UpdateStatus;
+
+public class EventHandler {
+    /** Event handler string */
+    private String eventHandler = "";
+
+    /** Specifies whether the event handler is enabled or not */
+    private Boolean eventHandlerEnabled = false;
+
+    /**
+     * Default constructor
+     */
+    public EventHandler() {
+        eventHandler = "";
+        eventHandlerEnabled = false;
+    }
+
+    /**
+     * Copy constructor
+     * @param eventHandler the EventHandler to copy
+     */
+    public EventHandler(EventHandler eventHandler) {
+        this.eventHandler = eventHandler.eventHandler;
+        this.eventHandlerEnabled = eventHandler.eventHandlerEnabled;
+    }
+
+    public Boolean getEventHandlerEnabled() {
+        return eventHandlerEnabled;
+    }
+
+    public UpdateStatus setEventHandlerEnabled(Boolean eventHandlerEnabled) {
+        if (eventHandlerEnabled == this.eventHandlerEnabled) {
+            return UpdateStatus.NoChange;
+        }
+        this.eventHandlerEnabled = eventHandlerEnabled;
+        return UpdateStatus.Success;
+    }
+
+    public String getEventHandlerString() {
+        return eventHandler;
+    }
+
+    public UpdateStatus setEventHandlerString(String eventHandler) {
+        if (this.eventHandler.compareTo(eventHandler) == 0) {
+            return UpdateStatus.NoChange;
+        }
+        this.eventHandler = eventHandler;
+        return UpdateStatus.Success;
+    }
+}
index bccf2c9..4e334fb 100644 (file)
@@ -49,6 +49,7 @@ import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProp
 import java.util.Arrays;
 
 import org.scilab.modules.graphic_objects.axes.AxesContainer;
+import org.scilab.modules.graphic_objects.event.EventHandler;
 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
 import org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties;
 import org.scilab.modules.graphic_objects.graphicObject.Visitor;
@@ -362,34 +363,6 @@ public class Figure extends GraphicObject implements AxesContainer {
         EVENTHANDLER, EVENTHANDLERENABLE
     };
 
-    /**
-     * EventHandler class
-     */
-    private class EventHandler {
-        /** Event handler string */
-        private String eventHandler = "";
-
-        /** Specifies whether the event handler is enabled or not */
-        private Boolean eventHandlerEnabled = false;
-
-        /**
-         * Default constructor
-         */
-        public EventHandler() {
-            eventHandler = "";
-            eventHandlerEnabled = false;
-        }
-
-        /**
-         * Copy constructor
-         * @param eventHandler the EventHandler to copy
-         */
-        public EventHandler(EventHandler eventHandler) {
-            this.eventHandler = eventHandler.eventHandler;
-            this.eventHandlerEnabled = eventHandler.eventHandlerEnabled;
-        }
-    }
-
     /** Figure dimensions */
     private FigureDimensions dimensions;
 
@@ -957,17 +930,17 @@ public class Figure extends GraphicObject implements AxesContainer {
      * @return the eventHandler string
      */
     public String getEventHandlerString() {
-        return eventHandler.eventHandler;
+        return eventHandler.getEventHandlerString();
     }
 
     /**
      * @param eventHandlerString the eventHandler string to set
      */
     public UpdateStatus setEventHandlerString(String eventHandlerString) {
-        if (eventHandler.eventHandler.equals(eventHandlerString)) {
+        if (eventHandler.getEventHandlerString().equals(eventHandlerString)) {
             return UpdateStatus.NoChange;
         }
-        eventHandler.eventHandler = eventHandlerString;
+        eventHandler.setEventHandlerString(eventHandlerString);
         return UpdateStatus.Success;
     }
 
@@ -975,17 +948,17 @@ public class Figure extends GraphicObject implements AxesContainer {
      * @return the eventHandlerEnabled
      */
     public Boolean getEventHandlerEnable() {
-        return eventHandler.eventHandlerEnabled;
+        return eventHandler.getEventHandlerEnabled();
     }
 
     /**
      * @param eventHandlerEnabled the eventHandlerEnabled to set
      */
     public UpdateStatus setEventHandlerEnable(Boolean eventHandlerEnabled) {
-        if (eventHandler.eventHandlerEnabled == eventHandlerEnabled) {
+        if (eventHandler.getEventHandlerEnabled() == eventHandlerEnabled) {
             return UpdateStatus.NoChange;
         }
-        eventHandler.eventHandlerEnabled = eventHandlerEnabled;
+        eventHandler.setEventHandlerEnabled(eventHandlerEnabled);
         return UpdateStatus.Success;
     }
 
index 53e7b79..96713db 100644 (file)
@@ -15,6 +15,8 @@
 package org.scilab.modules.graphic_objects.uicontrol;
 
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_BORDER_OPT_PADDING__;
+import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_EVENTHANDLER_ENABLE__;
+import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_EVENTHANDLER_NAME__;
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_GRID_OPT_GRID__;
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_GRID_OPT_PADDING__;
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_LAYOUT_SET__;
@@ -81,6 +83,7 @@ import java.util.Arrays;
 import java.util.StringTokenizer;
 
 import org.scilab.modules.graphic_objects.console.Console;
+import org.scilab.modules.graphic_objects.event.EventHandler;
 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
 import org.scilab.modules.graphic_objects.graphicObject.Visitor;
 import org.scilab.modules.graphic_objects.utils.LayoutType;
@@ -478,7 +481,8 @@ public class Uicontrol extends GraphicObject {
     private Boolean titleScroll = false;
     private TitlePositionType titlePosition = TitlePositionType.TOP;
 
-
+    private EventHandler eventHandler;
+    
     /**
      * All uicontrol properties
      */
@@ -530,7 +534,9 @@ public class Uicontrol extends GraphicObject {
         TITLE_POSITION,
         TITLE_SCROLL,
         SCROLLABLE,
-        ICON
+        ICON,
+        EVENTHANDLER,
+        EVENTHANDLERENABLE
     };
 
     /**
@@ -559,6 +565,7 @@ public class Uicontrol extends GraphicObject {
      */
     public Uicontrol() {
         super();
+        eventHandler = new EventHandler();
         setVisible(false); /* To avoid to see the object rendered before all its properties to be set (See bug #10346) */
 
         if (Console.getConsole().getUseDeprecatedLF()) {
@@ -766,6 +773,10 @@ public class Uicontrol extends GraphicObject {
                 return UicontrolProperty.SCROLLABLE;
             case __GO_UI_ICON__:
                 return UicontrolProperty.ICON;
+            case __GO_EVENTHANDLER_NAME__:
+                return UicontrolProperty.EVENTHANDLER;
+            case __GO_EVENTHANDLER_ENABLE__:
+                return UicontrolProperty.EVENTHANDLERENABLE;
             default :
                 return super.getPropertyFromName(propertyName);
         }
@@ -869,6 +880,10 @@ public class Uicontrol extends GraphicObject {
             return getScrollable();
         } else if (property == UicontrolProperty.ICON) {
             return getIcon();
+        } else if (property == UicontrolProperty.EVENTHANDLER) {
+            return getEventHandler();
+        } else if (property == UicontrolProperty.EVENTHANDLERENABLE) {
+            return getEventHandlerEnable();
         } else {
             return super.getProperty(property);
         }
@@ -969,6 +984,10 @@ public class Uicontrol extends GraphicObject {
                 return setScrollable((Boolean) value);
             case ICON:
                 return setIcon((String) value);
+            case EVENTHANDLER:
+                return setEventHandler((String) value);
+            case EVENTHANDLERENABLE:
+                return setEventHandlerEnable((Boolean) value);
             default:
                 return super.setProperty(property, value);
         }
@@ -1737,6 +1756,22 @@ public class Uicontrol extends GraphicObject {
         return icon;
     }
 
+    public UpdateStatus setEventHandler(String eventHandlerName) {
+        return eventHandler.setEventHandlerString(eventHandlerName);
+    }
+
+    public String getEventHandler() {
+        return eventHandler.getEventHandlerString();
+    }
+    
+    public UpdateStatus setEventHandlerEnable(Boolean eventHandlerEnabled) {
+        return eventHandler.setEventHandlerEnabled(eventHandlerEnabled);
+    }
+    
+    public boolean getEventHandlerEnable() {
+        return eventHandler.getEventHandlerEnabled();
+    }
+    
     public void accept(Visitor visitor) {
     }
 }
index 81dee1c..55ad3e1 100644 (file)
@@ -2,11 +2,11 @@
 <!--
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  * Copyright (C) INRIA - Jean-Baptiste Silvy
- * 
+ *
  * 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    
+ * are also available at
  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
  *
  -->
     </refnamediv>
     <refsynopsisdiv>
         <title>Calling Sequence</title>
-        <synopsis>event_handler_function(win,x,y,ibut)</synopsis>
+        <synopsis>event_handler_function(h,x,y,ibut)</synopsis>
     </refsynopsisdiv>
     <refsection>
         <title>Arguments</title>
         <variablelist>
             <varlistentry>
-                <term>win</term>
+                <term>h</term>
                 <listitem>
-                    <para>window number where the event had occurred.</para>
+                    <para>window number if the event occurred in an axe with a figure type parent. (DEPRECATED)</para>
+                    <para>handle of the axe's parent where the event occured.</para>
                 </listitem>
             </varlistentry>
             <varlistentry>
     </refsection>
     <refsection>
         <title>Examples</title>
-        <programlisting role="example"><![CDATA[ 
+        <programlisting role="example"><![CDATA[
 function my_eventhandler(win,x,y,ibut)
   if ibut==-1000 then return,end
   [x,y]=xchange(x,y,'i2f')
index fd83192..9efaa95 100644 (file)
@@ -29,6 +29,12 @@ import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProp
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING__;
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VALUE__;
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_VISIBLE__;
+import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_EVENTHANDLER_NAME__;
+import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_EVENTHANDLER_ENABLE__;
+
+
+
+
 
 import java.awt.BorderLayout;
 import java.awt.Color;
@@ -77,6 +83,7 @@ import org.scilab.modules.gui.console.Console;
 import org.scilab.modules.gui.dockable.Dockable;
 import org.scilab.modules.gui.editbox.EditBox;
 import org.scilab.modules.gui.editor.EditorEventListener;
+import org.scilab.modules.gui.events.ScilabEventListener;
 import org.scilab.modules.gui.events.callback.CommonCallBack;
 import org.scilab.modules.gui.frame.Frame;
 import org.scilab.modules.gui.frame.SimpleFrame;
@@ -113,6 +120,8 @@ public class SwingScilabFrame extends JPanel implements SwingViewObject, SimpleF
     private Border defaultBorder = null;
     private SwingScilabCanvas canvas = null;
     private EditorEventListener editorEventHandler = null;
+    private ScilabEventListener eventHandler;
+    private boolean eventEnabled = false;
 
     /**
      * Constructor
@@ -1018,6 +1027,16 @@ public class SwingScilabFrame extends JPanel implements SwingViewObject, SimpleF
                 }
                 break;
             }
+            case __GO_EVENTHANDLER_ENABLE__ : {
+                Boolean enabled = (Boolean) GraphicController.getController().getProperty(getId(), __GO_EVENTHANDLER_ENABLE__);
+                setEventHandlerEnabled(enabled);
+                break;
+            }
+            case __GO_EVENTHANDLER_NAME__: {
+                String eventHandlerName = (String) GraphicController.getController().getProperty(getId(), __GO_EVENTHANDLER_NAME__);
+                setEventHandler(eventHandlerName);
+                break;
+            }
             default:
                 SwingViewWidget.update(this, property, value);
                 break;
@@ -1090,4 +1109,60 @@ public class SwingScilabFrame extends JPanel implements SwingViewObject, SimpleF
             repaint();
         }
     }
+    
+    /**
+     * Set the event handler of the Canvas
+     * @param funName the name of the Scilab function to call
+     */
+    public void setEventHandler(String funName) {
+        disableEventHandler();
+        eventHandler = new ScilabEventListener(funName, getId());
+        if (eventEnabled) {
+            editorEventHandler.setEnable(false);
+            enableEventHandler();
+        }
+    }
+
+    /**
+     * Set the status of the event handler of the Canvas
+     * @param status is true to set the event handler active
+     */
+    public void setEventHandlerEnabled(boolean status) {
+        if (status && eventEnabled) {
+            return;
+        }
+
+        if (status) {
+            editorEventHandler.setEnable(false);
+            enableEventHandler();
+            eventEnabled = true;
+        } else {
+            editorEventHandler.setEnable(true);
+            disableEventHandler();
+            eventEnabled = false;
+        }
+    }
+    
+    /**
+     * Turn on event handling.
+     */
+    private void enableEventHandler() {
+        if (canvas != null) {
+            canvas.addEventHandlerKeyListener(eventHandler);
+            canvas.addEventHandlerMouseListener(eventHandler);
+            canvas.addEventHandlerMouseMotionListener(eventHandler);
+        }
+    }
+
+    /**
+     * Turn off event handling.
+     */
+    private void disableEventHandler() {
+        if (eventHandler != null && canvas != null) {
+            canvas.removeEventHandlerKeyListener(eventHandler);
+            canvas.removeEventHandlerMouseListener(eventHandler);
+            canvas.removeEventHandlerMouseMotionListener(eventHandler);
+        }
+    }
+
 }
index 2634eb8..1008519 100644 (file)
@@ -1341,7 +1341,7 @@ public class SwingScilabDockablePanel extends View implements SimpleTab, FocusLi
      */
     public void setEventHandler(String funName) {
         disableEventHandler();
-        eventHandler = new ScilabEventListener(funName, getId());
+        eventHandler = new ScilabEventListener(funName, getId(), false);
         if (eventEnabled) {
             editorEventHandler.setEnable(false);
             enableEventHandler();
index 41f9331..8a1a6fe 100644 (file)
@@ -224,7 +224,7 @@ public class SwingScilabStaticPanel extends SwingScilabScrollPane implements Swi
      */
     public void setEventHandler(String funName) {
         disableEventHandler();
-        eventHandler = new ScilabEventListener(funName, getId());
+        eventHandler = new ScilabEventListener(funName, getId(), false);
         if (eventEnabled) {
             editorEventHandler.setEnable(false);
             enableEventHandler();
index 5c2ba4f..68c8586 100644 (file)
@@ -36,6 +36,7 @@ public class ScilabEventListener implements KeyListener, MouseListener, MouseMot
     private SciTranslator eventTranslator = new SciTranslator();
     private boolean freedom = true;
     private boolean inCanvas = false;
+    private boolean useHandle = true;
 
     public ScilabEventListener(String callback, Integer windowsUID) {
         eventTranslator.setClickAction(SciTranslator.UNMANAGED);
@@ -43,11 +44,24 @@ public class ScilabEventListener implements KeyListener, MouseListener, MouseMot
         this.windowsUID        = windowsUID;
     }
 
+    // Remove this constructor
+    // once event_handler call are unified using handle 
+    public ScilabEventListener(String callback, Integer windowsUID, boolean useHandle) {
+        eventTranslator.setClickAction(SciTranslator.UNMANAGED);
+        this.callback = callback;
+        this.windowsUID = windowsUID;
+        this.useHandle = useHandle;
+    }
+
     private void callScilab() {
         // @FIXME : choose to send it to scilab or to display it
         //
-        int windowsId = (Integer) GraphicController.getController().getProperty(windowsUID, __GO_ID__);
-        InterpreterManagement.requestScilabExec(callback + '(' + windowsId + ',' + mouseX + ',' + mouseY + ',' + eventTranslator.getClickAction() + ')');
+        if (useHandle) { 
+            InterpreterManagement.requestScilabExec(callback + "(getcallbackobject(" + windowsUID + ")," + mouseX + ',' + mouseY + ',' + eventTranslator.getClickAction() + ')');
+        } else {
+            int windowsId = (Integer) GraphicController.getController().getProperty(windowsUID, __GO_ID__);
+            InterpreterManagement.requestScilabExec(callback + '(' + windowsId + ',' + mouseX + ',' + mouseY + ',' + eventTranslator.getClickAction() + ')');
+        }
         //
         //System.out.println("call " + callback+'('+windowsId+','+mouseX+','+mouseY+','+eventTranslator.getClickAction()+')');
     }
@@ -55,8 +69,12 @@ public class ScilabEventListener implements KeyListener, MouseListener, MouseMot
     private void invokeScilab() {
         // @FIXME : choose to send it to scilab or to display it
         //
-        int windowsId = (Integer) GraphicController.getController().getProperty(windowsUID, __GO_ID__);
-        InterpreterManagement.requestScilabExec(callback + '(' + windowsId + ',' + mouseX + ',' + mouseY + ',' + eventTranslator.javaClick2Scilab() + ')');
+        if (useHandle) { 
+            InterpreterManagement.requestScilabExec(callback + "(getcallbackobject(" + windowsUID + ")," + mouseX + ',' + mouseY + ',' + eventTranslator.javaClick2Scilab() + ')');
+        } else {
+            int windowsId = (Integer) GraphicController.getController().getProperty(windowsUID, __GO_ID__);
+            InterpreterManagement.requestScilabExec(callback + '(' + windowsId + ',' + mouseX + ',' + mouseY + ',' + eventTranslator.javaClick2Scilab() + ')');
+        }
         //
         //System.out.println("invoke " + callback+'('+windowsId+','+mouseX+','+mouseY+','+eventTranslator.javaClick2Scilab()+')');
     }
@@ -138,6 +156,7 @@ public class ScilabEventListener implements KeyListener, MouseListener, MouseMot
                 public void run() {
                     invokeScilab();
                     freedom = true;
+                    eventTranslator.setClickAction(SciTranslator.UNMANAGED);
                 }
             };
             launchMe.start();
@@ -145,8 +164,8 @@ public class ScilabEventListener implements KeyListener, MouseListener, MouseMot
     }
 
     public void mouseReleased(MouseEvent arg0) {
-        // TODO Auto-generated method stub
-        if (eventTranslator.getClickAction() == SciTranslator.UNMANAGED) {
+        if (eventTranslator.getClickAction() == SciTranslator.UNMANAGED || 
+                eventTranslator.getClickAction() == SciTranslator.SCIMOVED) { 
             eventTranslator.setClickAction(
                 SciTranslator.javaButton2Scilab(arg0.getButton(),
                                                 SciTranslator.RELEASED,
@@ -171,6 +190,7 @@ public class ScilabEventListener implements KeyListener, MouseListener, MouseMot
             mouseY = arg0.getY();
             callScilab();
         }
+        eventTranslator.setClickAction(SciTranslator.UNMANAGED);
     }
 
     public void mouseMoved(MouseEvent arg0) {
@@ -178,6 +198,7 @@ public class ScilabEventListener implements KeyListener, MouseListener, MouseMot
         mouseX = arg0.getX();
         mouseY = arg0.getY();
         callScilab();
+        eventTranslator.setClickAction(SciTranslator.UNMANAGED);
     }
 
 }