KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > dom > CSSOMStyleDeclaration


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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  */

18 package org.apache.batik.css.dom;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.batik.css.engine.value.Value;
24 import org.w3c.dom.DOMException JavaDoc;
25 import org.w3c.dom.css.CSSRule;
26 import org.w3c.dom.css.CSSStyleDeclaration;
27 import org.w3c.dom.css.CSSValue;
28
29 /**
30  * This class represents a style declaration.
31  *
32  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
33  * @version $Id: CSSOMStyleDeclaration.java,v 1.7 2004/08/18 07:12:47 vhardy Exp $
34  */

35 public class CSSOMStyleDeclaration implements CSSStyleDeclaration {
36
37     /**
38      * The associated value.
39      */

40     protected ValueProvider valueProvider;
41
42     /**
43      * The modifications handler.
44      */

45     protected ModificationHandler handler;
46
47     /**
48      * The parent rule.
49      */

50     protected CSSRule parentRule;
51
52     /**
53      * The values.
54      */

55     protected Map JavaDoc values;
56
57     /**
58      * Creates a new style declaration.
59      */

60     public CSSOMStyleDeclaration(ValueProvider vp, CSSRule parent) {
61         valueProvider = vp;
62         parentRule = parent;
63     }
64
65     /**
66      * Sets the modification handler of this value.
67      */

68     public void setModificationHandler(ModificationHandler h) {
69         handler = h;
70     }
71
72     /**
73      * <b>DOM</b>: Implements {@link
74      * org.w3c.dom.css.CSSStyleDeclaration#getCssText()}.
75      */

76     public String JavaDoc getCssText() {
77         return valueProvider.getText();
78     }
79
80     /**
81      * <b>DOM</b>: Implements {@link
82      * org.w3c.dom.css.CSSStyleDeclaration#setCssText(String)}.
83      */

84     public void setCssText(String JavaDoc cssText) throws DOMException JavaDoc {
85     if (handler == null) {
86             throw new DOMException JavaDoc
87                 (DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
88     } else {
89             values = null;
90             handler.textChanged(cssText);
91     }
92     }
93
94     /**
95      * <b>DOM</b>: Implements {@link
96      * org.w3c.dom.css.CSSStyleDeclaration#getPropertyValue(String)}.
97      */

98     public String JavaDoc getPropertyValue(String JavaDoc propertyName) {
99         Value value = valueProvider.getValue(propertyName);
100         if (value == null) {
101             return "";
102         }
103         return value.getCssText();
104     }
105
106     /**
107      * <b>DOM</b>: Implements {@link
108      * org.w3c.dom.css.CSSStyleDeclaration#getPropertyCSSValue(String)}.
109      */

110     public CSSValue getPropertyCSSValue(String JavaDoc propertyName) {
111         Value value = valueProvider.getValue(propertyName);
112         if (value == null) {
113             return null;
114         }
115         return getCSSValue(propertyName);
116     }
117
118     /**
119      * <b>DOM</b>: Implements {@link
120      * org.w3c.dom.css.CSSStyleDeclaration#removeProperty(String)}.
121      */

122     public String JavaDoc removeProperty(String JavaDoc propertyName) throws DOMException JavaDoc {
123         String JavaDoc result = getPropertyValue(propertyName);
124         if (result.length() > 0) {
125             if (handler == null) {
126                 throw new DOMException JavaDoc
127                     (DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
128             } else {
129                 if (values != null) {
130                     values.remove(propertyName);
131                 }
132                 handler.propertyRemoved(propertyName);
133             }
134         }
135         return result;
136     }
137
138     /**
139      * <b>DOM</b>: Implements {@link
140      * org.w3c.dom.css.CSSStyleDeclaration#getPropertyPriority(String)}.
141      */

142     public String JavaDoc getPropertyPriority(String JavaDoc propertyName) {
143         return (valueProvider.isImportant(propertyName)) ? "important" : "";
144     }
145
146     /**
147      * <b>DOM</b>: Implements {@link
148      * org.w3c.dom.css.CSSStyleDeclaration#setProperty(String,String,String)}.
149      */

150     public void setProperty(String JavaDoc propertyName, String JavaDoc value, String JavaDoc prio)
151     throws DOMException JavaDoc {
152         if (handler == null) {
153             throw new DOMException JavaDoc
154                 (DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
155         } else {
156             handler.propertyChanged(propertyName, value, prio);
157         }
158     }
159
160     /**
161      * <b>DOM</b>: Implements {@link
162      * org.w3c.dom.css.CSSStyleDeclaration#getLength()}.
163      */

164     public int getLength() {
165         return valueProvider.getLength();
166     }
167
168     /**
169      * <b>DOM</b>: Implements {@link
170      * org.w3c.dom.css.CSSStyleDeclaration#item(int)}.
171      */

172     public String JavaDoc item(int index) {
173         return valueProvider.item(index);
174     }
175
176     /**
177      * <b>DOM</b>: Implements {@link
178      * org.w3c.dom.css.CSSStyleDeclaration#getParentRule()}.
179      */

180     public CSSRule getParentRule() {
181         return parentRule;
182     }
183
184     /**
185      * Gets the CSS value associated with the given property.
186      */

187     protected CSSValue getCSSValue(String JavaDoc name) {
188         CSSValue result = null;
189         if (values != null) {
190             result = (CSSValue)values.get(name);
191         }
192         if (result == null) {
193             result = createCSSValue(name);
194             if (values == null) {
195                 values = new HashMap JavaDoc(11);
196             }
197             values.put(name, result);
198         }
199         return result;
200     }
201
202     /**
203      * Creates the CSS value associated with the given property.
204      */

205     protected CSSValue createCSSValue(String JavaDoc name) {
206         return new StyleDeclarationValue(name);
207     }
208
209     /**
210      * To provides the values.
211      */

212     public interface ValueProvider {
213
214         /**
215          * Returns the current value associated with this object.
216          */

217         Value getValue(String JavaDoc name);
218
219         /**
220          * Tells whether the given property is important.
221          */

222         boolean isImportant(String JavaDoc name);
223
224         /**
225          * Returns the text of the declaration.
226          */

227         String JavaDoc getText();
228
229         /**
230          * Returns the length of the declaration.
231          */

232         int getLength();
233
234         /**
235          * Returns the value at the given.
236          */

237         String JavaDoc item(int idx);
238
239     }
240
241     /**
242      * To manage the modifications on a CSS value.
243      */

244     public interface ModificationHandler {
245
246         /**
247          * Called when the value text has changed.
248          */

249         void textChanged(String JavaDoc text) throws DOMException JavaDoc;
250
251         /**
252          * Called when a property was removed.
253          */

254         void propertyRemoved(String JavaDoc name) throws DOMException JavaDoc;
255
256         /**
257          * Called when a property was changed.
258          */

259         void propertyChanged(String JavaDoc name, String JavaDoc value, String JavaDoc prio)
260             throws DOMException JavaDoc;
261
262     }
263
264     /**
265      * This class represents a CSS value returned by this declaration.
266      */

267     public class StyleDeclarationValue
268         extends CSSOMValue
269         implements CSSOMValue.ValueProvider {
270         
271         /**
272          * The property name.
273          */

274         protected String JavaDoc property;
275
276         /**
277          * Creates a new StyleDeclarationValue.
278          */

279         public StyleDeclarationValue(String JavaDoc prop) {
280             super(null);
281             this.valueProvider = this;
282             this.setModificationHandler(new AbstractModificationHandler() {
283                     protected Value getValue() {
284                         return StyleDeclarationValue.this.getValue();
285                     }
286                     public void textChanged(String JavaDoc text) throws DOMException JavaDoc {
287                         if (values == null ||
288                             values.get(this) == null ||
289                             StyleDeclarationValue.this.handler == null) {
290                             throw new DOMException JavaDoc
291                                 (DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
292                         }
293                         String JavaDoc prio = getPropertyPriority(property);
294                         CSSOMStyleDeclaration.this.
295                             handler.propertyChanged(property, text, prio);
296                     }
297                 });
298
299             property = prop;
300         }
301
302         // ValueProvider ///////////////////////////////
303

304         /**
305          * Returns the current value associated with this object.
306          */

307         public Value getValue() {
308             return CSSOMStyleDeclaration.this.valueProvider.getValue(property);
309         }
310
311     }
312 }
313
Popular Tags