templates.diagram
Class TemplateDiagram

java.lang.Object
  extended by templates.diagram.TemplateDiagram
All Implemented Interfaces:
ides.api.model.fsa.FSASubscriber, TemplateModelSubscriber

public class TemplateDiagram
extends java.lang.Object
implements TemplateModelSubscriber, ides.api.model.fsa.FSASubscriber

The class describing and maintaining the graphical representation of a TemplateModel.

Author:
Lenko Grigorov

Field Summary
protected  java.util.Map<TemplateComponent,Entity> component2Entity
          The Entitys in the temlpate diagram, indexed by their corresponding TemplateComponents.
protected  java.util.Map<TemplateComponent,ides.api.model.fsa.FSAModel> component2FSA
          Keep track of the FSAModel associated with each TemplateComponent in the template diagram.
protected  java.util.Set<Connector> connectors
          Collection of all the Connectors in the template diagram.
static int DESIRED_DIAGRAM_INSET
          The inset of the diagram (how much empty space to leave from the border of the canvas).
protected  java.util.Set<Entity> entities
          Collection of all the Entitys in the template diagram.
protected  java.util.Map<ides.api.model.fsa.FSAModel,TemplateComponent> FSA2component
          The TemplateComponents in the template diagram, indexed by the FSAModels they contain.
protected  java.util.Map<TemplateLink,Connector> link2Connector
          The Connectors in the template diagram, indexed by the TemplateLinks they represent.
protected  TemplateModel model
          The TemplateModel represented by this template diagram.
protected  java.util.Collection<DiagramElement> selection
          Collection of the currently selected elements in the template diagram.
 
Constructor Summary
TemplateDiagram(TemplateModel m)
          Construct a template diagram for the given TemplateModel.
 
Method Summary
 void add(Connector c)
          Add a Connector to the template diagram.
 void add(Entity entity)
          Add an Entity to the template design.
 void addLink(Connector c, TemplateLink link)
          Add a given TemplateLink to a given Connector.
 void addSubscriber(TemplateDiagramSubscriber subscriber)
          Attaches the given subscriber to this template diagram.
 void clearSelection()
          Deselect all DiagramElements in the template diagram.
 void commitTranslation(java.util.Collection<DiagramElement> elements, java.awt.Point delta)
          Notifies listeners for changes to this template diagram that the given DiagramElements were translated, and adds the UndoableEdit for the translation to the undo stack.
 Connector createConnector(Entity left, Entity right)
          Create a Connector between the given Entitys and add it to the template diagram.
 Entity createEntity(java.awt.Point location)
          Create an Entity (and the underlying TemplateComponent) at the given location and add it to the template diagram.
protected  EntityLayout createLayout(TemplateComponent component)
          Create new layout information for the given TemplateComponent.
 TemplateLink createLink(Connector c, java.lang.String leftEvent, java.lang.String rightEvent)
          Create a new TemplateLink within a given Connector, linking the given events from the "left" and "right" TemplateComponents, correspondingly.
 void draw(java.awt.Graphics2D g2d)
          Render the template diagram in the given graphical context, disregarding the inconsistency setting.
 void draw(java.awt.Graphics2D g2d, boolean showInconsistency)
          Render the diagram element in the given graphical context, according to the choice of differentiating or not inconsistent diagram elements.
protected  void fireDiagramChanged(TemplateDiagramMessage message)
          Notifies all subscribers that there has been a change to an element of this template diagram.
protected  void fireDiagramSelectionChanged(TemplateDiagramMessage message)
          Notifies all subscribers that there has been a change to the set of selected elements in this template diagram.
protected  void flagModel(ides.api.model.fsa.FSAModel model)
          Flags the TemplateComponent associated with the given FSAModel.
 void fsaEventSetChanged(ides.api.model.fsa.FSAMessage arg0)
          Flags the TemplateComponent associated with the FSAModel (since the FSAModel has been modified).
 void fsaStructureChanged(ides.api.model.fsa.FSAMessage arg0)
          Flags the TemplateComponent associated with the FSAModel (since the FSAModel has been modified).
 java.util.Collection<Connector> getAdjacentConnectors(Entity entity)
          Retrieve a collection of all the Connectors linking the given Entity to other Entitys.
