Class StyleGroup

All Implemented Interfaces:
Iterable<Element>

public class StyleGroup
extends Style
implements Iterable<Element>
A group of graph elements that share the same style.

The purpose of a style group is to allow retrieving all elements with the same style easily. Most of the time, with graphic engines, pushing the graphic state (the style, colors, line width, textures, gradients) is a costly operation. Doing it once for several elements can speed up things a lot. This is the purpose of the style group.

The action of drawing elements in group (first push style, then draw all elements) are called bulk drawing. All elements that can be drawn at once this way are called bulk elements.

In a style group it is not always possible do draw elements in a such a "bulk" operation. If the style contains "dynamic values" for example, that is value that depend on the value of an attribute stored on the element, or if the element is modified by an event (clicked, selected), the element will not be drawn the same as others.

The style group provides iterators on each of these categories of elements :

  • elements() allows to browse all elements contained in the group without exception.
  • dynamicElements() allows to browse the subset of elements having a attribute that modify their style.
  • elementsEvents() allows to browse the subset of elements modified by an event.
  • bulkElements() allows to browse all remaining elements that have no dynamic attribute or event.
The calling the three last iterators would yield the same elements as calling the first one. When drawing you can optimise the drawing by first pushing the graphic state and then drawing at once all bulk elements. If the dynamic and event subsets are not empty you then must draw such elements modifying the graphic state for each one.

  • Field Details

  • Constructor Details

  • Method Details

    • getId

      public String getId()
      The group unique identifier.
      Returns:
      A style group identifier.
    • getType

      public Selector.Type getType()
      Type of graph element concerned by this style (node, edge, sprite, graph).
      Returns:
      The type of the style group elements.
    • hasDynamicElements

      public boolean hasDynamicElements()
      True if at least one of the style properties is dynamic (set according to an attribute of the element to draw). Such elements cannot therefore be drawn in a group operation, but one by one.
      Returns:
      True if one property is dynamic.
    • hasEventElements

      public boolean hasEventElements()
      If true this group contains some elements that are actually changed by an event. Such elements cannot therefore be drawn in a group operation, but one by one.
      Returns:
      True if the group contains some elements changed by an event.
    • elementHasEvents

      public boolean elementHasEvents​(Element element)
      True if the given element actually has active events.
      Parameters:
      element - The element to test.
      Returns:
      True if the element has actually active events.
    • elementIsDynamic

      public boolean elementIsDynamic​(Element element)
      True if the given element has dynamic style values provided by specific attributes.
      Parameters:
      element - The element to test.
      Returns:
      True if the element has actually specific style attributes.
    • getValue

      public Object getValue​(String property, String... events)
      Get the value of a given property. This is a redefinition of the method in Style to consider the fact a style group aggregates several style rules.
      Overrides:
      getValue in class Style
      Parameters:
      property - The style property the value is searched for.
    • isEmpty

      public boolean isEmpty()
      True if there are no elements in the group.
      Returns:
      True if the group is empty of elements.
    • contains

      public boolean contains​(String elementId)
      True if the group contains the element whose identifier is given.
      Parameters:
      elementId - The element to search.
      Returns:
      true if the element is in the group.
    • contains

      public boolean contains​(Element element)
      True if the group contains the element given.
      Parameters:
      element - The element to search.
      Returns:
      true if the element is in the group.
    • getElement

      public Element getElement​(String id)
      Return an element of the group, knowing its identifier.
      Parameters:
      id - The searched element identifier.
      Returns:
      The element corresponding to the identifier or null if not found.
    • getElementCount

      public int getElementCount()
      The number of elements of the group.
      Returns:
      The element count.
    • getElementIterator

      public Iterator<? extends Element> getElementIterator()
      Iterator on the set of graph elements of this group.
      Returns:
      The elements iterator.
    • elements

      public Iterable<? extends Element> elements()
      Iterable set of elements. This the complete set of elements contained in this group without regard to the fact they are modified by an event or are dynamic. If you plan to respect events or dynamic elements, you must check the elements are not modified by events using elementHasEvents(Element) and are not dynamic by using elementIsDynamic(Element) and then draw modified elements using elementsEvents() and dynamicElements(). But the easiest way of drawing is to use first bulkElements() for all non dynamic non event elements, then the dynamicElements() and elementsEvents() to draw all dynamic and event elements.
      Returns:
      All the elements in no particular order.
    • bulkElements

      public Iterable<? extends Element> bulkElements()
      Iterable set of elements that can be drawn in a bulk operation, that is the subset of all elements that are not dynamic or modified by an event.
      Returns:
      The iterable set of bulk elements.
    • elementsEvents

      public Iterable<StyleGroup.ElementEvents> elementsEvents()
      Subset of elements that are actually modified by one or more events. The StyleGroup.ElementEvents class contains the element and an array of events that can be pushed on the style group set.
      Returns:
      The subset of elements modified by one or more events.
    • dynamicElements

      public Iterable<Element> dynamicElements()
      Subset of elements that have dynamic style values and therefore must be rendered one by one, not in groups like others. Even though elements style can specify some dynamics, the elements must individually have attributes that specify the dynamic value. If the elements do not have these attributes they can be rendered in bulk operations.
      Returns:
      The subset of dynamic elements of the group.
    • iterator

      public Iterator<Element> iterator()
      Specified by:
      iterator in interface Iterable<Element>
    • getRenderer

      public GraphicElement.SwingElementRenderer getRenderer​(String id)
      The associated renderers.
      Returns:
      A renderer or null if not found.
    • getEventsFor

      public StyleGroup.ElementEvents getEventsFor​(Element element)
      Set of events for a given element or null if the element has not currently occurring events.
      Returns:
      A set of events or null if none occurring at that time.
    • isElementDynamic

      public boolean isElementDynamic​(Element element)
      Test if an element is pushed as dynamic.
    • addElement

      public void addElement​(Element element)
      Add a new graph element to the group.
      Parameters:
      element - The new graph element to add.
    • removeElement

      public Element removeElement​(Element element)
      Remove a graph element from the group.
      Parameters:
      element - The element to remove.
      Returns:
      The removed element, or null if the element was not found.
    • activateEventsFor

      public void activateEventsFor​(Element element)
      Before drawing an element that has events, use this method to activate the events, the style values will be modified accordingly. Events for this element must have been registered via pushEventFor(Element, String). After rendering the deactivateEvents() MUST be called.
      Parameters:
      element - The element to push events for.
    • deactivateEvents

      public void deactivateEvents()
      De-activate any events activated for an element. This method MUST be called if activateEventsFor(Element) has been called.
    • release

      public void release()
      Remove all graph elements of this group, and remove this group from the group list of each style rule.
    • setValue

      public void setValue​(String property, Object value)
      Redefinition of the Style to forbid changing the values.
      Overrides:
      setValue in class Style
    • addRenderer

      public void addRenderer​(String id, GraphicElement.SwingElementRenderer renderer)
      Add a renderer to this group.
      Parameters:
      id - The renderer identifier.
      renderer - The renderer.
    • removeRenderer

      public GraphicElement.SwingElementRenderer removeRenderer​(String id)
      Remove a renderer.
      Parameters:
      id - The renderer identifier.
      Returns:
      The removed renderer or null if not found.
    • toString

      public String toString()
      Overrides:
      toString in class Style
    • toString

      public String toString​(int level)
      Overrides:
      toString in class Style