com.miginfocom.ashape.shapes
Class AbstractAShape

java.lang.Object
  extended by com.miginfocom.ashape.shapes.AbstractAShape
All Implemented Interfaces:
AShape, PropertyProvider
Direct Known Subclasses:
ContainerAShape, DividerAShape, FilterAShape, ImageAShape, JComponentAShape, RootAShape, TextAShape, VectorAShape

public abstract class AbstractAShape
extends java.lang.Object
implements AShape

Contains the framework for the attributed shapes. Extend this to construct a simple and standard AShape. It supports serializing to XMl via the beans framework introduced in JDK 1.4.
The name is the only attribute that is not stored in the attribute map.

Note! All sub shapes need to provide an empty constructor to be serializable to XML. as long as all attributes are stores with setAttribute(PropertyKey, Object) and getAttribute(PropertyKey) they will be handled autmatically.


Field Summary
protected  java.awt.Shape hitShape
          After a paint this field should always represent the hit shape for mouse com.miginfocom.tests and collision com.miginfocom.tests.
static boolean RETURN_LIVE_ATTR_MAP
          Can be set to true to enable leading of pre 5.3 shapes.
protected static PropertyKey[] SIZE_CONV_MAP
          Indexed with [(type << 1) + dim] type is 0..2 and dim is 0 or 1.
 
Fields inherited from interface com.miginfocom.ashape.shapes.AShape
A_ALIGN_X, A_ALIGN_Y, A_ANTI_ALIAS, A_BORDER, A_CLIP_TYPE, A_COMPOSITE, A_CROP, A_CROP_TO_VISIBILITY_BOUNDS, A_FONT, A_HIT_AREA_Z, A_ICON, A_IMAGE, A_INHERIT, A_LAYOUT, A_MAXIMUM_HEIGHT, A_MAXIMUM_WIDTH, A_MINIMUM_HEIGHT, A_MINIMUM_WIDTH, A_MOUSE_CURSOR, A_PAINT, A_PAINT_LAYER, A_PLACE_RECT, A_PREFERRED_HEIGHT, A_PREFERRED_WIDTH, A_REPORT_HIT_AREA, A_SHADOW_TARGET_PAINT, A_SHAPE, A_SHAPES, A_STROKE, A_SUB_SHAPE_SORTER, A_TEXT, A_TEXTURE_PAINT_ANCHOR, A_UNDERLINE_HEIGHT, A_VISIBILITY, A_WRAPPED_ASHAPE, CLIP_PARENT_BOUNDS, CLIP_PARENT_SHAPE
 
Constructor Summary
protected AbstractAShape()
          Contructor for XML builder.
protected AbstractAShape(java.lang.String name)
          Contructor.
 
Method Summary
 void addAttributes(java.util.Map map)
          Adds all attributes to the attribute map
 void addSubShape(AShape aShape)
          Add a shape to this shape as a sub-shape
 void addSubShape(AShape aShape, int index)
          Add a shape to this shape as a sub-shape at index-
protected  void addToHitShapeList(java.util.Collection haList)
          Add this shape's hit area and name and the ask it's children to do the same.
 boolean containsProperty(PropertyKey property)
          Return if the provider contains the property.
 java.lang.Object getAttribute(PropertyKey key)
          Returns an attribute.
 java.util.Map getAttributes()
          Returns a cloned map of all attributes.
protected  java.awt.Shape getClipShape(java.awt.Shape s)
          Returns a shape that is to be used for clipping for THIS shape.
 double getDoubleAttribute(PropertyKey key, double def)
          Returns an attribute as an int.
 float getFloatAttribute(PropertyKey key, float def)
          Returns an attribute as an int.
 java.awt.Shape getHitShape()
          Returns the current hit shape as setted by setHitShape(java.awt.Shape, java.awt.Shape) at the last repaint.
 int getIntAttribute(PropertyKey key, int def)
          Returns an attribute as an int.
