001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.perseus.model;
028:
029: import org.w3c.dom.DOMException;
030:
031: /**
032: * @version $Id: FloatTraitAnim.java,v 1.3 2006/06/29 10:47:31 ln156897 Exp $
033: */
034: class FloatTraitAnim extends TraitAnim {
035: /**
036: * Constructs a new FloatTraitAnim for a given ElementNode trait.
037: *
038: * @param targetElement the ElementNode whose trait is animated.
039: * @param targetTrait the name of the animated trait.
040: * @param traitType the trait type.
041: */
042: public FloatTraitAnim(final ElementNode targetElement,
043: final String traitName, final String traitType) {
044: super (targetElement, ElementNode.NULL_NS, traitName, traitType);
045: }
046:
047: /**
048: * Returns the BaseValue as an array of objects.
049: *
050: * @return the base value as an object array. The dimensions of the
051: * returned array depend on the trait.
052: * @see com.sun.perseus.model.BaseValue
053: */
054: public Object[] getBaseValue() {
055: return targetElement
056: .validateFloatArrayTrait(traitName,
057: getSpecifiedTraitNS(), targetElement
058: .getNamespaceURI(), targetElement
059: .getLocalName(), traitNamespace,
060: traitName);
061: }
062:
063: /**
064: * Applies the animation effect. The implementation makes sure it
065: * implements the sandwich model by 'pulling' values from the
066: * root animation (i.e., the animation with the highest priority).
067: */
068: void apply() {
069: float[][] v = (float[][]) rootAnim.compute();
070: targetElement.setFloatArrayTrait(traitName, v);
071: }
072:
073: /**
074: * @return the trait's value, as a String.
075: */
076: protected String getTraitImpl() {
077: // This returns the computed trait value, using the specified
078: // trait value.
079: float[][] v = targetElement
080: .validateFloatArrayTrait(traitName,
081: specifiedTraitValue, targetElement
082: .getNamespaceURI(), targetElement
083: .getLocalName(), traitNamespace,
084: traitName);
085:
086: // Now, convert that value to a string.
087: return targetElement.toStringTrait(traitName, v);
088: }
089:
090: /**
091: * Sets the trait's base value, as a String.
092: *
093: * @param value the new trait base value.
094: *
095: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
096: * value is an invalid value for the given trait or null.
097: */
098: void setTraitImpl(String value) throws DOMException {
099: targetElement.validateFloatArrayTrait(traitName, value,
100: targetElement.getNamespaceURI(), targetElement
101: .getLocalName(), traitNamespace, traitName);
102:
103: specifiedTraitValue = value;
104: }
105:
106: /**
107: * Converts the input values set to a RefValues object.
108: *
109: * @param anim the <code>Animation</code> for which the values should be
110: * converted.
111: * @param values a semi-colon seperated list of values which need to be
112: * validated.
113: * @param reqTraitNamespace the namespace of the trait which has the values
114: * value on the requesting element.
115: * @param reqTraitName the name of the trait which has the values value on
116: * the requesting element.
117: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
118: * value is incompatible with the given trait.
119: * @throws NullPointerException if values is null.
120: */
121: RefValues toRefValues(final Animation anim, String[] values,
122: final String reqTraitNamespace, final String reqTraitName)
123: throws DOMException {
124: FloatRefValues refValues = new FloatRefValues();
125:
126: if (values.length < 1) {
127: throw new IllegalArgumentException();
128: }
129:
130: if (values.length == 1) {
131: String[] tmpValues = new String[2];
132: tmpValues[0] = values[0];
133: tmpValues[1] = values[0];
134: values = tmpValues;
135: }
136:
137: int nSegments = values.length - 1;
138: refValues.segments = new FloatSegment[nSegments];
139:
140: // Build the first segment.
141: refValues.segments[0] = new FloatSegment();
142: refValues.segments[0].start = targetElement
143: .validateFloatArrayTrait(anim.traitName, values[0],
144: anim.getNamespaceURI(), anim.getLocalName(),
145: reqTraitNamespace, reqTraitName);
146: refValues.segments[0].end = targetElement
147: .validateFloatArrayTrait(anim.traitName, values[1],
148: anim.getNamespaceURI(), anim.getLocalName(),
149: reqTraitNamespace, reqTraitName);
150:
151: FloatSegment prevSegment = refValues.segments[0];
152:
153: for (int i = 1; i < nSegments; i++) {
154: refValues.segments[i] = new FloatSegment();
155: refValues.segments[i].start = prevSegment.end;
156: refValues.segments[i].end = targetElement
157: .validateFloatArrayTrait(anim.traitName,
158: values[i + 1], anim.getNamespaceURI(), anim
159: .getLocalName(), reqTraitNamespace,
160: reqTraitName);
161: prevSegment = refValues.segments[i];
162: }
163:
164: return refValues;
165: }
166:
167: /**
168: * Used to sum two animated trait values.
169: *
170: * @param valueA the base value. May be null.
171: * @param valueB the value to add to the base value. If the baseValue
172: */
173: public Object[] sum(Object[] valueA, Object[] valueB) {
174: if (valueA == null) {
175: return valueB;
176: }
177:
178: float[][] fva = (float[][]) valueA;
179: float[][] fvb = (float[][]) valueB;
180:
181: for (int ci = 0; ci < fva.length; ci++) {
182: for (int di = 0; di < fva[ci].length; di++) {
183: fvb[ci][di] += fva[ci][di];
184: }
185: }
186:
187: return valueB;
188: }
189:
190: /**
191: * Used to multiply an animated trait value by a number of iterations.
192: *
193: * @param value the animated trait value to multiply.
194: * @param iter the number of iteration to account for.
195: * @return the multiply result.
196: */
197: public Object[] multiply(Object[] value, int iter) {
198: float[][] fv = (float[][]) value;
199: float[][] r = new float[fv.length][];
200:
201: for (int ci = 0; ci < fv.length; ci++) {
202: r[ci] = new float[fv[ci].length];
203: for (int di = 0; di < fv[ci].length; di++) {
204: r[ci][di] = fv[ci][di] * iter;
205: }
206: }
207:
208: return r;
209: }
210:
211: /**
212: * @return true, because FloatTraitAnim support interpolation.
213: */
214: boolean supportsInterpolation() {
215: return true;
216: }
217:
218: }
|