KickJava   Java API By Example, From Geeks To Geeks.

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


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.CSSEngine;
24 import org.apache.batik.css.engine.CSSStylableElement;
25 import org.apache.batik.css.engine.value.Value;
26 import org.w3c.dom.DOMException JavaDoc;
27 import org.w3c.dom.css.CSSRule;
28 import org.w3c.dom.css.CSSStyleDeclaration;
29 import org.w3c.dom.css.CSSValue;
30
31 /**
32  * This class represents the computed style of an element.
33  *
34  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
35  * @version $Id: CSSOMComputedStyle.java,v 1.6 2004/08/18 07:12:47 vhardy Exp $
36  */

37 public class CSSOMComputedStyle implements CSSStyleDeclaration {
38
39     /**
40      * The CSS engine used to compute the values.
41      */

42     protected CSSEngine cssEngine;
43
44     /**
45      * The associated element.
46      */

47     protected CSSStylableElement element;
48
49     /**
50      * The optional pseudo-element.
51      */

52     protected String JavaDoc pseudoElement;
53
54     /**
55      * The CSS values.
56      */

57     protected Map JavaDoc values = new HashMap JavaDoc();
58
59     /**
60      * Creates a new computed style.
61      */

62     public CSSOMComputedStyle(CSSEngine e,
63                               CSSStylableElement elt,
64                               String JavaDoc pseudoElt) {
65         cssEngine = e;
66         element = elt;
67         pseudoElement = pseudoElt;
68     }
69
70     /**
71      * <b>DOM</b>: Implements {@link
72      * org.w3c.dom.css.CSSStyleDeclaration#getCssText()}.
73      */

74     public String JavaDoc getCssText() {
75         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
76         for (int i = 0; i < cssEngine.getNumberOfProperties(); i++) {
77             sb.append(cssEngine.getPropertyName(i));
78             sb.append(": ");
79             sb.append(cssEngine.getComputedStyle(element, pseudoElement,
80                                                  i).getCssText());
81             sb.append(";\n");
82         }
83         return sb.toString();
84     }
85
86     /**
87      * <b>DOM</b>: Implements {@link
88      * org.w3c.dom.css.CSSStyleDeclaration#setCssText(String)}.
89      * Throws a NO_MODIFICATION_ALLOWED_ERR {@link org.w3c.dom.DOMException}.
90      */

91     public void setCssText(String JavaDoc cssText) throws DOMException JavaDoc {
92         throw new DOMException JavaDoc(DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
93     }
94
95     /**
96      * <b>DOM</b>: Implements {@link
97      * org.w3c.dom.css.CSSStyleDeclaration#getPropertyValue(String)}.
98      */

99     public String JavaDoc getPropertyValue(String JavaDoc propertyName) {
100         int idx = cssEngine.getPropertyIndex(propertyName);
101         if (idx == -1) {
102             return "";
103         }
104         Value v = cssEngine.getComputedStyle(element, pseudoElement, idx);
105         return v.getCssText();
106     }
107
108     /**
109      * <b>DOM</b>: Implements {@link
110      * org.w3c.dom.css.CSSStyleDeclaration#getPropertyCSSValue(String)}.
111      */

112     public CSSValue getPropertyCSSValue(String JavaDoc propertyName) {
113         CSSValue result = (CSSValue)values.get(propertyName);
114         if (result == null) {
115             int idx = cssEngine.getPropertyIndex(propertyName);
116             if (idx != -1) {
117                 result = createCSSValue(idx);
118                 values.put(propertyName, result);
119             }
120         }
121         return result;
122     }
123
124     /**
125      * <b>DOM</b>: Implements {@link
126      * org.w3c.dom.css.CSSStyleDeclaration#removeProperty(String)}.
127      */

128     public String JavaDoc removeProperty(String JavaDoc propertyName) throws DOMException JavaDoc {
129         throw new DOMException JavaDoc(DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
130     }
131
132     /**
133      * <b>DOM</b>: Implements {@link
134      * org.w3c.dom.css.CSSStyleDeclaration#getPropertyPriority(String)}.
135      */

136     public String JavaDoc getPropertyPriority(String JavaDoc propertyName) {
137         return "";
138     }
139
140     /**
141      * <b>DOM</b>: Implements {@link
142      * org.w3c.dom.css.CSSStyleDeclaration#setProperty(String,String,String)}.
143      */

144     public void setProperty(String JavaDoc propertyName, String JavaDoc value, String JavaDoc prio)
145     throws DOMException JavaDoc {
146         throw new DOMException JavaDoc(DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
147     }
148
149     /**
150      * <b>DOM</b>: Implements {@link
151      * org.w3c.dom.css.CSSStyleDeclaration#getLength()}.
152      */

153     public int getLength() {
154         return cssEngine.getNumberOfProperties();
155     }
156
157     /**
158      * <b>DOM</b>: Implements {@link
159      * org.w3c.dom.css.CSSStyleDeclaration#item(int)}.
160      */

161     public String JavaDoc item(int index) {
162         if (index < 0 || index >= cssEngine.getNumberOfProperties()) {
163             return "";
164         }
165         return cssEngine.getPropertyName(index);
166     }
167
168     /**
169      * <b>DOM</b>: Implements {@link
170      * org.w3c.dom.css.CSSStyleDeclaration#getParentRule()}.
171      * @return null.
172      */

173     public CSSRule getParentRule() {
174         return null;
175     }
176
177     /**
178      * Creates a CSSValue to manage the value at the given index.
179      */

180     protected CSSValue createCSSValue(int idx) {
181         return new ComputedCSSValue(idx);
182     }
183
184     /**
185      * To manage a computed CSSValue.
186      */

187     public class ComputedCSSValue
188         extends CSSOMValue
189         implements CSSOMValue.ValueProvider {
190         
191         /**
192          * The index of the associated value.
193          */

194         protected int index;
195
196         /**
197          * Creates a new ComputedCSSValue.
198          */

199         public ComputedCSSValue(int idx) {
200             super(null);
201             valueProvider = this;
202             index = idx;
203         }
204
205         /**
206          * Returns the Value associated with this object.
207          */

208         public Value getValue() {
209             return cssEngine.getComputedStyle(element, pseudoElement, index);
210         }
211     }
212 }
213
Popular Tags