protected  DiagramElement getAnyElement()
          Returns an arbitrary element from the template diagram, if one exists.
 java.awt.Rectangle getBounds()
          Retrieve the bounds of the template diagram (the smallest rectangle which contains all elements in the template diagram).
 java.util.Collection<Entity> getChannels()
          Retrieve a collection of all the channel Entitys in the template diagram.
 Connector getConnector(Entity left, Entity right)
          Retrieve the Connector between the given Entitys.
 Connector getConnectorAt(java.awt.Point location)
          Retrieve the Connector which contains the given point.
 Connector getConnectorFor(TemplateLink link)
          Retrieve the Connector which contains the given TemplateLink.
 java.util.Collection<Connector> getConnectors()
          Retrieve a collection of all Connectors in the template diagram.
 TemplateDiagramSubscriber[] getDiagramSubscribers()
          Returns all current subscribers to this template diagram.
 java.util.Collection<Entity> getEntities()
          Retrieve a collection of all the Entitys in the template diagram.
 Entity getEntityAt(java.awt.Point location)
          Retrieve the Entity which contains the given point.
 Entity getEntityFor(TemplateComponent component)
          Retrieve the Entity which represents the given TemplateComponent.
 Entity getEntityWithFSA(ides.api.model.fsa.FSAModel fsa)
          Retrieve the Entity which represents a TemplateComponent associated with the given FSAModel.
 TemplateModel getModel()
          Retrieve the TemplateModel represented by this template diagram.
 java.util.Collection<Entity> getModules()
          Retrieve a collection of all the module Entitys in the template diagram.
 java.util.Collection<DiagramElement> getSelection()
          Retrieve the collection of currently selected DiagramElements.
 boolean hasEntitiesAt(java.awt.Rectangle area)
          Check if there are Entitys in the given area.
 void labelEntity(Entity entity, java.lang.String label)
          Label the given Entity with the given label.
protected  void recoverLayout()
          Retrieve the layout annotations from the underlying TemplateModel elements (under the Annotable.LAYOUT key) and build the corresponding diagram elements.
 void release()
          Unsubscribe this template diagram from the underlying TemplateModel and stop listening to notifications of changes to the TemplateModel.
 void remove(Connector c)
          Remove a Connector from the template diagram (and the associated TemplateLinks from the underlying TemplateModel).
 void remove(Entity entity)
          Remove an Entity from the template design (and the corresponding TemplateComponent from the underlying TemplateModel).
 void removeLink(Connector c, TemplateLink link)
          Remove a given TemplateLink from a given Connector.
 void removeSubscriber(TemplateDiagramSubscriber subscriber)
          Removes the given subscriber to this template diagram.
 void setEntityIcon(Entity entity, EntityIcon icon)
          Set the icon of the given Entity.
 void setSelection(java.util.Collection<DiagramElement> selection)
          Set which DiagramElements are selected.
 void templateModelStructureChanged(TemplateModelMessage message)
          Called when there has been a change to the structure of the TemplateModel to whose TemplateModelPublisher this listener subscribes.
 void translate(java.util.Collection<DiagramElement> elements, java.awt.Point delta)
          Translates the given diagram elements with the given displacement and updates the rest of the diagram elements to reflect the change.
 void translate(java.awt.Point delta)
          Translates all elements in the diagram with the given displacement.
protected  void updateEmptyConnectorList()
          Update the list of empty connectors in the template diagram (i.e., Connectors which do not contain any TemplateLinks), and add the information to the underlying TemplateModel as a layout annotation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DESIRED_DIAGRAM_INSET

public static final int DESIRED_DIAGRAM_INSET
The inset of the diagram (how much empty space to leave from the border of the canvas).

See Also:
Constant Field Values

entities

protected java.util.Set<Entity> entities
Collection of all the Entitys in the template diagram.


component2Entity

protected java.util.Map<TemplateComponent,Entity> component2Entity
The Entitys in the temlpate diagram, indexed by their corresponding TemplateComponents.


connectors

protected java.util.Set<Connector> connectors
Collection of all the Connectors in the template diagram.


link2Connector

protected java.util.Map<TemplateLink,Connector> link2Connector
The Connectors in the template diagram, indexed by the TemplateLinks they represent.


component2FSA

protected java.util.Map<TemplateComponent,ides.api.model.fsa.FSAModel> component2FSA
Keep track of the FSAModel associated with each TemplateComponent in the template diagram. This is necessary in order to be able to unsubscribe from an FSAModel if it gets replaced (and thus no longer accessible through the TemplateComponent).


FSA2component

protected java.util.Map<ides.api.model.fsa.FSAModel,TemplateComponent> FSA2component
The TemplateComponents in the template diagram, indexed by the FSAModels they contain.


model

protected TemplateModel model
The TemplateModel represented by this template diagram.


selection

protected java.util.Collection<DiagramElement> selection
Collection of the currently selected elements in the template diagram.