protected  AShapeLayout getLayout()
          Returns the current layout or DEFAULT_LAYOUT if the attribute AShape.A_LAYOUT returns null.
 java.lang.Integer getLayoutSize(int type, int dim, java.lang.Integer otherDimSizeHint)
          Returns the min, max or preferred size in the horizontal or vertical dimension.
 long getLongAttribute(PropertyKey key, long def)
          Returns an attribute as an int.
 int getModCount()
          How many time the shape has been changed.
 java.lang.String getName()
          Returns the name for this shape.
 java.lang.Integer getPaintLayer()
          Returns the attribute value for AShape.A_PAINT_LAYER but exchanges null for Integer(0).
 AShape getParent()
          Returns the current parent.
 java.lang.Object getProperty(PropertyKey key)
          This is a method that should be dispatched to AShape.getAttribute(PropertyKey).
 java.awt.Rectangle getReferenceBounds()
          Returns the bounds set with AShape.setReferenceBounds(java.awt.Rectangle).
 RootAShape getRoot()
          Returns the root shape or null if the top of the tree isn't a RootAShape.
 java.awt.Rectangle getShapeBounds()
          Returns where on screen the shape would be painted.
 AShape getSubShapeDeep(java.lang.String name)
          Traverses the tree of shapes and returns the first one with the name name.
 AShape[] getSubShapes()
          Returns the live array of sub shapes.
