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: package com.sun.perseus.model;
027:
028: import com.sun.perseus.util.SVGConstants;
029:
030: import com.sun.perseus.j2d.PaintDef;
031: import com.sun.perseus.j2d.PaintTarget;
032: import com.sun.perseus.j2d.RadialGradientPaintDef;
033: import com.sun.perseus.j2d.Transform;
034:
035: import org.w3c.dom.DOMException;
036:
037: import org.w3c.dom.svg.SVGRect;
038:
039: /**
040: * <code>LienarGraident</code> represents an SVG Tiny 1.2
041: * <code><radialGradient></code> element.
042: * <br />
043: *
044: * @version $Id: RadialGradient.java,v 1.6 2006/06/29 10:47:33 ln156897 Exp $
045: */
046: public class RadialGradient extends GradientElement {
047: /**
048: * Gradient center on the x-axis
049: */
050: float cx = 0.5f;
051:
052: /**
053: * Gradient center on the y-axis
054: */
055: float cy = 0.5f;
056:
057: /**
058: * Gradient radius
059: */
060: float r = 0.5f;
061:
062: /**
063: * Constructor.
064: *
065: * @param ownerDocument this element's owner <code>DocumentNode</code>
066: */
067: public RadialGradient(final DocumentNode ownerDocument) {
068: super (ownerDocument);
069: }
070:
071: /**
072: * @return the SVGConstants.SVG_RADIAL_GRADIENT_TAG value
073: */
074: public String getLocalName() {
075: return SVGConstants.SVG_RADIAL_GRADIENT_TAG;
076: }
077:
078: /**
079: * Used by <code>DocumentNode</code> to create a new instance from
080: * a prototype <code>Rect</code>.
081: *
082: * @param doc the <code>DocumentNode</code> for which a new node is
083: * should be created.
084: * @return a new <code>RadialGradient</code> for the requested document.
085: */
086: public ElementNode newInstance(final DocumentNode doc) {
087: return new RadialGradient(doc);
088: }
089:
090: /**
091: * Sets the cx property.
092: *
093: * @param newCX the new origin along the x-axis
094: */
095: public void setCx(final float newCX) {
096: if (newCX == cx) {
097: return;
098: }
099:
100: this .cx = newCX;
101: onPaintChange();
102: }
103:
104: /**
105: * @return the cx property.
106: */
107: public float getCx() {
108: return cx;
109: }
110:
111: /**
112: * Sets the cy property.
113: *
114: * @param newCY the new origin along the y-axis
115: */
116: public void setCy(final float newCY) {
117: if (newCY == cy) {
118: return;
119: }
120:
121: this .cy = newCY;
122: onPaintChange();
123: }
124:
125: /**
126: * @return the cy property.
127: */
128: public float getCy() {
129: return cy;
130: }
131:
132: /**
133: * Sets the r property.
134: *
135: * @param newR the new end along the x-axis
136: */
137: public void setR(final float newR) {
138: if (newR == r) {
139: return;
140: }
141:
142: if (r < 0) {
143: throw new IllegalArgumentException();
144: }
145:
146: this .r = newR;
147: onPaintChange();
148: }
149:
150: /**
151: * @return the gradient's radius.
152: */
153: public float getR() {
154: return r;
155: }
156:
157: /**
158: * @param traitName the name of the trait which the element may support.
159: * @return true if this element supports the given trait in one of the
160: * trait accessor methods.
161: */
162: boolean supportsTrait(final String traitName) {
163: if (SVGConstants.SVG_CX_ATTRIBUTE == traitName
164: || SVGConstants.SVG_R_ATTRIBUTE == traitName
165: || SVGConstants.SVG_CY_ATTRIBUTE == traitName) {
166: return true;
167: } else {
168: return super .supportsTrait(traitName);
169: }
170: }
171:
172: /**
173: * RadialGradient handles cx, cy, r traits as
174: * FloatTraitAnims.
175: *
176: * @param traitName the trait name.
177: */
178: TraitAnim createTraitAnimImpl(final String traitName) {
179: if (SVGConstants.SVG_CX_ATTRIBUTE == traitName
180: || SVGConstants.SVG_R_ATTRIBUTE == traitName
181: || SVGConstants.SVG_CY_ATTRIBUTE == traitName) {
182: return new FloatTraitAnim(this , traitName, TRAIT_TYPE_FLOAT);
183: } else {
184: return super .createTraitAnimImpl(traitName);
185: }
186: }
187:
188: /**
189: * @param name the requested trait name (e.g., "ry")
190: * @return the trait's value, as a string.
191: *
192: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
193: * trait is not supported on this element or null.
194: * @throws DOMException with error code TYPE_MISMATCH_ERR if requested
195: * trait's computed value cannot be converted to a String (SVG Tiny only).
196: */
197: public String getTraitImpl(final String name) throws DOMException {
198: if (SVGConstants.SVG_CX_ATTRIBUTE == name) {
199: return Float.toString(cx);
200: } else if (SVGConstants.SVG_CY_ATTRIBUTE == name) {
201: return Float.toString(cy);
202: } else if (SVGConstants.SVG_R_ATTRIBUTE == name) {
203: return Float.toString(r);
204: } else {
205: return super .getTraitImpl(name);
206: }
207: }
208:
209: /**
210: * @param name the requested trait name (e.g., "y")
211: * @return the requested trait value
212: *
213: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
214: * trait is not supported on this element or null.
215: * @throws DOMException with error code TYPE_MISMATCH_ERR if requested
216: * trait's computed value cannot be converted to a float
217: * @throws SecurityException if the application does not have the necessary
218: * privilege rights to access this (SVG) content.
219: */
220: float getFloatTraitImpl(final String name) throws DOMException {
221: if (SVGConstants.SVG_CX_ATTRIBUTE == name) {
222: return cx;
223: } else if (SVGConstants.SVG_CY_ATTRIBUTE == name) {
224: return cy;
225: } else if (SVGConstants.SVG_R_ATTRIBUTE == name) {
226: return r;
227: } else {
228: return super .getFloatTraitImpl(name);
229: }
230: }
231:
232: /**
233: * Set the trait value as float array.
234: *
235: * @param name the trait's name.
236: * @param value the trait's value.
237: *
238: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
239: * trait is not supported on this element.
240: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
241: * trait's value cannot be specified as a float
242: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
243: * value is an invalid value for the given trait.
244: */
245: void setFloatArrayTrait(final String name, final float[][] value)
246: throws DOMException {
247: if (SVGConstants.SVG_CX_ATTRIBUTE == name) {
248: setCx(value[0][0]);
249: } else if (SVGConstants.SVG_CY_ATTRIBUTE == name) {
250: setCy(value[0][0]);
251: } else if (SVGConstants.SVG_R_ATTRIBUTE == name) {
252: setR(value[0][0]);
253: } else {
254: super .setFloatArrayTrait(name, value);
255: }
256: }
257:
258: /**
259: * Validates the input trait value.
260: *
261: * @param traitName the name of the trait to be validated.
262: * @param value the value to be validated
263: * @param reqNamespaceURI the namespace of the element requesting
264: * validation.
265: * @param reqLocalName the local name of the element requesting validation.
266: * @param reqTraitNamespace the namespace of the trait which has the values
267: * value on the requesting element.
268: * @param reqTraitName the name of the trait which has the values value on
269: * the requesting element.
270: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
271: * value is incompatible with the given trait.
272: */
273: public float[][] validateFloatArrayTrait(final String traitName,
274: final String value, final String reqNamespaceURI,
275: final String reqLocalName, final String reqTraitNamespace,
276: final String reqTraitName) throws DOMException {
277: if (SVGConstants.SVG_CX_ATTRIBUTE == traitName
278: || SVGConstants.SVG_CY_ATTRIBUTE == traitName) {
279: return new float[][] { { parseFloatTrait(traitName, value) } };
280: } else if (SVGConstants.SVG_R_ATTRIBUTE == traitName) {
281: return new float[][] { { parsePositiveFloatTrait(traitName,
282: value) } };
283: } else {
284: return super .validateFloatArrayTrait(traitName, value,
285: reqNamespaceURI, reqLocalName, reqTraitNamespace,
286: reqTraitName);
287: }
288:
289: }
290:
291: /**
292: * Supported traits: stroke-width, stroke-miterlimit, stroke-dashoffset,
293: * fill-rule, stroke-linejoin, stroke-linecap, display, visibility,
294: * color, fill, stroke, fill-opacity, stroke-opacity, stroke-dasharray
295: *
296: * @param name the name of the trait to set.
297: * @param value the value of the trait to set.
298: *
299: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
300: * trait is not supported on this element or null.
301: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
302: * trait's value cannot be specified as a String
303: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
304: * value is an invalid value for the given trait or null.
305: * @throws DOMException with error code NO_MODIFICATION_ALLOWED_ERR: if
306: * attempt is made to change readonly trait.
307: */
308: public void setTraitImpl(final String name, final String value)
309: throws DOMException {
310: if (SVGConstants.SVG_CX_ATTRIBUTE == name) {
311: setCx(parseFloatTrait(name, value));
312: } else if (SVGConstants.SVG_CY_ATTRIBUTE == name) {
313: setCy(parseFloatTrait(name, value));
314: } else if (SVGConstants.SVG_R_ATTRIBUTE == name) {
315: setR(parsePositiveFloatTrait(name, value));
316: } else {
317: super .setTraitImpl(name, value);
318: }
319: }
320:
321: /**
322: * Set the trait value as float.
323: *
324: * Supported float traits: stroke-width, stroke-miterlimit,
325: * stroke-dashoffset, fill-opacity, stroke-opacity.
326: *
327: * @param name the name of the trait to set.
328: * @param value the value of the trait to set.
329: *
330: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
331: * trait is not supported on this element.
332: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
333: * trait's value cannot be specified as a float
334: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
335: * value is an invalid value for the given trait.
336: * @throws SecurityException if the application does not have the necessary
337: * privilege rights to access this (SVG) content.
338: */
339: public void setFloatTraitImpl(final String name, final float value)
340: throws DOMException {
341: if (SVGConstants.SVG_CX_ATTRIBUTE == name) {
342: setCx(value);
343: } else if (SVGConstants.SVG_CY_ATTRIBUTE == name) {
344: setCy(value);
345: } else if (SVGConstants.SVG_R_ATTRIBUTE == name) {
346: checkPositive(name, value);
347: setR(value);
348: } else {
349: super .setFloatTraitImpl(name, value);
350: }
351: }
352:
353: /**
354: * @param name the name of the trait to convert.
355: * @param value the float trait value to convert.
356: */
357: String toStringTrait(final String name, final float[][] value) {
358: if (SVGConstants.SVG_CX_ATTRIBUTE == name
359: || SVGConstants.SVG_CY_ATTRIBUTE == name
360: || SVGConstants.SVG_R_ATTRIBUTE == name) {
361: return Float.toString(value[0][0]);
362: } else {
363: return super .toStringTrait(name, value);
364: }
365: }
366:
367: /**
368: * Computes the paint in user space on use.
369: *
370: * @return the computed PaintDef.
371: */
372: protected PaintDef computePaint() {
373: if (computedPaint == null) {
374: buildGradientColorMap();
375: computedPaint = new RadialGradientPaintDef(cx, cy, cx, cy,
376: r, lastColorMapFractions, lastColorMapRGBA,
377: RadialGradientPaintDef.CYCLE_NONE, isObjectBBox,
378: transform);
379: }
380: return computedPaint;
381: }
382: }
|