FileDocCategorySizeDatePackage
ShapeNode.javaAPI DocphoneME MR2 API (J2ME)17114Wed May 02 18:00:36 BST 2007com.sun.perseus.model

ShapeNode

public class ShapeNode extends AbstractShapeNode
A ShapesNode is an AbstractShapeNode which draws a java.awt.geom.GeneralPath.
Note that the paint method throws a NullPointerException when called before setting the shape property to a non-null value.
version
$Id: ShapeNode.java,v 1.17 2006/06/29 10:47:34 ln156897 Exp $

Fields Summary
static final com.sun.perseus.j2d.Path
EMPTY_PATH
EMPTY_PATH is used initially by the ShapeNode class. This constant is used so that a new ShapeNode, right upon creation, may be rendered. The only place where the path is mutated is in the animation code. But the animation can only work if the path has real data. Therefore, it is safe to use this final static constant to initialize all ShapeNode instances (and avoid instantiating Path objects that are always tossed away later on).
static final String[]
PATH_REQUIRED_TRAITS
The d attribute is required on
static final String[]
POLY_ALL_REQUIRED_TRAITS
The points attribute is required on and
protected com.sun.perseus.j2d.Path
path
The Path painted by this node. Should _never_ be set to null.
protected String
localName
The path's local name. One of SVGConstants.SVG_PATH_TAG, SVGConstants.SVG_POLYGON_TAG, or SVGConstants.SVG_POLYLINE_TAG.
Constructors Summary
public ShapeNode(DocumentNode ownerDocument)
Constructor.

param
ownerDocument this element's owner DocumentNode


                
        
        this(ownerDocument, SVGConstants.SVG_PATH_TAG);
        
        // The ShapeNode initially has an empty path, so that is reflected in 
        // the canRenderState.
        canRenderState |= CAN_RENDER_EMPTY_PATH_BIT;
    
public ShapeNode(DocumentNode ownerDocument, String localName)
Constructs a new ShapeNode to represent an path, polyline, or polygon (depending on the localName value).

param
ownerDocument this element's owner DocumentNode
param
localName the element's local name. One of SVGConstants.SVG_PATH_TAG, SVGConstants.SVG_POLYGON_TAG, or SVGConstants.SVG_POLYLINE_TAG.

        super(ownerDocument);

        if (SVGConstants.SVG_POLYLINE_TAG == localName
            ||
            SVGConstants.SVG_POLYGON_TAG == localName
            ||
            SVGConstants.SVG_PATH_TAG == localName) {
            this.localName = localName;
        } else {
            throw new IllegalArgumentException();
        }
    
Methods Summary
com.sun.perseus.j2d.BoxaddNodeBBox(com.sun.perseus.j2d.Box bbox, com.sun.perseus.j2d.Transform t)

param
bbox the bounding box to which this node's bounding box should be appended. That bounding box is in the target coordinate space. It may be null, in which case this node should create a new one.
param
t the transform from the node coordinate system to the coordinate system into which the bounds should be computed.
return
the bounding box of this node, in the target coordinate space,

        return addShapeBBox(bbox, path, t);
    
public booleancontains(float x, float y, int fillRule)

param
x the hit point coordinate along the x-axis, in user space.
param
y the hit point coordinate along the y-axis, in user space.
param
fillRule the fillRule to apply when testing for containment.
return
true if the hit point is contained within the shape.

        return PathSupport.isHit(path, fillRule, x, y);
    
TraitAnimcreateTraitAnimImpl(java.lang.String traitName)

param
traitName the trait name.

        if (SVGConstants.SVG_D_ATTRIBUTE == traitName
            ||
            SVGConstants.SVG_POINTS_ATTRIBUTE == traitName) {
            return new FloatTraitAnim(this, traitName, TRAIT_TYPE_SVG_PATH);
        } else {
            return super.createTraitAnimImpl(traitName);
        }
    
public voiddrawShape(com.sun.perseus.j2d.RenderGraphics rg)

param
rg the RenderGraphics on which to draw the shape.

        rg.draw(path);
    
public voidfillShape(com.sun.perseus.j2d.RenderGraphics rg)

param
rg the RenderGraphics on which to fill the shape.

        rg.fill(path);
    
public java.lang.StringgetLocalName()

return
the SVGConstants.SVG_PATH_TAG value

        return localName;
    
org.w3c.dom.svg.SVGPathgetPathTraitImpl(java.lang.String name)
ShapeNode handles the 'd' trait.

