KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > svg > SVGStylableElement


1 /*
2
3    Copyright 2001-2004 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.dom.svg;
19
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import org.apache.batik.css.dom.CSSOMSVGColor;
24 import org.apache.batik.css.dom.CSSOMSVGPaint;
25 import org.apache.batik.css.dom.CSSOMSVGStyleDeclaration;
26 import org.apache.batik.css.dom.CSSOMValue;
27 import org.apache.batik.css.engine.CSSEngine;
28 import org.apache.batik.css.engine.CSSStylableElement;
29 import org.apache.batik.css.engine.SVGCSSEngine;
30 import org.apache.batik.css.engine.StyleMap;
31 import org.apache.batik.css.engine.value.Value;
32 import org.apache.batik.css.engine.value.svg.SVGColorManager;
33 import org.apache.batik.css.engine.value.svg.SVGPaintManager;
34 import org.apache.batik.dom.AbstractDocument;
35 import org.w3c.dom.Attr JavaDoc;
36 import org.w3c.dom.DOMException JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.css.CSSStyleDeclaration;
39 import org.w3c.dom.css.CSSValue;
40 import org.w3c.dom.svg.SVGAnimatedString;
41
42 /**
43  * This class provides a common superclass for elements which implement
44  * SVGStylable.
45  *
46  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
47  * @version $Id: SVGStylableElement.java,v 1.17 2005/02/22 09:13:01 cam Exp $
48  */

