001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.pd;
031:
032: import java.util.ArrayList;
033: import java.util.Iterator;
034: import java.util.List;
035: import java.util.Map;
036:
037: import de.intarsys.pdf.cos.COSArray;
038: import de.intarsys.pdf.cos.COSBasedObject;
039: import de.intarsys.pdf.cos.COSName;
040: import de.intarsys.pdf.cos.COSObject;
041: import de.intarsys.pdf.cos.COSString;
042:
043: /**
044: * A logical choice field within an AcroForm.
045: *
046: */
047: public class PDAFChoiceField extends PDAcroFormField {
048: static public class MetaClass extends PDAcroFormField.MetaClass {
049: protected MetaClass(Class instanceClass) {
050: super (instanceClass);
051: }
052:
053: protected COSBasedObject doCreateCOSBasedObject(COSObject object) {
054: return new PDAFChoiceField(object);
055: }
056: }
057:
058: static public final COSName DK_Opt = COSName.constant("Opt");
059:
060: static public final COSName DK_TI = COSName.constant("TI");
061:
062: static public final COSName DK_I = COSName.constant("I");
063:
064: /** The meta class instance */
065: static public final MetaClass META = new MetaClass(MetaClass.class
066: .getDeclaringClass());
067:
068: private List cachedOptionNames;
069:
070: private List cachedExportValues;
071:
072: protected PDAFChoiceField(COSObject object) {
073: super (object);
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see de.intarsys.pdf.pd.PDAcroFormField#cosGetExpectedFieldType()
080: */
081: public COSName cosGetExpectedFieldType() {
082: return CN_FT_Ch;
083: }
084:
085: protected void createOptions() {
086: COSArray cosOptions = getOptions();
087: if ((cosOptions != null) && (cosOptions.size() > 0)) {
088: cachedOptionNames = new ArrayList(cosOptions.size());
089: cachedExportValues = new ArrayList(cosOptions.size());
090: for (Iterator i = cosOptions.iterator(); i.hasNext();) {
091: COSObject cosOption = (COSObject) i.next();
092: String exportValue = "null"; //$NON-NLS-1$
093: String optionName = "null"; //$NON-NLS-1$
094: if (cosOption instanceof COSArray) {
095: COSArray cosOptionArray = (COSArray) cosOption;
096: exportValue = cosOptionArray.get(0).stringValue();
097: optionName = cosOptionArray.get(1).stringValue();
098: } else if (cosOption instanceof COSString) {
099: COSString cosOptionString = (COSString) cosOption;
100: exportValue = cosOptionString.stringValue();
101: optionName = cosOptionString.stringValue();
102: }
103: cachedExportValues.add(exportValue);
104: cachedOptionNames.add(optionName);
105: }
106: } else {
107: cachedOptionNames = new ArrayList(0);
108: cachedExportValues = cachedOptionNames;
109: }
110: }
111:
112: /**
113: * A List containing the option export values as Strings.
114: *
115: * @return a List containing the option export values as Strings. The result
116: * will never be null.
117: */
118: public List getExportValues() {
119: if (cachedExportValues == null) {
120: createOptions();
121: }
122: return cachedExportValues;
123: }
124:
125: /**
126: * A List containing the option names as Strings.
127: *
128: * @return a List containing the option names as Strings. The result will
129: * never be null.
130: */
131: public List getOptionNames() {
132: if (cachedOptionNames == null) {
133: createOptions();
134: }
135: return cachedOptionNames;
136: }
137:
138: protected COSArray getOptions() {
139: return cosGetFieldInheritable(DK_Opt).asArray();
140: }
141:
142: protected int getTopIndex() {
143: return getFieldInt(DK_TI, 0);
144: }
145:
146: /**
147: * The value stored in this field as a {@link List} of {@link String}
148: * objects
149: *
150: * @return The value stored in this field as a {@link List} of
151: * {@link String} objects
152: */
153: public List getValueList() {
154: List result;
155: COSObject cosValue = super .cosGetValue();
156: if (cosValue.isNull()) {
157: result = new ArrayList(0);
158: return result;
159: }
160: if (cosValue instanceof COSArray) {
161: result = new ArrayList(((COSArray) cosValue).size());
162: for (Iterator i = ((COSArray) cosValue).iterator(); i
163: .hasNext();) {
164: result.add(((COSObject) i.next()).stringValue());
165: }
166: } else {
167: result = new ArrayList(1);
168: result.add(cosValue.stringValue());
169: }
170: return result;
171: }
172:
173: /*
174: * (non-Javadoc)
175: *
176: * @see de.intarsys.pdf.pd.PDAcroFormField#invalidateCaches()
177: */
178: public void invalidateCaches() {
179: super .invalidateCaches();
180: cachedExportValues = null;
181: cachedOptionNames = null;
182: }
183:
184: /**
185: * Convenience method to access "Combo" flag.
186: */
187: public boolean isCombo() {
188: return getFieldFlags().isCombo();
189: }
190:
191: /**
192: * Convenience method to access "CommitOnSelChange" flag.
193: */
194: public boolean isCommitOnSelChange() {
195: return getFieldFlags().isCommitOnSelChange();
196: }
197:
198: /**
199: * Convenience method to access "Edit" flag.
200: */
201: public boolean isEdit() {
202: return getFieldFlags().isEdit();
203: }
204:
205: /**
206: * Convenience method to access "MultiSelect" flag.
207: */
208: public boolean isMultiSelect() {
209: return getFieldFlags().isMultiSelect();
210: }
211:
212: /*
213: * (non-Javadoc)
214: *
215: * @see de.intarsys.pdf.pd.PDAcroFormField#isTypeCh()
216: */
217: public boolean isTypeCh() {
218: return true;
219: }
220:
221: protected void setIndices(List newIndices) {
222: // TODO 1 @wad cosSet type: array:integer
223: }
224:
225: protected void setOptions(COSArray options) {
226: cosSetFieldInheritable(DK_Opt, options);
227: }
228:
229: /**
230: * Assign the currently active options.
231: *
232: * @param options
233: * A {@link Map} containing the new options.
234: */
235: public void setOptions(Map options) {
236: COSArray cosOptions = COSArray.create(options.size());
237: for (Iterator i = options.entrySet().iterator(); i.hasNext();) {
238: Map.Entry entry = (Map.Entry) i.next();
239: String name = (String) entry.getKey();
240: String value = (String) entry.getValue();
241: if (value == null) {
242: value = name;
243: }
244: COSArray cosOption = COSArray.create(2);
245: cosOption.add(COSString.create(name));
246: cosOption.add(COSString.create(value));
247: cosOptions.add(cosOption);
248: }
249: setOptions(cosOptions);
250: }
251:
252: /**
253: * Assign the currently active options.
254: *
255: * @param names
256: * @param values
257: */
258: public void setOptions(String[] names, String[] values) {
259: COSArray cosOptions = COSArray.create(names.length);
260: for (int i = 0; i < names.length; i++) {
261: String name = (String) names[i];
262: String value = (String) values[i];
263: if (value == null) {
264: cosOptions.add(COSString.create(name));
265: } else {
266: COSArray cosOption = COSArray.create(2);
267: cosOption.add(COSString.create(value));
268: cosOption.add(COSString.create(name));
269: cosOptions.add(cosOption);
270: }
271: }
272: setOptions(cosOptions);
273: }
274:
275: protected void setTopIndex(int newTopIndex) {
276: if (newTopIndex == 0) { // default
277: cosRemoveField(DK_TI);
278: } else {
279: setFieldInt(DK_TI, newTopIndex);
280: }
281: }
282:
283: /*
284: * (non-Javadoc)
285: *
286: * @see de.intarsys.pdf.pd.PDAcroFormField#setValueString(java.lang.String)
287: */
288: public void setValueString(String value) {
289: if (value == null) {
290: super .setValueString(value);
291: } else {
292: // replace every flavour of new line with a literal \r
293: // this is recognized by acrobat as a new line indicator in text
294: // values
295: value = value.replace('\r', ' ');
296: value = value.replace('\n', '\r');
297: // set V field of field dict
298: super .setValueString(value);
299: }
300: }
301:
302: /*
303: * (non-Javadoc)
304: *
305: * @see de.intarsys.pdf.pd.PDAcroFormField#setValueStrings(java.util.List)
306: */
307: public void setValueStrings(List values) {
308: if ((values == null) || values.isEmpty()) {
309: cosSetValue(null);
310: return;
311: }
312: if (values.size() == 1) {
313: setValueString((String) values.get(0));
314: return;
315: }
316: COSArray array = COSArray.create(values.size());
317: Iterator iter = values.iterator();
318: while (iter.hasNext()) {
319: String value = (String) iter.next();
320: value = value.replace('\r', ' ');
321: value = value.replace('\n', '\r');
322: array.add(COSString.create(value));
323: }
324:
325: // set V field of field dict
326: cosSetValue(array);
327: }
328: }
|