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/HtmlMultiListBox.java $
024: //$Author: Dan $
025: //$Revision: 14 $
026: //$Modtime: 6/11/03 4:39p $
027: /////////////////////////
028:
029: import java.util.Hashtable;
030: import java.util.Vector;
031:
032: import com.salmonllc.html.events.ValueChangedEvent;
033: import com.salmonllc.properties.Props;
034: import com.salmonllc.util.FourObjectContainer;
035:
036: /**
037: * This class is used to allow for the selection of items from multiple lists. The items in the second list and subsequent lists are dependant on the selected item in the first or prior lists.
038: * The class can be used to allow the user to select one item from a tree or items
039: */
040: public class HtmlMultiListBox extends HtmlFormComponent {
041: private String _fontTagStart;
042: private String _fontTagEnd;
043: private int _size = 5;
044:
045: private Vector _options = new Vector();
046:
047: /**
048: * Constructs a new HTMLMultiListBox component.
049: * @param name The name of the component
050: * @param p The page the component will be placed in.
051: */
052: public HtmlMultiListBox(String name, com.salmonllc.html.HtmlPage p) {
053: this (name, null, p);
054: }
055:
056: /**
057: * Constructs a new HTMLMultiListBox component.
058: * @param name The name of the component
059: * @param theme The theme to use for loading properties.
060: * @param p The page the component will be placed in.
061: */
062: public HtmlMultiListBox(String name, String theme,
063: com.salmonllc.html.HtmlPage p) {
064: super (name, theme, p);
065: }
066:
067: public void generateHTML(java.io.PrintWriter p, int rowNo)
068: throws Exception {
069: if (!_visible)
070: return;
071: String name = getFullName();
072: if (rowNo > -1)
073: name += "_" + rowNo;
074: String value = getValue(rowNo, true);
075: if (_dsBuff != null) {
076: if (value == null)
077: setInternalValue(null);
078: else
079: setInternalValue(value.toString());
080: }
081:
082: p.println("<TABLE BORDER=\"0\"><TR>");
083: int parentHandle = -1;
084: for (int i = 0; i < _numberOfBoxes; i++) {
085: p.println("<TD>");
086: parentHandle = generateListBoxHtml(p, rowNo, i,
087: _numberOfBoxes - 1, parentHandle);
088: p.println("</TD>");
089: }
090: p.println("</TR></TABLE>");
091:
092: p.println("<SCRIPT Language=\"JavaScript\">");
093: if (_treeArray == null && _options.size() > 0) {
094: _treeArray = new StringBuffer();
095: _treeArray.append("var " + getFullName()
096: + "items=new Array(" + _root.getCount() * 3
097: + ");\n");
098: String prefix = getFullName() + "items";
099: appendToTree(_root, prefix);
100: }
101: p.println(_treeArray.toString());
102:
103: p.println("function clearBox(oBox){");
104: p.println(" var i;");
105: p
106: .println(" for (i = oBox.options.length - 1; i >= 0; i--) oBox.options[i] = null;");
107: p.println(" oBox.selectedIndex = -1;");
108: p.println("}");
109:
110: p.println("function populateBox(oBox, aArray){");
111: p.println(" clearBox(oBox);");
112: p.println(" if (aArray != null) {");
113: p.println(" for (var i = 0; i < aArray.length; i = i + 3)");
114: p
115: .println(" oBox.options[oBox.options.length] = new Option(aArray[i + 2], aArray[i + 1]);");
116: p.println(" }");
117: p.println("}");
118:
119: String tname = getFullName();
120: if (rowNo > -1)
121: tname += "_" + rowNo;
122: tname += "_";
123: for (int i = 0; i < _numberOfBoxes; i++)
124: writeShowHideJavaScript(p, tname, i);
125:
126: p.println("</SCRIPT>");
127:
128: }
129:
130: /**
131: * This method gets the end font tag for the component.
132: */
133: public String getFontEndTag() {
134: return _fontTagEnd;
135: }
136:
137: /**
138: * This method gets the start font tag for the component.
139: */
140: public String getFontStartTag() {
141: return _fontTagStart;
142: }
143:
144: /**
145: * This method returns the number of options in the component.
146: */
147: public int getOptionCount() {
148: return _options.size();
149: }
150:
151: /**
152: * Use this method get the value of the key at index.
153: */
154: public String getOptionKey(int handle) {
155: if (handle < 0 || handle >= _options.size())
156: return null;
157:
158: FourObjectContainer f = (FourObjectContainer) _options
159: .elementAt(handle);
160: return (String) f.getObject1();
161: }
162:
163: /**
164: * Use this method to find out if an option is selected.
165: */
166: public boolean getOptionSelected(int handle) {
167: if (handle < 0 || handle >= _options.size())
168: return false;
169:
170: FourObjectContainer f = (FourObjectContainer) _options
171: .elementAt(handle);
172: return ((Boolean) f.getObject3()).booleanValue();
173: }
174:
175: /**
176: * Use this method get the value of the option at index.
177: */
178: public String getOptionValue(int handle) {
179: if (handle < 0 || handle >= _options.size())
180: return null;
181:
182: FourObjectContainer f = (FourObjectContainer) _options
183: .elementAt(handle);
184: return (String) f.getObject2();
185: }
186:
187: /**
188: * This method gets the display size for the component in characters.
189: */
190: public int getSize() {
191: return _size;
192: }
193:
194: public boolean processParms(Hashtable parms, int rowNo)
195: throws Exception {
196: Object oldValue = _value;
197: String name = getFullName();
198: if (rowNo > -1) {
199: name += "_" + rowNo;
200: if (_dsBuff != null)
201: oldValue = _dsBuff.getAny(rowNo, _dsColNo);
202: } else {
203: if (_dsBuff != null)
204: oldValue = _dsBuff.getAny(_dsColNo);
205: }
206:
207: int optionsSize = _options.size();
208: FourObjectContainer f = null;
209: for (int i = 0; i < optionsSize; i++) {
210: f = (FourObjectContainer) _options.elementAt(i);
211: f.setObject3(new Boolean(false));
212: }
213:
214: _value = null;
215: for (int i = 0; i < _numberOfBoxes; i++) {
216: String val[] = (String[]) parms.get(name + "_" + i);
217: if (val != null) {
218: int index = -1;
219: for (int j = 0; j < val.length; j++) {
220: String item = val[j].substring(val[j]
221: .lastIndexOf("_") + 1);
222: if (!item.equals("X")) {
223: index = Integer.parseInt(item);
224: f = (FourObjectContainer) _options
225: .elementAt(index);
226: f.setObject3(new Boolean(true));
227: _value = (String) f.getObject1();
228: _lastVisibleBox = i + 1;
229: }
230: }
231: }
232: }
233:
234: if (!valuesEqual(oldValue, _value)) {
235: String s = null;
236: if (oldValue != null)
237: s = oldValue.toString();
238: ValueChangedEvent e = new ValueChangedEvent(getPage(),
239: this , getName(), getFullName(), s, _value, rowNo,
240: _dsColNo, _dsBuff);
241: addEvent(e);
242: }
243: return false;
244: }
245:
246: /**
247: * This method removes all options from the component.
248: */
249: public void resetOptions() {
250: _options.removeAllElements();
251: _treeArray = null;
252: _root = new TreeNode();
253: _lastVisibleBox = 0;
254: }
255:
256: /**
257: * This method sets the end font tag for the component.
258: */
259: public void setFontEndTag(String value) {
260: _fontTagEnd = value;
261: }
262:
263: /**
264: * This method sets the start font tag for the component.
265: */
266: public void setFontStartTag(String value) {
267: _fontTagStart = value;
268: }
269:
270: /**
271: * This method sets the first selected option in the list box to the one with the key.
272: */
273: private void setInternalValue(String value) {
274: Boolean trueVal = new Boolean(true);
275: Boolean falseVal = new Boolean(false);
276: //
277: int optionsSize = _options.size();
278: FourObjectContainer f = null;
279: String key = null;
280: Boolean selected = null;
281: //
282: for (int i = 0; i < optionsSize; i++) {
283: f = (FourObjectContainer) _options.elementAt(i);
284: key = (String) f.getObject1();
285: selected = falseVal;
286: if (key != null)
287: if (key.equals(value)) {
288: selected = trueVal;
289: }
290: f.setObject3(selected);
291: }
292: }
293:
294: /**
295: * Use this method set whether an option is selected.
296: */
297: public void setOptionSelected(int handle, boolean selected) {
298: if (handle < 0 || handle >= _options.size())
299: return;
300:
301: FourObjectContainer f = (FourObjectContainer) _options
302: .elementAt(handle);
303: f.setObject3(new Boolean(selected));
304: }
305:
306: /**
307: * This method sets the display size for the component in characters.
308: */
309: public void setSize(int size) {
310: _size = size;
311: }
312:
313: /**
314: * This method sets the property theme for the component.
315: * @param theme The theme to use.
316: */
317: public void setTheme(String theme) {
318:
319: super .setTheme(theme);
320: Props props = getPage().getPageProperties();
321: _fontTagStart = props.getThemeProperty(theme, Props.FONT_DDLB
322: + Props.TAG_START);
323: _fontTagEnd = props.getThemeProperty(theme, Props.FONT_DDLB
324: + Props.TAG_END);
325: }
326:
327: private int _lastVisibleBox = 0;
328: private int _numberOfBoxes = 3;
329: private TreeNode _root = new TreeNode();
330: StringBuffer _treeArray = null;
331:
332: private class TreeNode {
333: Vector _children = new Vector();
334:
335: void addNode(int handle) {
336: _children.addElement(new Integer(handle));
337: }
338:
339: int getHandle(int index) {
340: if (index >= 0 && index < _children.size())
341: return ((Integer) _children.elementAt(index))
342: .intValue();
343: else
344: return -1;
345: }
346:
347: void removeNode(int index) {
348: if (index >= 0 && index < _children.size())
349: _children.removeElementAt(index);
350: }
351:
352: int getCount() {
353: return _children.size();
354: }
355: }
356:
357: /**
358: * Use this method to add new choices to the list.
359: * @param parentHandle The items parent handle
360: * @param key The internal name of the item (must be unique)
361: * @param disp The value to be displayed on the list.
362: * @return the handle of the item to return;
363: */
364: public int addOption(int parentHandle, String key, String disp) {
365: return addOption(parentHandle, key, disp, false);
366: }
367:
368: /**
369: * Use this method to add new choices to the list.
370: * @param parentHandle The items parent handle
371: * @param key The internal name of the item (must be unique)
372: * @param disp The value to be displayed on the list.
373: * @param selected Whether or not the item is selected.
374: * @return the handle of the item to return;
375: */
376: public int addOption(int parentHandle, String key, String disp,
377: boolean selected) {
378: TreeNode this Node = new TreeNode();
379: FourObjectContainer f = new FourObjectContainer(key, disp,
380: new Boolean(selected), this Node);
381: _options.addElement(f);
382: int handle = _options.size() - 1;
383: if (parentHandle != -1) {
384: f = (FourObjectContainer) _options.elementAt(parentHandle);
385: TreeNode n = (TreeNode) f.getObject4();
386: n.addNode(handle);
387: } else {
388: _root.addNode(handle);
389: }
390: _treeArray = null;
391: return handle;
392:
393: }
394:
395: /**
396: * Use this method to add a new option to the first list box.
397: * @param key The internal name of the item (must be unique)
398: * @param disp The value to be displayed on the list.
399: * @return the handle of the item (this can be used later to add children to this node).
400: */
401: public int addOption(String key, String disp) {
402: return addOption(-1, key, disp, false);
403: }
404:
405: private void appendToTree(TreeNode n, String prefix) {
406:
407: int index = 0;
408: for (int i = 0; i < n.getCount(); i++) {
409: StringBuffer newPrefix = new StringBuffer(prefix);
410:
411: newPrefix.append("[");
412: newPrefix.append(index++);
413: newPrefix.append("]");
414:
415: int handle = n.getHandle(i);
416:
417: if (handle > -1) {
418: _treeArray.append(newPrefix);
419: _treeArray.append("= new Array();\n");
420:
421: _treeArray.append(prefix);
422: _treeArray.append("[");
423: _treeArray.append(index++);
424: _treeArray.append("]");
425: _treeArray.append("='");
426: _treeArray.append(getFullName());
427: _treeArray.append("_");
428: _treeArray.append(handle);
429: _treeArray.append("';\n");
430:
431: _treeArray.append(prefix);
432: _treeArray.append("[");
433: _treeArray.append(index++);
434: _treeArray.append("]");
435: _treeArray.append("='");
436: _treeArray
437: .append((String) ((FourObjectContainer) _options
438: .elementAt(handle)).getObject2());
439: _treeArray.append("';\n");
440:
441: TreeNode t = (TreeNode) ((FourObjectContainer) _options
442: .elementAt(handle)).getObject4();
443: appendToTree(t, newPrefix.toString());
444: }
445: }
446: }
447:
448: /**
449: * This method returns the index of the option with the specified key.
450: * @return The index of the key or -1 if not found.
451: */
452: public int findOptionHandleOf(String key) {
453: int optionsSize = _options.size();
454: FourObjectContainer f = null;
455: for (int i = 0; i < optionsSize; i++) {
456: f = (FourObjectContainer) _options.elementAt(i);
457: if (key.equals(f.getObject1()))
458: return i;
459: }
460: return -1;
461: }
462:
463: private int generateListBoxHtml(java.io.PrintWriter p, int rowNo,
464: int index, int max, int parentHandle) {
465: int ret = -1;
466: String name = getFullName();
467: if (rowNo > -1)
468: name += "_" + rowNo;
469: name += "_" + index;
470:
471: boolean netscape = getPage().getBrowserType() == HtmlPage.BROWSER_NETSCAPE;
472:
473: String tag = "";
474:
475: if (index < max) {
476: p.println("<SCRIPT LANGUAGE=\"JavaScript\">");
477: p.println("function " + name + "showHide(){");
478:
479: String tname = getFullName();
480: if (rowNo > -1)
481: tname += "_" + rowNo;
482:
483: if (index > 0) {
484: p.println("if (" + getFormString() + tname + "_"
485: + (index - 1) + ".selectedIndex < 0)");
486: p.println(" return;");
487: }
488:
489: p.println("show" + tname + "_" + (index + 1) + "();");
490: for (int i = index + 2; i <= max; i++) {
491: p.println("hide" + tname + "_" + i + "();");
492: }
493: p.println("}");
494: p.println("</SCRIPT>");
495: }
496:
497: StringBuffer b = new StringBuffer();
498: boolean optionPrinted = false;
499: if (index == 0 || parentHandle > -1) {
500: int number = 0;
501: TreeNode node = _root;
502: if (parentHandle > -1) {
503: FourObjectContainer f = (FourObjectContainer) _options
504: .elementAt(parentHandle);
505: node = (TreeNode) f.getObject4();
506: }
507: int handle = node.getHandle(number++);
508:
509: while (handle > -1) {
510: optionPrinted = true;
511: FourObjectContainer f = (FourObjectContainer) _options
512: .elementAt(handle);
513: b.append("<OPTION VALUE=\"");
514: b.append(getFullName());
515: b.append("_");
516: b.append(handle);
517: b.append("\"");
518: if (((Boolean) f.getObject3()).booleanValue()) {
519: b.append(" SELECTED");
520: ret = handle;
521: }
522:
523: if (f.getObject2() == null) {
524: b.append(">");
525: for (int i = 0; i < _minWidth; i++)
526: b.append(" ");
527: b.append("<OPTION>");
528: } else {
529: b.append(">");
530: b.append((String) f.getObject2());
531: if (number == 1) {
532: int len = ((String) f.getObject2()).length();
533: for (int i = len; i < _minWidth; i++)
534: b.append(" ");
535: }
536: b.append("</OPTION>");
537: }
538: handle = node.getHandle(number++);
539: }
540: tag = b.toString();
541: }
542: if (!optionPrinted) {
543: StringBuffer min = new StringBuffer(_minWidth * 6);
544: min.append("<OPTION VALUE=\"" + getFullName() + "_X\">");
545: for (int i = 0; i < _minWidth; i++)
546: min.append(" ");
547: min.append("</OPTION>");
548: tag += min.toString();
549: }
550:
551: String tagStart = "<SELECT ";
552: if (!netscape)
553: tagStart += " STYLE=\"visibility:"
554: + (index <= _lastVisibleBox && optionPrinted ? "visible"
555: : "hidden") + "\"";
556: tagStart += " NAME=\"" + name + "\"" + " SIZE=\"" + _size
557: + "\"";
558:
559: if (index < max) {
560: tagStart += " ONCHANGE=\"" + name + "showHide();\"";
561: tagStart += " ONCLICK=\"" + name + "showHide();\"";
562: }
563: tagStart += ">";
564:
565: tag = tagStart + tag + "</SELECT>";
566: if (_fontTagStart != null)
567: tag = _fontTagStart + tag + _fontTagEnd;
568: p.println(tag);
569: return ret;
570:
571: }
572:
573: private void writeShowHideJavaScript(java.io.PrintWriter p,
574: String name, int index) {
575: boolean netscape = getPage().getBrowserType() == HtmlPage.BROWSER_NETSCAPE;
576:
577: p.println("function hide" + name + index + "() {");
578: p.println(netscape ? "" : (getFormString() + "elements['"
579: + name + index + "'].style.visibility=\"hidden\";"));
580: p.println("clearBox(" + getFormString() + "elements['" + name
581: + index + "']);");
582: p.println("}");
583: p.println("");
584:
585: p.println("function show" + name + index + "() {");
586: p.print("var ar = " + getFullName() + "items");
587: for (int i = 0; i < index; i++) {
588: p.print("[(" + getFormString() + "elements['");
589: p.print(name + i);
590: p.print("'].selectedIndex * 3)]");
591: }
592: p.println(";");
593:
594: p.println("populateBox(" + getFormString() + "elements['"
595: + name + index + "'], ar);");
596: if (!netscape) {
597: p.println("if (" + getFormString() + "elements['" + name
598: + index + "'].length > 0)");
599: p.println(getFormString() + "elements['" + name + index
600: + "'].style.visibility=\"visible\";");
601: p.println("else");
602: p.println(getFormString() + "elements['" + name + index
603: + "'].style.visibility=\"hidden\";");
604: }
605:
606: p.println("}");
607:
608: }
609:
610: private int _minWidth = 20;
611:
612: /**
613: * Sets the minimum width (in non breaking spaces) for each list box
614: */
615: public void setMinimumWidth(int chars) {
616: _minWidth = chars;
617:
618: }
619:
620: /**
621: * This method displays the number of boxes that will be displayed in the list box
622: */
623: public void setNumberOfBoxes(int boxes) {
624: _numberOfBoxes = boxes;
625: }
626: }
|