49 public abstract class SVGStylableElement
50     extends SVGOMElement
51     implements CSSStylableElement {
52
53     /**
54      * The computed style map.
55      */

56     protected StyleMap computedStyleMap;
57
58     /**
59      * Creates a new SVGStylableElement object.
60      */

61     protected SVGStylableElement() {
62     }
63
64     /**
65      * Creates a new SVGStylableElement object.
66      * @param prefix The namespace prefix.
67      * @param owner The owner document.
68      */

69     protected SVGStylableElement(String JavaDoc prefix, AbstractDocument owner) {
70         super(prefix, owner);
71     }
72     
73     // CSSStylableElement //////////////////////////////////////////
74

75     /**
76      * Returns the computed style of this element/pseudo-element.
77      */

78     public StyleMap getComputedStyleMap(String JavaDoc pseudoElement) {
79         return computedStyleMap;
80     }
81
82     /**
83      * Sets the computed style of this element/pseudo-element.
84      */

85     public void setComputedStyleMap(String JavaDoc pseudoElement, StyleMap sm) {
86         computedStyleMap = sm;
87     }
88
89     /**
90      * Returns the ID of this element.
91      */

92     public String JavaDoc getXMLId() {
93         return getAttributeNS(null, "id");
94     }
95
96     /**
97      * Returns the class of this element.
98      */

99     public String JavaDoc getCSSClass() {
100         return getAttributeNS(null, "class");
101     }
102
103     /**
104      * Returns the CSS base URL of this element.
105      */

106     public URL JavaDoc getCSSBase() {
107         try {
108             String JavaDoc bu = XMLBaseSupport.getCascadedXMLBase(this);
109             if (bu == null) {
110                 return null;
111             }
112             return new URL JavaDoc(bu);
113         } catch (MalformedURLException JavaDoc e) {
114             // !!! TODO
115
e.printStackTrace();
116             throw new InternalError JavaDoc();
117         }
118     }
119
120     /**
121      * Tells whether this element is an instance of the given pseudo
122      * class.
123      */

124     public boolean isPseudoInstanceOf(String JavaDoc pseudoClass) {
125         if (pseudoClass.equals("first-child")) {
126             Node JavaDoc n = getPreviousSibling();
127             while (n != null && n.getNodeType() != ELEMENT_NODE) {
128                 n = n.getPreviousSibling();
129             }
130             return n == null;
131         }
132         return false;
133     }
134
135     // SVGStylable support ///////////////////////////////////////////////////
136

137     /**
138      * <b>DOM</b>: Implements {@link org.w3c.dom.svg.SVGStylable#getStyle()}.
139      */

140     public CSSStyleDeclaration getStyle() {
141         CSSStyleDeclaration result =
142             (CSSStyleDeclaration)getLiveAttributeValue(null,
143                                                        SVG_STYLE_ATTRIBUTE);
144         if (result == null) {
145             CSSEngine eng = ((SVGOMDocument)getOwnerDocument()).getCSSEngine();
146             result = new StyleDeclaration(eng);
147         }
148         return result;
149     }
150
151     /**
152      * <b>DOM</b>: Implements {@link
153      * org.w3c.dom.svg.SVGStylable#getPresentationAttribute(String)}.
154      */

155     public CSSValue getPresentationAttribute(String JavaDoc name) {
156         CSSValue result = (CSSValue)getLiveAttributeValue(null, name);
157         if (result != null)
158             return result;
159
160         CSSEngine eng = ((SVGOMDocument)getOwnerDocument()).getCSSEngine();
161         int idx = eng.getPropertyIndex(name);
162         if (idx == -1)
163             return null;
164
165         if (idx > SVGCSSEngine.FINAL_INDEX) {
166             if (eng.getValueManagers()[idx] instanceof SVGPaintManager) {
167                 result = new PresentationAttributePaintValue(eng, name);
168             }
169             if (eng.getValueManagers()[idx] instanceof SVGColorManager) {
170                 result = new PresentationAttributeColorValue(eng, name);
171             }
172         } else {
173             switch (idx) {
174             case SVGCSSEngine.FILL_INDEX:
175             case SVGCSSEngine.STROKE_INDEX:
176                 result = new PresentationAttributePaintValue(eng, name);
177                 break;
178                 
179             case SVGCSSEngine.FLOOD_COLOR_INDEX:
180             case SVGCSSEngine.LIGHTING_COLOR_INDEX:
181             case SVGCSSEngine.STOP_COLOR_INDEX:
182                 result = new PresentationAttributeColorValue(eng, name);
183                 break;
184                 
185             default:
186                 result = new PresentationAttributeValue(eng, name);
187             }
188         }
189         putLiveAttributeValue(null, name, (LiveAttributeValue)result);
190         return result;
191     }
192
193     /**
194      * <b>DOM</b>: Implements {@link
195      * org.w3c.dom.svg.SVGStylable#getClassName()}.
196      */

197     public SVGAnimatedString getClassName() {
198         return getAnimatedStringAttribute(null, SVG_CLASS_ATTRIBUTE);
199     }
200
201     /**
202      * To manage a presentation attribute value.
203      */

204     public class PresentationAttributeValue
205         extends CSSOMValue
206         implements LiveAttributeValue,
207                    CSSOMValue.ValueProvider {
208
209         /**
210          * The CSS engine.
211          */

212         protected CSSEngine cssEngine;
213
214         /**
215          * The property name.
216          */

217         protected String JavaDoc property;
218
219         /**
220          * The value.
221          */

222         protected Value value;
223
224         /**
225          * Whether the mutation comes from this object.
226          */

227         protected boolean mutate;
228
229         /**
230          * Creates a new PresentationAttributeValue.
231          */

232         public PresentationAttributeValue(CSSEngine eng, String JavaDoc prop) {
233             super(null);
234             valueProvider = this;
235             setModificationHandler(new AbstractModificationHandler() {
236                     protected Value getValue() {
237                         return PresentationAttributeValue.this.getValue();
238                     }
239                     public void textChanged(String JavaDoc text) throws DOMException JavaDoc {
240                         value = cssEngine.parsePropertyValue
241                             (SVGStylableElement.this, property, text);
242                         mutate = true;
243                         setAttributeNS(null, property, text);
244                         mutate = false;
245                     }
246                 });
247
248             cssEngine = eng;
249             property = prop;
250
251             Attr JavaDoc attr = getAttributeNodeNS(null, prop);
252             if (attr != null) {
253                 value = cssEngine.parsePropertyValue
254                     (SVGStylableElement.this, prop, attr.getValue());
255             }
256         }
257
258         // ValueProvider ///////////////////////////////
259

260         /**
261          * Returns the current value associated with this object.
262          */

263         public Value getValue() {
264             if (value == null) {
265                 throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
266             }
267             return value;
268         }
269
270         // LiveAttributeValue //////////////////////////////////////
271

272         /**
273          * Called when an Attr node has been added.
274          */

275         public void attrAdded(Attr JavaDoc node, String JavaDoc newv) {
276             if (!mutate) {
277                 value = cssEngine.parsePropertyValue
278                     (SVGStylableElement.this, property, newv);
279             }
280         }
281
282         /**
283          * Called when an Attr node has been modified.
284          */

285         public void attrModified(Attr JavaDoc node, String JavaDoc oldv, String JavaDoc newv) {
286             if (!mutate) {
287                 value = cssEngine.parsePropertyValue
288                     (SVGStylableElement.this, property, newv);
289             }
290         }
291
292         /**
293          * Called when an Attr node has been removed.
294          */

295         public void attrRemoved(Attr JavaDoc node, String JavaDoc oldv) {
296             if (!mutate) {
297                 value = null;
298             }
299         }
300     }
301
302     /**
303      * To manage a presentation attribute SVGColor value.
304      */

305     public class PresentationAttributeColorValue
306         extends CSSOMSVGColor
307         implements LiveAttributeValue,
308                    CSSOMSVGColor.ValueProvider {
309
310         /**
311          * The CSS engine.
312          */

313         protected CSSEngine cssEngine;
314
315         /**
316          * The property name.
317          */

318         protected String JavaDoc property;
319
320         /**
321          * The value.
322          */

323         protected Value value;
324
325         /**
326          * Whether the mutation comes from this object.
327          */

328         protected boolean mutate;
329
330         /**
331          * Creates a new PresentationAttributeColorValue.
332          */

333         public PresentationAttributeColorValue(CSSEngine eng, String JavaDoc prop) {
334             super(null);
335             valueProvider = this;
336             setModificationHandler(new AbstractModificationHandler() {
337                     protected Value getValue() {
338                         return PresentationAttributeColorValue.this.getValue();
339                     }
340                     public void textChanged(String JavaDoc text) throws DOMException JavaDoc {
341                         value = cssEngine.parsePropertyValue
342                             (SVGStylableElement.this, property, text);
343                         mutate = true;
344                         setAttributeNS(null, property, text);
345                         mutate = false;
346                     }
347                 });
348
349             cssEngine = eng;
350             property = prop;
351
352             Attr JavaDoc attr = getAttributeNodeNS(null, prop);
353             if (attr != null) {
354                 value = cssEngine.parsePropertyValue
355                     (SVGStylableElement.this, prop, attr.getValue());
356             }
357         }
358
359         // ValueProvider ///////////////////////////////
360

361         /**
362          * Returns the current value associated with this object.
363          */

364         public Value getValue() {
365             if (value == null) {
366                 throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
367             }
368             return value;
369         }
370
371         // LiveAttributeValue //////////////////////////////////////
372

373         /**
374          * Called when an Attr node has been added.
375          */

376         public void attrAdded(Attr JavaDoc node, String JavaDoc newv) {
377             if (!mutate) {
378                 value = cssEngine.parsePropertyValue
379                     (SVGStylableElement.this, property, newv);
380             }
381         }
382
383         /**
384          * Called when an Attr node has been modified.
385          */

386         public void attrModified(Attr JavaDoc node, String JavaDoc oldv, String JavaDoc newv) {
387             if (!mutate) {
388                 value = cssEngine.parsePropertyValue
389                     (SVGStylableElement.this, property, newv);
390             }
391         }
392
393         /**
394          * Called when an Attr node has been removed.
395          */

396         public void attrRemoved(Attr JavaDoc node, String JavaDoc oldv) {
397             if (!mutate) {
398                 value = null;
399             }
400         }
401     }
402
403     /**
404      * To manage a presentation attribute SVGPaint value.
405      */

406     public class PresentationAttributePaintValue
407         extends CSSOMSVGPaint
408         implements LiveAttributeValue,
409                    CSSOMSVGPaint.ValueProvider {
410
411         /**
412          * The CSS engine.
413          */

414         protected CSSEngine cssEngine;
415
416         /**
417          * The property name.
418          */

419         protected String JavaDoc property;
420
421         /**
422          * The value.
423          */

424         protected Value value;
425
426         /**
427          * Whether the mutation comes from this object.
428          */

429         protected boolean mutate;
430
431         /**
432          * Creates a new PresentationAttributeColorValue.
433          */

434         public PresentationAttributePaintValue(CSSEngine eng, String JavaDoc prop) {
435             super(null);
436             valueProvider = this;
437             setModificationHandler(new AbstractModificationHandler() {
438                     protected Value getValue() {
439                         return PresentationAttributePaintValue.this.getValue();
440                     }
441                     public void textChanged(String JavaDoc text) throws DOMException JavaDoc {
442                         value = cssEngine.parsePropertyValue
443                             (SVGStylableElement.this, property, text);
444                         mutate = true;
445                         setAttributeNS(null, property, text);
446                         mutate = false;
447                     }
448                 });
449
450
451             cssEngine = eng;
452             property = prop;
453
454             Attr JavaDoc attr = getAttributeNodeNS(null, prop);
455             if (attr != null) {
456                 value = cssEngine.parsePropertyValue
457                     (SVGStylableElement.this, prop, attr.getValue());
458             }
459         }
460
461         // ValueProvider ///////////////////////////////
462

463         /**
464          * Returns the current value associated with this object.
465          */

466         public Value getValue() {
467             if (value == null) {
468                 throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR, "");
469             }
470             return value;
471         }
472
473         // LiveAttributeValue //////////////////////////////////////
474

475         /**
476          * Called when an Attr node has been added.
477          */

478         public void attrAdded(Attr JavaDoc node, String JavaDoc newv) {
479             if (!mutate) {
480                 value = cssEngine.parsePropertyValue
481                     (SVGStylableElement.this, property, newv);
482             }
483         }
484
485         /**
486          * Called when an Attr node has been modified.
487          */

488         public void attrModified(Attr JavaDoc node, String JavaDoc oldv, String JavaDoc newv) {
489             if (!mutate) {
490                 value = cssEngine.parsePropertyValue
491                     (SVGStylableElement.this, property, newv);
492             }
493         }
494
495         /**
496          * Called when an Attr node has been removed.
497          */

498         public void attrRemoved(Attr JavaDoc node, String JavaDoc oldv) {
499             if (!mutate) {
500                 value = null;
501             }
502         }
503     }
504
505     /**
506      * This class represents the 'style' attribute.
507      */

