org.graphstream.ui.spriteManager
Class Sprite

java.lang.Object
  extended by org.graphstream.ui.spriteManager.Sprite
All Implemented Interfaces:
Element

public class Sprite
extends Object
implements Element

A gentle little sprite.

Sprite objects allow to add data representations in a graphic display of a graph. A sprite is a graphical representation that can double anywhere in the graph drawing surface, or be "attached" to nodes or edges. When attached to an edge, a sprite can be positioned easily at any point along the edge, or perpendicular to it with one or two coordinates. When attached to a node, a sprite "orbits" around the node at any given radius and angle around it.

Sprites can have many shapes. Most of the CSS nodes shapes are available for sprites, but more are possible. Some shapes follow the form of the element (node or edge) they are attached to.

Sprites can be moved and animated easily along edges, around nodes, or anywhere on the graph surface. Their shape can change. Some sprites allows to draw pie charts or statistics, or images.

Sprites are not part of a graph so to speak. Furthermore they make sense only when a graph is displayed with a viewer that supports sprites. Therefore they are handled by a SpriteManager which is always associated to a graph and is in charge of handling the whole set of sprites, creating them, enumerating them, and destroying them.

Implementation note: sprites do not exist ! In fact the sprite class only handles a set of attributes that are stored in the graph (the one associated with the sprite manager that created the sprite). These special attributes are handled for you by the sprite class. This technique allows to pass sprites informations through the I/O system of GraphStream. Indeed sprites appearing in a graph can therefore be stored in files and retrieved if the graph file format supports attributes. If this is a dynamic graph format, like DGS, the whole sprite history is remembered: when it moved, when it changed, etc.

Second implementation node : often you will need to extend the sprite class. This is easily possible, but you must remember that you cannot create sprites yourself, you must use the SpriteManager. In order to create a sprite of a special kind, you can either use a SpriteFactory with the SpriteManager or the special SpriteManager.addSprite(String, Class) method of the SpriteManager. In both cases, the init(String, SpriteManager, Values) method of the sprite will be called. Override this method to initialise your sprite.

See Also:
SpriteManager, SpriteFactory

Method Summary
 void addAttribute(String attribute, Object... values)
          Add or replace the value of an attribute.
 void addAttributes(Map<String,Object> attributes)
          Add or replace each attribute found in attributes.
 boolean attached()
          True if attached to an edge or node.
 void attachToEdge(String id)
          Attach the sprite to an edge with the given identifier.
 void attachToNode(String id)
          Attach the sprite to a node with the given identifier.
 void changeAttribute(String attribute, Object... values)
          Like Element.addAttribute(String, Object...) but for consistency.
 void clearAttributes()
          Remove all registered attributes.
 void detach()
          Detach the sprite from the element it is attached to (if any).
 Object[] getArray(String key)
          Get the array of objects bound to key.
 Element getAttachment()
          The element the sprite is attached to or null if the sprite is not attached.
<T> T
getAttribute(String key)
          Get the attribute object bound to the given key.
<T> T
getAttribute(String key, Class<T> clazz)
          Get the attribute object bound to the given key if it is an instance of the given class.
 int getAttributeCount()
          Quite expensive operation !.
 Iterator<String> getAttributeKeyIterator()
          Iterator on all attributes keys.
 Iterable<String> getAttributeKeySet()
          An iterable view on the set of attributes keys usable with the for-each loop.
 Map<String,Object> getAttributeMap()
           
<T> T
getFirstAttributeOf(Class<T> clazz, String... keys)
          Like Element.getAttribute(String, Class), but returns the first existing attribute in a list of keys, instead of only one key.
<T> T
getFirstAttributeOf(String... keys)
          Like Element.getAttribute(String), but returns the first existing attribute in a list of keys, instead of only one key.
 HashMap<?,?> getHash(String key)
          Get the hash bound to key.
 String getId()
          Unique identifier of this element.
 int getIndex()
          The current index of this element
 CharSequence getLabel(String key)
          Get the label string bound to the given key key.
 double getNumber(String key)
          Get the number bound to key.
 StyleConstants.Units getUnits()
           
 ArrayList<? extends Number> getVector(String key)
          Get the vector of number bound to key.
 double getX()
          X position.
 double getY()
          Y position.
 double getZ()
          Z position.
 boolean hasArray(String key)
          Does this element store an array value for the given key?
 boolean hasAttribute(String key)
          Does this element store a value for the given attribute key?
 boolean hasAttribute(String key, Class<?> clazz)
          Does this element store a value for the given attribute key and this value is an instance of the given class?
 boolean hasHash(String key)
          Does this element store a hash value for the given key?
 boolean hasLabel(String key)
          Does this element store a label value for the given key?
 boolean hasNumber(String key)
          Does this element store a number for the given key?
 boolean hasVector(String key)
          Does this element store a vector value for the given key?
 void removeAttribute(String attribute)
          Remove an attribute.
 void setAttribute(String attribute, Object... values)
          Like Element.addAttribute(String, Object...) but for consistency.
 void setPosition(double percent)
           
 void setPosition(double x, double y, double z)
           
 void setPosition(StyleConstants.Units units, double x, double y, double z)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getAttachment

