Source code: com/aendvari/tethys/tag/html/OptionsTag.java
1 /*
2 * OptionsTag.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.tethys.tag.html;
11
12 import java.util.*;
13 import java.io.IOException;
14
15 import javax.servlet.http.*;
16 import javax.servlet.jsp.*;
17 import javax.servlet.jsp.tagext.*;
18
19 import com.aendvari.common.model.*;
20
21 import com.aendvari.tethys.context.*;
22 import com.aendvari.tethys.tag.*;
23 import com.aendvari.tethys.tag.model.*;
24
25
26 /**
27 * Used to create HTML <code>option</code> tags from a collection.
28 *
29 * @author Scott Milne
30 *
31 */
32
33 public class OptionsTag extends ModelTreeBodyTag
34 {
35 /** The path to the option label. */
36 private String labelPath = null;
37
38 /** The path to the option value. */
39 private String valuePath = null;
40
41
42 /* Accessors */
43
44 public String getLabelPath() { return labelPath; }
45 public void setLabelPath(String param) { labelPath = param; }
46
47 public String getValuePath() { return valuePath; }
48 public void setValuePath(String param) { valuePath = param; }
49
50
51
52 public int doStartTag() throws JspException
53 {
54 return SKIP_BODY;
55 }
56
57 public int doEndTag() throws JspException
58 {
59 try
60 {
61 // get the match list
62 // make sure this tag is within a "SelectTag"
63 SelectTag selectTag = (SelectTag)findAncestorWithClass(this, SelectTag.class);
64 if( selectTag == null )
65 {
66 throw new JspTagException("OptionTag: without SelectTag");
67 }
68
69 // generate the tag string
70 StringBuffer tagString = new StringBuffer("\r\n");
71
72 // establish tag context
73 establishModelContext();
74
75 // get the context model node
76 ModelNode contextNode = modelContext.getModelNode();
77
78 // get the model tree
79 ModelTree modelTree = contextNode.getOwnerModelTree();
80
81 // generate options from the list
82 Iterator iterator = modelTree.getNodes(contextNode, path).iterator();
83
84 while (iterator.hasNext())
85 {
86 ModelNode modelNode = (ModelNode)iterator.next();
87
88 String label = (modelTree.getNode(modelNode, labelPath)).getNodeValue();
89 String value = (modelTree.getNode(modelNode, valuePath)).getNodeValue();
90 boolean matched = selectTag.isMatched(value);
91
92 tagString.append("<option value=\"");
93 tagString.append(value);
94 tagString.append("\"");
95
96 if (matched)
97 {
98 tagString.append(" selected");
99 }
100
101 tagString.append(">");
102 tagString.append(label);
103 tagString.append("</option>\r\n");
104 }
105
106 // print out the finalized string
107 pageContext.getOut().write(tagString.toString());
108 }
109 catch (Exception e)
110 {
111 throw new JspTagException("OptionsTag Error:" + e.toString());
112 }
113
114 // continue evaluating this page
115 return EVAL_PAGE;
116 }
117
118 public void release()
119 {
120 super.release();
121
122 valuePath = null;
123 labelPath = null;
124 }
125 }
126