Constructor Detail

TemplateDiagram

public TemplateDiagram(TemplateModel m)
Construct a template diagram for the given TemplateModel.

Parameters:
m - the TemplateModel to be represented by the template diagram
Method Detail

addSubscriber

public void addSubscriber(TemplateDiagramSubscriber subscriber)
Attaches the given subscriber to this template diagram. The given subscriber will receive notifications of changes from this template diagram.

Parameters:
subscriber - the subscriber to add

removeSubscriber

public void removeSubscriber(TemplateDiagramSubscriber subscriber)
Removes the given subscriber to this template diagram. The given subscriber will no longer receive notifications of changes from this template diagram.

Parameters:
subscriber - the subscriber to remove

getDiagramSubscribers

public TemplateDiagramSubscriber[] getDiagramSubscribers()
Returns all current subscribers to this template diagram.

Returns:
all current subscribers to this template diagram

fireDiagramChanged

protected void fireDiagramChanged(TemplateDiagramMessage message)
Notifies all subscribers that there has been a change to an element of this template diagram. Also notifies the underlying TemplateModel that layout information has changed.

Parameters:
message - the description of the change

fireDiagramSelectionChanged

protected void fireDiagramSelectionChanged(TemplateDiagramMessage message)
Notifies all subscribers that there has been a change to the set of selected elements in this template diagram.

Parameters:
message - the description of the new set of selected elements

recoverLayout

protected void recoverLayout()
Retrieve the layout annotations from the underlying TemplateModel elements (under the Annotable.LAYOUT key) and build the corresponding diagram elements. In essence, computes how to render the underlying TemplateModel.


createLayout

protected EntityLayout createLayout(TemplateComponent component)
Create new layout information for the given TemplateComponent. Called internally when the layout annotation of the TemplateComponent is missing.

Parameters:
component - the TemplateComponent whose layout has to be generated
Returns:
the new layout information for the given TemplateComponent

templateModelStructureChanged

public void templateModelStructureChanged(TemplateModelMessage message)
Description copied from interface: TemplateModelSubscriber
Called when there has been a change to the structure of the TemplateModel to whose TemplateModelPublisher this listener subscribes.

Specified by:
templateModelStructureChanged in interface TemplateModelSubscriber
Parameters:
message - the description of the change

getModel

public TemplateModel getModel()
Retrieve the TemplateModel represented by this template diagram.

Returns:
the TemplateModel represented by this template diagram

release

public void release()
Unsubscribe this template diagram from the underlying TemplateModel and stop listening to notifications of changes to the TemplateModel.

This method should be called only just before disposing of this template diagram.


createEntity

public Entity createEntity(java.awt.Point location)
Create an Entity (and the underlying TemplateComponent) at the given location and add it to the template diagram. The new TemplateComponent will be of type module.

Parameters:
location - the location for the new Entity
Returns:
the newly created Entity

add

public void add(Entity entity)
Add an Entity to the template design.

Parameters:
entity - the Entity to add

remove

public void remove(Entity entity)
Remove an Entity from the template design (and the corresponding TemplateComponent from the underlying TemplateModel). All Connectors linked to the Entity (and the corresponding TemplateLinks) are removed as well. If the Entity is not a part of the template design, the method does nothing.

Parameters:
entity - the Entity to remove

labelEntity

public void labelEntity(Entity entity,
                        java.lang.String label)
Label the given Entity with the given label.

Parameters:
entity - the Entity to be labelled
label - the new label for the Entity

setEntityIcon

public void setEntityIcon(Entity entity,
                          EntityIcon icon)
Set the icon of the given Entity.

Parameters:
entity - the Entity whose icon has to be set
icon - the new icon for the Entity

getEntityAt

public Entity getEntityAt(java.awt.Point location)
Retrieve the Entity which contains the given point. If there is more than one Entity which contains the point, returns one of these Entitys arbitrarily. If there is no Entity which contains the point, returns null.

Parameters:
location - the point to be used to locate the Entity
Returns:
one of the Entitys which contain the point; null if no Entity contains the point

hasEntitiesAt

public boolean hasEntitiesAt(java.awt.Rectangle area)
Check if there are Entitys in the given area. An Entity is considered to be in the area if a part of it intersects the area.

Parameters:
area - the area to examine for Entitys
Returns:
true if a part of at least one Entity intersects the given area; false otherwise

getEntities

public java.util.Collection<Entity> getEntities()
Retrieve a collection of all the Entitys in the template diagram.

Returns:
a collection of all the Entitys in the template diagram

