001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.dom.svg;
020:
021: import org.apache.batik.anim.values.AnimatableIntegerValue;
022: import org.apache.batik.anim.values.AnimatableValue;
023: import org.apache.batik.dom.anim.AnimationTarget;
024:
025: import org.w3c.dom.Attr;
026: import org.w3c.dom.DOMException;
027: import org.w3c.dom.svg.SVGAnimatedInteger;
028:
029: /**
030: * This class implements the {@link SVGAnimatedInteger} interface.
031: *
032: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
033: * @version $Id: SVGOMAnimatedInteger.java 490655 2006-12-28 05:19:44Z cam $
034: */
035: public class SVGOMAnimatedInteger extends AbstractSVGAnimatedValue
036: implements SVGAnimatedInteger {
037:
038: /**
039: * The default value.
040: */
041: protected int defaultValue;
042:
043: /**
044: * Whether the base value is valid.
045: */
046: protected boolean valid;
047:
048: /**
049: * The current base value.
050: */
051: protected int baseVal;
052:
053: /**
054: * The current animated value.
055: */
056: protected int animVal;
057:
058: /**
059: * Whether the value is changing.
060: */
061: protected boolean changing;
062:
063: /**
064: * Creates a new SVGOMAnimatedInteger.
065: * @param elt The associated element.
066: * @param ns The attribute's namespace URI.
067: * @param ln The attribute's local name.
068: * @param val The default value, if the attribute is not specified.
069: */
070: public SVGOMAnimatedInteger(AbstractElement elt, String ns,
071: String ln, int val) {
072: super (elt, ns, ln);
073: defaultValue = val;
074: }
075:
076: /**
077: * <b>DOM</b>: Implements {@link SVGAnimatedInteger#getBaseVal()}.
078: */
079: public int getBaseVal() {
080: if (!valid) {
081: update();
082: }
083: return baseVal;
084: }
085:
086: /**
087: * Updates the base value from the attribute.
088: */
089: protected void update() {
090: Attr attr = element.getAttributeNodeNS(namespaceURI, localName);
091: if (attr == null) {
092: baseVal = defaultValue;
093: } else {
094: baseVal = Integer.parseInt(attr.getValue());
095: }
096: valid = true;
097: }
098:
099: /**
100: * <b>DOM</b>: Implements {@link SVGAnimatedInteger#setBaseVal(int)}.
101: */
102: public void setBaseVal(int baseVal) throws DOMException {
103: try {
104: this .baseVal = baseVal;
105: valid = true;
106: changing = true;
107: element.setAttributeNS(namespaceURI, localName, String
108: .valueOf(baseVal));
109: } finally {
110: changing = false;
111: }
112: }
113:
114: /**
115: * <b>DOM</b>: Implements {@link SVGAnimatedInteger#getAnimVal()}.
116: */
117: public int getAnimVal() {
118: if (hasAnimVal) {
119: return animVal;
120: }
121: if (!valid) {
122: update();
123: }
124: return baseVal;
125: }
126:
127: /**
128: * Returns the base value of the attribute as an {@link AnimatableValue}.
129: */
130: public AnimatableValue getUnderlyingValue(AnimationTarget target) {
131: return new AnimatableIntegerValue(target, getBaseVal());
132: }
133:
134: /**
135: * Updates the animated value with the given {@link AnimatableValue}.
136: */
137: protected void updateAnimatedValue(AnimatableValue val) {
138: if (val == null) {
139: hasAnimVal = false;
140: } else {
141: hasAnimVal = true;
142: this .animVal = ((AnimatableIntegerValue) val).getValue();
143: }
144: fireAnimatedAttributeListeners();
145: }
146:
147: /**
148: * Called when an Attr node has been added.
149: */
150: public void attrAdded(Attr node, String newv) {
151: if (!changing) {
152: valid = false;
153: }
154: fireBaseAttributeListeners();
155: if (!hasAnimVal) {
156: fireAnimatedAttributeListeners();
157: }
158: }
159:
160: /**
161: * Called when an Attr node has been modified.
162: */
163: public void attrModified(Attr node, String oldv, String newv) {
164: if (!changing) {
165: valid = false;
166: }
167: fireBaseAttributeListeners();
168: if (!hasAnimVal) {
169: fireAnimatedAttributeListeners();
170: }
171: }
172:
173: /**
174: * Called when an Attr node has been removed.
175: */
176: public void attrRemoved(Attr node, String oldv) {
177: if (!changing) {
178: valid = false;
179: }
180: fireBaseAttributeListeners();
181: if (!hasAnimVal) {
182: fireAnimatedAttributeListeners();
183: }
184: }
185: }
|