1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.pdmodel.interactive.action.type; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 22 import org.apache.pdfbox.cos.COSArray; 23 import org.apache.pdfbox.cos.COSBase; 24 import org.apache.pdfbox.cos.COSDictionary; 25 import org.apache.pdfbox.cos.COSName; 26 27 import org.apache.pdfbox.pdmodel.common.COSArrayList; 28 import org.apache.pdfbox.pdmodel.common.PDDestinationOrAction; 29 import org.apache.pdfbox.pdmodel.interactive.action.PDActionFactory; 30 31 /** 32 * This represents an action that can be executed in a PDF document. 33 * 34 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 35 * @author Panagiotis Toumasis (ptoumasis@mail.gr) 36 * @version $Revision: 1.3 $ 37 */ 38 public abstract class PDAction implements PDDestinationOrAction 39 { 40 /** 41 * The type of PDF object. 42 */ 43 public static final String TYPE = "Action"; 44 45 /** 46 * The action dictionary. 47 */ 48 protected COSDictionary action; 49 50 /** 51 * Default constructor. 52 */ 53 public PDAction() 54 { 55 action = new COSDictionary(); 56 setType( TYPE ); 57 } 58 59 /** 60 * Constructor. 61 * 62 * @param a The action dictionary. 63 */ 64 public PDAction( COSDictionary a ) 65 { 66 action = a; 67 } 68 69 /** 70 * Convert this standard java object to a COS object. 71 * 72 * @return The cos object that matches this Java object. 73 */ 74 public COSBase getCOSObject() 75 { 76 return action; 77 } 78 79 /** 80 * Convert this standard java object to a COS object. 81 * 82 * @return The cos object that matches this Java object. 83 */ 84 public COSDictionary getCOSDictionary() 85 { 86 return action; 87 } 88 89 /** 90 * This will get the type of PDF object that the actions dictionary describes. 91 * If present must be Action for an action dictionary. 92 * 93 * @return The Type of PDF object. 94 */ 95 public String getType() 96 { 97 return action.getNameAsString( "Type" ); 98 } 99 100 /** 101 * This will set the type of PDF object that the actions dictionary describes. 102 * If present must be Action for an action dictionary. 103 * 104 * @param type The new Type for the PDF object. 105 */ 106 public void setType( String type ) 107 { 108 action.setName( "Type", type ); 109 } 110 111 /** 112 * This will get the type of action that the actions dictionary describes. 113 * If present, must be Action for an action dictionary. 114 * 115 * @return The S entry of actions dictionary. 116 */ 117 public String getSubType() 118 { 119 return action.getNameAsString( "S" ); 120 } 121 122 /** 123 * This will set the type of action that the actions dictionary describes. 124 * If present, must be Action for an action dictionary. 125 * 126 * @param s The new type of action. 127 */ 128 public void setSubType( String s ) 129 { 130 action.setName( "S", s ); 131 } 132 133 /** 134 * This will get the next action, or sequence of actions, to be performed after this one. 135 * The value is either a single action dictionary or an array of action dictionaries 136 * to be performed in order. 137 * 138 * @return The Next action or sequence of actions. 139 */ 140 public List getNext() 141 { 142 List retval = null; 143 COSBase next = action.getDictionaryObject( "Next" ); 144 if( next instanceof COSDictionary ) 145 { 146 PDAction pdAction = PDActionFactory.createAction( (COSDictionary) next ); 147 retval = new COSArrayList(pdAction, next, action, COSName.getPDFName( "Next" )); 148 } 149 else if( next instanceof COSArray ) 150 { 151 COSArray array = (COSArray)next; 152 List actions = new ArrayList(); 153 for( int i=0; i<array.size(); i++ ) 154 { 155 actions.add( PDActionFactory.createAction( (COSDictionary) array.getObject( i ))); 156 } 157 retval = new COSArrayList( actions, array ); 158 } 159 160 return retval; 161 } 162 163 /** 164 * This will set the next action, or sequence of actions, to be performed after this one. 165 * The value is either a single action dictionary or an array of action dictionaries 166 * to be performed in order. 167 * 168 * @param next The Next action or sequence of actions. 169 */ 170 public void setNext( List next ) 171 { 172 action.setItem( "Next", COSArrayList.converterToCOSArray( next ) ); 173 } 174 }