001: package com.salmonllc.swing;
002:
003: //** Copyright Statement ***************************************************
004: //The Salmon Open Framework for Internet Applications (SOFIA)
005: // Copyright (C) 1999 - 2002, Salmon LLC
006: //
007: // This program is free software; you can redistribute it and/or
008: // modify it under the terms of the GNU General Public License version 2
009: // as published by the Free Software Foundation;
010: //
011: // This program is distributed in the hope that it will be useful,
012: // but WITHOUT ANY WARRANTY; without even the implied warranty of
013: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: // GNU General Public License for more details.
015: //
016: // You should have received a copy of the GNU General Public License
017: // along with this program; if not, write to the Free Software
018: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: //
020: // For more information please visit http://www.salmonllc.com
021: //** End Copyright Statement ***************************************************
022:
023: import com.salmonllc.swing.events.ValueChangedEvent;
024: import com.salmonllc.swing.events.ValueChangedListener;
025: import com.salmonllc.sql.DataStoreBuffer;
026: import com.salmonllc.sql.ModelChangedListener;
027: import com.salmonllc.sql.ModelChangedEvent;
028:
029: import javax.swing.*;
030: import java.util.Enumeration;
031: import java.awt.*;
032:
033: /**
034: * SOFIA implementation of a Radio Button Group. It lets you bind the group to a datastore column. The button group is a non visual component. You must create the SComponent buttons individually and use this to group them and bind them to a datastore column.
035: */
036:
037: public class SButtonGroup extends ButtonGroup implements SComponent,
038: ModelChangedListener {
039: String _col;
040: DataStoreBuffer _ds;
041: SComponentHelper _helper;
042:
043: /**
044: * Creates a new button group bound to a datastore columm
045: * @param ds The DataStore to bind to
046: * @param col The column to bind to
047: */
048: public SButtonGroup(DataStoreBuffer ds, String col) {
049: _ds = ds;
050: _col = col;
051: _ds.addModelChangedListener(this );
052: _helper = new SComponentHelper(this );
053: }
054:
055: /**
056: * This method is used by the framework and should not be called directly
057: */
058: public ValueChangedEvent generateValueChangedEvent() {
059: try {
060: if (_ds == null)
061: return null;
062: else {
063: String newValue = getText();
064: return new ValueChangedEvent(this , _ds
065: .getFormattedString(_col), newValue, _ds, _ds
066: .getRow(), _ds.getColumnIndex(_col));
067:
068: }
069: } catch (Exception e) {
070: e.printStackTrace();
071: return null;
072: }
073: }
074:
075: /**
076: * Returns the helper for this component.
077: */
078: public SComponentHelper getHelper() {
079: return _helper;
080: }
081:
082: /**
083: * Returns the value of the selected radio button as a string
084: */
085: public String getText() {
086: Enumeration e = getElements();
087: while (e.hasMoreElements()) {
088: AbstractButton b = (AbstractButton) e.nextElement();
089: if (b.isSelected()) {
090: if (b instanceof SRadioButton)
091: return ((SRadioButton) b).getOption().getKey();
092: else if (b instanceof SGroupedToggleButton)
093: return ((SGroupedToggleButton) b).getOption()
094: .getKey();
095: else
096: return b.getActionCommand();
097: }
098: }
099: return null;
100: }
101:
102: /**
103: * Sets the text value for the group and selects the appropriate button
104: */
105: public void setText(String text) {
106: Enumeration e = getElements();
107: boolean found = false;
108: AbstractButton nullOption = null;
109:
110: while (e.hasMoreElements()) {
111: AbstractButton b = (AbstractButton) e.nextElement();
112: String key = null;
113: if (b instanceof SRadioButton)
114: key = ((SRadioButton) b).getOption().getKey();
115: else if (b instanceof SGroupedToggleButton)
116: key = ((SGroupedToggleButton) b).getOption().getKey();
117: else
118: key = b.getActionCommand();
119: if (key == null)
120: nullOption = b;
121: if (SComponentHelper.valuesEqual(key, text)) {
122: b.setSelected(true);
123: found = true;
124: break;
125: }
126: }
127:
128: if ((!found) && nullOption != null)
129: nullOption.setSelected(true);
130: }
131:
132: /**
133: * Part of the model changed listener implementation. Do not call directly
134: */
135: public void modelChanged(ModelChangedEvent evt) {
136: _helper.setDataDirty(true);
137: try {
138: setText(_ds.getFormattedString(_col));
139: } catch (Exception ex) {
140: setText(null);
141: }
142: _helper.setDataDirty(false);
143: }
144:
145: /**
146: * Adds all the buttons in the panel to the group
147: */
148: public void add(JPanel c) {
149: Component comp[] = c.getComponents();
150: if (comp != null) {
151: for (int i = 0; i < comp.length; i++) {
152: if (comp[i] instanceof AbstractButton)
153: add((AbstractButton) comp[i]);
154: }
155: }
156: }
157:
158: /**
159: * Adds a single button to the group
160: */
161: public void add(AbstractButton b) {
162: super .add(b);
163: b.addActionListener(_helper);
164: }
165:
166: /**
167: * This method adds a listener the will be notified when the value in this component changes.
168: * @param l The listener to add.
169: */
170: public void addValueChangedListener(ValueChangedListener l) {
171: _helper.addValueChangedListener(l);
172: }
173:
174: /**
175: * This method removes a listener from the list that will be notified if the text in the component changes.
176: * @param l The listener to remove.
177: */
178: public void removeValueChangedListener(ValueChangedListener l) {
179: _helper.removeValueChangedListener(l);
180: }
181: }
|