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.AnimatablePreserveAspectRatioValue;
022: import org.apache.batik.anim.values.AnimatableValue;
023: import org.apache.batik.dom.anim.AnimationTarget;
024:
025: import org.apache.batik.util.SVGConstants;
026:
027: import org.w3c.dom.Attr;
028: import org.w3c.dom.DOMException;
029: import org.w3c.dom.svg.SVGAnimatedPreserveAspectRatio;
030: import org.w3c.dom.svg.SVGPreserveAspectRatio;
031:
032: /**
033: * This class implements the {@link SVGAnimatedPreserveAspectRatio} interface.
034: *
035: * @author <a href="mailto:tonny@kiyut.com">Tonny Kohar</a>
036: * @version $Id: SVGOMAnimatedPreserveAspectRatio.java 490655 2006-12-28 05:19:44Z cam $
037: */
038: public class SVGOMAnimatedPreserveAspectRatio extends
039: AbstractSVGAnimatedValue implements
040: SVGAnimatedPreserveAspectRatio {
041:
042: /**
043: * The base value.
044: */
045: protected BaseSVGPARValue baseVal;
046:
047: /**
048: * The animated value.
049: */
050: protected AnimSVGPARValue animVal;
051:
052: /**
053: * Whether the value is changing.
054: */
055: protected boolean changing;
056:
057: /**
058: * Creates a new SVGOMAnimatedPreserveAspectRatio.
059: * @param elt The associated element.
060: */
061: public SVGOMAnimatedPreserveAspectRatio(AbstractElement elt) {
062: super (elt, null,
063: SVGConstants.SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE);
064: }
065:
066: /**
067: * <b>DOM</b>: Implements {@link SVGAnimatedPreserveAspectRatio#getBaseVal()}.
068: */
069: public SVGPreserveAspectRatio getBaseVal() {
070: if (baseVal == null) {
071: baseVal = new BaseSVGPARValue();
072: }
073: return baseVal;
074: }
075:
076: /**
077: * <b>DOM</b>: Implements {@link SVGAnimatedPreserveAspectRatio#getAnimVal()}.
078: */
079: public SVGPreserveAspectRatio getAnimVal() {
080: if (animVal == null) {
081: animVal = new AnimSVGPARValue();
082: }
083: return animVal;
084: }
085:
086: /**
087: * Returns the base value of the attribute as an {@link AnimatableValue}.
088: */
089: public AnimatableValue getUnderlyingValue(AnimationTarget target) {
090: SVGPreserveAspectRatio par = getBaseVal();
091: return new AnimatablePreserveAspectRatioValue(target, par
092: .getAlign(), par.getMeetOrSlice());
093: }
094:
095: /**
096: * Updates the animated value with the given {@link AnimatableValue}.
097: */
098: protected void updateAnimatedValue(AnimatableValue val) {
099: if (val == null) {
100: hasAnimVal = false;
101: } else {
102: hasAnimVal = true;
103: if (animVal == null) {
104: animVal = new AnimSVGPARValue();
105: }
106: AnimatablePreserveAspectRatioValue animPAR = (AnimatablePreserveAspectRatioValue) val;
107: animVal.setAnimatedValue(animPAR.getAlign(), animPAR
108: .getMeetOrSlice());
109: }
110: fireAnimatedAttributeListeners();
111: }
112:
113: /**
114: * Called when an Attr node has been added.
115: */
116: public void attrAdded(Attr node, String newv) {
117: if (!changing && baseVal != null) {
118: baseVal.invalidate();
119: }
120: fireBaseAttributeListeners();
121: if (!hasAnimVal) {
122: fireAnimatedAttributeListeners();
123: }
124: }
125:
126: /**
127: * Called when an Attr node has been modified.
128: */
129: public void attrModified(Attr node, String oldv, String newv) {
130: if (!changing && baseVal != null) {
131: baseVal.invalidate();
132: }
133: fireBaseAttributeListeners();
134: if (!hasAnimVal) {
135: fireAnimatedAttributeListeners();
136: }
137: }
138:
139: /**
140: * Called when an Attr node has been removed.
141: */
142: public void attrRemoved(Attr node, String oldv) {
143: if (!changing && baseVal != null) {
144: baseVal.invalidate();
145: }
146: fireBaseAttributeListeners();
147: if (!hasAnimVal) {
148: fireAnimatedAttributeListeners();
149: }
150: }
151:
152: /**
153: * This class represents the SVGPreserveAspectRatio returned by {@link
154: * #getBaseVal()}.
155: */
156: public class BaseSVGPARValue extends AbstractSVGPreserveAspectRatio {
157:
158: /**
159: * Creates a new BaseSVGPARValue.
160: */
161: public BaseSVGPARValue() {
162: invalidate();
163: }
164:
165: /**
166: * Create a DOMException.
167: */
168: protected DOMException createDOMException(short type,
169: String key, Object[] args) {
170: return element.createDOMException(type, key, args);
171: }
172:
173: /**
174: * Sets the associated DOM attribute.
175: */
176: protected void setAttributeValue(String value)
177: throws DOMException {
178: try {
179: changing = true;
180: element
181: .setAttributeNS(
182: null,
183: SVGConstants.SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE,
184: value);
185: } finally {
186: changing = false;
187: }
188: }
189:
190: /**
191: * Re-reads the DOM attribute value.
192: */
193: protected void invalidate() {
194: String s = element.getAttributeNS(null,
195: SVGConstants.SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE);
196: setValueAsString(s);
197: }
198: }
199:
200: /**
201: * This class represents the SVGPreserveAspectRatio returned by {@link
202: * #getAnimVal()}.
203: */
204: public class AnimSVGPARValue extends AbstractSVGPreserveAspectRatio {
205:
206: /**
207: * Create a DOMException.
208: */
209: protected DOMException createDOMException(short type,
210: String key, Object[] args) {
211: return element.createDOMException(type, key, args);
212: }
213:
214: /**
215: * Sets the associated DOM attribute. Does nothing, since animated
216: * values aren't reflected in the DOM.
217: */
218: protected void setAttributeValue(String value)
219: throws DOMException {
220: }
221:
222: /**
223: * <b>DOM</b>: Implements {@link SVGPreserveAspectRatio#getAlign()}.
224: */
225: public short getAlign() {
226: if (hasAnimVal) {
227: return super .getAlign();
228: }
229: return getBaseVal().getAlign();
230: }
231:
232: /**
233: * <b>DOM</b>: Implements {@link SVGPreserveAspectRatio#getMeetOrSlice()}.
234: */
235: public short getMeetOrSlice() {
236: if (hasAnimVal) {
237: return super .getMeetOrSlice();
238: }
239: return getBaseVal().getMeetOrSlice();
240: }
241:
242: /**
243: * <b>DOM</b>: Implements {@link SVGPreserveAspectRatio#setAlign(short)}.
244: */
245: public void setAlign(short align) {
246: throw element.createDOMException(
247: DOMException.NO_MODIFICATION_ALLOWED_ERR,
248: "readonly.preserve.aspect.ratio", null);
249: }
250:
251: /**
252: * <b>DOM</b>: Implements {@link SVGPreserveAspectRatio#setMeetOrSlice(short)}.
253: */
254: public void setMeetOrSlice(short meetOrSlice) {
255: throw element.createDOMException(
256: DOMException.NO_MODIFICATION_ALLOWED_ERR,
257: "readonly.preserve.aspect.ratio", null);
258: }
259:
260: /**
261: * Updates the animated value.
262: */
263: protected void setAnimatedValue(short align, short meetOrSlice) {
264: this.align = align;
265: this.meetOrSlice = meetOrSlice;
266: }
267: }
268: }
|