PDFFieldChoice.java 20.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.internal.pdftoolkit.core.cos.CosArray
 *  com.adobe.internal.pdftoolkit.core.cos.CosCloneMgr
 *  com.adobe.internal.pdftoolkit.core.cos.CosDictionary
 *  com.adobe.internal.pdftoolkit.core.cos.CosDocument
 *  com.adobe.internal.pdftoolkit.core.cos.CosNumeric
 *  com.adobe.internal.pdftoolkit.core.cos.CosObject
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
 *  com.adobe.internal.pdftoolkit.core.types.ASName
 *  com.adobe.internal.pdftoolkit.core.types.ASString
 */
package com.adobe.internal.pdftoolkit.pdf.interactive.forms;

import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosCloneMgr;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosNumeric;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCatalog;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosUtils;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFText;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFRectangle;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationWidget;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFDefaultAppearance;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFFieldNode;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFFieldUtils;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFFieldWithOptions;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPage;
import com.adobe.internal.pdftoolkit.pdf.utils.PDFUtil;
import java.util.ArrayList;
import java.util.List;

public class PDFFieldChoice
extends PDFFieldWithOptions {
    public static final int kCombo = 131072;
    public static final int kEdit = 262144;
    public static final int kSort = 524288;
    public static final int kMultiSelect = 2097152;
    public static final int kDoNotSpellCheck = 4194304;
    public static final int kCommitOnSelChange = 67108864;
    private static final String DEFAULT_CHOICEFIELD_NAME = "Button";

    private PDFFieldChoice(CosObject cosObject) throws PDFInvalidDocumentException {
        super(cosObject);
    }

    public static PDFFieldChoice getInstance(CosObject cosObject) throws PDFInvalidDocumentException {
        if (PDFCosObject.checkNullCosObject(cosObject) == null) {
            return null;
        }
        PDFFieldChoice pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFFieldChoice.class);
        if (pdfObject == null) {
            pdfObject = new PDFFieldChoice(cosObject);
        }
        return pdfObject;
    }

    public static PDFFieldChoice newInstance(String qualifiedFieldName, PDFPage page, PDFRectangle annotRect, PDFDefaultAppearance da, boolean combineFieldAnnot) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        if (page == null) {
            throw new PDFInvalidParameterException("A page must be provided for the annotation when creating fields.");
        }
        if (annotRect == null) {
            throw new PDFInvalidParameterException("An annotation rectangle must be specified when creating fields.");
        }
        CosDictionary cosObject = PDFCosObject.newCosDictionary(page.getPDFDocument());
        PDFFieldChoice field = new PDFFieldChoice((CosObject)cosObject);
        PDFInteractiveForm iform = page.getPDFDocument().requireCatalog().procureInteractiveForm();
        PDFFieldNode parent = iform.procureIntermediateFieldNodes(qualifiedFieldName);
        String fieldName = PDFFieldUtils.getTerminalFieldName(qualifiedFieldName);
        field.init(parent, fieldName, page, annotRect, combineFieldAnnot, da);
        return field;
    }

    public static PDFFieldChoice newInstance(PDFFieldNode parent, PDFPage page, PDFRectangle annotRect, PDFDefaultAppearance da, boolean combineFieldAnnot) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        if (parent == null) {
            throw new PDFInvalidParameterException("A parent field node must be specified when creating fields.");
        }
        if (page == null) {
            throw new PDFInvalidParameterException("A page must be provided for the annotation when creating fields.");
        }
        if (annotRect == null) {
            throw new PDFInvalidParameterException("An annotation rectangle must be specified when creating fields.");
        }
        CosDictionary cosObject = PDFCosObject.newCosDictionary(page.getPDFDocument());
        PDFFieldChoice field = new PDFFieldChoice((CosObject)cosObject);
        field.init(parent, null, page, annotRect, combineFieldAnnot, da);
        return field;
    }

    private void init(PDFFieldNode parent, String fieldName, PDFPage page, PDFRectangle annotRect, boolean combineFieldAnnot, PDFDefaultAppearance da) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        this.init(parent, fieldName, "Button", da, false);
        this.initWidgetAnnot(page, annotRect, combineFieldAnnot);
        this.setDictionaryNameValue(ASName.k_FT, ASName.k_Ch);
    }

    public boolean isComboBox() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return (this.getFlags() & 131072) != 0;
    }

    public void setComboBox(boolean value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (value) {
            this.setFlags(this.getFlags() | 131072);
        } else {
            this.setFlags(this.getFlags() & -131073);
        }
    }

    public boolean hasEditableTextBox() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.isComboBox() && (this.getFlags() & 262144) != 0;
    }

    public void setEditable(boolean value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.isComboBox()) {
            if (value) {
                this.setFlags(this.getFlags() | 262144);
            } else {
                this.setFlags(this.getFlags() & -262145);
            }
        }
    }

    public boolean shouldSort() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return (this.getFlags() & 524288) != 0;
    }

    public void setSort(boolean value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (value) {
            this.setFlags(this.getFlags() | 524288);
        } else {
            this.setFlags(this.getFlags() & -524289);
        }
    }

    public boolean isMultipleSelectionAllowed() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return (this.getFlags() & 2097152) != 0;
    }

    public void setMultipleSelectionAllowed(boolean value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (value) {
            this.setFlags(this.getFlags() | 2097152);
        } else {
            this.setFlags(this.getFlags() & -2097153);
        }
    }

    public void setcommitOnSelChange(boolean value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (value) {
            this.setFlags(this.getFlags() | 67108864);
        } else {
            this.setFlags(this.getFlags() & -67108865);
        }
    }

    public boolean shouldNotSpellCheck() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.isComboBox() && this.hasEditableTextBox() && (this.getFlags() & 4194304) != 0;
    }

    public CosObject getOptionValue() throws PDFCosParseException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFFieldNode parent;
        CosObject options = this.getDictionaryCosObjectValue(ASName.k_Opt);
        if (options == null && this.isAcrobatAnnotation() && (parent = this.getParent()) != null) {
            options = parent.getDictionaryCosObjectValue(ASName.k_Opt);
        }
        return options;
    }

    public int getOptionValueSize() throws PDFCosParseException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return ((CosArray)this.getOptionValue()).size();
    }

    protected void setOptionValue(CosObject options) throws PDFCosParseException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryValue(ASName.k_Opt, options);
    }

    public boolean commitOnSelectionChange() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return (this.getFlags() & 67108864) != 0;
    }

    public boolean hasTopIndex() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.dictionaryContains(ASName.k_TI);
    }

    public int getTopIndex() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFFieldNode parent;
        Number topIdx = this.getDictionaryNumericValue(ASName.k_TI);
        if (topIdx == null && this.isAcrobatAnnotation() && (parent = this.getParent()) != null) {
            topIdx = parent.getDictionaryNumericValue(ASName.k_TI);
        }
        return topIdx == null ? 0 : topIdx.intValue();
    }

    public void setTopIndex(int topIndex) throws PDFInvalidParameterException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (topIndex < 0) {
            throw new PDFInvalidParameterException("Invalid value of TopIndex: " + topIndex);
        }
        this.setDictionaryIntValue(ASName.k_TI, topIndex);
    }

    public boolean hasIndexArray() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return PDFCosUtils.containsInheritableKey(ASName.k_I, ASName.k_Parent, this);
    }

    public void setIndexArray(int[] arr) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (arr == null || arr.length == 0) {
            this.removeValue(ASName.k_I);
            return;
        }
        CosArray inArr = PDFCosObject.newCosArray(this.getPDFDocument());
        for (int i = 0; i < arr.length; ++i) {
            inArr.addInt(i, arr[i]);
        }
        this.setDictionaryArrayValue(ASName.k_I, inArr);
        Object selected = this.getSelectedItem();
        this.setStringValue(selected.toString());
    }

    public int[] getIndexArray() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject cosObject = PDFCosUtils.getInheritableValue(ASName.k_I, ASName.k_Parent, this);
        if (!(cosObject instanceof CosArray)) {
            return null;
        }
        CosArray cosArray = (CosArray)cosObject;
        if (cosArray == null || cosArray.size() == 0) {
            return null;
        }
        ArrayList<Integer> indexArray = new ArrayList<Integer>();
        for (int arrayInd = 0; arrayInd < cosArray.size(); ++arrayInd) {
            CosObject arrayItem = cosArray.get(arrayInd);
            if (!(arrayItem instanceof CosNumeric)) continue;
            indexArray.add(arrayItem.intValue());
        }
        if (indexArray.isEmpty()) {
            return null;
        }
        int[] array = new int[indexArray.size()];
        for (int idx = 0; idx < indexArray.size(); ++idx) {
            array[idx] = (Integer)indexArray.get(idx);
        }
        return array;
    }

    public void insertItemAt(int pos, String item, String exportValue) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject options = this.cloneOptionValue();
        ASString astr = new ASString(item);
        CosArray valArr = this.getPDFDocument().getCosDocument().createCosArray();
        if (options instanceof CosArray) {
            this.checkDuplicate((CosArray)options, astr);
            valArr.addString(new ASString(exportValue));
            valArr.addString(astr);
            ((CosArray)options).add(pos, (CosObject)valArr);
        } else {
            options = PDFCosObject.newCosArray(this.getPDFDocument());
            this.setOptionValue(options);
            valArr.addString(new ASString(exportValue));
            valArr.addString(astr);
            ((CosArray)options).add(pos, (CosObject)valArr);
        }
        Object selected = this.getSelectedItem();
        if (selected != null) {
            this.setStringValue(selected.toString());
        }
    }

    private int checkDuplicate(CosArray options, ASString astr) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        for (int i = 0; i < options.size(); ++i) {
            CosArray arr = options.getCosArray(i);
            if (arr.findString(astr) == -1) continue;
            options.remove(i);
        }
        return -1;
    }

    public Object getSelectedItem() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject arr = this.getOptionValue();
        int[] iarr = this.getIndexArray();
        String value = null;
        if (arr != null && iarr != null) {
            int idxSelection = iarr[0];
            List options = this.getOptionList();
            if (options.size() > idxSelection) {
                value = this.getValueFromOptions(this.getOptionList().get(idxSelection), this.getQualifiedName());
                return value;
            }
            if (options.get(0) instanceof List) {
                options = (List)options.get(0);
                value = this.getValueFromOptions(options.get(idxSelection), this.getQualifiedName());
            }
            return value;
        }
        if (arr instanceof CosArray) {
            CosObject selected = ((CosArray)arr).get(0);
            if (selected instanceof CosArray) {
                return ((CosArray)selected).get(0).textValue();
            }
            return selected.textValue();
        }
        return null;
    }

    private String getValueFromOptions(Object value, String fieldName) throws PDFInvalidDocumentException {
        String optionValue = null;
        if (value instanceof String) {
            optionValue = (String)value;
        }
        if (value instanceof ArrayList && value != null && ((ArrayList)value).size() > 0) {
            if (((ArrayList)value).size() > 2) {
                throw new PDFInvalidDocumentException("More than 2 elements in Opt array for choice field " + fieldName + ".");
            }
            optionValue = ((ArrayList)value).size() == 1 ? (String)((ArrayList)value).get(0) : (String)((ArrayList)value).get(1);
        }
        return optionValue;
    }

    public Object getItemAt(int pos, boolean exportValue) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject arr = this.getOptionValue();
        if (arr instanceof CosArray) {
            CosObject selected = ((CosArray)arr).get(pos);
            if (selected instanceof CosArray) {
                if (exportValue) {
                    return ((CosArray)selected).get(0).textValue();
                }
                return ((CosArray)selected).get(1).textValue();
            }
            return selected.textValue();
        }
        return null;
    }

    public void deleteItemAt(int pos) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject options = this.cloneOptionValue();
        if (options instanceof CosArray) {
            ((CosArray)options).remove(pos);
        }
    }

    public void removeAllItems() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject options = this.cloneOptionValue();
        if (options instanceof CosArray) {
            ((CosArray)options).clear();
        }
        this.removeValue(ASName.k_V);
        this.removeValue(ASName.k_DV);
        this.removeValue(ASName.k_I);
    }

    public void setdoNotSpellCheck(boolean value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.isComboBox();
        if (value) {
            this.setFlags(this.getFlags() | 4194304);
        } else {
            this.setFlags(this.getFlags() | -4194305);
        }
    }

    public Object getValue() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.getSelectedItem();
    }

    private CosObject cloneOptionValue() throws PDFCosParseException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        boolean needClone = false;
        CosObject options = this.getDictionaryCosObjectValue(ASName.k_Opt);
        if (options != null) {
            if (options.isIndirect()) {
                needClone = true;
            }
        } else {
            options = this.getOptionValue();
            if (options != null && options.isIndirect()) {
                needClone = true;
            }
        }
        if (needClone) {
            options = new CosCloneMgr(this.getPDFDocument().getCosDocument()).clone(options);
            this.setOptionValue(options);
        }
        return options;
    }

    public void setOptIndex() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        List values = this.getValueList();
        List options = this.getOptionList();
        if (values == null || options == null) {
            return;
        }
        String[] optionStrings = new String[options.size()];
        for (int optInd = 0; optInd < options.size(); ++optInd) {
            Object curOption = options.get(optInd);
            if (curOption instanceof ArrayList) {
                curOption = ((ArrayList)curOption).get(0);
            }
            optionStrings[optInd] = (String)curOption;
        }
        ArrayList<Integer> optIndeces = new ArrayList<Integer>(optionStrings.length);
        for (Object curValue : values) {
            int valueIndex;
            if (!(curValue instanceof String) || (valueIndex = PDFUtil.findStringInArray((String)curValue, optionStrings)) < 0) continue;
            optIndeces.add(valueIndex);
        }
        this.setDictionaryArrayValue(ASName.k_I, optIndeces);
    }

    public boolean hasOptionList() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return super.getCosDictionary().get(ASName.k_Opt) != null;
    }

    public List getOptionList() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject options = this.getOptionValue();
        if (options == null) {
            return null;
        }
        return this.addOptionsItem(null, options);
    }

    private List addOptionsItem(ArrayList optionList, CosObject cosItem) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (cosItem.getType() == 4) {
            PDFText text = PDFText.getInstance(cosItem);
            if (optionList == null) {
                optionList = new ArrayList<String>();
            }
            optionList.add(text.stringValue());
        } else if (cosItem.getType() == 5) {
            CosArray cosArray = (CosArray)cosItem;
            ArrayList arrayVal = new ArrayList(2);
            for (int arrayInd = 0; arrayInd < cosArray.size(); ++arrayInd) {
                CosObject arrayItem = cosArray.get(arrayInd);
                this.addOptionsItem(arrayVal, arrayItem);
            }
            if (optionList == null) {
                optionList = arrayVal;
            } else {
                optionList.add((String)((Object)arrayVal));
            }
        }
        return optionList;
    }

    public void setOptionList(List option) throws PDFIOException, PDFInvalidParameterException, PDFInvalidDocumentException, PDFSecurityException {
        this.setDictionaryArrayValue(ASName.k_Opt, option);
    }

    public void setOptionList(String option) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryStringValue(ASName.k_Opt, option);
    }
}