]> _ Git - cubeextranet.git/blob
9b463fe4cb9764b06e233e21ecdba1e0fb5b8e78
[cubeextranet.git] /
1 /*\r
2  * Licensed to the Apache Software Foundation (ASF) under one or more\r
3  * contributor license agreements.  See the NOTICE file distributed with\r
4  * this work for additional information regarding copyright ownership.\r
5  * The ASF licenses this file to You under the Apache License, Version 2.0\r
6  * (the "License"); you may not use this file except in compliance with\r
7  * the License.  You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 package org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure;\r
18 \r
19 import org.apache.pdfbox.cos.COSBase;\r
20 import org.apache.pdfbox.cos.COSDictionary;\r
21 import org.apache.pdfbox.cos.COSName;\r
22 import org.apache.pdfbox.pdmodel.common.PDDictionaryWrapper;\r
23 \r
24 /**\r
25  * A user property.\r
26  * \r
27  * @author <a href="mailto:Johannes%20Koch%20%3Ckoch@apache.org%3E">Johannes Koch</a>\r
28  * @version $Revision: $\r
29  */\r
30 public class PDUserProperty extends PDDictionaryWrapper\r
31 {\r
32 \r
33     private final PDUserAttributeObject userAttributeObject;\r
34 \r
35     /**\r
36      * Creates a new user property.\r
37      * \r
38      * @param the user attribute object\r
39      */\r
40     public PDUserProperty(PDUserAttributeObject userAttributeObject)\r
41     {\r
42         this.userAttributeObject = userAttributeObject;\r
43     }\r
44 \r
45     /**\r
46      * Creates a user property with a given dictionary.\r
47      * \r
48      * @param dictionary the dictionary\r
49      * @param the user attribute object\r
50      */\r
51     public PDUserProperty(COSDictionary dictionary,\r
52         PDUserAttributeObject userAttributeObject)\r
53     {\r
54         super(dictionary);\r
55         this.userAttributeObject = userAttributeObject;\r
56     }\r
57 \r
58 \r
59     /**\r
60      * Returns the property name.\r
61      * \r
62      * @return the property name\r
63      */\r
64     public String getName()\r
65     {\r
66         return this.getCOSDictionary().getNameAsString(COSName.N);\r
67     }\r
68 \r
69     /**\r
70      * Sets the property name.\r
71      * \r
72      * @param name the property name\r
73      */\r
74     public void setName(String name)\r
75     {\r
76         this.potentiallyNotifyChanged(this.getName(), name);\r
77         this.getCOSDictionary().setName(COSName.N, name);\r
78     }\r
79 \r
80     /**\r
81      * Returns the property value.\r
82      * \r
83      * @return the property value\r
84      */\r
85     public COSBase getValue()\r
86     {\r
87         return this.getCOSDictionary().getDictionaryObject(COSName.V);\r
88     }\r
89 \r
90     /**\r
91      * Sets the property value.\r
92      * \r
93      * @param value the property value\r
94      */\r
95     public void setValue(COSBase value)\r
96     {\r
97         this.potentiallyNotifyChanged(this.getValue(), value);\r
98         this.getCOSDictionary().setItem(COSName.V, value);\r
99     }\r
100 \r
101     /**\r
102      * Returns the string for the property value.\r
103      * \r
104      * @return the string for the property value\r
105      */\r
106     public String getFormattedValue()\r
107     {\r
108         return this.getCOSDictionary().getString(COSName.F);\r
109     }\r
110 \r
111     /**\r
112      * Sets the string for the property value.\r
113      * \r
114      * @param formattedValue the string for the property value\r
115      */\r
116     public void setFormattedValue(String formattedValue)\r
117     {\r
118         this.potentiallyNotifyChanged(this.getFormattedValue(), formattedValue);\r
119         this.getCOSDictionary().setString(COSName.F, formattedValue);\r
120     }\r
121 \r
122     /**\r
123      * Shall the property be hidden?\r
124      * \r
125      * @return <code>true</code> if the property shall be hidden,\r
126      * <code>false</code> otherwise\r
127      */\r
128     public boolean isHidden()\r
129     {\r
130         return this.getCOSDictionary().getBoolean(COSName.H, false);\r
131     }\r
132 \r
133     /**\r
134      * Specifies whether the property shall be hidden.\r
135      * \r
136      * @param hidden <code>true</code> if the property shall be hidden,\r
137      * <code>false</code> otherwise\r
138      */\r
139     public void setHidden(boolean hidden)\r
140     {\r
141         this.potentiallyNotifyChanged(this.isHidden(), hidden);\r
142         this.getCOSDictionary().setBoolean(COSName.H, hidden);\r
143     }\r
144 \r
145 \r
146     @Override\r
147     public String toString()\r
148     {\r
149         return new StringBuilder("Name=").append(this.getName())\r
150             .append(", Value=").append(this.getValue())\r
151             .append(", FormattedValue=").append(this.getFormattedValue())\r
152             .append(", Hidden=").append(this.isHidden()).toString();\r
153     }\r
154 \r
155 \r
156     /**\r
157      * Notifies the user attribute object if the user property is changed.\r
158      * \r
159      * @param oldEntry old entry\r
160      * @param newEntry new entry\r
161      */\r
162     private void potentiallyNotifyChanged(Object oldEntry, Object newEntry)\r
163     {\r
164         if (this.isEntryChanged(oldEntry, newEntry))\r
165         {\r
166             this.userAttributeObject.userPropertyChanged(this);\r
167         }\r
168     }\r
169 \r
170     /**\r
171      * Is the value changed?\r
172      * \r
173      * @param oldEntry old entry\r
174      * @param newEntry new entry\r
175      * @return <code>true</code> if the entry is changed, <code>false</code>\r
176      * otherwise\r
177      */\r
178     private boolean isEntryChanged(Object oldEntry, Object newEntry)\r
179     {\r
180         if (oldEntry == null)\r
181         {\r
182             if (newEntry == null)\r
183             {\r
184                 return false;\r
185             }\r
186             return true;\r
187         }\r
188         return !oldEntry.equals(newEntry);\r
189     }\r
190 \r
191 }\r