param
name the trait's name (e.g, "d")
return
the trait's SVGPath value.
throws
DOMException with error code NOT_SUPPORTED_ERROR if the requested trait is not supported on this element or null.
throws
DOMException with error code TYPE_MISMATCH_ERR if requested trait's computed value cannot be converted to {@link org.w3c.dom.svg.SVGPath SVGPath}
throws
SecurityException if the application does not have the necessary privilege rights to access this (SVG) content.

        if (SVGConstants.SVG_PATH_TAG.equals(localName)
            && SVGConstants.SVG_D_ATTRIBUTE.equals(name)) {
            return new Path(path);
        } else {
            return super.getPathTraitImpl(name);
        }
    
public java.lang.String[]getRequiredTraits()

return
an array of traits that are required by this element.

        if (SVGConstants.SVG_PATH_TAG == localName) {
            return PATH_REQUIRED_TRAITS;
        } else {
            return POLY_ALL_REQUIRED_TRAITS;
        }
    
public com.sun.perseus.j2d.PathgetShape()

return
the Path drawn by this node

        return path;
    
java.lang.ObjectgetStrokedPath(com.sun.perseus.j2d.GraphicsProperties gp)
Returns the stroked shape, using the given stroke properties.

param
gp the GraphicsProperties defining the rendering context.
return
the shape's stroked path.

        return PathSupport.getStrokedPath(path, gp);
    
public java.lang.StringgetTraitImpl(java.lang.String name)
ShapeNode handles the 'd' trait.

param
name the trait name (e.g., "d")
return
the trait's value as a string (e.g., "M0,0L50,20Z")
throws
DOMException with error code NOT_SUPPORTED_ERROR if the requested trait is not supported on this element or null.
throws
DOMException with error code TYPE_MISMATCH_ERR if requested trait's computed value cannot be converted to a String (SVG Tiny only).

        if (SVGConstants.SVG_PATH_TAG == localName) {
            if (SVGConstants.SVG_D_ATTRIBUTE == name) {
                return path.toString();
            }
        } else if (SVGConstants.SVG_POINTS_ATTRIBUTE == name) {
            return path.toPointsString();
        }
        return super.getTraitImpl(name);
    
public ElementNodenewInstance(DocumentNode doc)
Used by DocumentNode to create a new instance from a prototype SVG.

param
doc the DocumentNode for which a new node is should be created.
return
a new SVG for the requested document.

        return new ShapeNode(doc, localName);
    
voidsetFloatArrayTrait(java.lang.String name, float[][] value)
Set the trait value as float.

param
name the trait's name.
param
value the trait's value.
throws
DOMException with error code NOT_SUPPORTED_ERROR if the requested trait is not supported on this element.
throws
DOMException with error code TYPE_MISMATCH_ERR if the requested trait's value cannot be specified as a float
throws
DOMException with error code INVALID_ACCESS_ERR if the input value is an invalid value for the given trait.

        if (SVGConstants.SVG_D_ATTRIBUTE == name
            ||
            SVGConstants.SVG_POINTS_ATTRIBUTE == name) {
            if (!path.equals(value)) {
                modifyingNode();
                renderingDirty();
                path.setData(value[0]);
                modifiedNode();
            }
        } else {
            super.setFloatArrayTrait(name, value);
        }
    
public voidsetPath(com.sun.perseus.j2d.Path newPath)

param
newPath the new path for this node

        if (equal(newPath, path)) {
            return;
        }

        modifyingNode();
        renderingDirty();
        if (newPath != null) {
            this.path = newPath;
        } else {
            this.path = new Path();
        }
        computeCanRenderEmptyPathBit(this.path);
        modifiedNode();
    
voidsetPathTraitImpl(java.lang.String name, org.w3c.dom.svg.SVGPath path)
ShapeNode handles the 'd' trait.

param
name the trait's name (e.g., "d")
param
value the trait's new SVGPath value.
throws
DOMException with error code NOT_SUPPORTED_ERROR if the requested trait is not supported on this element or null.
throws
DOMException with error code TYPE_MISMATCH_ERR if the requested trait's value cannot be specified as an {@link org.w3c.dom.svg.SVGPath SVGPath}
throws
DOMException with error code INVALID_ACCESS_ERR if the input value is an invalid value for the given trait or null. SVGPath is invalid if it does not begin with a MOVE_TO segment.

        // Note that here, we use equals because the strings 
        // have not been interned.
        if (SVGConstants.SVG_PATH_TAG.equals(localName)
            &&
            SVGConstants.SVG_D_ATTRIBUTE.equals(name)) {
            if (path.getNumberOfSegments() > 0
                &&
                path.getSegment(0) != SVGPath.MOVE_TO) {
                // The first command _must_ be a moveTo.
                // However, note than an empty path is accepted.
                throw illegalTraitValue(name, path.toString());
            }
            setPath(new Path((Path) path));
        } else {
            super.setPathTraitImpl(name, path);
        }
    
