UIW: add a property to get the selected radio or checkbox in a group 46/13046/1
Calixte DENIZET [Wed, 30 Oct 2013 18:37:54 +0000 (19:37 +0100)]
Change-Id: I9f3df8d83f299bfddd258710051103ed56f092c2

scilab/modules/gui/src/java/org/scilab/modules/gui/uiwidget/UIButtonGroup.java [new file with mode: 0644]
scilab/modules/gui/src/java/org/scilab/modules/gui/uiwidget/UIComponent.java
scilab/modules/gui/src/java/org/scilab/modules/gui/uiwidget/components/UICheckbox.java
scilab/modules/gui/src/java/org/scilab/modules/gui/uiwidget/components/UIRadio.java

diff --git a/scilab/modules/gui/src/java/org/scilab/modules/gui/uiwidget/UIButtonGroup.java b/scilab/modules/gui/src/java/org/scilab/modules/gui/uiwidget/UIButtonGroup.java
new file mode 100644 (file)
index 0000000..5d2f2a9
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
+ *
+ * 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.uiwidget;
+
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.swing.AbstractButton;
+import javax.swing.ButtonGroup;
+
+/**
+ * The main interest of this class is to have the method getSelected() to get the selected
+ * UIComponent which belongs to a ButtonGroup
+ */
+public class UIButtonGroup extends ButtonGroup {
+
+    private Map<AbstractButton, UIComponent> comps = new HashMap<AbstractButton, UIComponent>();
+
+    /**
+     * Default constructor
+     */
+    public UIButtonGroup() {
+        super();
+    }
+
+    /**
+     * Add an UIComponent to this Group
+     * @param c the UIComponent to add
+     */
+    public void add(UIComponent c) {
+        if (c != null && (c.component instanceof AbstractButton)) {
+            comps.put((AbstractButton) c.component, c);
+            add((AbstractButton) c.component);
+        }
+    }
+
+    /**
+     * Remove an UIComponent from this Group
+     * @param c the UIComponent to remove
+     */
+    public void remove(UIComponent c) {
+        if (c != null && (c.component instanceof AbstractButton)) {
+            comps.remove((AbstractButton) c.component);
+            remove((AbstractButton) c.component);
+        }
+    }
+
+    /**
+     * Get the selected UIComponent in this Group
+     * @return the selected UIComponent
+     */
+    public UIComponent getSelected() {
+        Enumeration<AbstractButton> e = getElements();
+        while (e.hasMoreElements()) {
+            AbstractButton button = e.nextElement();
+            if (button.isSelected()) {
+                return comps.get(button);
+            }
+        }
+
+        return null;
+    }
+}
index 1abcd11..3ebf17c 100644 (file)
@@ -41,8 +41,6 @@ import java.util.Set;
 import java.util.TreeMap;
 
 import javax.imageio.ImageIO;
-import javax.swing.AbstractButton;
-import javax.swing.ButtonGroup;
 import javax.swing.JComponent;
 import javax.swing.JFrame;
 import javax.swing.JPopupMenu;
