Update the model even if there is no callback 96/7596/6
Vincent COUVERT [Tue, 19 Jun 2012 14:08:50 +0000 (16:08 +0200)]
Change-Id: I9b41a33283e46832b47bbb658098c2a13ade29d9

scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/checkbox/SwingScilabCheckBox.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/editbox/SwingScilabEditBox.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/listbox/SwingScilabListBox.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/popupmenu/SwingScilabPopupMenu.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/radiobutton/SwingScilabRadioButton.java

index 23342c2..8645ea1 100644 (file)
@@ -59,6 +59,21 @@ public class SwingScilabCheckBox extends JCheckBox implements SwingViewObject, S
         /* Avoid the L&F to erase user background settings */
         setContentAreaFilled(false);
         setOpaque(true);
+        actListener = new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                Double[] value = new Double[1];
+                value[0] = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MIN__);
+                if (isSelected()) {
+                    value[0] = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__);
+                }
+                GraphicController.getController().setProperty(uid, __GO_UI_VALUE__, value);
+                if (callback != null) {
+                    callback.actionPerformed(e);
+                }
+            }
+        };
+        addActionListener(actListener);
     }
 
     /**
@@ -112,25 +127,7 @@ public class SwingScilabCheckBox extends JCheckBox implements SwingViewObject, S
      * @param cb the callback to set
      */
     public void setCallback(CommonCallBack cb) {
-        if (actListener != null) {
-            removeActionListener(actListener);
-        }
-
         callback = cb;
-
-        actListener = new ActionListener() {
-            @Override
-            public void actionPerformed(ActionEvent e) {
-                Double[] value = new Double[1];
-                value[0] = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MIN__);
-                if (isSelected()) {
-                    value[0] = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__);
-                }
-                GraphicController.getController().setProperty(uid, __GO_UI_VALUE__, value);
-                callback.actionPerformed(e);
-            }
-        };
-        addActionListener(actListener);
     }
 
     /**
index ca54af7..ec96f81 100644 (file)
@@ -3,11 +3,11 @@
  * Copyright (C) 2007-2008 - INRIA - Vincent Couvert
  * Copyright (C) 2007 - INRIA - Marouane BEN JELLOUL
  * Copyright (C) 2011 - DIGITEO - Vincent COUVERT
- * 
+ *
  * 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-en.txt
  *
  */
@@ -24,6 +24,7 @@ import java.awt.event.FocusListener;
 import javax.swing.JTextField;
 
 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
+import org.scilab.modules.gui.SwingView;
 import org.scilab.modules.gui.SwingViewWidget;
 import org.scilab.modules.gui.SwingViewObject;
 import org.scilab.modules.gui.editbox.SimpleEditBox;