public Element getAttachment()
The element the sprite is attached to or null if the sprite is not attached.

Returns:
An element the sprite is attached to or null.

attached

public boolean attached()
True if attached to an edge or node.

Returns:
False if not attached.

getX

public double getX()
X position.

Returns:
The position in abscissa.

getY

public double getY()
Y position.

Returns:
The position in ordinate.

getZ

public double getZ()
Z position.

Returns:
The position in depth.

getUnits

public StyleConstants.Units getUnits()

attachToNode

public void attachToNode(String id)
Attach the sprite to a node with the given identifier. If needed the sprite is first detached. If the given node identifier does not exist, the sprite stays in detached state.

Parameters:
id - Identifier of the node to attach to.

attachToEdge

public void attachToEdge(String id)
Attach the sprite to an edge with the given identifier. If needed the sprite is first detached. If the given edge identifier does not exist, the sprite stays in detached state.

Parameters:
id - Identifier of the edge to attach to.

detach

public void detach()
Detach the sprite from the element it is attached to (if any).


setPosition

public void setPosition(double percent)

setPosition

public void setPosition(double x,
                        double y,
                        double z)

setPosition

public void setPosition(StyleConstants.Units units,
                        double x,
                        double y,
                        double z)

getId

public String getId()
Description copied from interface: Element
Unique identifier of this element.

Specified by:
getId in interface Element
Returns:
The identifier value.

getLabel

public CharSequence getLabel(String key)
Description copied from interface: Element
Get the label string bound to the given key key. Labels are special attributes whose value is a character sequence. If an attribute with the same name exists but is not a character sequence, null is returned.

Specified by:
getLabel in interface Element
Parameters:
key - The label to search.
Returns:
The label string value or null if not found.

getAttribute

public <T> T getAttribute(String key)
Description copied from interface: Element
Get the attribute object bound to the given key. The returned value may be null to indicate the attribute does not exists or is not supported.

Specified by:
getAttribute in interface Element
Parameters:
key - Name of the attribute to search.
Returns:
The object bound to the given key or null if no object match this attribute name.

getAttribute

public <T> T getAttribute(String key,
                          Class<T> clazz)
Description copied from interface: Element
Get the attribute object bound to the given key if it is an instance of the given class. Some The returned value maybe null to indicate the attribute does not exists or is not an instance of the given class.

Specified by:
getAttribute in interface Element
Parameters:
key - The attribute name to search.
clazz - The expected attribute class.
Returns:
The object bound to the given key or null if no object match this attribute.

getAttributeCount

public int getAttributeCount()
Quite expensive operation !.

Specified by:
getAttributeCount in interface Element
Returns:
the number of attributes.

getAttributeKeyIterator

public Iterator<String> getAttributeKeyIterator()
Description copied from interface: Element
Iterator on all attributes keys.

Specified by:
getAttributeKeyIterator in interface Element
Returns:
An iterator on the key set of attributes.

getAttributeKeySet

public Iterable<String> getAttributeKeySet()
Description copied from interface: Element
An iterable view on the set of attributes keys usable with the for-each loop.

Specified by:
getAttributeKeySet in interface Element
Returns:
an iterable view on each attribute key, null if there are no attributes.

getAttributeMap

public Map<String,Object> getAttributeMap()

getFirstAttributeOf

public <T> T getFirstAttributeOf(String... keys)
Description copied from interface: Element
Like Element.getAttribute(String), but returns the first existing attribute in a list of keys, instead of only one key. The key list order matters.

Specified by:
getFirstAttributeOf in interface Element
Parameters:
keys - Several strings naming attributes.
Returns:
The first attribute that exists.

getFirstAttributeOf

public <T> T getFirstAttributeOf(Class<T> clazz,
                                 String... keys)
Description copied from interface: Element
Like Element.getAttribute(String, Class), but returns the first existing attribute in a list of keys, instead of only one key. The key list order matters.

Specified by:
getFirstAttributeOf in interface Element
Parameters:
clazz - The class the attribute must be instance of.
keys - Several string naming attributes.
Returns:
The first attribute that exists.

getArray

public Object[] getArray(String key)
Description copied from interface: Element
Get the array of objects bound to key. Arrays of objects are special attributes whose value is a sequence of objects. If an attribute with the same name exists but is not an array, null is returned.

Specified by:
getArray in interface Element
Parameters:
key - The name of the array to search.
Returns:
The array of objects or null if not found.

getHash

public HashMap<?,?> getHash(String key)
Description copied from interface: Element
Get the hash bound to key. Hashes are special attributes whose value is a set of pairs (name,object). Instances of object implementing the CompoundAttribute interface are considered like hashes since they can be transformed to a hash. If an attribute with the same name exists but is not a hash, null is returned. We cannot enforce the type of the key. It is considered a string and you should use "Object.toString()" to get it.

Specified by:
getHash in interface Element
Parameters:
key - The name of the hash to search.
Returns:
The hash or null if not found.

getNumber

