001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.widgets;
023:
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026:
027: import javax.swing.JRadioButton;
028:
029: import org.beryl.gui.GUIException;
030: import org.beryl.gui.Widget;
031: import org.beryl.gui.WidgetInfo;
032: import org.beryl.gui.model.MapChangeEvent;
033: import org.beryl.gui.model.MapDataModel;
034: import org.beryl.gui.model.ModelChangeEvent;
035:
036: public class ButtonGroup extends Panel {
037: protected static WidgetInfo buttonGroupInfo = null;
038: private String key = null;
039: private javax.swing.ButtonGroup buttonGroup = null;
040:
041: static {
042: buttonGroupInfo = new WidgetInfo(ButtonGroup.class, panelInfo);
043: buttonGroupInfo.addProperty("key", "string", "");
044: };
045:
046: public ButtonGroup(Widget parentWidget, String name)
047: throws GUIException {
048: super (parentWidget, name);
049: buttonGroup = new javax.swing.ButtonGroup();
050: }
051:
052: public void setProperty(String name, Object value)
053: throws GUIException {
054: if ("key".equals(name)) {
055: this .key = (String) value;
056: } else {
057: super .setProperty(name, value);
058: }
059: }
060:
061: public void addChild(Widget widget, Object constraint)
062: throws GUIException {
063: if (widget instanceof RadioButton) {
064: final RadioButton radioButton = (RadioButton) widget;
065: JRadioButton button = (JRadioButton) radioButton
066: .getRealWidget();
067: super .addChild(widget, constraint);
068: buttonGroup.add(button);
069:
070: button.addActionListener(new ActionListener() {
071: public void actionPerformed(ActionEvent e) {
072: try {
073: MapDataModel model = getDataModel();
074: if (model != null && key != null) {
075: model.setValue(ButtonGroup.this , key,
076: radioButton.getValue());
077: }
078: } catch (GUIException ex) {
079: throw new RuntimeException(ex);
080: }
081: }
082: });
083: } else {
084: super .addChild(widget, constraint);
085: }
086: }
087:
088: public void removeChildWidget(Widget widget) throws GUIException {
089: if (widget instanceof RadioButton) {
090: RadioButton radioButton = (RadioButton) widget;
091: buttonGroup.remove((JRadioButton) radioButton
092: .getRealWidget());
093: }
094: super .removeChildWidget(widget);
095: }
096:
097: private void reload() throws GUIException {
098: Object value = (Object) getDataModel().getValue(key);
099: if (value != null) {
100: RadioButton button = findButton(value);
101: if (button != null)
102: ((JRadioButton) button.getRealWidget())
103: .setSelected(true);
104: else
105: throw new GUIException("Invalid radio button value ["
106: + value + "]");
107: }
108: }
109:
110: public void modelChanged(ModelChangeEvent e) throws GUIException {
111: if (e.getSource() == this ) {
112: /* New data model */
113: reload();
114: } else if (e instanceof MapChangeEvent) {
115: MapChangeEvent event = (MapChangeEvent) e;
116: if (event.getKey() == null) {
117: reload();
118: } else if (event.getKey().equals(key)) {
119: RadioButton button = findButton(event.getNewValue());
120: if (button != null)
121: ((JRadioButton) button.getRealWidget())
122: .setSelected(true);
123: else
124: throw new GUIException(
125: "Invalid radio button value ["
126: + event.getNewValue() + "]");
127: }
128: }
129: }
130:
131: private RadioButton findButton(Object object) throws GUIException {
132: for (int i = 0, count = getChildCount(); i < count; i++) {
133: Widget widget = (Widget) getChild(i);
134: if (widget instanceof RadioButton) {
135: RadioButton button = (RadioButton) widget;
136: if (button.getValue() == object
137: || button.getValue().equals(object)) {
138: return button;
139: }
140: }
141: }
142:
143: throw new GUIException("Value not found in ButtonGroup");
144: }
145:
146: public WidgetInfo getWidgetInfo() {
147: return buttonGroupInfo;
148: }
149: }
|