001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.enterprise.xi.enhydrabarracuda.htmlwidgets;
016:
017: import java.util.ArrayList;
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.w3c.dom.Document;
024: import org.w3c.dom.html.HTMLCollection;
025: import org.w3c.dom.html.HTMLOptionElement;
026: import org.w3c.dom.html.HTMLSelectElement;
027:
028: import com.metaboss.enterprise.ui.UIUnexpectedProgramConditionException;
029: import com.metaboss.enterprise.xi.enhydrabarracuda.Widget;
030: import com.metaboss.enterprise.xi.enhydrabarracuda.WidgetModel;
031:
032: /** This class is the very simple text selection implementation. It implements a simple
033: * single selection model. */
034: public class TextSelection extends Widget {
035: /** This class represents the data model of the text selection. The model is very strict
036: * and only allows to set the text if it is a valid member of the selection array */
037: public static class Model implements WidgetModel {
038: private HTMLSelectElement mSampleSelectElement;
039: private String mSelectedValue = null;
040: private String mSelectedText = null;
041: private Map mSampleValidValuesMap = null;
042: private List mValidValuesList = null;
043: private List mValidTextsList = null;
044:
045: private Model(HTMLSelectElement pSampleSelectElement)
046: throws UIUnexpectedProgramConditionException {
047: mSampleSelectElement = pSampleSelectElement;
048: // Go through all options and build the text selection array
049: HTMLCollection lOptionsCollection = pSampleSelectElement
050: .getOptions();
051: int lNumberOfOptions = lOptionsCollection.getLength();
052: if (lNumberOfOptions > 0) {
053: mSampleValidValuesMap = new HashMap();
054: for (int lOptionIndex = 0; lOptionIndex < lNumberOfOptions; lOptionIndex++) {
055: HTMLOptionElement lOption = (HTMLOptionElement) lOptionsCollection
056: .item(lOptionIndex);
057: mSampleValidValuesMap.put(lOption.getValue(),
058: lOption.getText());
059: }
060: }
061: }
062:
063: /** Returns the text set into the text selection widget.
064: * @return the string set into the text widget. If null is returned from here the text will stay unmodified */
065: public String getSelectedText() {
066: return mSelectedText;
067: }
068:
069: /** Returns the value set into the text widget.
070: * @return the string set into the text widget. If null is returned from here the text will stay unmodified */
071: public String getSelectedValue() {
072: return mSelectedValue;
073: }
074:
075: /** Sets the text to be set into the text widget.
076: * @exception IllegalArgumentException if the text being set is not allowed */
077: public void setSelectedText(String pSelectedText)
078: throws IllegalArgumentException {
079: // Ensure that this text is allowed before setting it
080: if (mValidTextsList != null) {
081: int lValidTextIndex = mValidTextsList
082: .indexOf(pSelectedText);
083: if (lValidTextIndex >= 0) {
084: mSelectedText = (String) mValidTextsList
085: .get(lValidTextIndex);
086: mSelectedValue = (String) mValidValuesList
087: .get(lValidTextIndex);
088: return;
089: }
090: } else if (mSampleValidValuesMap != null) {
091: if (mSampleValidValuesMap.containsValue(pSelectedText)) {
092: for (Iterator lIter = mSampleValidValuesMap
093: .entrySet().iterator(); lIter.hasNext();) {
094: Map.Entry lEntry = (Map.Entry) lIter.next();
095: if (lEntry.getValue().equals(pSelectedText)) {
096: mSelectedText = (String) lEntry.getValue();
097: mSelectedValue = (String) lEntry.getKey();
098: return;
099: }
100: }
101: }
102: }
103: throw new IllegalArgumentException("Text '" + pSelectedText
104: + "' is not allowed.");
105: }
106:
107: /** Sets the value to be set into the text widget.
108: * @exception IllegalArgumentException if the text being set is not allowed */
109: public void setSelectedValue(String pSelectedValue)
110: throws IllegalArgumentException {
111: // Ensure that this text is allowed before setting it
112: if (mValidValuesList != null) {
113: int lValidValueIndex = mValidValuesList
114: .indexOf(pSelectedValue);
115: if (lValidValueIndex >= 0) {
116: mSelectedText = (String) mValidTextsList
117: .get(lValidValueIndex);
118: mSelectedValue = (String) mValidValuesList
119: .get(lValidValueIndex);
120: return;
121: }
122: } else if (mSampleValidValuesMap != null) {
123: if (mSampleValidValuesMap.containsKey(pSelectedValue)) {
124: for (Iterator lIter = mSampleValidValuesMap
125: .entrySet().iterator(); lIter.hasNext();) {
126: Map.Entry lEntry = (Map.Entry) lIter.next();
127: if (lEntry.getKey().equals(pSelectedValue)) {
128: mSelectedText = (String) lEntry.getValue();
129: mSelectedValue = (String) lEntry.getKey();
130: return;
131: }
132: }
133: }
134: }
135: throw new IllegalArgumentException("Value '"
136: + pSelectedValue + "' is not allowed.");
137: }
138:
139: /** Provides facility to build the collection of valid values
140: * The values must be added in the order they have to be displayed.
141: * If no values are added using this method - sample values from the HTML page will be used */
142: public void addValidValue(String pValidValue, String pValidText) {
143: if (mValidValuesList == null) {
144: mValidValuesList = new ArrayList();
145: mValidTextsList = new ArrayList();
146: } else if (mValidValuesList.contains(pValidValue))
147: throw new IllegalArgumentException(
148: "Duplicate values are not allowed. Value '"
149: + pValidValue
150: + "' has already been added.");
151: mValidValuesList.add(pValidValue);
152: mValidTextsList.add(pValidText);
153: }
154:
155: /** Provides facility to build the collection of valid values.
156: * The values must be added in the order they have to be displayed.
157: * If no values are added using this method - sample values from the HTML page will be used */
158: public void addValidValues(String[] pValidValues,
159: String[] pValidTexts) {
160: if (pValidValues == null || pValidTexts == null)
161: throw new IllegalArgumentException(
162: "null arguments are not allowed");
163: if (pValidValues.length != pValidTexts.length)
164: throw new IllegalArgumentException(
165: "Valid values and valid texts arrays must be of the same size");
166: for (int i = 0; i < pValidValues.length; i++)
167: addValidValue(pValidValues[i], pValidTexts[i]);
168: }
169:
170: /** Provides facility to clear previously built valid values map.
171: * If no values are added after this method was used - sample values from the HTML page will be used */
172: public void clearValidValues() {
173: if (mValidValuesList != null) {
174: mValidValuesList.clear();
175: mValidTextsList.clear();
176: mValidValuesList = null;
177: mValidTextsList = null;
178: }
179: }
180:
181: /** This method is used to apply the values passed from the form back into the model.
182: * @return the model object after the form values have been applied (The model object may just return itself)
183: * @exception UIUnexpectedProgramConditionException thrown when this widget model does not support editing */
184: public void applyFormValues(String[] pValues)
185: throws UIUnexpectedProgramConditionException {
186: if (pValues == null || pValues.length != 1
187: || pValues[0] == null)
188: throw new UIUnexpectedProgramConditionException(
189: "Unexpected value passed from form. Expecting single string value");
190: try {
191: setSelectedValue(pValues[0]);
192: } catch (IllegalArgumentException e) {
193: throw new UIUnexpectedProgramConditionException(
194: "Unexpected value passed from form. Expecting single string value matching one of the allowed values.");
195: }
196: }
197:
198: /** This method used primarily for debug purposes. It is expected to output a single string value reflecting the content of the model */
199: public String getDebugString() {
200: return mSelectedText != null ? mSelectedText
201: : "nothing selected";
202: }
203: }
204:
205: /** This standard method creates an instance of the model object for this widget */
206: public WidgetModel createModel()
207: throws UIUnexpectedProgramConditionException {
208: // Get the input element and create the model object initialised with it
209: HTMLSelectElement lSelectElement = (HTMLSelectElement) getBoundDocumentElement();
210: Model lModel = new Model(lSelectElement);
211: return lModel;
212: }
213:
214: /** This lifecycle method is expected to render the control's image onto the document */
215: public void render() {
216: Model lModel = (Model) getModel();
217: HTMLSelectElement lSelectElement = (HTMLSelectElement) getBoundDocumentElement();
218: // If model has values list - we will have to rebuild the options list on the fly
219: if (lModel.mValidValuesList != null) {
220: // Remove all sample options if necessary
221: HTMLCollection lSampleOptionsCollection = lSelectElement
222: .getOptions();
223: int lNumberOfSampleOptions = lSampleOptionsCollection
224: .getLength();
225: if (lNumberOfSampleOptions > 0) {
226: for (int i = 0; i < lNumberOfSampleOptions; i++)
227: lSelectElement.remove(i);
228: }
229: // Now add all new ones
230: Document lOwnerDocument = lSelectElement.getOwnerDocument();
231: int lNumberOfValidValues = lModel.mValidValuesList.size();
232: for (int i = 0; i < lNumberOfValidValues; i++) {
233: HTMLOptionElement lOptionElement = (HTMLOptionElement) lOwnerDocument
234: .createElement("OPTION");
235: lOptionElement
236: .setValue((String) lModel.mValidValuesList
237: .get(i));
238: lOptionElement.appendChild(lOwnerDocument
239: .createTextNode((String) lModel.mValidTextsList
240: .get(i)));
241: lOptionElement
242: .setDefaultSelected(lModel.mSelectedValue != null
243: && lOptionElement.getValue().equals(
244: lModel.mSelectedValue));
245: lSelectElement.add(lOptionElement, null);
246: }
247: } else {
248: // Looks like we are using the list from the html page.
249: // we still have to find the option which is selected and set it as such
250: HTMLCollection lOptions = lSelectElement.getOptions();
251: for (int i = lOptions.getLength() - 1; i >= 0; i--) {
252: HTMLOptionElement lOptionElement = (HTMLOptionElement) lOptions
253: .item(i);
254: lOptionElement
255: .setSelected(lModel.mSelectedValue != null
256: && lOptionElement.getValue().equals(
257: lModel.mSelectedValue));
258: }
259: }
260: }
261: }
|