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.AnimatableBooleanValue;
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.SVGAnimatedBoolean;
028:
029: /**
030: * This class implements the {@link SVGAnimatedBoolean} interface.
031: *
032: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
033: * @version $Id: SVGOMAnimatedBoolean.java 490655 2006-12-28 05:19:44Z cam $
034: */
035: public class SVGOMAnimatedBoolean extends AbstractSVGAnimatedValue
036: implements SVGAnimatedBoolean {
037:
038: /**
039: * The default value.
040: */
041: protected boolean 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 boolean baseVal;
052:
053: /**
054: * The current animated value.
055: */
056: protected boolean animVal;
057:
058: /**
059: * Whether the value is changing.
060: */
061: protected boolean changing;
062:
063: /**
064: * Creates a new SVGOMAnimatedBoolean.
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 SVGOMAnimatedBoolean(AbstractElement elt, String ns,
071: String ln, boolean val) {
072: super (elt, ns, ln);
073: defaultValue = val;
074: }
075:
076: /**
077: * <b>DOM</b>: Implements {@link SVGAnimatedBoolean#getBaseVal()}.
078: */
079: public boolean 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 = attr.getValue().equals("true");
095: }
096: valid = true;
097: }
098:
099: /**
100: * <b>DOM</b>: Implements {@link SVGAnimatedBoolean#setBaseVal(boolean)}.
101: */
102: public void setBaseVal(boolean 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 SVGAnimatedBoolean#getAnimVal()}.
116: */
117: public boolean getAnimVal() {
118: if (hasAnimVal) {
119: return animVal;
120: }
121: if (!valid) {
122: update();
123: }
124: return baseVal;
125: }
126:
127: /**
128: * Sets the animated value.
129: */
130: public void setAnimatedValue(boolean animVal) {
131: hasAnimVal = true;
132: this .animVal = animVal;
133: fireAnimatedAttributeListeners();
134: }
135:
136: /**
137: * Updates the animated value with the given {@link AnimatableValue}.
138: */
139: protected void updateAnimatedValue(AnimatableValue val) {
140: if (val == null) {
141: hasAnimVal = false;
142: } else {
143: hasAnimVal = true;
144: this .animVal = ((AnimatableBooleanValue) val).getValue();
145: }
146: fireAnimatedAttributeListeners();
147: }
148:
149: /**
150: * Returns the base value of the attribute as an {@link AnimatableValue}.
151: */
152: public AnimatableValue getUnderlyingValue(AnimationTarget target) {
153: return new AnimatableBooleanValue(target, getBaseVal());
154: }
155:
156: /**
157: * Called when an Attr node has been added.
158: */
159: public void attrAdded(Attr node, String newv) {
160: if (!changing) {
161: valid = false;
162: }
163: fireBaseAttributeListeners();
164: if (!hasAnimVal) {
165: fireAnimatedAttributeListeners();
166: }
167: }
168:
169: /**
170: * Called when an Attr node has been modified.
171: */
172: public void attrModified(Attr node, String oldv, String newv) {
173: if (!changing) {
174: valid = false;
175: }
176: fireBaseAttributeListeners();
177: if (!hasAnimVal) {
178: fireAnimatedAttributeListeners();
179: }
180: }
181:
182: /**
183: * Called when an Attr node has been removed.
184: */
185: public void attrRemoved(Attr node, String oldv) {
186: if (!changing) {
187: valid = false;
188: }
189: fireBaseAttributeListeners();
190: if (!hasAnimVal) {
191: fireAnimatedAttributeListeners();
192: }
193: }
194: }
|