--- /dev/null
+/*
+ * 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;
+ }
+}
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;
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;
* @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);
}
/**
+ * 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);
}
}
jc.removeAll();
}
+ buttonGroups = null;
component = null;
modifiableComponent = null;
root = null;
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;
}
}
/**
+ * 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 {
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;
}
}
/**
+ * 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 {