@@ -59,6 +60,41 @@ public class SwingScilabEditBox extends JTextField implements SwingViewObject, S
      */
     public SwingScilabEditBox() {
         super();
+        // Create a focus listener to call the callback action
+        focusListener = new FocusListener() {
+            public void focusGained(FocusEvent arg0) {
+                // Do nothing
+            }
+            public void focusLost(FocusEvent arg0) {
+                // Validates user input
+                if (getParent() != null) { // To avoid to execute the callback when then parent figure is destroyed
+                    String[] stringProperty = new String[] {getText()};
+                    GraphicController.getController().setProperty(uid, __GO_UI_STRING__, stringProperty);
+
+                    if (SwingView.getFromId(uid) != null && callback != null) {
+                        callback.actionPerformed(null);
+                    }
+                }
+            }
+        };
+        addFocusListener(focusListener);
+
+        // Create n action listener to get ENTER keystrokes
+        actionListener = new ActionListener() {
+
+            public void actionPerformed(ActionEvent arg0) {
+                // Validates user input
+                if (getParent() != null) { // To avoid to execute the callback when then parent figure is destroyed
+                    String[] stringProperty = new String[] {getText()};
+                    GraphicController.getController().setProperty(uid, __GO_UI_STRING__, stringProperty);
+                    if (SwingView.getFromId(uid) != null && callback != null) {
+                        callback.actionPerformed(null);
+                    }
+                }
+            }
+
+        };
+        addActionListener(actionListener);
     }
 
     /**
@@ -112,45 +148,7 @@ public class SwingScilabEditBox extends JTextField implements SwingViewObject, S
      * @param cb the callback to set.
      */
     public void setCallback(CommonCallBack cb) {
-
-        // Remove previous callback
-        if (focusListener != null) {
-            removeFocusListener(focusListener);
-            removeActionListener(actionListener);
-        }
-
         this.callback = cb;
-
-        // Create a focus listener to call the callback action
-        focusListener = new FocusListener() {
-            public void focusGained(FocusEvent arg0) {
-                // Do nothing
-            }
-            public void focusLost(FocusEvent arg0) {
-                // Validates user input
-                if (getParent() != null) { // To avoid to execute the callback when then parent figure is destroyed
-                    String[] stringProperty = new String[1];
-                    stringProperty[0] = getText();
-                    GraphicController.getController().setProperty(uid, __GO_UI_STRING__, stringProperty);
-                    callback.actionPerformed(null);
-                }
-            }
-        };
-        addFocusListener(focusListener);
-
-        // Create n action listener to get ENTER keystrokes
-        actionListener = new ActionListener() {
-
-            public void actionPerformed(ActionEvent arg0) {
-                // Validates user input
-                String[] stringProperty = new String[1];
-                stringProperty[0] = getText();
-                GraphicController.getController().setProperty(uid, __GO_UI_STRING__, stringProperty);
-                callback.actionPerformed(null);
-            }
-
-        };
-        addActionListener(actionListener);
     }
 
     /**
@@ -159,7 +157,7 @@ public class SwingScilabEditBox extends JTextField implements SwingViewObject, S
      */
     public void setEnabled(boolean status) {
         super.setEnabled(status);
-        /* (Des)Activate the callback */ 
+        /* (Des)Activate the callback */
         if (callback != null) {
             if (status) {
                 removeFocusListener(focusListener); /* To be sure the callback is not added two times */
index 620bebf..91fded6 100644 (file)
@@ -70,6 +70,26 @@ public class SwingScilabListBox extends JScrollPane implements SwingViewObject,
     public SwingScilabListBox() {
         super();
         getViewport().add(getList());
+
+        mouseListener = new MouseListener() {
+            public void mouseClicked(MouseEvent e) {
+                // Scilab indices in Value begin at 1 and Java indices begin at 0
+                int[] javaIndices = getList().getSelectedIndices().clone();
+                Double[] scilabIndices = new Double[javaIndices.length];
+                for (int i = 0; i < getList().getSelectedIndices().length; i++) {
+                    scilabIndices[i] = (double) javaIndices[i] + 1;
+                }
+                GraphicController.getController().setProperty(uid, __GO_UI_VALUE__, scilabIndices);
+                if (e.getButton() == MouseEvent.BUTTON1 && callback != null) {
+                    callback.actionPerformed(null);
+                }
+            }
+            public void mouseEntered(MouseEvent arg0) { }
+            public void mouseExited(MouseEvent arg0) { }
+            public void mousePressed(MouseEvent arg0) { }
+            public void mouseReleased(MouseEvent arg0) { }
+        };
+        getList().addMouseListener(mouseListener);
     }
 
     /**
@@ -200,35 +220,7 @@ public class SwingScilabListBox extends JScrollPane implements SwingViewObject,
      * @param cb the callback to set.
      */
     public void setCallback(CommonCallBack cb) {
-        if (mouseListener != null) {
-            getList().removeMouseListener(mouseListener);
-        }
         this.callback = cb;
-
-        mouseListener = new MouseListener() {
-
-            public void mouseClicked(MouseEvent e) {
-                // Scilab indices in Value begin at 1 and Java indices begin at 0
-                int[] javaIndices = getList().getSelectedIndices().clone();
-                Double[] scilabIndices = new Double[javaIndices.length];
-                for (int i = 0; i < getList().getSelectedIndices().length; i++) {
-                    scilabIndices[i] = (double) javaIndices[i] + 1;
-                }
-                GraphicController.getController().setProperty(uid, __GO_UI_VALUE__, scilabIndices);
-                if (e.getButton() == MouseEvent.BUTTON1) {
-                    callback.actionPerformed(null);
-                }
-            }
-
-            public void mouseEntered(MouseEvent arg0) { }
-            public void mouseExited(MouseEvent arg0) { }
-            public void mousePressed(MouseEvent arg0) { }
-            public void mouseReleased(MouseEvent arg0) { }
-        };
-
-        if (isEnabled()) {
-            getList().addMouseListener(mouseListener);
-        }
     }
 
     /**
index d0216c9..1553605 100644 (file)
 package org.scilab.modules.gui.bridge.popupmenu;
 
 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 java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.MouseEvent;
 import java.util.StringTokenizer;
 
 import javax.swing.JComboBox;
@@ -56,6 +60,19 @@ public class SwingScilabPopupMenu extends JComboBox implements SwingViewObject,
         super();
         /* Bug 3635 fixed: allow arrow keys to browse items */
         putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
+        ActionListener actionListener = new ActionListener() {
+            public void actionPerformed(ActionEvent e) {
+                Double[] scilabIndices = new Double[1];
+                scilabIndices[0] = (double) getUserSelectedIndex();
+                GraphicController.getController().setProperty(uid, __GO_UI_VALUE__, scilabIndices);
+                if (callback != null) {
+                    callback.actionPerformed(null);
+                }
+
+
+            }
+        };
+        addActionListener(actionListener);
     }
 
     /**
@@ -109,11 +126,7 @@ public class SwingScilabPopupMenu extends JComboBox implements SwingViewObject,
      * @param callback the callback to set.
      */
     public void setCallback(CommonCallBack callback) {
-        if (this.callback != null) {
-            removeActionListener(this.callback);
-        }
         this.callback = callback;
-        addActionListener(this.callback);
     }
 
     /**
@@ -338,12 +351,12 @@ public class SwingScilabPopupMenu extends JComboBox implements SwingViewObject,
     /**
      * Class created as a workaround for bug: http://bugzilla.scilab.org/show_bug.cgi?id=7898
      * This bug is a Java bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4133743
-     * 
+     *
      * This workaround has been proposed by a user on Java bug tracker.
-     * 
+     *
      * The toString method will be used to display the elements, but because the class inherits its
      * equals method from Object instead of String, none of the elements are considered duplicates.
-     * 
+     *
      */
     private class SwingScilabPopupMenuItem {
 
index 11142b6..3b82b97 100644 (file)
@@ -60,6 +60,21 @@ public class SwingScilabRadioButton extends JRadioButton implements SwingViewObj
         /* Avoid the L&F to erase user background settings */
         setContentAreaFilled(false);
         setOpaque(true);
+        actListener = new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                Double[] value = new Double[1];
+                value[0] = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MIN__);
+                if (isSelected()) {
+                    value[0] = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__);
+                }
+                GraphicController.getController().setProperty(uid, __GO_UI_VALUE__, value);
+                if (callback != null) {
+                    callback.actionPerformed(e);
+                }
+            }
+        };
+        addActionListener(actListener);
     }
 
     /**
@@ -113,25 +128,7 @@ public class SwingScilabRadioButton extends JRadioButton implements SwingViewObj
      * @param cb the callback to set.
      */
     public void setCallback(CommonCallBack cb) {
-        if (actListener != null) {
-            removeActionListener(actListener);
-        }
-
         callback = cb;
-
-        actListener = new ActionListener() {
-            @Override
-            public void actionPerformed(ActionEvent e) {
-                Double[] value = new Double[1];
-                value[0] = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MIN__);
-                if (isSelected()) {
-                    value[0] = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__);
-                }
-                GraphicController.getController().setProperty(uid, __GO_UI_VALUE__, value);
-                callback.actionPerformed(e);
-            }
-        };
-        addActionListener(actListener);
     }
 
     /**