KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > interactive > action > type > PDAction


1 /**
2  * Copyright (c) 2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.interactive.action.type;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.pdfbox.cos.COSArray;
37 import org.pdfbox.cos.COSBase;
38 import org.pdfbox.cos.COSDictionary;
39
40 import org.pdfbox.pdmodel.common.COSArrayList;
41 import org.pdfbox.pdmodel.common.PDDestinationOrAction;
42 import org.pdfbox.pdmodel.interactive.action.PDActionFactory;
43
44 /**
45  * This represents an action that can be executed in a PDF document.
46  *
47  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
48  * @author Panagiotis Toumasis (ptoumasis@mail.gr)
49  * @version $Revision: 1.3 $
50  */

51 public abstract class PDAction implements PDDestinationOrAction
52 {
53     /**
54      * The type of PDF object.
55      */

56     public static final String JavaDoc TYPE = "Action";
57     
58     /**
59      * The action dictionary.
60      */

61     protected COSDictionary action;
62
63     /**
64      * Default constructor.
65      */

66     public PDAction()
67     {
68         action = new COSDictionary();
69         setType( TYPE );
70     }
71
72     /**
73      * Constructor.
74      *
75      * @param a The action dictionary.
76      */

77     public PDAction( COSDictionary a )
78     {
79         action = a;
80     }
81
82     /**
83      * Convert this standard java object to a COS object.
84      *
85      * @return The cos object that matches this Java object.
86      */

87     public COSBase getCOSObject()
88     {
89         return action;
90     }
91
92     /**
93      * Convert this standard java object to a COS object.
94      *
95      * @return The cos object that matches this Java object.
96      */

97     public COSDictionary getCOSDictionary()
98     {
99         return action;
100     }
101
102     /**
103      * This will get the type of PDF object that the actions dictionary describes.
104      * If present must be Action for an action dictionary.
105      *
106      * @return The Type of PDF object.
107      */

108     public String JavaDoc getType()
109     {
110        return action.getNameAsString( "Type" );
111     }
112
113     /**
114      * This will set the type of PDF object that the actions dictionary describes.
115      * If present must be Action for an action dictionary.
116      *
117      * @param type The new Type for the PDF object.
118      */

119     public void setType( String JavaDoc type )
120     {
121        action.setName( "Type", type );
122     }
123
124     /**
125      * This will get the type of action that the actions dictionary describes.
126      * If present, must be Action for an action dictionary.
127      *
128      * @return The S entry of actions dictionary.
129      */

130     public String JavaDoc getSubType()
131     {
132        return action.getNameAsString( "S" );
133     }
134
135     /**
136      * This will set the type of action that the actions dictionary describes.
137      * If present, must be Action for an action dictionary.
138      *
139      * @param s The new type of action.
140      */

141     public void setSubType( String JavaDoc s )
142     {
143        action.setName( "S", s );
144     }
145
146     /**
147      * This will get the next action, or sequence of actions, to be performed after this one.
148      * The value is either a single action dictionary or an array of action dictionaries
149      * to be performed in order.
150      *
151      * @return The Next action or sequence of actions.
152      */

153     public List JavaDoc getNext()
154     {
155         List JavaDoc retval = null;
156         COSBase next = action.getDictionaryObject( "Next" );
157         if( next instanceof COSDictionary )
158         {
159             PDAction pdAction = PDActionFactory.createAction( (COSDictionary) next );
160             retval = new COSArrayList(pdAction, next, action, "Next" );
161         }
162         else if( next instanceof COSArray )
163         {
164             COSArray array = (COSArray)next;
165             List JavaDoc actions = new ArrayList JavaDoc();
166             for( int i=0; i<array.size(); i++ )
167             {
168                 actions.add( PDActionFactory.createAction( (COSDictionary) array.getObject( i )));
169             }
170             retval = new COSArrayList( actions, array );
171         }
172
173         return retval;
174     }
175
176     /**
177      * This will set the next action, or sequence of actions, to be performed after this one.
178      * The value is either a single action dictionary or an array of action dictionaries
179      * to be performed in order.
180      *
181      * @param next The Next action or sequence of actions.
182      */

183     public void setNext( List JavaDoc next )
184     {
185         action.setItem( "Next", COSArrayList.converterToCOSArray( next ) );
186     }
187 }
Popular Tags