508     public class StyleDeclaration
509         extends CSSOMSVGStyleDeclaration
510         implements LiveAttributeValue,
511                    CSSOMSVGStyleDeclaration.ValueProvider,
512                    CSSOMSVGStyleDeclaration.ModificationHandler,
513                    CSSEngine.MainPropertyReceiver {
514         
515         /**
516          * The associated CSS object.
517          */

518         protected org.apache.batik.css.engine.StyleDeclaration declaration;
519
520         /**
521          * Whether the mutation comes from this object.
522          */

523         protected boolean mutate;
524
525         /**
526          * Creates a new StyleDeclaration.
527          */

528         public StyleDeclaration(CSSEngine eng) {
529             super(null, null, eng);
530             valueProvider = this;
531             setModificationHandler(this);
532
533             declaration = cssEngine.parseStyleDeclaration
534                 (SVGStylableElement.this,
535                  getAttributeNS(null, SVG_STYLE_ATTRIBUTE));
536         }
537
538         // ValueProvider ////////////////////////////////////////
539

540         /**
541          * Returns the current value associated with this object.
542          */

543         public Value getValue(String JavaDoc name) {
544             int idx = cssEngine.getPropertyIndex(name);
545             for (int i = 0; i < declaration.size(); i++) {
546                 if (idx == declaration.getIndex(i)) {
547                     return declaration.getValue(i);
548                 }
549             }
550             return null;
551         }
552
553         /**
554          * Tells whether the given property is important.
555          */

556         public boolean isImportant(String JavaDoc name) {
557             int idx = cssEngine.getPropertyIndex(name);
558             for (int i = 0; i < declaration.size(); i++) {
559                 if (idx == declaration.getIndex(i)) {
560                     return declaration.getPriority(i);
561                 }
562             }
563             return false;
564         }
565
566         /**
567          * Returns the text of the declaration.
568          */

569         public String JavaDoc getText() {
570             return declaration.toString(cssEngine);
571         }
572
573         /**
574          * Returns the length of the declaration.
575          */

576         public int getLength() {
577             return declaration.size();
578         }
579
580         /**
581          * Returns the value at the given.
582          */

583         public String JavaDoc item(int idx) {
584             return cssEngine.getPropertyName(declaration.getIndex(idx));
585         }
586
587         // LiveAttributeValue //////////////////////////////////////
588

589         /**
590          * Called when an Attr node has been added.
591          */

592         public void attrAdded(Attr JavaDoc node, String JavaDoc newv) {
593             if (!mutate) {
594                 declaration = cssEngine.parseStyleDeclaration
595                     (SVGStylableElement.this, newv);
596             }
597         }
598
599         /**
600          * Called when an Attr node has been modified.
601          */

602         public void attrModified(Attr JavaDoc node, String JavaDoc oldv, String JavaDoc newv) {
603             if (!mutate) {
604                 declaration = cssEngine.parseStyleDeclaration
605                     (SVGStylableElement.this, newv);
606             }
607         }
608
609         /**
610          * Called when an Attr node has been removed.
611          */

612         public void attrRemoved(Attr JavaDoc node, String JavaDoc oldv) {
613             if (!mutate) {
614                 declaration =
615                     new org.apache.batik.css.engine.StyleDeclaration();
616             }
617         }
618
619         // ModificationHandler ////////////////////////////////////
620

621         /**
622          * Called when the value text has changed.
623          */

624         public void textChanged(String JavaDoc text) throws DOMException JavaDoc {
625             declaration = cssEngine.parseStyleDeclaration
626                 (SVGStylableElement.this, text);
627             mutate = true;
628             setAttributeNS(null, SVG_STYLE_ATTRIBUTE, text);
629             mutate = false;
630         }
631
632         /**
633          * Called when a property was removed.
634          */

635         public void propertyRemoved(String JavaDoc name) throws DOMException JavaDoc {
636             int idx = cssEngine.getPropertyIndex(name);
637             for (int i = 0; i < declaration.size(); i++) {
638                 if (idx == declaration.getIndex(i)) {
639                     declaration.remove(i);
640                     mutate = true;
641                     setAttributeNS(null, SVG_STYLE_ATTRIBUTE,
642                                    declaration.toString(cssEngine));
643                     mutate = false;
644                     return;
645                 }
646             }
647         }
648
649         public void setMainProperty(String JavaDoc name, Value v, boolean important) {
650             int idx = cssEngine.getPropertyIndex(name);
651             if (idx == -1)
652                 return; // unknown property
653

654             int i=0;
655             for (; i < declaration.size(); i++) {
656                 if (idx == declaration.getIndex(i))
657                     break;
658             }
659             if (i < declaration.size())
660                 declaration.put(i, v, idx, important);
661             else
662                 declaration.append(v, idx, important);
663         }
664
665         /**
666          * Called when a property was changed.
667          */

668         public void propertyChanged(String JavaDoc name, String JavaDoc value, String JavaDoc prio)
669             throws DOMException JavaDoc {
670             boolean important = ((prio != null) && (prio.length() >0));
671             cssEngine.setMainProperties(SVGStylableElement.this,
672                                         this, name, value, important);
673             mutate = true;
674             setAttributeNS(null, SVG_STYLE_ATTRIBUTE,
675                            declaration.toString(cssEngine));
676             mutate = false;
677         }
678     }
679 }
680
Popular Tags