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.anim.values;
020:
021: import org.apache.batik.dom.anim.AnimationTarget;
022:
023: /**
024: * A number-or-identifier value in the animation system.
025: *
026: * @author <a href="mailto:cam%40mcc%2eid%2eau">Cameron McCormack</a>
027: * @version $Id: AnimatableNumberOrIdentValue.java 492528 2007-01-04 11:45:47Z cam $
028: */
029: public class AnimatableNumberOrIdentValue extends AnimatableNumberValue {
030:
031: /**
032: * Whether this value is an identifier.
033: */
034: protected boolean isIdent;
035:
036: /**
037: * The identifier.
038: */
039: protected String ident;
040:
041: /**
042: * Whether numbers should be considered as numeric keywords, as with the
043: * font-weight property.
044: */
045: protected boolean numericIdent;
046:
047: /**
048: * Creates a new, uninitialized AnimatableNumberOrIdentValue.
049: */
050: protected AnimatableNumberOrIdentValue(AnimationTarget target) {
051: super (target);
052: }
053:
054: /**
055: * Creates a new AnimatableNumberOrIdentValue for a Number value.
056: */
057: public AnimatableNumberOrIdentValue(AnimationTarget target,
058: float v, boolean numericIdent) {
059: super (target, v);
060: this .numericIdent = numericIdent;
061: }
062:
063: /**
064: * Creates a new AnimatableNumberOrIdentValue for an identifier value.
065: */
066: public AnimatableNumberOrIdentValue(AnimationTarget target,
067: String ident) {
068: super (target);
069: this .ident = ident;
070: this .isIdent = true;
071: }
072:
073: /**
074: * Returns whether two values of this type can have their distance
075: * computed, as needed by paced animation.
076: */
077: public boolean canPace() {
078: return false;
079: }
080:
081: /**
082: * Returns the absolute distance between this value and the specified other
083: * value.
084: */
085: public float distanceTo(AnimatableValue other) {
086: return 0f;
087: }
088:
089: /**
090: * Returns a zero value of this AnimatableValue's type.
091: */
092: public AnimatableValue getZeroValue() {
093: return new AnimatableNumberOrIdentValue(target, 0f,
094: numericIdent);
095: }
096:
097: /**
098: * Returns the CSS text representation of the value.
099: */
100: public String getCssText() {
101: if (isIdent) {
102: return ident;
103: }
104: if (numericIdent) {
105: return Integer.toString((int) value);
106: }
107: return super .getCssText();
108: }
109:
110: /**
111: * Performs interpolation to the given value.
112: */
113: public AnimatableValue interpolate(AnimatableValue result,
114: AnimatableValue to, float interpolation,
115: AnimatableValue accumulation, int multiplier) {
116: AnimatableNumberOrIdentValue res;
117: if (result == null) {
118: res = new AnimatableNumberOrIdentValue(target);
119: } else {
120: res = (AnimatableNumberOrIdentValue) result;
121: }
122:
123: if (to == null) {
124: if (isIdent) {
125: res.hasChanged = !res.isIdent
126: || !res.ident.equals(ident);
127: res.ident = ident;
128: res.isIdent = true;
129: } else if (numericIdent) {
130: res.hasChanged = res.value != value || res.isIdent;
131: res.value = value;
132: res.isIdent = false;
133: res.hasChanged = true;
134: res.numericIdent = true;
135: } else {
136: float oldValue = res.value;
137: super .interpolate(res, to, interpolation, accumulation,
138: multiplier);
139: res.numericIdent = false;
140: if (res.value != oldValue) {
141: res.hasChanged = true;
142: }
143: }
144: } else {
145: AnimatableNumberOrIdentValue toValue = (AnimatableNumberOrIdentValue) to;
146: if (isIdent || toValue.isIdent || numericIdent) {
147: if (interpolation >= 0.5) {
148: if (res.isIdent != toValue.isIdent
149: || res.value != toValue.value
150: || res.isIdent && toValue.isIdent
151: && !toValue.ident.equals(ident)) {
152: res.isIdent = toValue.isIdent;
153: res.ident = toValue.ident;
154: res.value = toValue.value;
155: res.numericIdent = toValue.numericIdent;
156: res.hasChanged = true;
157: }
158: } else {
159: if (res.isIdent != isIdent || res.value != value
160: || res.isIdent && isIdent
161: && !res.ident.equals(ident)) {
162: res.isIdent = isIdent;
163: res.ident = ident;
164: res.value = value;
165: res.numericIdent = numericIdent;
166: res.hasChanged = true;
167: }
168: }
169: } else {
170: super .interpolate(res, to, interpolation, accumulation,
171: multiplier);
172: res.numericIdent = false;
173: }
174: }
175: return res;
176: }
177: }
|