getModules

public java.util.Collection<Entity> getModules()
Retrieve a collection of all the module Entitys in the template diagram.

Returns:
a collection of all the module Entitys in the template diagram

getChannels

public java.util.Collection<Entity> getChannels()
Retrieve a collection of all the channel Entitys in the template diagram.

Returns:
a collection of all the channel Entitys in the template diagram

getAdjacentConnectors

public java.util.Collection<Connector> getAdjacentConnectors(Entity entity)
Retrieve a collection of all the Connectors linking the given Entity to other Entitys.

Parameters:
entity - the Entity whose Connectors should be retrieved
Returns:
a collection of all the Connectors linking the given Entity to other Entitys

getConnector

public Connector getConnector(Entity left,
                              Entity right)
Retrieve the Connector between the given Entitys. If there is more than one Connector between the given Entity s, return one of these Connectors arbitrarily (note that this situation can happen only in inconsistent template diagrams). If there is no Connector between the given Entitys, return null.

A connector is symmetric. "Left" and "right" are used only to distinguish between the linked entities.

Parameters:
left - the first Entity
right - the second Entity
Returns:
the Connector between the given Entitys; null if no such Connector exists

createConnector

public Connector createConnector(Entity left,
                                 Entity right)
Create a Connector between the given Entitys and add it to the template diagram. If a Connector between the given Entitys already exists, do nothing.

A connector is symmetric. "Left" and "right" are used only to distinguish between the linked entities.

Parameters:
left - the first Entity to be linked
right - the second Entity to be linked
Returns:
the newly created Connector, or the existing Connector if a Connector between the given Entitys already exists
Throws:
InconsistentModificationException - if one or both of the given Entitys are not part of the template diagram

add

public void add(Connector c)
Add a Connector to the template diagram.

Parameters:
c - the Connector to be added
Throws:
InconsistentModificationException - if one or both of the Entitys linked by the Connector are not part of the template diagram, or if there is already a Connector between the Entitys linked by the given Connector

remove

public void remove(Connector c)
Remove a Connector from the template diagram (and the associated TemplateLinks from the underlying TemplateModel). If the given Connector is not a part of the diagram, the method does nothing.

Parameters:
c - the Connector to remove

createLink

public TemplateLink createLink(Connector c,
                               java.lang.String leftEvent,
                               java.lang.String rightEvent)
Create a new TemplateLink within a given Connector, linking the given events from the "left" and "right" TemplateComponents, correspondingly. If the given Connector is not a part of the template diagram, the method does nothing and returns null.

A connector is symmetric. "Left" and "right" are used only to distinguish between the linked components.

Parameters:
c - the connector to include the new TemplateLink
leftEvent - the event (from the "left" TemplateComponent) to be linked
rightEvent - the event (from the "right" TemplateComponent) to be linked
Returns:
the newly created TemplateLink, or null if the given connector is not a part of the template diagram

addLink

public void addLink(Connector c,
                    TemplateLink link)
Add a given TemplateLink to a given Connector. If the given Connector is not a part of the template diagram, the method does nothing.

Parameters:
c - the Connector to which to add the TemplateLink
link - the TemplateLink to be added to the Connector
Throws:
InconsistentModificationException - if the TemplateLink to be added does not link the same TemplateComponents linked by the Connector

removeLink

public void removeLink(Connector c,
                       TemplateLink link)
Remove a given TemplateLink from a given Connector. If the given Connector is not a part of the template diagram, or if the TemplateLink is not contained in the Connector, the method does nothing.

Parameters:
c - the Connector from which to remove the TemplateLink
link - the TemplateLink to remove from the Connector

getConnectors

public java.util.Collection<Connector> getConnectors()
Retrieve a collection of all Connectors in the template diagram.

Returns:
a collection of all Connectors in the template diagram

getConnectorAt

public Connector getConnectorAt(java.awt.Point location)
Retrieve the Connector which contains the given point. If there is more than one Connector which contains the point, returns one of these Connectors arbitrarily. If there is no Connector which contains the point, returns null.

Parameters:
location - the point to be used to locate the Connector
Returns:
one of the Connectors which contain the point; null if no Connector contains the point

getBounds

public java.awt.Rectangle getBounds()
Retrieve the bounds of the template diagram (the smallest rectangle which contains all elements in the template diagram). If the template diagram is empty, returns a rectangle at position (0,0) and dimensions of 0.

Returns:
the bounds of the template diagram (the smallest rectangle which contains all elements in the template diagram), or a rectangle at position (0,0) and dimensions of 0 if the template diagram is empty

