001: /**
002: * Copyright (c) 2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. 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: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.interactive.action.type;
031:
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: import org.pdfbox.cos.COSArray;
036: import org.pdfbox.cos.COSBase;
037: import org.pdfbox.cos.COSDictionary;
038:
039: import org.pdfbox.pdmodel.common.COSArrayList;
040: import org.pdfbox.pdmodel.common.PDDestinationOrAction;
041: import org.pdfbox.pdmodel.interactive.action.PDActionFactory;
042:
043: /**
044: * This represents an action that can be executed in a PDF document.
045: *
046: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
047: * @author Panagiotis Toumasis (ptoumasis@mail.gr)
048: * @version $Revision: 1.3 $
049: */
050: public abstract class PDAction implements PDDestinationOrAction {
051: /**
052: * The type of PDF object.
053: */
054: public static final String TYPE = "Action";
055:
056: /**
057: * The action dictionary.
058: */
059: protected COSDictionary action;
060:
061: /**
062: * Default constructor.
063: */
064: public PDAction() {
065: action = new COSDictionary();
066: setType(TYPE);
067: }
068:
069: /**
070: * Constructor.
071: *
072: * @param a The action dictionary.
073: */
074: public PDAction(COSDictionary a) {
075: action = a;
076: }
077:
078: /**
079: * Convert this standard java object to a COS object.
080: *
081: * @return The cos object that matches this Java object.
082: */
083: public COSBase getCOSObject() {
084: return action;
085: }
086:
087: /**
088: * Convert this standard java object to a COS object.
089: *
090: * @return The cos object that matches this Java object.
091: */
092: public COSDictionary getCOSDictionary() {
093: return action;
094: }
095:
096: /**
097: * This will get the type of PDF object that the actions dictionary describes.
098: * If present must be Action for an action dictionary.
099: *
100: * @return The Type of PDF object.
101: */
102: public String getType() {
103: return action.getNameAsString("Type");
104: }
105:
106: /**
107: * This will set the type of PDF object that the actions dictionary describes.
108: * If present must be Action for an action dictionary.
109: *
110: * @param type The new Type for the PDF object.
111: */
112: public void setType(String type) {
113: action.setName("Type", type);
114: }
115:
116: /**
117: * This will get the type of action that the actions dictionary describes.
118: * If present, must be Action for an action dictionary.
119: *
120: * @return The S entry of actions dictionary.
121: */
122: public String getSubType() {
123: return action.getNameAsString("S");
124: }
125:
126: /**
127: * This will set the type of action that the actions dictionary describes.
128: * If present, must be Action for an action dictionary.
129: *
130: * @param s The new type of action.
131: */
132: public void setSubType(String s) {
133: action.setName("S", s);
134: }
135:
136: /**
137: * This will get the next action, or sequence of actions, to be performed after this one.
138: * The value is either a single action dictionary or an array of action dictionaries
139: * to be performed in order.
140: *
141: * @return The Next action or sequence of actions.
142: */
143: public List getNext() {
144: List retval = null;
145: COSBase next = action.getDictionaryObject("Next");
146: if (next instanceof COSDictionary) {
147: PDAction pdAction = PDActionFactory
148: .createAction((COSDictionary) next);
149: retval = new COSArrayList(pdAction, next, action, "Next");
150: } else if (next instanceof COSArray) {
151: COSArray array = (COSArray) next;
152: List actions = new ArrayList();
153: for (int i = 0; i < array.size(); i++) {
154: actions.add(PDActionFactory
155: .createAction((COSDictionary) array
156: .getObject(i)));
157: }
158: retval = new COSArrayList(actions, array);
159: }
160:
161: return retval;
162: }
163:
164: /**
165: * This will set the next action, or sequence of actions, to be performed after this one.
166: * The value is either a single action dictionary or an array of action dictionaries
167: * to be performed in order.
168: *
169: * @param next The Next action or sequence of actions.
170: */
171: public void setNext(List next) {
172: action.setItem("Next", COSArrayList.converterToCOSArray(next));
173: }
174: }
|