public double getNumber(String key)
Description copied from interface: Element
Get the number bound to key. Numbers are special attributes whose value is an instance of Number. If an attribute with the same name exists but is not a Number, NaN is returned.

Specified by:
getNumber in interface Element
Parameters:
key - The name of the number to search.
Returns:
The number value or NaN if not found.

getVector

public ArrayList<? extends Number> getVector(String key)
Description copied from interface: Element
Get the vector of number bound to key. Vectors of numbers are special attributes whose value is a sequence of numbers. If an attribute with the same name exists but is not a vector of number, null is returned.

Specified by:
getVector in interface Element
Parameters:
key - The name of the number to search.
Returns:
The vector of numbers or null if not found.

hasAttribute

public boolean hasAttribute(String key)
Description copied from interface: Element
Does this element store a value for the given attribute key?

Specified by:
hasAttribute in interface Element
Parameters:
key - The name of the attribute to search.
Returns:
True if a value is present for this attribute.

hasArray

public boolean hasArray(String key)
Description copied from interface: Element
Does this element store an array value for the given key? A vector is an attribute whose value is an array of objects.

Specified by:
hasArray in interface Element
Parameters:
key - The name of the array.
Returns:
True if a value is present for this attribute and can contain an array object.

hasAttribute

public boolean hasAttribute(String key,
                            Class<?> clazz)
Description copied from interface: Element
Does this element store a value for the given attribute key and this value is an instance of the given class?

Specified by:
hasAttribute in interface Element
Parameters:
key - The name of the attribute to search.
clazz - The expected class of the attribute value.
Returns:
True if a value is present for this attribute.

hasHash

public boolean hasHash(String key)
Description copied from interface: Element
Does this element store a hash value for the given key? A hash is a set of pairs (key,value) or objects that implement the CompoundAttribute class.

Specified by:
hasHash in interface Element
Parameters:
key - The name of the hash.
Returns:
True if a value is present for this attribute and can contain a hash.

hasLabel

public boolean hasLabel(String key)
Description copied from interface: Element
Does this element store a label value for the given key? A label is an attribute whose value is a string.

Specified by:
hasLabel in interface Element
Parameters:
key - The name of the label.
Returns:
True if a value is present for this attribute and implements CharSequence.

hasNumber

public boolean hasNumber(String key)
Description copied from interface: Element
Does this element store a number for the given key? A number is an attribute whose value is an instance of Number.

Specified by:
hasNumber in interface Element
Parameters:
key - The name of the number.
Returns:
True if a value is present for this attribute and can contain a double (inherits from Number).

hasVector

public boolean hasVector(String key)
Description copied from interface: Element
Does this element store a vector value for the given key? A vector is an attribute whose value is a sequence of numbers.

Specified by:
hasVector in interface Element
Parameters:
key - The name of the vector.
Returns:
True if a value is present for this attribute and can contain a sequence of numbers.

addAttribute

public void addAttribute(String attribute,
                         Object... values)
Description copied from interface: Element
Add or replace the value of an attribute. Existing attributes are overwritten silently. All classes inheriting from Number can be considered as numbers. All classes inheriting from CharSequence can be considered as labels. You can pass zero, one or more arguments for the attribute values. If no value is given, a boolean with value "true" is added. If there is more than one value, an array is stored. If there is only one value, the value is stored (but not in an array).

Specified by:
addAttribute in interface Element
Parameters:
attribute - The attribute name.
values - The attribute value or set of values.

addAttributes

public void addAttributes(Map<String,Object> attributes)
Description copied from interface: Element
Add or replace each attribute found in attributes. Existing attributes are overwritten silently. All classes inheriting from Number can be considered as numbers. All classes inheriting from CharSequence can be considered as labels.

Specified by:
addAttributes in interface Element
Parameters:
attributes - A set of (key,value) pairs.

setAttribute

public void setAttribute(String attribute,
                         Object... values)
Description copied from interface: Element
Like Element.addAttribute(String, Object...) but for consistency.

Specified by:
setAttribute in interface Element
Parameters:
attribute - The attribute name.
values - The attribute value or array of values.
See Also:
Element.addAttribute(String, Object...)

changeAttribute

public void changeAttribute(String attribute,
                            Object... values)
Description copied from interface: Element
Like Element.addAttribute(String, Object...) but for consistency.

Specified by:
changeAttribute in interface Element
Parameters:
attribute - The attribute name.
values - The attribute value or array of values.
See Also:
Element.addAttribute(String, Object...)

clearAttributes

public void clearAttributes()
Description copied from interface: Element
Remove all registered attributes. This includes numbers, labels and vectors.

Specified by:
clearAttributes in interface Element

removeAttribute

public void removeAttribute(String attribute)
Description copied from interface: Element
Remove an attribute. Non-existent attributes errors are ignored silently.

Specified by:
removeAttribute in interface Element
Parameters:
attribute - Name of the attribute to remove.

getIndex

public int getIndex()
Description copied from interface: Element
The current index of this element

Specified by:
getIndex in interface Element
Returns:
The index value


Copyright © 2013. All Rights Reserved.