001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.swing;
021:
022: import com.salmonllc.swing.events.ValueChangedEvent;
023: import com.salmonllc.swing.events.ValueChangedListener;
024: import com.salmonllc.sql.DataStoreBuffer;
025: import com.salmonllc.sql.ModelChangedEvent;
026: import com.salmonllc.sql.ModelChangedListener;
027:
028: import javax.swing.*;
029:
030: /**
031: * SOFIA implementation of a toggle button. This toggle button box can be bound to a DataStore column.
032: */
033: public class SToggleButton extends JToggleButton implements SComponent,
034: ModelChangedListener {
035: SComponentHelper _helper;
036: String _trueValue = "1", _falseValue = "0";
037: private String _dsCol;
038: private DataStoreBuffer _ds;
039:
040: /**
041: * Creates an initially unselected toggle button button with no text, no icon.
042: */
043: public SToggleButton() {
044: _helper = new SComponentHelper(this );
045: }
046:
047: /**
048: * Creates an initially unselected toggle button with an icon.
049: *
050: * @param icon the Icon image to display
051: */
052: public SToggleButton(Icon icon) {
053: super (icon);
054: _helper = new SComponentHelper(this );
055: if (icon instanceof SIcon)
056: ((SIcon) icon).setParent(this );
057:
058: }
059:
060: /**
061: * Creates a toggle button with an icon and specifies whether
062: * or not it is initially selected.
063: *
064: * @param icon the Icon image to display
065: * @param selected a boolean value indicating the initial selection
066: * state. If <code>true</code> the toggle button is selected
067: */
068: public SToggleButton(Icon icon, boolean selected) {
069: super (icon, selected);
070: _helper = new SComponentHelper(this );
071: if (icon instanceof SIcon)
072: ((SIcon) icon).setParent(this );
073:
074: }
075:
076: /**
077: * Creates an initially unselected toggle button with text.
078: *
079: * @param text the text of the toggle button.
080: */
081: public SToggleButton(String text) {
082: super (text);
083: _helper = new SComponentHelper(this );
084: }
085:
086: /**
087: * Creates an initially unselected toggle button with
088: * the specified text and icon.
089: *
090: * @param text the text of the toggle button.
091: * @param icon the Icon image to display
092: */
093: public SToggleButton(String text, Icon icon) {
094: super (text, icon);
095: _helper = new SComponentHelper(this );
096: if (icon instanceof SIcon)
097: ((SIcon) icon).setParent(this );
098:
099: }
100:
101: /**
102: * Creates a toggle button with text and icon,
103: * and specifies whether or not it is initially selected.
104: *
105: * @param text the text of the toggle button.
106: * @param icon the Icon image to display
107: * @param selected a boolean value indicating the initial selection
108: * state. If <code>true</code> the toggle button is selected
109: */
110: public SToggleButton(String text, Icon icon, boolean selected) {
111: super (text, icon, selected);
112: _helper = new SComponentHelper(this );
113: }
114:
115: /**
116: * Creates a toggle button with text and specifies whether
117: * or not it is initially selected.
118: *
119: * @param text the text of the toggle button.
120: * @param selected a boolean value indicating the initial selection
121: * state. If <code>true</code> the toggle button is selected
122: */
123: public SToggleButton(String text, boolean selected) {
124: super (text, selected);
125: _helper = new SComponentHelper(this );
126: }
127:
128: /**
129: * This method is used by the framework and should not be called directly
130: */
131: public ValueChangedEvent generateValueChangedEvent() {
132: try {
133: if (_ds == null)
134: return null;
135: else {
136: String newValue = isSelected() ? _trueValue
137: : _falseValue;
138: return new ValueChangedEvent(this , _ds
139: .getFormattedString(_dsCol), newValue, _ds, _ds
140: .getRow(), _ds.getColumnIndex(_dsCol));
141:
142: }
143: } catch (Exception e) {
144: e.printStackTrace();
145: return null;
146: }
147: }
148:
149: /**
150: * This method is used by the framework and should not be called directly
151: */
152:
153: public SComponentHelper getHelper() {
154: return _helper;
155: }
156:
157: /**
158: * This method adds a listener the will be notified when the value in this component changes.
159: * @param l The listener to add.
160: */
161: public void addValueChangedListener(ValueChangedListener l) {
162: _helper.addValueChangedListener(l);
163: }
164:
165: /**
166: * This method removes a listener from the list that will be notified if the text in the component changes.
167: * @param l The listener to remove.
168: */
169: public void removeValueChangedListener(ValueChangedListener l) {
170: _helper.removeValueChangedListener(l);
171: }
172:
173: /**
174: * Binds the component to a DataStore column
175: * @param dsb The DataStore to bind to
176: * @param column The column to bind to
177: */
178: public void setColumn(DataStoreBuffer dsb, String column) {
179: _ds = dsb;
180: _dsCol = column;
181: _ds.addModelChangedListener(this );
182: evalColumn();
183: }
184:
185: /**
186: * Binds the component to a DataStore column
187: * @param dsb The DataStore to bind to
188: * @param column The column to bind to
189: * @param trueValue The value that represents a true state for the checkbox
190: * @param falseValue The value that represents a false state for the checkbox
191: */
192: public void setColumn(DataStoreBuffer dsb, String column,
193: String trueValue, String falseValue) {
194: setColumn(dsb, column);
195: _trueValue = trueValue;
196: _falseValue = falseValue;
197: evalColumn();
198: }
199:
200: /**
201: * @see ModelChangedListener#modelChanged(ModelChangedEvent)
202: */
203: public void modelChanged(ModelChangedEvent evt) {
204: evalColumn();
205: }
206:
207: private void evalColumn() {
208: try {
209: if (_ds != null && _dsCol != null) {
210: _helper.setDataDirty(true);
211: String value = _ds.getFormattedString(_dsCol);
212: boolean selected = false;
213: if (value != null)
214: if (value.equals(_trueValue))
215: selected = true;
216: if (selected != isSelected())
217: setSelected(selected);
218: _helper.setDataDirty(false);
219: }
220: } catch (Exception e) {
221: setSelected(false);
222: }
223: }
224:
225: /**
226: * Returns the value representing an unpressed button
227: */
228: public String getFalseValue() {
229: return _falseValue;
230: }
231:
232: /**
233: * Sets the value representing an unpressed button
234: */
235:
236: public void setFalseValue(String falseValue) {
237: _falseValue = falseValue;
238: }
239:
240: /**
241: * Returns the value representing a pressed button
242: */
243:
244: public String getTrueValue() {
245: return _trueValue;
246: }
247:
248: /**
249: * Sets the value representing a pressed button
250: */
251:
252: public void setTrueValue(String trueValue) {
253: _trueValue = trueValue;
254: }
255:
256: }
|