001: /*
002: * Instance.java
003: *
004: * Version: $Revision: 1.2 $
005: *
006: * Date: $Date: 2006/07/06 17:02:03 $
007: *
008: * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040:
041: package org.dspace.app.xmlui.wing.element;
042:
043: import java.util.ArrayList;
044: import java.util.List;
045:
046: import org.dspace.app.xmlui.wing.Message;
047: import org.dspace.app.xmlui.wing.WingContext;
048: import org.dspace.app.xmlui.wing.WingException;
049: import org.xml.sax.ContentHandler;
050: import org.xml.sax.SAXException;
051: import org.xml.sax.ext.LexicalHandler;
052: import org.xml.sax.helpers.NamespaceSupport;
053:
054: /**
055: * Instance represents multiple value instances of a field.
056: *
057: *
058: * @author Scott Phillips
059: */
060:
061: public class Instance extends Container {
062: /** The name of the field instance element */
063: public static final String E_INSTANCE = "instance";
064:
065: /**
066: * Construct a new field value, when used in a multiple value context
067: *
068: * @param context
069: * (Required) The context this element is contained in
070: */
071: protected Instance(WingContext context) throws WingException {
072: super (context);
073: }
074:
075: /** ******************************************************************** */
076: /** Values * */
077: /** ******************************************************************** */
078:
079: /**
080: * Set the raw value of the field removing any previous raw values.
081: *
082: * @param characters
083: * (May be null) Field value as a string
084: */
085: public Value setValue() throws WingException {
086: this .removeValueOfType(Value.TYPE_RAW);
087: Value value = new Value(context, Value.TYPE_RAW);
088: contents.add(value);
089: return value;
090: }
091:
092: /**
093: * Set the raw value of the field removing any previous raw values.
094: *
095: * @param characters
096: * (May be null) Field value as a string
097: */
098: public void setValue(String characters) throws WingException {
099: Value value = this .setValue();
100: value.addContent(characters);
101: }
102:
103: /**
104: * Set the raw value of the field removing any previous raw values.
105: *
106: * @param message
107: * (Required) A key into the i18n catalogue for translation into
108: * the user's preferred language.
109: */
110: public void setValue(Message message) throws WingException {
111: Value value = this .setValue();
112: value.addContent(message);
113: }
114:
115: /**
116: * Set the raw value of the field removing any previous raw values. This
117: * will set the field as either checked or unchecked. This should only be
118: * used on checkbox or radio button fields.
119: *
120: * @param checked
121: * (Required) Whether the checkbox is checked or not.
122: */
123: public void setValue(boolean checked) throws WingException {
124: this .removeValueOfType(Value.TYPE_RAW);
125: Value value = new Value(context, Value.TYPE_RAW, checked);
126: contents.add(value);
127: }
128:
129: /**
130: * Set the given option as selected.
131: *
132: * @param returnValue
133: * (Required) The return value of the option to be selected.
134: */
135: public void setOptionSelected(String returnValue)
136: throws WingException {
137: Value value = new Value(context, Value.TYPE_OPTION, returnValue);
138: contents.add(value);
139: }
140:
141: /**
142: * Set the given option as selected.
143: *
144: * @param returnValue
145: * (Required) The return value of the option to be selected.
146: */
147: public void setOptionSelected(int returnValue) throws WingException {
148: setOptionSelected(String.valueOf(returnValue));
149: }
150:
151: /** ******************************************************************** */
152: /** Interpreted Values * */
153: /** ******************************************************************** */
154:
155: /**
156: * Set the interpreted value of the field removing any previous interpreted
157: * values.
158: *
159: * @param characters
160: * (May be null) Field value as a string
161: */
162: public Value setInterpretedValue() throws WingException {
163: removeValueOfType(Value.TYPE_INTERPRETED);
164: Value value = new Value(context, Value.TYPE_INTERPRETED);
165: contents.add(value);
166: return value;
167: }
168:
169: /**
170: * Set the interpreted value of the field removing any previous interpreted
171: * values.
172: *
173: * @param characters
174: * (May be null) Field value as a string
175: */
176: public void setInterpretedValue(String characters)
177: throws WingException {
178: Value value = this .setInterpretedValue();
179: value.addContent(characters);
180: }
181:
182: /**
183: * Set the interpreted value of the field removing any previous interpreted
184: * values.
185: *
186: * @param message
187: * (Required) A key into the i18n catalogue for translation into
188: * the user's preferred language.
189: */
190: public void setInterpretedValue(Message message)
191: throws WingException {
192: Value value = this .setInterpretedValue();
193: value.addContent(message);
194: }
195:
196: /** ******************************************************************** */
197: /** Special Values * */
198: /** ******************************************************************** */
199:
200: /**
201: * Add an option value, there may be many of these. These values refrence
202: * an option all ready added to the field.
203: *
204: * @param option
205: * (Required) The return value of the selected option.
206: */
207: public Value addOptionValue(String option) throws WingException {
208: Value value = new Value(context, Value.TYPE_OPTION, option);
209: contents.add(value);
210: return value;
211: }
212:
213: /**
214: * Set the checkbox (or radio) value of this field. This is a parameter
215: * wheather the field is selected or not along with the return string that
216: * should be used with this parameter.
217: *
218: * @param checked
219: * (Required) determine if the value is selected or not.
220: * @param characters
221: * (may be null) The returned value for this field, if selected.
222: */
223: public void setCheckedValue(boolean checked, String characters)
224: throws WingException {
225: this .removeValueOfType(Value.TYPE_RAW);
226: Value value = new Value(context, Value.TYPE_RAW, checked);
227: contents.add(value);
228: value.addContent(characters);
229: }
230:
231: /**
232: * Translate this element and all contained elements into SAX events. The
233: * events should be routed to the contentHandler found in the WingContext.
234: *
235: * @param contentHandler
236: * (Required) The registered contentHandler where SAX events
237: * should be routed too.
238: * @param lexicalHandler
239: * (Required) The registered lexicalHandler where lexical events
240: * (such as CDATA, DTD, etc) should be routed too.
241: * @param namespaces
242: * (Required) SAX Helper class to keep track of namespaces able
243: * to determine the correct prefix for a given namespace URI.
244: */
245:
246: public void toSAX(ContentHandler contentHandler,
247: LexicalHandler lexicalHandler, NamespaceSupport namespaces)
248: throws SAXException {
249: startElement(contentHandler, namespaces, E_INSTANCE, null);
250: super .toSAX(contentHandler, lexicalHandler, namespaces);
251: endElement(contentHandler, namespaces, E_INSTANCE);
252: }
253:
254: /**
255: * dispose
256: */
257: public void dispose() {
258: super .dispose();
259: }
260:
261: /**
262: * Private function to remove all values of a particular type.
263: *
264: * @param removeType
265: * The type to be removed.
266: */
267: private void removeValueOfType(String removeType) {
268: List<Value> found = new ArrayList<Value>();
269: for (AbstractWingElement awe : contents) {
270: if (awe instanceof Value) {
271: Value value = (Value) awe;
272: if (value.getType().equals(removeType)) {
273: found.add(value);
274: }
275: }
276: }
277:
278: for (Value remove : found) {
279: contents.remove(remove);
280: remove.dispose();
281: }
282: }
283:
284: }
|