protected  AShape[] getSubShapesImpl()
          Returns the sub shapes and if there is a sorter (AShape.A_SUB_SHAPE_SORTER clones the array and sorts them first.
 boolean hasAttribute(PropertyKey key)
          Returns if the attribute exists and has a mapping.
 void increaseModCount()
          Increases the modecount which noramlly invalidates any caches.
 boolean isRoot()
           
 boolean isVisible()
          Returns if THIS shape is visible.
 void layout()
          Lays out the children, if any bu calling setBounds() on them and then call .layout().
Layout also clears the 'hit shape'.
protected  void paintBorder(java.awt.Graphics2D g2)
          Paints the AShape.A_BORDER if one is present.
 void paintSubTree(java.awt.Graphics2D g2, java.awt.Shape parentShape, java.awt.Rectangle visBounds, java.lang.Integer layer)
          Paints the shape, if it it visible, and then the sub shapes with the appropriate clip.
 void removeAttribute(PropertyKey key)
          Removes a attribute if it exist.
 void removeSubShape(AShape aShape)
          removes a sub-shape if it exist.
 void setAttribute(PropertyKey key, java.lang.Object value)
          Sets a attribute object.
 void setAttributeDeep(PropertyKey key, java.lang.Object value, int maxLevels)
          Convenience method for setting an attributeon this shape and recursively down the sub shape tree.
 void setAttributes(java.util.Map map)
          Sets the attributes, discarding any old ones.
protected  void setHitShape(java.awt.Shape clip, java.awt.Shape shape)
          Set the area that this shape presents to the user.
 void setName(java.lang.String name)
          Sets the name.
 void setParent(AShape parent)
          Sets the parent shape.
 void setReferenceBounds(int x, int y, int w, int h)
          Sets the bounds that this shape should relate to with it's (AbsRect) placeRect.
 void setReferenceBounds(java.awt.Rectangle b)
          Sets the bounds that this shape should relate to with it's (AbsRect) placeRect.
 void setSubShapes(AShape[] shapes)
          Sets the array of sub shapes discarding any old ones.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.miginfocom.ashape.shapes.AShape
getHitBounds, getNaturalSize, paintShape
 

Field Detail

hitShape

protected transient java.awt.Shape hitShape
After a paint this field should always represent the hit shape for mouse com.miginfocom.tests and collision com.miginfocom.tests. May be null if no area is spanned.

See Also:
setHitShape(java.awt.Shape, java.awt.Shape).

SIZE_CONV_MAP

protected static final PropertyKey[] SIZE_CONV_MAP
Indexed with [(type << 1) + dim] type is 0..2 and dim is 0 or 1.


RETURN_LIVE_ATTR_MAP

public static boolean RETURN_LIVE_ATTR_MAP
Can be set to true to enable leading of pre 5.3 shapes. AShapeUtil.load does this

Constructor Detail

AbstractAShape

protected AbstractAShape()
Contructor for XML builder. Name is temprarily set to a random name since name can be null-


AbstractAShape

protected AbstractAShape(java.lang.String name)
Contructor.

Parameters:
name - The name. Not null!
Method Detail

getModCount

public int getModCount()
Description copied from interface: AShape
How many time the shape has been changed. May be used to validate caches.

Specified by:
getModCount in interface AShape
Returns:
How many time the shape has been changed

increaseModCount

public final void increaseModCount()
Description copied from interface: AShape
Increases the modecount which noramlly invalidates any caches. All operations in the implementing classes that change anything that affects something should call this method.

Specified by:
increaseModCount in interface AShape

paintSubTree

public final void paintSubTree(java.awt.Graphics2D g2,
                               java.awt.Shape parentShape,
                               java.awt.Rectangle visBounds,
                               java.lang.Integer layer)
Description copied from interface: AShape
Paints the shape, if it it visible, and then the sub shapes with the appropriate clip. This method should normally not be called by client code.

Specified by:
paintSubTree in interface AShape
Parameters:
g2 - The object to draw in. Settings won't change
parentShape - The shape to clip the graphics with. This shape is not already added to the clip in the Grahpics2D object sent in since this shape will also possibly reduce the interaction area (hit area) that the shape presents. The Grahpics2D object's clip will not however since it may only be repainting a part of the shape. Can be null.
visBounds - The bounds of the container that holds this shape. Used for determining the "visual" bounds to, for instance, move text within these bounds in certain cases. Can be null.
layer - The level that is currently being painted. May be null. Only shapes in this level should paint themselves.
See Also:
AShape.A_CROP_TO_VISIBILITY_BOUNDS, AShape.A_CLIP_TYPE, AShape.A_PAINT_LAYER

paintBorder

protected void paintBorder(java.awt.Graphics2D g2)
Paints the AShape.A_BORDER if one is present. Does NOT reduce the bounds.

Parameters:
g2 - The object to draw in. Settings won't change

getPaintLayer

public final java.lang.Integer getPaintLayer()
Description copied from interface: AShape
Returns the attribute value for AShape.A_PAINT_LAYER but exchanges null for Integer(0).

Specified by:
getPaintLayer in interface AShape
Returns:
The paint layer for this shape. Not null.

setReferenceBounds

public void setReferenceBounds(java.awt.Rectangle b)
Description copied from interface: AShape
Sets the bounds that this shape should relate to with it's (AbsRect) placeRect.

Specified by:
setReferenceBounds in interface AShape
Parameters:
b - The bounds. Not null. The object's values are copied and the reference is not kept.

setReferenceBounds

public void setReferenceBounds(int x,
                               int y,
                               int w,
                               int h)
Description copied from interface: AShape
Sets the bounds that this shape should relate to with it's (AbsRect) placeRect.

Specified by:
setReferenceBounds in interface AShape
Parameters:
x - X
y - Y
w - Width
h - Height

getReferenceBounds

public java.awt.Rectangle getReferenceBounds()
Description copied from interface: AShape
Returns the bounds set with AShape.setReferenceBounds(java.awt.Rectangle). Note that this is just the "relative bounds", the real on-screen shape bounds can be retrieved with AShape.getShapeBounds().

READ_ONLY! Not cloned since this might be read thousands of times per frame.

Specified by:
getReferenceBounds in interface AShape
Returns:
The contained bounds object, to avoid object creation, so it's read-only or stange bugs will appear.

getLayoutSize

public java.lang.Integer getLayoutSize(int type,
                                       int dim,
                                       java.lang.Integer otherDimSizeHint)
Description copied from interface: AShape
Returns the min, max or preferred size in the horizontal or vertical dimension. For instance a text shape would want to have a reference width to be able to calculate the preferred height. Also usable for shapes that want's to maintain some static aspect ratio or span a certain area.
This information is normally used by a AShapeLayout.
If no layout size is available for the shape, or any of its sub shape, and the preferred size is asked for, the natural size (for instance via AShape.getNaturalSize()) of the shape may be returned.

Specified by:
getLayoutSize in interface AShape
Parameters:
type - The type of size to get. E.g. Distributor.MIN
dim - The dimension for which to return the layout size. E.g. SwingConstants.VERTICAL.
otherDimSizeHint - If the width/height layout size is interconnected is any way and the size in that other dimension is known already, that size if entered here.
Returns:
The size or null if there was no preference by any of the sub shapes.
See Also:
AShape.A_PREFERRED_WIDTH

getLayout

protected AShapeLayout getLayout()
Returns the current layout or DEFAULT_LAYOUT if the attribute AShape.A_LAYOUT returns null.

Returns:
The current layout, never null.

layout

public void layout()
Description copied from interface: AShape
Lays out the children, if any bu calling setBounds() on them and then call .layout().
Layout also clears the 'hit shape'. The hit shape is set again when the shape is painted, which normally happens right after it is laid out.

Specified by:
layout in interface AShape

getShapeBounds

public java.awt.Rectangle getShapeBounds()
Returns where on screen the shape would be painted. Implementation only compensates for the "place rect". Subclasses that paints outside this or bases the placing on screeen on something else must override this and make a correct calculation of the bounds.

Specified by:
getShapeBounds in interface AShape
Returns:
A new Rectangle representing the bounds or null if, for any reason, the shape can't or wont paint itself within these bounds.
See Also:
getReferenceBounds(), AShape.A_PLACE_RECT

getClipShape

protected java.awt.Shape getClipShape(java.awt.Shape s)
Returns a shape that is to be used for clipping for THIS shape.

Parameters:
s - The most accurate shape availabe for clipping.
Returns:
s, the bounds of s or null depending on the value of the attribute AShape.A_CLIP_TYPE. Note that if s is a Rectangle and the bounds should be returned, s itself are returned to avoid object creation. The returned value should therefore be considered read-only.

isVisible

public boolean isVisible()
Description copied from interface: AShape
Returns if THIS shape is visible. Doesn't take parent's visibility into acount. If AShape.A_VISIBILITY is non-null that value will be returned. If null true will be returned.
The bounds should be correct before calling this methid since the bounds might be a criteria on which to decide.

Specified by:
isVisible in interface AShape
Returns:
If THIS shape is visible or not, not checking the parent shape.

setHitShape

protected void setHitShape(java.awt.Shape clip,
                           java.awt.Shape shape)
Set the area that this shape presents to the user. This is not the same as the bounds since a part of the bounds could been cut off or not painted. Normally though, this is the painted bounds and clip is some cropping or the like that is set on the shape.

This method should be called everytime the shape is repainted since the controller of this shape will after repainting get this area to use for mouse hit detection, collisions and such. The method is optimized to recognize Rectangle2Ds for both clip and shape but any shape is ok, but slower since it means Area intersection calculations.

Parameters:
clip - An optional clip to exclude from shape. Can be null.
shape - The shape to add. If null that means this shape doesn't span an area. The actual shape will be overtaken and stored so it can't be changed by the caller anymore.

getHitShape

public java.awt.Shape getHitShape()
Returns the current hit shape as setted by setHitShape(java.awt.Shape, java.awt.Shape) at the last repaint.

Returns:
The current hit shape. May be null. The live object so don't change it!

addToHitShapeList

protected void addToHitShapeList(java.util.Collection haList)
Add this shape's hit area and name and the ask it's children to do the same. The object type to add must be NameValuePair with name set to the name of this shape and value set to the hit shape, which may be null.

Parameters:
haList - The list to add to. Never null.
See Also:
setHitShape(java.awt.Shape, java.awt.Shape), RootAShape.getHitShapes()

getSubShapesImpl

protected AShape[] getSubShapesImpl()
Returns the sub shapes and if there is a sorter (AShape.A_SUB_SHAPE_SORTER clones the array and sorts them first.

Returns:
The sub shapes and if there is a sorter.

getName

public java.lang.String getName()
Description copied from interface: AShape
Returns the name for this shape.

Specified by:
getName in interface AShape
Returns:
The name for this shape. Not null !

setName

public void setName(java.lang.String name)
Description copied from interface: AShape
Sets the name.

Specified by:
setName in interface AShape
Parameters:
name - The name. Not null !

addSubShape

public void addSubShape(AShape aShape)
Description copied from interface: AShape
Add a shape to this shape as a sub-shape

Specified by:
addSubShape in interface AShape
Parameters:
aShape - The AShape to add

addSubShape

public void addSubShape(AShape aShape,
                        int index)
Description copied from interface: AShape
Add a shape to this shape as a sub-shape at index-

Specified by:
addSubShape in interface AShape
Parameters:
aShape - The AShape to add
index - The index to add. If larger than array it will always be added last.

removeSubShape

public void removeSubShape(AShape aShape)
Description copied from interface: AShape
removes a sub-shape if it exist.

Specified by:
removeSubShape in interface AShape
Parameters:
aShape - The shape to remove

getSubShapes

public AShape[] getSubShapes()
Description copied from interface: AShape
Returns the live array of sub shapes. Should not be changed.

Specified by:
getSubShapes in interface AShape
Returns:
The live array of sub shapes. Not null.

setSubShapes

public void setSubShapes(AShape[] shapes)
Sets the array of sub shapes discarding any old ones.

Parameters:
shapes - The new list of sub shapes. Object will be overtaken and not cloned!

getSubShapeDeep

public AShape getSubShapeDeep(java.lang.String name)
Description copied from interface: AShape
Traverses the tree of shapes and returns the first one with the name name.

Specified by:
getSubShapeDeep in interface AShape
Parameters:
name - The name.
Returns:
The first found or null if name was not found.

getAttribute

public java.lang.Object getAttribute(PropertyKey key)
Description copied from interface: AShape
Returns an attribute. The current Interactors are first asked whether it has an override for this attribute, and of so, returns that object.

Specified by:
getAttribute in interface AShape
Parameters:
key - The key/name that it was stored under. E.g. AShape.A_PAINT, but can be anything.
Returns:
The object. null if null is stored or key doesn't exist.

getProperty

public final java.lang.Object getProperty(PropertyKey key)
Description copied from interface: AShape
This is a method that should be dispatched to AShape.getAttribute(PropertyKey). It is called getProperty only to satisfy the interface PropertyProvider.

Specified by:
getProperty in interface AShape
Specified by:
getProperty in interface PropertyProvider
Parameters:
key - The key/name that it was stored under. E.g. AShape.A_FONT but can be anything.
Returns:
The object. null if null is stored or key doesn't exist.
See Also:
AShape.getAttribute(PropertyKey)

containsProperty

public boolean containsProperty(PropertyKey property)
Description copied from interface: PropertyProvider
Return if the provider contains the property.

Specified by:
containsProperty in interface PropertyProvider
Parameters:
property - The property key. Not null.
Returns:
If the provider contains the property.

getIntAttribute

public int getIntAttribute(PropertyKey key,
                           int def)
Returns an attribute as an int. The value stored must be a Number and .intValue() is used to retrieve the value. If null def is returned.

Parameters:
key - The key/name that it was stored under. E.g. AShape.A_PREFERRED_HEIGHT but can be anything.
def - The default value if the value is null.
Returns:
An attribute as an int.

getFloatAttribute

public float getFloatAttribute(PropertyKey key,
                               float def)
Returns an attribute as an int. The value stored must be a Number and .floatValue() is used to retrieve the value. If null def is returned.

Parameters:
key - The key/name that it was stored under. E.g. AShape.A_PREFERRED_HEIGHT but can be anything.
def - The default value if the value is null.
Returns:
An attribute as an int.

getDoubleAttribute

public double getDoubleAttribute(PropertyKey key,
                                 double def)
Returns an attribute as an int. The value stored must be a Number and .doubleValue() is used to retrieve the value. If null def is returned.

Parameters:
key - The key/name that it was stored under. E.g. AShape.A_PREFERRED_HEIGHT but can be anything.
def - The default value if the value is null.
Returns:
An attribute as an int.

getLongAttribute

public long getLongAttribute(PropertyKey key,
                             long def)
Returns an attribute as an int. The value stored must be a Number and .longValue() is used to retrieve the value. If null def is returned.

Parameters:
key - The key/name that it was stored under. E.g. AShape.A_PREFERRED_HEIGHT but can be anything.
def - The default value if the value is null.
Returns:
An attribute as an int.

setAttribute

public void setAttribute(PropertyKey key,
                         java.lang.Object value)
Description copied from interface: AShape
Sets a attribute object.

Specified by:
setAttribute in interface AShape
Parameters:
key - The key/name that it is to be stored under. E.g. AShape.A_PAINT
value - The value to store.

setAttributeDeep

public void setAttributeDeep(PropertyKey key,
                             java.lang.Object value,
                             int maxLevels)
Description copied from interface: AShape
Convenience method for setting an attributeon this shape and recursively down the sub shape tree.

Specified by:
setAttributeDeep in interface AShape
Parameters:
key - The key/name that it is to be stored under. E.g. AShape.A_PAINT
value - The value to store.
maxLevels - Only recursively add to this level. 1 will only add to this shape's sub subshapes.

removeAttribute

public void removeAttribute(PropertyKey key)
Description copied from interface: AShape
Removes a attribute if it exist.

Specified by:
removeAttribute in interface AShape
Parameters:
key - The key/name that it was stored under. E.g. AShape.A_PAINT

hasAttribute

public boolean hasAttribute(PropertyKey key)
Description copied from interface: AShape
Returns if the attribute exists and has a mapping. Note that is can still be null.

Specified by:
hasAttribute in interface AShape
Parameters:
key - The key/name that is to be tested. E.g. AShape.A_PAINT
Returns:
If the attribute exists

addAttributes

public void addAttributes(java.util.Map map)
Adds all attributes to the attribute map

Parameters:
map - The attributes. Keys that aren't PropertyKeys will be converted to that type.

getAttributes

public java.util.Map getAttributes()
Returns a cloned map of all attributes.

Returns:
All attributes.

setAttributes

public void setAttributes(java.util.Map map)
Sets the attributes, discarding any old ones.

Parameters:
map - The new attributes. The attributes are copied and the map itself is not kept.

getParent

public final AShape getParent()
Description copied from interface: AShape
Returns the current parent.

Specified by:
getParent in interface AShape
Returns:
The current parent. null for root parent.

getRoot

public RootAShape getRoot()
Description copied from interface: AShape
Returns the root shape or null if the top of the tree isn't a RootAShape. Iterates every time.

Specified by:
getRoot in interface AShape
Returns:
The root shape or this if the top of the tree isn't a RootAShape.

isRoot

public boolean isRoot()

setParent

public void setParent(AShape parent)
Description copied from interface: AShape
Sets the parent shape. Handled automatically for added subshapes.

Specified by:
setParent in interface AShape
Parameters:
parent - The parent. The root shape has a null parent.


Copyright © 2009 MiG InfoCom AB. All Rights Reserved.