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/HtmlCheckBox.java $
024: //$Author: Dan $
025: //$Revision: 19 $
026: //$Modtime: 10/17/03 1:05p $
027: /////////////////////////
028:
029: import com.salmonllc.html.events.*;
030: import com.salmonllc.properties.*;
031: import java.util.*;
032:
033: /**
034: * This class is used to place a check box component on a page.
035: */
036: public class HtmlCheckBox extends HtmlFormComponent {
037: private String _fontTagStart;
038: private String _fontTagEnd;
039: private String _onClick;
040: private String _trueValue;
041: private String _falseValue;
042: private String _imageOn;
043: private String _imageOff;
044: private Integer _tabIndex;
045: private String _accessKey;
046:
047: /**
048: * Constructs a new HTMLTextEdit component.
049: * @param name The name of the component
050: * @param p The page the component will be placed in.
051: * @param trueValue A string representation that will be returned from getValue() when the checkbox is checked.
052: * @param falseValue A string representation that will be returned from getValue() when the checkbox is not checked.
053: */
054: public HtmlCheckBox(String name, com.salmonllc.html.HtmlPage p,
055: String trueValue, String falseValue) {
056: this (name, null, p, trueValue, falseValue);
057:
058: }
059:
060: /**
061: * Constructs a new HTMLTextEdit component.
062: * @param name The name of the component
063: * @param theme The theme to use for loading properties.
064: * @param p The page the component will be placed in.
065: * @param trueValue A string representation that will be returned from getValue() when the checkbox is checked.
066: * @param falseValue A string representation that will be returned from getValue() when the checkbox is not checked.
067: */
068: public HtmlCheckBox(String name, String theme,
069: com.salmonllc.html.HtmlPage p, String trueValue,
070: String falseValue) {
071: super (name, theme, p);
072:
073: _trueValue = trueValue;
074: _falseValue = falseValue;
075:
076: }
077:
078: public void generateHTML(java.io.PrintWriter p, int rowNo)
079: throws Exception {
080: if (!_visible)
081: return;
082:
083: String value = getValue(rowNo, true);
084:
085: boolean checked = false;
086: if (value == null && _trueValue == null)
087: checked = true;
088: else if (value != null)
089: if (value.equals(_trueValue))
090: checked = true;
091:
092: String name = getFullName();
093: if (rowNo > -1)
094: name += "_" + rowNo;
095:
096: String tag = "<INPUT TYPE=\"CHECKBOX\" NAME=\"" + name + "\"";
097:
098: if ((!_enabled) && useDisabledAttribute())
099: tag += " disabled=\"" + true + "\"";
100: else if (!_enabled && (_imageOn != null) && (_imageOff != null)) {
101: String out = "<IMG SRC=\"";
102: if (checked)
103: out += _imageOn;
104: else
105: out += _imageOff;
106: out += "\">";
107: if (_fontTagStart != null)
108: out = _fontTagStart + out + _fontTagEnd;
109: p.print(out);
110: return;
111: }
112:
113: if (checked)
114: tag += " CHECKED";
115:
116: if (_onClick != null)
117: tag += " onClick=\"" + _onClick + "\"";
118:
119: if (_class != null)
120: tag += " class=\"" + _class + "\"";
121:
122: if (_tabIndex != null)
123: tag += " tabindex=\"" + _tabIndex + "\"";
124:
125: if (_accessKey != null)
126: tag += " accesskey=\"" + _accessKey + "\"";
127:
128: tag += ">";
129:
130: if (_fontTagStart != null)
131: tag = _fontTagStart + tag + _fontTagEnd;
132:
133: p.println(tag);
134: writeFocusScript(p, rowNo);
135: }
136:
137: /**
138: * This method returns the the String representation that will be returned from getValue() when the checkbox is not checked.
139: */
140: public String getFalseValue() {
141: return _falseValue;
142: }
143:
144: /**
145: * This method gets the end font tag for the component.
146: */
147: public String getFontEndTag() {
148: return _fontTagEnd;
149: }
150:
151: /**
152: * This method gets the start font tag for the component.
153: */
154: public String getFontStartTag() {
155: return _fontTagStart;
156: }
157:
158: /**
159: * This method gets the javascript to be executed when the component gets clicked.
160: */
161: public String getOnClick() {
162: return _onClick;
163: }
164:
165: /**
166: * This method returns the the String representation that will be returned from getValue() when the checkbox is checked.
167: */
168: public String getTrueValue() {
169: return _trueValue;
170: }
171:
172: public boolean processParms(Hashtable parms, int rowNo)
173: throws Exception {
174: // fc: 10/18/02 Commented out the below lines as they are no longer required,
175: // since a better approach is to check to see if the field is contained in
176: // the form when submitted. see other change below
177: // if (!getVisible() || !getEnabled())
178: // return false;
179:
180: Object oldValue = _value;
181:
182: String name = getFullName();
183: if (rowNo > -1) {
184: name += "_" + rowNo;
185: if (_dsBuff != null) {
186: try {
187: oldValue = _dsBuff.getAny(rowNo, _dsColNo);
188: } catch (Exception e) {
189: oldValue = null;
190: }
191: }
192: } else {
193: if (_dsBuff != null) {
194: try {
195: oldValue = _dsBuff.getAny(_dsColNo);
196: } catch (Exception e) {
197: oldValue = null;
198: }
199: }
200: }
201:
202: //fc: 10/18/02 Added check to see if component exists in form if not
203: // then do not process. For Checkboxes if Component is not checked
204: // then parms does not contain the key. If checked it contains the key.
205: // Similarly if the Component does not exist on the form it will not be
206: // in the parms. This is why the check for getVisible & getEnabled is done.
207: if (parms.containsKey(name))
208: _value = _trueValue;
209: else if (getVisible() && getEnabled())
210: _value = _falseValue;
211: else
212: _value = oldValue == null ? null : oldValue.toString();
213:
214: if (!valuesEqual(oldValue, _value)) {
215: String s = null;
216: if (oldValue != null)
217: s = oldValue.toString();
218: ValueChangedEvent e = new ValueChangedEvent(getPage(),
219: this , getName(), getFullName(), s, _value, rowNo,
220: _dsColNo, _dsBuff);
221: addEvent(e);
222: }
223: return false;
224: }
225:
226: /**
227: * This method sets the the String representation that will be returned from getValue() when the checkbox is not checked.
228: */
229: public void setFalseValue(String falseValue) {
230: _falseValue = falseValue;
231: }
232:
233: /**
234: * This method sets the end font tag for the component.
235: */
236: public void setFontEndTag(String value) {
237: _fontTagEnd = value;
238: }
239:
240: /**
241: * This method sets the start font tag for the component.
242: */
243: public void setFontStartTag(String value) {
244: _fontTagStart = value;
245: }
246:
247: /**
248: * This method sets the javascript to be executed when the component is checked.
249: */
250: public void setOnClick(String value) {
251: _onClick = value;
252: }
253:
254: /**
255: * This method sets the property theme for the component.
256: * @param theme The theme to use.
257: */
258: public void setTheme(String theme) {
259:
260: super .setTheme(theme);
261:
262: Props props = getPage().getPageProperties();
263:
264: _fontTagStart = props.getThemeProperty(theme,
265: Props.FONT_TEXT_EDIT + Props.TAG_START);
266: _fontTagEnd = props.getThemeProperty(theme,
267: Props.FONT_TEXT_EDIT + Props.TAG_END);
268: _imageOn = props.getThemeProperty(theme,
269: Props.CHECKBOX_IMAGE_ON);
270: _imageOff = props.getThemeProperty(theme,
271: Props.CHECKBOX_IMAGE_OFF);
272:
273: }
274:
275: /**
276: * This method sets the the String representation that will be returned from getValue() when the checkbox is checked.
277: */
278: public void setTrueValue(String trueValue) {
279: _trueValue = trueValue;
280: }
281:
282: /**
283: * @returns the access key html attribute
284: */
285: public String getAccessKey() {
286: return _accessKey;
287: }
288:
289: /**
290: * @returns the tab index html attribute
291: */
292: public int getTabIndex() {
293: if (_tabIndex == null)
294: return -1;
295: return _tabIndex.intValue();
296: }
297:
298: /**
299: * @param sets the access key html attribute
300: */
301: public void setAccessKey(String string) {
302: _accessKey = string;
303: }
304:
305: /**
306: * @param sets the tab index html attribute. You can also pass TAB_INDEX_DEFAULT to use the default tab index for the component or TAB_INDEX_NONE to keep this component from being tabbed to
307: */
308: public void setTabIndex(int val) {
309: if (val == -1)
310: _tabIndex = null;
311: else
312: _tabIndex = new Integer(val);
313: }
314: }
|