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.html;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/html/HtmlRowSelector.java $
024: //$Author: Dan $
025: //$Revision: 16 $
026: //$Modtime: 6/11/03 4:39p $
027: /////////////////////////
028:
029: import com.salmonllc.html.events.ValueChangedEvent;
030: import com.salmonllc.properties.Props;
031: import com.salmonllc.sql.DataStoreBuffer;
032:
033: /**
034: * This type can be used to add a row selector to an html table. The row selector is either radio buttons that allows the selection of one row or check boxes that allow multiple selections.
035: */
036: public class HtmlRowSelector extends HtmlFormComponent {
037: private boolean _selectMultiple = false;
038: private String _selectedValue = "1";
039: private String _unSelectedValue = "0";
040: private HtmlCheckBox _cbx;
041: private String _imageOn, _imageOff;
042: private String _onClick;
043:
044: /**
045: * Constructs a Html row selector object for an HtmlDataTable
046: */
047: public HtmlRowSelector(String name, DataStoreBuffer ds,
048: String dataStoreColumn, HtmlPage p) {
049: this (name, ds, dataStoreColumn, null, p);
050: }
051:
052: /**
053: * Constructs a Html row selector object for an HtmlDataTable
054: */
055: public HtmlRowSelector(String name, DataStoreBuffer ds,
056: String dataStoreColumn, String theme, HtmlPage p) {
057: super (name, p);
058: setColumn(ds, dataStoreColumn);
059: setTheme(theme);
060: }
061:
062: public boolean executeEvent(int eventType) throws Exception {
063: if (_cbx != null)
064: return _cbx.executeEvent(eventType);
065: else {
066: if (_events != null)
067: if (_events.size() > 0 && _dsBuff != null)
068: for (int i = 0; i < _dsBuff.getRowCount(); i++)
069: _dsBuff.setAny(i, _dsColNo, _unSelectedValue);
070:
071: return super .executeEvent(eventType);
072: }
073: }
074:
075: public void generateHTML(java.io.PrintWriter p, int rowNo)
076: throws Exception {
077:
078: if (_cbx != null)
079: _cbx.generateHTML(p, rowNo);
080: else {
081: boolean checked = false;
082: _value = getValue(rowNo);
083:
084: if (_value != null) {
085: if (_value.equals(_selectedValue))
086: checked = true;
087: }
088:
089: if (!getEnabled() && (_imageOn != null)
090: && (_imageOff != null)) {
091: String out = "<IMG SRC=\"";
092: if (checked)
093: out += _imageOn + "\"";
094: else
095: out += _imageOff + "\"";
096: out += ">";
097: p.println(out);
098: return;
099: }
100:
101: String name = getFullName();
102: String tag = "<INPUT TYPE=\"RADIO\" NAME=\"" + name
103: + "\" VALUE=\"" + rowNo + "\"";
104:
105: if (_onClick != null && !_onClick.trim().equals(""))
106: tag += " ONCLICK=\"" + _onClick + "\"";
107: if (checked)
108: tag += " CHECKED";
109:
110: tag += ">";
111:
112: p.println(tag);
113: }
114: }
115:
116: /**
117: * This method returns whether the component can select multiple rows or not.
118: */
119: public boolean getMultiple() {
120: return _selectMultiple;
121: }
122:
123: /**
124: * This method gets the javascript to be executed when the component gets clicked.
125: */
126: public String getOnClick() {
127: return _onClick;
128: }
129:
130: /**
131: * This method gets the value to use if the row is selected.
132: */
133: public String getSelectedValue() {
134: return _selectedValue;
135: }
136:
137: /**
138: * This method gets the value to use if the row is not selected.
139: */
140: public String getUnSelectedValue() {
141: return _unSelectedValue;
142: }
143:
144: public boolean processParms(java.util.Hashtable parms, int row)
145: throws Exception {
146: if (_cbx != null)
147: return _cbx.processParms(parms, row);
148: else {
149: if (!getEnabled())
150: return false;
151: Object oldValue = _value;
152:
153: String name = getFullName();
154: if (row > -1) {
155: if (_dsBuff != null)
156: oldValue = _dsBuff.getAny(row, _dsColNo);
157: } else {
158: if (_dsBuff != null)
159: oldValue = _dsBuff.getAny(_dsColNo);
160: }
161:
162: String val[] = (String[]) parms.get(name);
163:
164: int selectedRow = -1;
165: if (val != null)
166: selectedRow = Integer.parseInt(val[0]);
167:
168: if (selectedRow == row)
169: _value = _selectedValue;
170: else
171: _value = _unSelectedValue;
172:
173: if (!valuesEqual(oldValue, _value)
174: && _value.equals(_selectedValue)) {
175: String s = null;
176: if (oldValue != null)
177: s = oldValue.toString();
178: ValueChangedEvent e = new ValueChangedEvent(getPage(),
179: this , getName(), getFullName(), s, _value, row,
180: _dsColNo, _dsBuff);
181: addEvent(e);
182: }
183:
184: return false;
185: }
186: }
187:
188: /**
189: * This method sets whether the component can select multiple rows or not.
190: */
191: public void setMultiple(boolean multiple) {
192: if (multiple) {
193: _cbx = new HtmlCheckBox(getName(), getPage(),
194: _selectedValue, _unSelectedValue);
195: _cbx.setColumn(_dsBuff, _dsColNo);
196: } else {
197: _cbx = null;
198: }
199:
200: _selectMultiple = multiple;
201: }
202:
203: /**
204: * This method sets the javascript to be executed when the component is checked.
205: */
206: public void setOnClick(String value) {
207: _onClick = value;
208: }
209:
210: /**
211: * This method sets the value to use if the row is selected.
212: */
213: public void setSelectedValue(String value) {
214: _selectedValue = value;
215: }
216:
217: /**
218: * This method sets the property theme for the component.
219: * @param theme The theme to use.
220: */
221: public void setTheme(String theme) {
222:
223: super .setTheme(theme);
224:
225: Props props = getPage().getPageProperties();
226: _imageOn = props.getThemeProperty(theme,
227: Props.RADIOBUTTON_IMAGE_ON);
228: _imageOff = props.getThemeProperty(theme,
229: Props.RADIOBUTTON_IMAGE_OFF);
230:
231: }
232:
233: /**
234: * This method sets the value to use if the row is not selected.
235: */
236: public void setUnSelectedValue(String value) {
237: _unSelectedValue = value;
238: }
239: }
|