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 org.w3c.dom.DOMException;
031:
032: import org.w3c.dom.svg.SVGRGBColor;
033:
034: import com.sun.perseus.j2d.PaintDef;
035: import com.sun.perseus.j2d.RGB;
036: import com.sun.perseus.j2d.Transform;
037:
038: /**
039: * <code>SolidColor</code> represents an SVG Tiny 1.2
040: * <code><solidColor></code> element.
041: * <br />
042: *
043: * @version $Id: SolidColor.java,v 1.5 2006/06/29 10:47:34 ln156897 Exp $
044: */
045: public class SolidColor extends PaintElement {
046: /**
047: * solid-color is required on <solidColor
048: */
049: static final String[] REQUIRED_TRAITS = { SVGConstants.SVG_SOLID_COLOR_ATTRIBUTE };
050:
051: /**
052: * The initial solid color value.
053: */
054: static final RGB DEFAULT_SOLID_COLOR = RGB.black;
055:
056: /**
057: * The default solid opacity
058: */
059: static final float DEFAULT_SOLID_OPACITY = 1f;
060:
061: /**
062: * The solid color value.
063: */
064: RGB solidColor = DEFAULT_SOLID_COLOR;
065:
066: /**
067: * The solid opacity value
068: */
069: float solidOpacity = DEFAULT_SOLID_OPACITY;
070:
071: /**
072: * The actual solid color value, which combines the solidColor
073: * and the solidOpacity
074: */
075: RGB compoundColor = DEFAULT_SOLID_COLOR;
076:
077: /**
078: * Constructor.
079: *
080: * @param ownerDocument this element's owner <code>DocumentNode</code>
081: */
082: public SolidColor(final DocumentNode ownerDocument) {
083: super (ownerDocument);
084:
085: // Turn off objectBBox space so that the PaintServerReference
086: // does not concatenate an extra transform unnecessariliy.
087: isObjectBBox = false;
088: }
089:
090: /**
091: * @return the SVGConstants.SVG_SOLID_COLOR_TAG value
092: */
093: public String getLocalName() {
094: return SVGConstants.SVG_SOLID_COLOR_TAG;
095: }
096:
097: /**
098: * Used by <code>DocumentNode</code> to create a new instance from
099: * a prototype <code>Rect</code>.
100: *
101: * @param doc the <code>DocumentNode</code> for which a new node is
102: * should be created.
103: * @return a new <code>SolidColor</code> for the requested document.
104: */
105: public ElementNode newInstance(final DocumentNode doc) {
106: return new SolidColor(doc);
107: }
108:
109: /**
110: * Sets the solid-color property.
111: *
112: * @param newSolidColor the new solid-color property
113: */
114: public void setSolidColor(final RGB newSolidColor) {
115: if (solidColor.equals(newSolidColor)) {
116: return;
117: }
118:
119: solidColor = newSolidColor;
120: onPaintChange();
121: }
122:
123: /**
124: * Sets the solid-opacity property.
125: *
126: * @param newSolidOpacity the new solid-opacity property
127: */
128: public void setSolidOpacity(final float newSolidOpacity) {
129: if (newSolidOpacity == solidOpacity) {
130: return;
131: }
132:
133: solidOpacity = newSolidOpacity;
134:
135: if (solidOpacity < 0) {
136: solidOpacity = 0;
137: } else if (solidOpacity > 1) {
138: solidOpacity = 1;
139: }
140: onPaintChange();
141: }
142:
143: /**
144: * Updates the compound color value and notfies related
145: * PaintServerReferences.
146: */
147: void onPaintChange() {
148: compoundColor = null;
149: notifyPaintChange();
150: }
151:
152: /**
153: * Computes the paint in user space on use.
154: *
155: * @return the computed PaintDef.
156: */
157: protected PaintDef computePaint() {
158: if (compoundColor == null) {
159: compoundColor = new RGB((int) (solidOpacity * 255f),
160: solidColor.getRed(), solidColor.getGreen(),
161: solidColor.getBlue());
162: }
163: return compoundColor;
164: }
165:
166: /**
167: * SolidColor handles solid-color and solid-opacity traits.
168: *
169: * @param traitName the name of the trait which the element may support.
170: * @return true if this element supports the given trait in one of the
171: * trait accessor methods.
172: */
173: boolean supportsTrait(final String traitName) {
174: if (SVGConstants.SVG_SOLID_COLOR_ATTRIBUTE == traitName
175: || SVGConstants.SVG_SOLID_OPACITY_ATTRIBUTE == traitName) {
176: return true;
177: } else {
178: return super .supportsTrait(traitName);
179: }
180: }
181:
182: /**
183: * @return an array of traits that are required by this element.
184: */
185: public String[] getRequiredTraits() {
186: return REQUIRED_TRAITS;
187: }
188:
189: /**
190: * SolidColor handles solid-color and solid-opacity traits as
191: * FloatTraitAnims
192: *
193: * @param traitName the trait name.
194: */
195: TraitAnim createTraitAnimImpl(final String traitName) {
196: if (SVGConstants.SVG_SOLID_OPACITY_ATTRIBUTE == traitName) {
197: return new FloatTraitAnim(this , traitName, TRAIT_TYPE_FLOAT);
198: } else if (SVGConstants.SVG_SOLID_COLOR_ATTRIBUTE == traitName) {
199: return new FloatTraitAnim(this , traitName,
200: TRAIT_TYPE_SVG_RGB_COLOR);
201: } else {
202: return super .createTraitAnimImpl(traitName);
203: }
204: }
205:
206: /**
207: * SolidColor handles solid-color and solid-opacity traits.
208: *
209: * @param name the requested trait name (e.g., "ry")
210: * @return the trait's value, as a string.
211: *
212: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
213: * trait is not supported on this element or null.
214: * @throws DOMException with error code TYPE_MISMATCH_ERR if requested
215: * trait's computed value cannot be converted to a String (SVG Tiny only).
216: */
217: public String getTraitImpl(final String name) throws DOMException {
218: if (SVGConstants.SVG_SOLID_COLOR_ATTRIBUTE == name) {
219: return solidColor.toString();
220: } else if (SVGConstants.SVG_SOLID_OPACITY_ATTRIBUTE == name) {
221: return Float.toString(solidOpacity);
222: } else {
223: return super .getTraitImpl(name);
224: }
225: }
226:
227: /**
228: * SolidColor handles the solid-opacity as a float trait
229: *
230: * @param name the requested trait name (e.g., "y")
231: * @return the requested trait value
232: *
233: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
234: * trait is not supported on this element or null.
235: * @throws DOMException with error code TYPE_MISMATCH_ERR if requested
236: * trait's computed value cannot be converted to a float
237: * @throws SecurityException if the application does not have the necessary
238: * privilege rights to access this (SVG) content.
239: */
240: float getFloatTraitImpl(final String name) throws DOMException {
241: if (SVGConstants.SVG_SOLID_OPACITY_ATTRIBUTE == name) {
242: return solidOpacity;
243: } else {
244: return super .getFloatTraitImpl(name);
245: }
246: }
247:
248: /**
249: * Supported color traits: solid-color
250: *
251: * @param name the requested trait's name.
252: * @return the requested trait's value, as an <code>SVGRGBColor</code>.
253: *
254: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
255: * trait is not supported on this element or null.
256: * @throws DOMException with error code TYPE_MISMATCH_ERR if requested
257: * trait's computed value cannot be converted to {@link
258: * org.w3c.dom.svg.SVGRGBColor SVGRGBColor}
259: * @throws SecurityException if the application does not have the necessary
260: * privilege rights to access this (SVG) content.
261: */
262: SVGRGBColor getRGBColorTraitImpl(final String name)
263: throws DOMException {
264: // We use .equals here because the name is not interned.
265: if (SVGConstants.SVG_SOLID_COLOR_ATTRIBUTE.equals(name)) {
266: return solidColor;
267: } else {
268: return super .getRGBColorTraitImpl(name);
269: }
270: }
271:
272: /**
273: * Set the trait value as float array.
274: *
275: * @param name the trait's name.
276: * @param value the trait's value.
277: *
278: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
279: * trait is not supported on this element.
280: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
281: * trait's value cannot be specified as a float
282: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
283: * value is an invalid value for the given trait.
284: */
285: void setFloatArrayTrait(final String name, final float[][] value)
286: throws DOMException {
287: if (SVGConstants.SVG_SOLID_COLOR_ATTRIBUTE == name) {
288: setSolidColor(new RGB((int) value[0][0], (int) value[0][1],
289: (int) value[0][2]));
290: } else if (SVGConstants.SVG_SOLID_OPACITY_ATTRIBUTE == name) {
291: setSolidOpacity(value[0][0]);
292: } else {
293: super .setFloatArrayTrait(name, value);
294: }
295: }
296:
297: /**
298: * Validates the input trait value.
299: *
300: * @param traitName the name of the trait to be validated.
301: * @param value the value to be validated
302: * @param reqNamespaceURI the namespace of the element requesting
303: * validation.
304: * @param reqLocalName the local name of the element requesting validation.
305: * @param reqTraitNamespace the namespace of the trait which has the values
306: * value on the requesting element.
307: * @param reqTraitName the name of the trait which has the values value on
308: * the requesting element.
309: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
310: * value is incompatible with the given trait.
311: */
312: public float[][] validateFloatArrayTrait(final String traitName,
313: final String value, final String reqNamespaceURI,
314: final String reqLocalName, final String reqTraitNamespace,
315: final String reqTraitName) throws DOMException {
316: if (SVGConstants.SVG_SOLID_COLOR_ATTRIBUTE == traitName) {
317: RGB color = DEFAULT_SOLID_COLOR;
318: if (!SVGConstants.CSS_INHERIT_VALUE.equals(value)) {
319: color = parseColorTrait(
320: SVGConstants.SVG_COLOR_ATTRIBUTE, value);
321: }
322: if (color == null) {
323: throw illegalTraitValue(traitName, value);
324: }
325: return new float[][] { { color.getRed(), color.getGreen(),
326: color.getBlue() } };
327: } else if (SVGConstants.SVG_SOLID_OPACITY_ATTRIBUTE == traitName) {
328: float so = DEFAULT_SOLID_OPACITY;
329: if (!SVGConstants.CSS_INHERIT_VALUE.equals(value)) {
330: so = parseFloatTrait(traitName, value);
331: }
332:
333: if (so < 0) {
334: so = 0;
335: } else if (so > 1) {
336: so = 1;
337: }
338: return new float[][] { { so } };
339: } else {
340: return super .validateFloatArrayTrait(traitName, value,
341: reqNamespaceURI, reqLocalName, reqTraitNamespace,
342: reqTraitName);
343: }
344:
345: }
346:
347: /**
348: * CompositeGraphicsNode handles the graphics node traits.
349: * Other attributes are handled by the super class.
350: *
351: * Supported traits: stroke-width, stroke-miterlimit, stroke-dashoffset,
352: * fill-rule, stroke-linejoin, stroke-linecap, display, visibility,
353: * color, fill, stroke, fill-opacity, stroke-opacity, stroke-dasharray
354: *
355: * @param name the name of the trait to set.
356: * @param value the value of the trait to set.
357: *
358: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
359: * trait is not supported on this element or null.
360: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
361: * trait's value cannot be specified as a String
362: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
363: * value is an invalid value for the given trait or null.
364: * @throws DOMException with error code NO_MODIFICATION_ALLOWED_ERR: if
365: * attempt is made to change readonly trait.
366: */
367: public void setTraitImpl(final String name, final String value)
368: throws DOMException {
369: if (SVGConstants.SVG_SOLID_OPACITY_ATTRIBUTE == name) {
370:
371: // ======================= solid-opacity ===================== //
372:
373: if (SVGConstants.CSS_INHERIT_VALUE.equals(value)) {
374: setSolidOpacity(DEFAULT_SOLID_OPACITY);
375: } else {
376: setSolidOpacity(parseFloatTrait(name, value));
377: }
378: } else if (SVGConstants.SVG_SOLID_COLOR_ATTRIBUTE == name) {
379:
380: // ======================== solid-color ===================== //
381:
382: if (SVGConstants.CSS_INHERIT_VALUE.equals(value)) {
383: setSolidColor(DEFAULT_SOLID_COLOR);
384: } else {
385: setSolidColor(parseColorTrait(
386: SVGConstants.SVG_SOLID_COLOR_ATTRIBUTE, value));
387: }
388: } else {
389: super .setTraitImpl(name, value);
390: }
391: }
392:
393: /**
394: * Set the trait value as float.
395: *
396: * Supported float traits: stroke-width, stroke-miterlimit,
397: * stroke-dashoffset, fill-opacity, stroke-opacity.
398: *
399: * @param name the name of the trait to set.
400: * @param value the value of the trait to set.
401: *
402: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
403: * trait is not supported on this element.
404: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
405: * trait's value cannot be specified as a float
406: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
407: * value is an invalid value for the given trait.
408: * @throws SecurityException if the application does not have the necessary
409: * privilege rights to access this (SVG) content.
410: */
411: public void setFloatTraitImpl(final String name, final float value)
412: throws DOMException {
413: if (SVGConstants.SVG_SOLID_OPACITY_ATTRIBUTE == name) {
414: setSolidOpacity(value);
415: } else {
416: super .setFloatTraitImpl(name, value);
417: }
418: }
419:
420: /**
421: * @param name the name of the trait to convert.
422: * @param value the float trait value to convert.
423: */
424: String toStringTrait(final String name, final float[][] value) {
425: if (SVGConstants.SVG_SOLID_OPACITY_ATTRIBUTE == name) {
426: return Float.toString(value[0][0]);
427: } else if (SVGConstants.SVG_SOLID_COLOR_ATTRIBUTE == name) {
428: return toRGBString(name, value);
429: } else {
430: return super .toStringTrait(name, value);
431: }
432: }
433:
434: /**
435: * Set the trait value as {@link org.w3c.dom.svg.SVGRGBColor SVGRGBColor}.
436: *
437: * Supported color traits: color, fill, stroke
438: *
439: * @param name the name of the trait to set.
440: * @param value the value of the trait to set.
441: *
442: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
443: * trait is not supported on this element or null.
444: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
445: * trait's value cannot be specified as an {@link
446: * org.w3c.dom.svg.SVGRGBColor SVGRGBColor}
447: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
448: * value is null.
449: * @throws SecurityException if the application does not have the necessary
450: * privilege rights to access this (SVG) content.
451: */
452: void setRGBColorTraitImpl(final String name, final SVGRGBColor color)
453: throws DOMException {
454: try {
455: if (SVGConstants.SVG_SOLID_COLOR_ATTRIBUTE.equals(name)) {
456: setSolidColor((RGB) color);
457: } else {
458: super .setRGBColorTraitImpl(name, color);
459: }
460: } catch (IllegalArgumentException iae) {
461: throw new DOMException(DOMException.INVALID_ACCESS_ERR, iae
462: .getMessage());
463: }
464: }
465: }
|