KickJava   Java API By Example, From Geeks To Geeks.

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


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.dom.svg;
19
20 import org.apache.batik.parser.UnitProcessor;
21 import org.w3c.dom.Attr JavaDoc;
22 import org.w3c.dom.svg.SVGAnimatedLength;
23 import org.w3c.dom.svg.SVGLength;
24
25 /**
26  * This class provides an implementation of the {@link
27  * SVGAnimatedLength} interface.
28  *
29  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
30  * @version $Id: AbstractSVGAnimatedLength.java,v 1.8 2004/08/18 07:13:13 vhardy Exp $
31  */

32 public abstract class AbstractSVGAnimatedLength
33     implements SVGAnimatedLength,
34                LiveAttributeValue {
35
36     /**
37      * This constant represents horizontal lengths.
38      */

39     public final static short HORIZONTAL_LENGTH =
40         UnitProcessor.HORIZONTAL_LENGTH;
41
42     /**
43      * This constant represents vertical lengths.
44      */

45     public final static short VERTICAL_LENGTH =
46         UnitProcessor.VERTICAL_LENGTH;
47
48     /**
49      * This constant represents other lengths.
50      */

51     public final static short OTHER_LENGTH =
52         UnitProcessor.OTHER_LENGTH;
53
54     /**
55      * The associated element.
56      */

57     protected AbstractElement element;
58
59     /**
60      * The attribute's namespace URI.
61      */

62     protected String JavaDoc namespaceURI;
63
64     /**
65      * The attribute's local name.
66      */

67     protected String JavaDoc localName;
68
69     /**
70      * This length's direction.
71      */

72     protected short direction;
73
74     /**
75      * The base value.
76      */

77     protected BaseSVGLength baseVal;
78
79     /**
80      * Whether the value is changing.
81      */

82     protected boolean changing;
83
84     /**
85      * Creates a new SVGAnimatedLength.
86      * @param elt The associated element.
87      * @param ns The attribute's namespace URI.
88      * @param ln The attribute's local name.
89      * @param dir The length's direction.
90      */

91     protected AbstractSVGAnimatedLength(AbstractElement elt,
92                                         String JavaDoc ns,
93                                         String JavaDoc ln,
94                                         short dir) {
95         element = elt;
96         namespaceURI = ns;
97         localName = ln;
98         direction = dir;
99     }
100
101     /**
102      * Returns the default value to use when the associated attribute
103      * was not specified.
104      */

105     protected abstract String JavaDoc getDefaultValue();
106
107     /**
108      * <b>DOM</b>: Implements {@link SVGAnimatedLength#getBaseVal()}.
109      */

110     public SVGLength getBaseVal() {
111         if (baseVal == null) {
112             baseVal = new BaseSVGLength(direction);
113         }
114         return baseVal;
115     }
116
117     /**
118      * <b>DOM</b>: Implements {@link SVGAnimatedLength#getAnimVal()}.
119      */

120     public SVGLength getAnimVal() {
121         throw new RuntimeException JavaDoc("!!! TODO: getAnimVal()");
122     }
123
124     /**
125      * Called when an Attr node has been added.
126      */

127     public void attrAdded(Attr JavaDoc node, String JavaDoc newv) {
128         if (!changing && baseVal != null) {
129             baseVal.invalidate();
130         }
131     }
132
133     /**
134      * Called when an Attr node has been modified.
135      */

136     public void attrModified(Attr JavaDoc node, String JavaDoc oldv, String JavaDoc newv) {
137         if (!changing && baseVal != null) {
138             baseVal.invalidate();
139         }
140     }
141
142     /**
143      * Called when an Attr node has been removed.
144      */

145     public void attrRemoved(Attr JavaDoc node, String JavaDoc oldv) {
146         if (!changing && baseVal != null) {
147             baseVal.invalidate();
148         }
149     }
150
151     /**
152      * This class represents the SVGLength returned by getBaseVal().
153      */

154     protected class BaseSVGLength extends AbstractSVGLength {
155
156         /**
157          * Whether this length is valid.
158          */

159         protected boolean valid;
160         
161         /**
162          * Creates a new BaseSVGLength.
163          */

164         public BaseSVGLength(short direction) {
165             super(direction);
166         }
167
168         /**
169          * Invalidates this length.
170          */

171         public void invalidate() {
172             valid = false;
173         }
174
175         /**
176          * Resets the value of the associated attribute.
177          */

178         protected void reset() {
179             try {
180                 changing = true;
181                 String JavaDoc value = getValueAsString();
182                 element.setAttributeNS(namespaceURI, localName, value);
183             } finally {
184                 changing = false;
185             }
186         }
187
188         /**
189          * Initializes the length, if needed.
190          */

191         protected void revalidate() {
192             if (valid) {
193                 return;
194             }
195
196             String JavaDoc s = null;
197
198             Attr JavaDoc attr = element.getAttributeNodeNS(namespaceURI, localName);
199
200             if (attr == null) {
201                 s = getDefaultValue();
202             }
203             else{
204                 s = attr.getValue();
205             }
206
207             parse(s);
208
209             valid = true;
210         }
211
212         /**
213          */

214         protected SVGOMElement getAssociatedElement(){
215             return (SVGOMElement)element;
216         }
217     }
218 }
219
Popular Tags