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.documentinterchange.logicalstructure; 18 19 import org.apache.pdfbox.cos.COSBase; 20 import org.apache.pdfbox.cos.COSDictionary; 21 import org.apache.pdfbox.cos.COSName; 22 import org.apache.pdfbox.pdmodel.common.PDDictionaryWrapper; 23 24 /** 25 * A user property. 26 * 27 * @author <a href="mailto:Johannes%20Koch%20%3Ckoch@apache.org%3E">Johannes Koch</a> 28 * @version $Revision: $ 29 */ 30 public class PDUserProperty extends PDDictionaryWrapper 31 { 32 33 private final PDUserAttributeObject userAttributeObject; 34 35 /** 36 * Creates a new user property. 37 * 38 * @param the user attribute object 39 */ 40 public PDUserProperty(PDUserAttributeObject userAttributeObject) 41 { 42 this.userAttributeObject = userAttributeObject; 43 } 44 45 /** 46 * Creates a user property with a given dictionary. 47 * 48 * @param dictionary the dictionary 49 * @param the user attribute object 50 */ 51 public PDUserProperty(COSDictionary dictionary, 52 PDUserAttributeObject userAttributeObject) 53 { 54 super(dictionary); 55 this.userAttributeObject = userAttributeObject; 56 } 57 58 59 /** 60 * Returns the property name. 61 * 62 * @return the property name 63 */ 64 public String getName() 65 { 66 return this.getCOSDictionary().getNameAsString(COSName.N); 67 } 68 69 /** 70 * Sets the property name. 71 * 72 * @param name the property name 73 */ 74 public void setName(String name) 75 { 76 this.potentiallyNotifyChanged(this.getName(), name); 77 this.getCOSDictionary().setName(COSName.N, name); 78 } 79 80 /** 81 * Returns the property value. 82 * 83 * @return the property value 84 */ 85 public COSBase getValue() 86 { 87 return this.getCOSDictionary().getDictionaryObject(COSName.V); 88 } 89 90 /** 91 * Sets the property value. 92 * 93 * @param value the property value 94 */ 95 public void setValue(COSBase value) 96 { 97 this.potentiallyNotifyChanged(this.getValue(), value); 98 this.getCOSDictionary().setItem(COSName.V, value); 99 } 100 101 /** 102 * Returns the string for the property value. 103 * 104 * @return the string for the property value 105 */ 106 public String getFormattedValue() 107 { 108 return this.getCOSDictionary().getString(COSName.F); 109 } 110 111 /** 112 * Sets the string for the property value. 113 * 114 * @param formattedValue the string for the property value 115 */ 116 public void setFormattedValue(String formattedValue) 117 { 118 this.potentiallyNotifyChanged(this.getFormattedValue(), formattedValue); 119 this.getCOSDictionary().setString(COSName.F, formattedValue); 120 } 121 122 /** 123 * Shall the property be hidden? 124 * 125 * @return <code>true</code> if the property shall be hidden, 126 * <code>false</code> otherwise 127 */ 128 public boolean isHidden() 129 { 130 return this.getCOSDictionary().getBoolean(COSName.H, false); 131 } 132 133 /** 134 * Specifies whether the property shall be hidden. 135 * 136 * @param hidden <code>true</code> if the property shall be hidden, 137 * <code>false</code> otherwise 138 */ 139 public void setHidden(boolean hidden) 140 { 141 this.potentiallyNotifyChanged(this.isHidden(), hidden); 142 this.getCOSDictionary().setBoolean(COSName.H, hidden); 143 } 144 145 146 @Override 147 public String toString() 148 { 149 return new StringBuilder("Name=").append(this.getName()) 150 .append(", Value=").append(this.getValue()) 151 .append(", FormattedValue=").append(this.getFormattedValue()) 152 .append(", Hidden=").append(this.isHidden()).toString(); 153 } 154 155 156 /** 157 * Notifies the user attribute object if the user property is changed. 158 * 159 * @param oldEntry old entry 160 * @param newEntry new entry 161 */ 162 private void potentiallyNotifyChanged(Object oldEntry, Object newEntry) 163 { 164 if (this.isEntryChanged(oldEntry, newEntry)) 165 { 166 this.userAttributeObject.userPropertyChanged(this); 167 } 168 } 169 170 /** 171 * Is the value changed? 172 * 173 * @param oldEntry old entry 174 * @param newEntry new entry 175 * @return <code>true</code> if the entry is changed, <code>false</code> 176 * otherwise 177 */ 178 private boolean isEntryChanged(Object oldEntry, Object newEntry) 179 { 180 if (oldEntry == null) 181 { 182 if (newEntry == null) 183 { 184 return false; 185 } 186 return true; 187 } 188 return !oldEntry.equals(newEntry); 189 } 190 191 }