@@ -91,7 +89,7 @@ public abstract class UIComponent {
     private int uid;
     protected UIComponent root;
     protected UIComponent parent;
-    protected Map<String, ButtonGroup> buttonGroups;
+    protected Map<String, UIButtonGroup> buttonGroups;
     protected Map<String, UIComponent> children;
     protected List<UIComponent> childrenList;
     protected Map<String, Map<String, String>> style;
@@ -806,14 +804,14 @@ public abstract class UIComponent {
      * @param name the name of the button-group
      * @param button the button to add
      */
-    public void addToButtonGroup(String name, AbstractButton button) {
+    public void addToButtonGroup(String name, UIComponent button) {
         if (isRoot()) {
             if (buttonGroups == null) {
-                buttonGroups = new HashMap<String, ButtonGroup>();
+                buttonGroups = new HashMap<String, UIButtonGroup>();
             }
-            ButtonGroup bg = buttonGroups.get(name);
+            UIButtonGroup bg = buttonGroups.get(name);
             if (bg == null) {
-                bg = new ButtonGroup();
+                bg = new UIButtonGroup();
                 buttonGroups.put(name, bg);
             }
             bg.add(button);
@@ -821,13 +819,29 @@ public abstract class UIComponent {
     }
 
     /**
+     * Get the uicomponent which is selected in the group
+     * @param name the group name
+     * @return the selected component
+     */
+    protected UIComponent getSelectedInGroup(String name) {
+        if (getRoot().buttonGroups != null) {
+            UIButtonGroup group = getRoot().buttonGroups.get(name);
+            if (group != null) {
+                return group.getSelected();
+            }
+        }
+
+        return null;
+    }
+
+    /**
      * Remove a button from a button-group
      * @param name the name of the button-group
      * @param button the button to remove
      */
-    public void removeFromButtonGroup(String name, AbstractButton button) {
+    public void removeFromButtonGroup(String name, UIComponent button) {
         if (isRoot() && buttonGroups != null) {
-            ButtonGroup bg = buttonGroups.get(name);
+            UIButtonGroup bg = buttonGroups.get(name);
             if (bg != null) {
                 bg.remove(button);
             }
@@ -928,6 +942,7 @@ public abstract class UIComponent {
             }
             jc.removeAll();
         }
+        buttonGroups = null;
         component = null;
         modifiableComponent = null;
         root = null;
index d094206..a011a8b 100644 (file)
@@ -61,20 +61,27 @@ public class UICheckbox extends UIComponent {
             checkbox = new JCheckBox(text, selected);
         }
 
-        setButtonGroup(group);
+        this.buttonGroup = group;
 
         return checkbox;
     }
 
     /**
+     * {@inheritDoc}
+     */
+    public void initialize() {
+        setButtonGroup(this.buttonGroup);
+    }
+
+    /**
      * Set the button group where this button belongs.
      * @param group the group name
      */
     public void setButtonGroup(String group) {
         if (group != null && !group.isEmpty()) {
-            getRoot().addToButtonGroup(group, checkbox);
+            getRoot().addToButtonGroup(group, this);
         } else {
-            getRoot().removeFromButtonGroup(this.buttonGroup, checkbox);
+            getRoot().removeFromButtonGroup(this.buttonGroup, this);
         }
         this.buttonGroup = group;
     }
@@ -88,6 +95,18 @@ public class UICheckbox extends UIComponent {
     }
 
     /**
+     * Get the uicomponent which is selected in the group
+     * @return the selected component
+     */
+    public UIComponent getSelectedInGroup() {
+        if (buttonGroup != null && !buttonGroup.isEmpty()) {
+            return getSelectedInGroup(buttonGroup);
+        }
+
+        return null;
+    }
+
+    /**
      * {@inheritDoc}
      */
     public void setUiStyle(Map<String, String> style) throws UIWidgetException {
index 08c2735..5da08b4 100644 (file)
@@ -61,20 +61,27 @@ public class UIRadio extends UIComponent {
             radio = new JRadioButton(text, selected);
         }
 
-        setButtonGroup(group);
+        this.buttonGroup = group;
 
         return radio;
     }
 
     /**
+     * {@inheritDoc}
+     */
+    public void initialize() {
+        setButtonGroup(this.buttonGroup);
+    }
+
+    /**
      * Set the button group where this button belongs.
      * @param group the group name
      */
     public void setButtonGroup(String group) {
         if (group != null && !group.isEmpty()) {
-            getRoot().addToButtonGroup(group, radio);
+            getRoot().addToButtonGroup(group, this);
         } else {
-            getRoot().removeFromButtonGroup(this.buttonGroup, radio);
+            getRoot().removeFromButtonGroup(this.buttonGroup, this);
         }
         this.buttonGroup = group;
     }
@@ -88,6 +95,18 @@ public class UIRadio extends UIComponent {
     }
 
     /**
+     * Get the uicomponent which is selected in the group
+     * @return the selected component
+     */
+    public UIComponent getSelectedInGroup() {
+        if (buttonGroup != null && !buttonGroup.isEmpty()) {
+            return getSelectedInGroup(buttonGroup);
+        }
+
+        return null;
+    }
+
+    /**
      * {@inheritDoc}
      */
     public void setUiStyle(Map<String, String> style) throws UIWidgetException {