public voidsetTraitImpl(java.lang.String name, java.lang.String value)
ShapeNode handles the 'd' trait if it is a path and the 'points' trait if it is a polygon or a polyline.

param
name the trait's name (e.g, "d")
param
value the trait's new value (e.g., "M0, 0 L50, 50 Z"), using the SVG path syntax.
throws
DOMException with error code NOT_SUPPORTED_ERROR if the requested trait is not supported on this element or null.
throws
DOMException with error code INVALID_ACCESS_ERR if the input value is an invalid value for the given trait or null.
throws
DOMException with error code NO_MODIFICATION_ALLOWED_ERR: if attempt is made to change readonly trait.

        if (SVGConstants.SVG_PATH_TAG == localName) {
            if (SVGConstants.SVG_D_ATTRIBUTE == name) {
                setPath(parsePathTrait(name, value));
            } else {
                super.setTraitImpl(name, value);
            }
        } else if (SVGConstants.SVG_POINTS_ATTRIBUTE == name) {
            setPath(parsePointsTrait(name, value));
            
            // As per the specification, section 9.7 of the SVG 1.1 spec.,
            // we need to close the path if we are dealing with a polygon.
            // Only do so if the document does not have a delayed exception
            // which means this element was loading and an error was detected 
            // in the points data.
            if (ownerDocument.getDelayedException() == null) {
                if (SVGConstants.SVG_POLYGON_TAG == localName) {
                    path.close();
                }
            }
        } else {
            super.setTraitImpl(name, value);
        }
    
booleansupportsTrait(java.lang.String traitName)
ShapeNode handles the 'd' trait.

param
traitName the name of the trait which the element may support.
return
true if this element supports the given trait in one of the trait accessor methods.

        if (SVGConstants.SVG_PATH_TAG == localName) {
            if (SVGConstants.SVG_D_ATTRIBUTE == traitName) {
                return true;
            }
        } else if (SVGConstants.SVG_POINTS_ATTRIBUTE == traitName) {
            // For polygon and polyline, the points trait is
            // supported.
            return true;
        }
        
        return super.supportsTrait(traitName);
    
java.lang.StringtoStringTrait(java.lang.String name, float[][] value)

param
name the name of the trait to convert.
param
value the float trait value to convert.

        if (SVGConstants.SVG_PATH_TAG.equals(localName)
            &&
            SVGConstants.SVG_D_ATTRIBUTE.equals(name)) {
            return path.toString(value[0]);
        } else if (SVGConstants.SVG_POINTS_ATTRIBUTE.equals(name)) {
            return path.toPointsString(value[0]);
        } else {
            return super.toStringTrait(name, value);
        }
    
public float[][]validateFloatArrayTrait(java.lang.String traitName, java.lang.String value, java.lang.String reqNamespaceURI, java.lang.String reqLocalName, java.lang.String reqTraitNamespace, java.lang.String reqTraitName)
Validates the input trait value.

param
traitName the name of the trait to be validated.
param
value the value to be validated
param
reqNamespaceURI the namespace of the element requesting validation.
param
reqLocalName the local name of the element requesting validation.
param
reqTraitNamespace the namespace of the trait which has the values value on the requesting element.
param
reqTraitName the name of the trait which has the values value on the requesting element.
throws
DOMException with error code INVALID_ACCESS_ERR if the input value is incompatible with the given trait.

        if (SVGConstants.SVG_D_ATTRIBUTE == traitName) {
            Path path = parsePathTrait(traitName, value);
            return toAnimatedFloatArray(path);
        } else if (SVGConstants.SVG_POINTS_ATTRIBUTE == traitName) {
            Path path = parsePointsTrait(traitName, value);
            if (SVGConstants.SVG_POLYGON_TAG == localName) {
                path.close();
            }
            return toAnimatedFloatArray(path);
        } else {
            return super.validateFloatArrayTrait(traitName,
                                                 value,
                                                 reqNamespaceURI,
                                                 reqLocalName,
                                                 reqTraitNamespace,
                                                 reqTraitName);
        }