001: /*
002: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
003: * @author <a href="mailto:oliver.wehrens@aei.mpg.de">Oliver Wehrens</a>
004: * @version $Id: ListBoxTag.java 6385 2007-10-25 14:02:26Z wehrens $
005: */
006: package org.gridsphere.provider.portletui.tags;
007:
008: import org.gridsphere.provider.portletui.beans.ListBoxBean;
009: import org.gridsphere.provider.portletui.beans.ListBoxItemBean;
010: import org.gridsphere.provider.portletui.beans.TagBean;
011:
012: import javax.servlet.jsp.JspException;
013: import javax.servlet.jsp.JspWriter;
014: import java.util.ArrayList;
015:
016: /**
017: * A <code>ListBoxTag</code> represents a list box element
018: */
019: public class ListBoxTag extends ContainerTag {
020:
021: protected ListBoxBean listbox = null;
022: protected boolean isMultiple = false;
023: protected int size = 1;
024: protected String onChange = null;
025: protected String onBlur = null;
026: protected String onFocus = null;
027: protected boolean submitOnChange = false;
028:
029: /**
030: * Returns the (html) size of the field.
031: *
032: * @return size of the field
033: */
034: public int getSize() {
035: return size;
036: }
037:
038: /**
039: * Sets the (html) size of the field
040: *
041: * @param size size of the field
042: */
043: public void setSize(int size) {
044: this .size = size;
045: }
046:
047: /**
048: * Returns true if this listbox should invoke a form submission if the selection is changed
049: *
050: * @return true if this listbox should invoke a form submission if the selection is changed
051: */
052: public boolean getSubmitOnChange() {
053: return submitOnChange;
054: }
055:
056: /**
057: * Sets whether this listbox should invoke a form submission if the selection is changed
058: *
059: * @param submitOnChange if true a form submission is invoked if selection changes
060: */
061: public void setSubmitOnChange(boolean submitOnChange) {
062: this .submitOnChange = submitOnChange;
063: }
064:
065: /**
066: * Returns the onChange JavaScript function
067: *
068: * @return onChange JavaScript function
069: */
070: public String getOnChange() {
071: return onChange;
072: }
073:
074: /**
075: * Sets the onChange JavaScript function
076: *
077: * @param onChange the onChange JavaScript function
078: */
079: public void setOnChange(String onChange) {
080: this .onChange = onChange;
081: }
082:
083: /**
084: * Returns the onBlur JavaScript function
085: *
086: * @return onBlur JavaScript function
087: */
088: public String getOnBlur() {
089: return onBlur;
090: }
091:
092: /**
093: * Sets the onBlur JavaScript function
094: *
095: * @param onBlur the onBlur JavaScript function
096: */
097: public void setOnBlur(String onBlur) {
098: this .onBlur = onBlur;
099: }
100:
101: /**
102: * Returns the onFocus JavaScript function
103: *
104: * @return onFocus JavaScript function
105: */
106: public String getOnFocus() {
107: return onChange;
108: }
109:
110: /**
111: * Sets the onFocus JavaScript function
112: *
113: * @param onFocus the onFocus JavaScript function
114: */
115: public void setOnFocus(String onFocus) {
116: this .onFocus = onFocus;
117: }
118:
119: /**
120: * Sets multiple selection
121: *
122: * @param isMultiple is true if listbox provides multiple selections, false otherwise
123: */
124: public void setMultiple(boolean isMultiple) {
125: this .isMultiple = isMultiple;
126: }
127:
128: public void setListBoxBean(ListBoxBean listbox) {
129: this .listbox = listbox;
130: }
131:
132: public ListBoxBean getListBoxBean() {
133: return listbox;
134: }
135:
136: /**
137: * Indicates if multiple selection is provided
138: *
139: * @return true if this listbox supports multiple selection, false otherwise
140: */
141: public boolean getMultiple() {
142: return isMultiple;
143: }
144:
145: public int doStartTag() throws JspException {
146: list = new ArrayList<TagBean>();
147: if (submitOnChange)
148: onChange = "GridSphere_SelectSubmit( this.form )";
149: if (!beanId.equals("")) {
150: listbox = (ListBoxBean) getTagBean();
151: if (listbox == null) {
152: listbox = new ListBoxBean();
153: listbox.setSize(size);
154: listbox.setMultipleSelection(isMultiple);
155: this .setBaseComponentBean(listbox);
156: } else {
157: if (size != 1)
158: listbox.setSize(size);
159: this .updateBaseComponentBean(listbox);
160: }
161: } else {
162: listbox = new ListBoxBean();
163: listbox.setSize(size);
164: listbox.setMultipleSelection(isMultiple);
165: this .setBaseComponentBean(listbox);
166: }
167:
168: if (onChange != null)
169: listbox.setOnChange(onChange);
170: if (onBlur != null)
171: listbox.setOnBlur(onBlur);
172: if (onFocus != null)
173: listbox.setOnFocus(onFocus);
174:
175: try {
176: JspWriter out = pageContext.getOut();
177: out.print(listbox.toStartString());
178: } catch (Exception e) {
179: throw new JspException(e.getMessage());
180: }
181: return EVAL_BODY_INCLUDE;
182: }
183:
184: public int doEndTag() throws JspException {
185: for (TagBean aList : list) {
186: ListBoxItemBean itembean = (ListBoxItemBean) aList;
187: listbox.addBean(itembean);
188: }
189: try {
190: JspWriter out = pageContext.getOut();
191: out.print(listbox.toEndString());
192: } catch (Exception e) {
193: throw new JspException(e.getMessage());
194: }
195:
196: return EVAL_PAGE;
197: }
198:
199: }
|