getAnyElement

protected DiagramElement getAnyElement()
Returns an arbitrary element from the template diagram, if one exists. If the template diagram is empty, returns null.

Returns:
an arbitrary element from the template diagram, or null if the template diagram is empty

translate

public void translate(java.awt.Point delta)
Translates all elements in the diagram with the given displacement.

Parameters:
delta - the displacement to be used for the translation

translate

public void translate(java.util.Collection<DiagramElement> elements,
                      java.awt.Point delta)
Translates the given diagram elements with the given displacement and updates the rest of the diagram elements to reflect the change.

Parameters:
elements - the diagram elements to be translated
delta - the displacement to be used for the translation

commitTranslation

public void commitTranslation(java.util.Collection<DiagramElement> elements,
                              java.awt.Point delta)
Notifies listeners for changes to this template diagram that the given DiagramElements were translated, and adds the UndoableEdit for the translation to the undo stack.

This method is to be called only if diagram elements are translated without invoking the translate(Point) and translate(Collection, Point) methods (e.g., when dragging elements with the mouse), once after all related translations are complete (e.g., at the end of mouse dragging).

Parameters:
elements - the translated DiagramElements
delta - the displacement of the translation

draw

public void draw(java.awt.Graphics2D g2d)
Render the template diagram in the given graphical context, disregarding the inconsistency setting. I.e., inconsistent diagram elements should be rendered as if they were consistent.

Parameters:
g2d - the graphical context where the diagram element has to be rendered

draw

public void draw(java.awt.Graphics2D g2d,
                 boolean showInconsistency)
Render the diagram element in the given graphical context, according to the choice of differentiating or not inconsistent diagram elements.

Parameters:
g2d - the graphical context where the diagram element has to be rendered
showInconsistency - choice for rendering inconsistent diagram elements differently or not; if true, inconsistent diagram elements have to be rendered differently; iffalse, inconsistent diagram elements should be rendered as if they were consistent

setSelection

public void setSelection(java.util.Collection<DiagramElement> selection)
Set which DiagramElements are selected.

Parameters:
selection - the new collection of selected DiagramElements (can be empty)

getSelection

public java.util.Collection<DiagramElement> getSelection()
Retrieve the collection of currently selected DiagramElements.

Returns:
the collection of currently selected DiagramElements

clearSelection

public void clearSelection()
Deselect all DiagramElements in the template diagram.


getConnectorFor

public Connector getConnectorFor(TemplateLink link)
Retrieve the Connector which contains the given TemplateLink.

Parameters:
link - the TemplateLink whose Connector is to be retrieved
Returns:
the Connector which contains the given TemplateLink, or null if no Connector in the template diagram contains the given TemplateLink

getEntityFor

public Entity getEntityFor(TemplateComponent component)
Retrieve the Entity which represents the given TemplateComponent.

Parameters:
component - the TemplateComponent whose Entity has to be retrieved
Returns:
the Entity which represents the given TemplateComponent, or null if no Entity in the template diagram represents the given TemplateComponent

getEntityWithFSA

public Entity getEntityWithFSA(ides.api.model.fsa.FSAModel fsa)
Retrieve the Entity which represents a TemplateComponent associated with the given FSAModel.

Parameters:
fsa - the FSAModel to be used to retrieve the Entity
Returns:
the Entity which represents a TemplateComponent associated with the given FSAModel, or null if no Entity in the template diagram represents a TemplateComponent which is associated with the given FSAModel

updateEmptyConnectorList

protected void updateEmptyConnectorList()
Update the list of empty connectors in the template diagram (i.e., Connectors which do not contain any TemplateLinks), and add the information to the underlying TemplateModel as a layout annotation. Such an additional annotation is necessary as no TemplateLinks will carry the information about the empty connectors.


fsaEventSetChanged

public void fsaEventSetChanged(ides.api.model.fsa.FSAMessage arg0)
Flags the TemplateComponent associated with the FSAModel (since the FSAModel has been modified).

Specified by:
fsaEventSetChanged in interface ides.api.model.fsa.FSASubscriber

fsaStructureChanged

public void fsaStructureChanged(ides.api.model.fsa.FSAMessage arg0)
Flags the TemplateComponent associated with the FSAModel (since the FSAModel has been modified).

Specified by:
fsaStructureChanged in interface ides.api.model.fsa.FSASubscriber

flagModel

protected void flagModel(ides.api.model.fsa.FSAModel model)
Flags the TemplateComponent associated with the given FSAModel. If no TemplateComponent is associated with the given FSAModel, the method does nothing.