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/HtmlRadioButton.java $
024: //$Author: Dan $
025: //$Revision: 18 $
026: //$Modtime: 10/20/03 12:43p $
027: /////////////////////////
028:
029: import java.util.Hashtable;
030:
031: import com.salmonllc.html.events.ValueChangedEvent;
032: import com.salmonllc.properties.Props;
033:
034: /**
035: * This class is used for adding a single radio button to the page. As an alternative to
036: * <a href="com.salmonllc.html.HtmlRadioButtonGroup.html"><code>HtmlRadioButtonGroup</code></a>,
037: * this class is more flexible but more work is required.
038: * You can put radio buttons in any desired layout, interspersed with text and
039: * other controls. To link a set of individual radio buttons (so they will behave as a group),
040: * give each one the same name.
041: */
042: public class HtmlRadioButton extends HtmlFormComponent {
043: public static final int CAPTIONS_ON_LEFT = 0;
044: public static final int CAPTIONS_ON_RIGHT = 1;
045:
046: private String _fontTagStart = null;
047: private String _fontTagEnd = null;
048:
049: private String _key, _display;
050:
051: private int _captionLayout = CAPTIONS_ON_LEFT;
052: private String _onClick = null;
053: private boolean _useProportions = false;
054:
055: private String _imageOn = null;
056: private String _imageOff = null;
057:
058: private String _group = null;
059: private Integer _tabIndex;
060: private String _accessKey;
061:
062: /**
063: * Constructs a new HtmlRadioButton component.
064: * @param name The name of the component
065: * @param p The page the component will be placed in.
066: */
067: public HtmlRadioButton(String name, HtmlPage p, String key,
068: String display) {
069: super (name, p);
070: _key = key;
071: _display = display;
072:
073: Props props = p.getPageProperties();
074: _fontTagStart = props.getProperty(Props.FONT_DEFAULT
075: + Props.TAG_START);
076: _fontTagEnd = props.getProperty(Props.FONT_DEFAULT
077: + Props.TAG_END);
078: _imageOn = props.getProperty(Props.RADIOBUTTON_IMAGE_ON);
079: _imageOff = props.getProperty(Props.RADIOBUTTON_IMAGE_OFF);
080:
081: }
082:
083: public void generateHTML(java.io.PrintWriter p, int rowNo)
084: throws Exception {
085: if (!_visible)
086: return;
087: if ((!getEnabled()) && (!useDisabledAttribute())) {
088: generateHTMLDisabled(p, rowNo);
089: return;
090: }
091:
092: _value = getValue(rowNo, true);
093:
094: String tag = "";
095: if (_captionLayout == CAPTIONS_ON_LEFT)
096: tag += getCaptionTag() + getRadioButtonTag(rowNo, _value);
097: else
098: tag += getRadioButtonTag(rowNo, _value) + getCaptionTag();
099:
100: p.print(tag);
101: writeFocusScript(p, rowNo);
102: }
103:
104: public void generateHTMLDisabled(java.io.PrintWriter p, int rowNo)
105: throws Exception {
106: _value = getValue(rowNo, true);
107: String tag = "";
108:
109: if (_captionLayout == CAPTIONS_ON_LEFT)
110: tag += getDisabledCaptionTag()
111: + getDisabledRadioButtonTag(rowNo, _value);
112: else
113: tag += getDisabledRadioButtonTag(rowNo, _value)
114: + getDisabledCaptionTag();
115:
116: p.print(tag);
117: writeFocusScript(p, rowNo);
118:
119: }
120:
121: /**
122: * Returns the caption layout of the component. Valid Values are CAPTIONS_ON_LEFT, CAPTIONS_ON_RIGHT and CAPTIONS_ON_TOP.
123: */
124: public int getCaptionLayout() {
125: return _captionLayout;
126: }
127:
128: private String getCaptionTag() {
129: if (_display == null)
130: return " ";
131: String fontS = _fontTagStart;
132: if (fontS == null)
133: fontS = "";
134: String fontE = _fontTagEnd;
135: if (fontE == null)
136: fontE = "";
137: return fontS + _display + fontE;
138: }
139:
140: private String getDisabledCaptionTag() {
141: if (_display == null)
142: return " ";
143: String fontS = _disabledFontStartTag;
144: if (fontS == null)
145: fontS = "";
146: String fontE = _disabledFontEndTag;
147: if (fontE == null)
148: fontE = "";
149: return fontS + _display + fontE;
150: }
151:
152: private String getDisabledRadioButtonTag(int rowNo, String val) {
153: if (_key == null)
154: return " ";
155:
156: boolean checked = false;
157: if (val != null) {
158: if (val.equals(_key))
159: checked = true;
160: }
161:
162: if (!_enabled && (_imageOn != null) && (_imageOff != null)) {
163: String out = "<IMG SRC=\"";
164: if (checked)
165: out += _imageOn + "\"";
166: else
167: out += _imageOff + "\"";
168: out += ">";
169: return out;
170: }
171: String name = "";
172: if (_group != null)
173: name = _group;
174: else
175: name = getFullName();
176: if (rowNo > -1)
177: name += "_" + rowNo;
178:
179: String tag = "<INPUT TYPE=\"RADIO\" NAME=\"" + name
180: + "\" VALUE=\"" + _key + "\"";
181:
182: if (_onClick != null)
183: tag += " ONCLICK=\"" + _onClick + "\"";
184: if (checked)
185: tag += " CHECKED";
186: tag += ">";
187:
188: if (_disabledFontStartTag != null)
189: tag = _disabledFontStartTag + tag + _disabledFontEndTag;
190:
191: return tag;
192: }
193:
194: /**
195: * This method gets the end font tag for the component.
196: */
197: public String getFontEndTag() {
198: return _fontTagEnd;
199: }
200:
201: /**
202: * This method gets the start font tag for the component.
203: */
204: public String getFontStartTag() {
205: return _fontTagStart;
206: }
207:
208: /**
209: * This method gets the group Name.
210: */
211: public String getGroup() {
212: return _group;
213: }
214:
215: /**
216: * This method gets the javascript to be executed when the component gets clicked.
217: */
218: public String getOnClick() {
219: return _onClick;
220: }
221:
222: private String getRadioButtonTag(int rowNo, String val) {
223: if (_key == null)
224: return " ";
225:
226: boolean checked = false;
227: if (val != null) {
228: if (val.equals(_key))
229: checked = true;
230: }
231:
232: String name = "";
233: if (_group != null)
234: name = _group;
235: else
236: name = getFullName();
237: if (rowNo > -1)
238: name += "_" + rowNo;
239:
240: String tag = "<INPUT TYPE=\"RADIO\" NAME=\"" + name
241: + "\" VALUE=\"" + _key + "\"";
242:
243: if ((!_enabled) && useDisabledAttribute())
244: tag += " disabled=\"true\"";
245: else if (!_enabled && (_imageOn != null) && (_imageOff != null)) {
246: String out = "<IMG SRC=\"";
247: if (checked)
248: out += _imageOn + "\"";
249: else
250: out += _imageOff + "\"";
251: out += ">";
252: return out;
253: }
254:
255: if (_onClick != null)
256: tag += " ONCLICK=\"" + _onClick + "\"";
257: if (_class != null)
258: tag += " CLASS=\"" + _class + "\"";
259: if (checked)
260: tag += " CHECKED";
261: if (_tabIndex != null)
262: tag += " tabindex=\"" + _tabIndex + "\"";
263: if (_accessKey != null)
264: tag += " accesskey=\"" + _accessKey + "\"";
265:
266: tag += ">";
267:
268: if (_fontTagStart != null)
269: tag = _fontTagStart + tag + _fontTagEnd;
270:
271: return tag;
272: }
273:
274: /**
275: * Returns whether or not to use a fixed percent of available space for each button in the radio button group (Horizontal Orientation only)
276: */
277: public boolean getUseProportions() {
278: return _useProportions;
279: }
280:
281: public boolean processParms(Hashtable parms, int rowNo)
282: throws Exception {
283: if (!getEnabled())
284: return false;
285: Object oldValue = _value;
286: String name = "";
287: if (_group != null)
288: name = _group;
289: else
290: name = getFullName();
291: if (rowNo > -1) {
292: name += "_" + rowNo;
293: if (_dsBuff != null)
294: oldValue = _dsBuff.getAny(rowNo, _dsColNo);
295: } else {
296: if (_dsBuff != null)
297: oldValue = _dsBuff.getAny(_dsColNo);
298: }
299:
300: String val[] = (String[]) parms.get(name);
301:
302: if (val == null)
303: _value = null;
304: else
305: _value = val[0];
306:
307: if (!valuesEqual(oldValue, _value) && _value != null) {
308: String s = null;
309: if (oldValue != null)
310: s = oldValue.toString();
311: ValueChangedEvent e = new ValueChangedEvent(getPage(),
312: this , getName(), getFullName(), s, _value, rowNo,
313: _dsColNo, _dsBuff);
314: addEvent(e);
315: }
316:
317: return false;
318: }
319:
320: /**
321: * Sets the caption layout of the component. Valid Values are CAPTIONS_ON_LEFT, CAPTIONS_ON_RIGHT and CAPTIONS_ON_TOP.
322: */
323: public void setCaptionLayout(int layout) {
324: _captionLayout = layout;
325: }
326:
327: /**
328: * This method sets the layout property for the component
329: */
330: public void setCaptionLayout(String layout) {
331: setCaptionLayout(layout == null
332: || !layout.toUpperCase().equals("RIGHT") ? CAPTIONS_ON_LEFT
333: : CAPTIONS_ON_RIGHT);
334: }
335:
336: /**
337: * This method sets the end font tag for the component.
338: */
339: public void setFontEndTag(String value) {
340: _fontTagEnd = value;
341: }
342:
343: /**
344: * This method sets the start font tag for the component.
345: */
346: public void setFontStartTag(String value) {
347: _fontTagStart = value;
348: }
349:
350: /**
351: * This method sets the group.
352: */
353: public void setGroup(String value) {
354: _group = value;
355: }
356:
357: /**
358: * This method sets the javascript to be executed when the component is checked.
359: */
360: public void setOnClick(String value) {
361: _onClick = value;
362: }
363:
364: /**
365: * This method is used to set whether the each button in the group will occupy a fixed percent available space (Horizontal Orientation only)
366: */
367: public void setUseProportions(boolean use) {
368: _useProportions = use;
369: }
370:
371: /**
372: * @returns the access key html attribute
373: */
374: public String getAccessKey() {
375: return _accessKey;
376: }
377:
378: /**
379: * @returns the tab index html attribute
380: */
381: public int getTabIndex() {
382: if (_tabIndex == null)
383: return -1;
384: return _tabIndex.intValue();
385: }
386:
387: /**
388: * @param sets the access key html attribute
389: */
390: public void setAccessKey(String string) {
391: _accessKey = string;
392: }
393:
394: /**
395: * @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
396: */
397: public void setTabIndex(int val) {
398: if (val == -1)
399: _tabIndex = null;
400: else
401: _tabIndex = new Integer(val);
402: }
403: }
|