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.AnimatableStringValue;
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.SVGAnimatedEnumeration;
028:
029: /**
030: * This class provides an implementation of the {@link
031: * SVGAnimatedEnumeration} interface.
032: *
033: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
034: * @version $Id: SVGOMAnimatedEnumeration.java 490655 2006-12-28 05:19:44Z cam $
035: */
036: public class SVGOMAnimatedEnumeration extends AbstractSVGAnimatedValue
037: implements SVGAnimatedEnumeration {
038:
039: /**
040: * The values in this enumeration.
041: */
042: protected String[] values;
043:
044: /**
045: * The default value, if the attribute is not specified.
046: */
047: protected short defaultValue;
048:
049: /**
050: * Whether the current base value is valid.
051: */
052: protected boolean valid;
053:
054: /**
055: * The current base value.
056: */
057: protected short baseVal;
058:
059: /**
060: * The current animated value.
061: */
062: protected short animVal;
063:
064: /**
065: * Whether the value is changing.
066: */
067: protected boolean changing;
068:
069: /**
070: * Creates a new SVGOMAnimatedEnumeration.
071: * @param elt The associated element.
072: * @param ns The attribute's namespace URI.
073: * @param ln The attribute's local name.
074: * @param val The values in this enumeration.
075: * @param def The default value to use.
076: */
077: public SVGOMAnimatedEnumeration(AbstractElement elt, String ns,
078: String ln, String[] val, short def) {
079: super (elt, ns, ln);
080: values = val;
081: defaultValue = def;
082: }
083:
084: /**
085: * <b>DOM</b>: Implements {@link SVGAnimatedEnumeration#getBaseVal()}.
086: */
087: public short getBaseVal() {
088: if (!valid) {
089: update();
090: }
091: return baseVal;
092: }
093:
094: /**
095: * Returns the base value as a string.
096: */
097: public String getBaseValAsString() {
098: if (!valid) {
099: update();
100: }
101: return values[baseVal];
102: }
103:
104: /**
105: * Updates the base value from the attribute.
106: */
107: protected void update() {
108: String val = element.getAttributeNS(namespaceURI, localName);
109: if (val.length() == 0) {
110: baseVal = defaultValue;
111: } else {
112: baseVal = getEnumerationNumber(val);
113: }
114: valid = true;
115: }
116:
117: /**
118: * Returns the enumeration number of the specified string.
119: */
120: protected short getEnumerationNumber(String s) {
121: for (short i = 0; i < values.length; i++) {
122: if (s.equals(values[i])) {
123: return i;
124: }
125: }
126: return 0;
127: }
128:
129: /**
130: * <b>DOM</b>: Implements {@link
131: * SVGAnimatedEnumeration#setBaseVal(short)}.
132: */
133: public void setBaseVal(short baseVal) throws DOMException {
134: if (baseVal >= 0 && baseVal < values.length) {
135: try {
136: this .baseVal = baseVal;
137: valid = true;
138: changing = true;
139: element.setAttributeNS(namespaceURI, localName,
140: values[baseVal]);
141: } finally {
142: changing = false;
143: }
144: }
145: }
146:
147: /**
148: * <b>DOM</b>: Implements {@link SVGAnimatedEnumeration#getAnimVal()}.
149: */
150: public short getAnimVal() {
151: if (hasAnimVal) {
152: return animVal;
153: }
154: if (!valid) {
155: update();
156: }
157: return baseVal;
158: }
159:
160: /**
161: * Returns the base value of the attribute as an {@link AnimatableValue}.
162: */
163: public AnimatableValue getUnderlyingValue(AnimationTarget target) {
164: return new AnimatableStringValue(target, getBaseValAsString());
165: }
166:
167: /**
168: * Called when an Attr node has been added.
169: */
170: public void attrAdded(Attr node, String newv) {
171: if (!changing) {
172: valid = false;
173: }
174: fireBaseAttributeListeners();
175: if (!hasAnimVal) {
176: fireAnimatedAttributeListeners();
177: }
178: }
179:
180: /**
181: * Updates the animated value with the given {@link AnimatableValue}.
182: */
183: protected void updateAnimatedValue(AnimatableValue val) {
184: if (val == null) {
185: hasAnimVal = false;
186: } else {
187: hasAnimVal = true;
188: this .animVal = getEnumerationNumber(((AnimatableStringValue) val)
189: .getString());
190: fireAnimatedAttributeListeners();
191: }
192: fireAnimatedAttributeListeners();
193: }
194:
195: /**
196: * Called when an Attr node has been modified.
197: */
198: public void attrModified(Attr node, String oldv, String newv) {
199: if (!changing) {
200: valid = false;
201: }
202: fireBaseAttributeListeners();
203: if (!hasAnimVal) {
204: fireAnimatedAttributeListeners();
205: }
206: }
207:
208: /**
209: * Called when an Attr node has been removed.
210: */
211: public void attrRemoved(Attr node, String oldv) {
212: if (!changing) {
213: valid = false;
214: }
215: fireBaseAttributeListeners();
216: if (!hasAnimVal) {
217: fireAnimatedAttributeListeners();
218: }
219: }
220: }
|