fiore@0: /* fiore@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool fiore@0: fiore@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) fiore@0: fiore@0: This program is free software: you can redistribute it and/or modify fiore@0: it under the terms of the GNU General Public License as published by fiore@0: the Free Software Foundation, either version 3 of the License, or fiore@0: (at your option) any later version. fiore@0: fiore@0: This program is distributed in the hope that it will be useful, fiore@0: but WITHOUT ANY WARRANTY; without even the implied warranty of fiore@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fiore@0: GNU General Public License for more details. fiore@0: fiore@0: You should have received a copy of the GNU General Public License fiore@0: along with this program. If not, see . fiore@0: */ fiore@0: package uk.ac.qmul.eecs.ccmi.gui; fiore@0: fiore@0: import java.awt.geom.Point2D; fiore@0: import java.util.Set; fiore@0: fiore@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.CollectionModel; fiore@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramElement; fiore@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramModel; fiore@3: import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramTreeNode; fiore@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties; fiore@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.TreeModel; fiore@0: import uk.ac.qmul.eecs.ccmi.gui.persistence.PrototypePersistenceDelegate; fiore@3: import uk.ac.qmul.eecs.ccmi.network.AwarenessMessage; fiore@3: import uk.ac.qmul.eecs.ccmi.network.DiagramEventActionSource; fiore@0: fiore@0: /** fiore@3: * The {@code Diagram} class holds all the data needed for a representation of the diagram. It is used by component classes fiore@0: * such as {@link GraphPanel} and {@link DiagramTree} to draw the diagram by accessing the diagram model or by fiore@0: * {@link EditorTabbedPane} to assign a title to the tabs out of the diagram name. fiore@0: * fiore@0: */ fiore@0: public abstract class Diagram implements Cloneable { fiore@0: fiore@3: /** fiore@3: * Crates a new instance of a Diagram. The diagram created through this method is not shared with any peer via fiore@3: * a server. fiore@3: * @param name the name of the diagram. fiore@3: * @param nodes an array of node prototypes. Nodes inserted by users in the diagram will be created by cloning these nodes. fiore@3: * @param edges an array of edge prototypes. Edges inserted by users in the diagram will be created by cloning these edges. fiore@3: * @param prototypePersistenceDelegate a delegate class to handle nodes and edges persistence. fiore@3: * @return a new instance of {@code Diagram} fiore@3: */ fiore@0: public static Diagram newInstance(String name, Node[] nodes, Edge[] edges, PrototypePersistenceDelegate prototypePersistenceDelegate){ fiore@0: return new LocalDiagram(name,nodes,edges,prototypePersistenceDelegate); fiore@0: } fiore@0: fiore@3: /** fiore@3: * Returns the name of the diagram. The name identifies the diagram uniquely in the editor. There cannot fiore@3: * be two diagrams with the same name open at the same time. This makes things easier when sharing diagrams fiore@3: * with other users via the network. fiore@3: * fiore@3: * @return the name of the diagram fiore@3: */ fiore@0: public abstract String getName(); fiore@0: fiore@3: /** fiore@3: * Assign this diagram a new name. fiore@3: * @param name the new name of the diagram fiore@3: */ fiore@0: public abstract void setName(String name); fiore@0: fiore@3: /** fiore@3: * Returns an array with the node prototypes. Node prototypes are used when creating new node fiore@3: * instances via the {@code clone()} method. fiore@3: * fiore@3: * @return an array of nodes fiore@3: */ fiore@0: public abstract Node[] getNodePrototypes(); fiore@0: fiore@3: /** fiore@3: * Returns an array with the edge prototypes. Edge prototypes are used when creating new edge fiore@3: * instances via the {@code clone()} method. fiore@3: * fiore@3: * @return an array of edges fiore@3: */ fiore@0: public abstract Edge[] getEdgePrototypes(); fiore@0: fiore@3: /** fiore@3: * Returns the tree model of this diagram. Note that each diagram holds a {@code DiagramModel} fiore@3: * which has two sub-models ({@code TreeModel} and {@code CollectionModel}). Changes on one fiore@3: * sub-model will affect the other model as well. fiore@3: * fiore@3: * @return the tree model of this diagram fiore@3: * fiore@3: * @see uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramModel uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramModel fiore@3: */ fiore@0: public abstract TreeModel getTreeModel(); fiore@0: fiore@3: /** fiore@3: * Returns the collection model of this diagram. Note that each diagram holds a {@code DiagramModel} fiore@3: * which has two sub-models ({@code TreeModel} and {@code CollectionModel}). Changes on one fiore@3: * sub-model will affect the other model as well. fiore@3: * fiore@3: * @return the tree model of this diagram fiore@3: * fiore@3: * @see uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramModel uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramModel fiore@3: */ fiore@0: public abstract CollectionModel getCollectionModel(); fiore@0: fiore@3: /** fiore@3: * Returns the model updater of this diagram. The model updater is the delegate for all the fiore@3: * update operations affecting the diagram model. fiore@3: * fiore@3: * @return the model updater for this diagram fiore@3: */ fiore@0: public abstract DiagramModelUpdater getModelUpdater(); fiore@0: fiore@3: /** fiore@3: * Returns the label of the diagram. The label is slightly different from the name as it's the string fiore@3: * appearing in the tabbed pane of the editor. It includes asterisk character at the end when the {@code DiagramModel} fiore@3: * of this class has been changed and not yet saved on hard disk. fiore@3: * fiore@3: * @return a label for this diagram fiore@3: */ fiore@0: public abstract String getLabel(); fiore@0: fiore@3: /** fiore@3: * Returns the delegates for this diagram for nodes and edges prototypes persistence. fiore@3: * When saving a diagram to an xml file each node and edge of the prototypes is encoded fiore@3: * in the xml file. Indeed the template of a diagram is made of of its prototypes. fiore@3: * In the template is held the general attributes common to all the nodes and edges, like fiore@3: * for instance the type of a node but not its current position. fiore@3: * fiore@3: * @return the PrototypePersistenceDelegate for this diagram fiore@3: */ fiore@0: public abstract PrototypePersistenceDelegate getPrototypePersistenceDelegate(); fiore@0: fiore@0: @Override fiore@0: public Object clone(){ fiore@0: try { fiore@0: return super.clone(); fiore@0: } catch (CloneNotSupportedException e) { fiore@0: throw new RuntimeException(e); fiore@0: } fiore@0: } fiore@0: fiore@0: private static class LocalDiagram extends Diagram { fiore@0: fiore@0: private LocalDiagram(String name, Node[] nodes, Edge[] edges,PrototypePersistenceDelegate prototypePersistenceDelegate){ fiore@0: this.name = name; fiore@0: this.nodes = nodes; fiore@0: this.edges = edges; fiore@0: this.prototypePersistenceDelegate = prototypePersistenceDelegate; fiore@0: diagramModel = new DiagramModel(nodes,edges); fiore@0: innerModelUpdater = new InnerModelUpdater(); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public String getName(){ fiore@0: return name; fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void setName(String name){ fiore@0: this.name = name; fiore@0: } fiore@0: fiore@0: @Override fiore@0: public Node[] getNodePrototypes(){ fiore@0: return nodes; fiore@0: } fiore@0: fiore@0: @Override fiore@0: public Edge[] getEdgePrototypes(){ fiore@0: return edges; fiore@0: } fiore@0: fiore@0: @Override fiore@0: public TreeModel getTreeModel(){ fiore@0: return diagramModel.getTreeModel(); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public CollectionModel getCollectionModel(){ fiore@0: return diagramModel.getDiagramCollection(); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public String getLabel(){ fiore@0: return name; fiore@0: } fiore@0: fiore@0: @Override fiore@0: public DiagramModelUpdater getModelUpdater(){ fiore@0: return innerModelUpdater; fiore@0: } fiore@0: fiore@0: @Override fiore@0: public String toString(){ fiore@0: return name; fiore@0: } fiore@0: fiore@0: @Override fiore@0: public PrototypePersistenceDelegate getPrototypePersistenceDelegate(){ fiore@0: return prototypePersistenceDelegate; fiore@0: } fiore@0: fiore@3: /** fiore@3: * Creates a new {@code Diagram} by clonation. fiore@3: */ fiore@0: @Override fiore@0: public Object clone(){ fiore@0: LocalDiagram clone = (LocalDiagram)super.clone(); fiore@0: clone.name = getName(); fiore@0: clone.nodes = getNodePrototypes(); fiore@0: clone.edges = getEdgePrototypes(); fiore@0: /* constructor with no args makes just a dummy wrapper */ fiore@0: clone.diagramModel = new DiagramModel(nodes,edges); fiore@0: clone.innerModelUpdater = clone.new InnerModelUpdater(); fiore@0: return clone; fiore@0: } fiore@0: fiore@0: private DiagramModel diagramModel; fiore@0: private InnerModelUpdater innerModelUpdater; fiore@0: private PrototypePersistenceDelegate prototypePersistenceDelegate; fiore@0: private String name; fiore@0: private Node[] nodes; fiore@0: private Edge[] edges; fiore@0: fiore@0: private class InnerModelUpdater implements DiagramModelUpdater { fiore@0: fiore@0: @Override fiore@3: public boolean getLock(DiagramTreeNode treeNode, Lock lock, DiagramEventActionSource source) { fiore@0: /* using a non shared diagram requires no actual lock, therefore the answer is always yes */ fiore@0: return true; fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void yieldLock(DiagramTreeNode treeNode, Lock lock, DiagramEventActionSource actionSource) {} fiore@0: fiore@3: @Override fiore@3: public void sendAwarenessMessage(AwarenessMessage.Name awMsgName, Object source){} fiore@3: fiore@0: @Override fiore@3: public void insertInCollection(DiagramElement element,DiagramEventSource source) { fiore@0: if(element instanceof Node) fiore@3: diagramModel.getDiagramCollection().insert((Node)element,source); fiore@0: else fiore@3: diagramModel.getDiagramCollection().insert((Edge)element,source); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void insertInTree(DiagramElement element) { fiore@0: if(element instanceof Node) fiore@3: diagramModel.getTreeModel().insertTreeNode((Node)element,DiagramEventSource.TREE); fiore@0: else fiore@3: diagramModel.getTreeModel().insertTreeNode((Edge)element,DiagramEventSource.TREE); fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void takeOutFromCollection(DiagramElement element, DiagramEventSource source) { fiore@3: diagramModel.getDiagramCollection().takeOut(element,source); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void takeOutFromTree(DiagramElement element) { fiore@3: diagramModel.getTreeModel().takeTreeNodeOut(element,DiagramEventSource.TREE); fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void setName(DiagramElement element, String name,DiagramEventSource source) { fiore@3: element.setName(name,source); fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void setNotes(DiagramTreeNode treeNode, String notes,DiagramEventSource source) { fiore@3: diagramModel.getTreeModel().setNotes(treeNode, notes,source); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void setProperty(Node node, String type, int index, fiore@3: String value,DiagramEventSource source) { fiore@3: node.setProperty(type, index, value,source); fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void setProperties(Node node, NodeProperties properties,DiagramEventSource source) { fiore@3: node.setProperties(properties,source); fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void clearProperties(Node node,DiagramEventSource source) { fiore@3: node.clearProperties(source); fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void addProperty(Node node, String type, String value,DiagramEventSource source) { fiore@3: node.addProperty(type, value,source); fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void removeProperty(Node node, String type, int index,DiagramEventSource source) { fiore@3: node.removeProperty(type, index,source); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void setModifiers(Node node, String type, int index, fiore@3: Set modifiers,DiagramEventSource source) { fiore@3: node.setModifierIndexes(type, index, modifiers,source); fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void setEndLabel(Edge edge, Node node, String label,DiagramEventSource source) { fiore@3: edge.setEndLabel(node, label,source); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void setEndDescription(Edge edge, Node node, fiore@3: int index,DiagramEventSource source) { fiore@3: edge.setEndDescription(node, index,source); fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void translate(GraphElement ge, Point2D p, double x, double y,DiagramEventSource source) { fiore@3: ge.translate(p, x, y,source); fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void startMove(GraphElement ge, Point2D p,DiagramEventSource source) { fiore@3: ge.startMove(p,source); fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void bend(Edge edge, Point2D p,DiagramEventSource source) { fiore@3: edge.bend(p,source); fiore@0: } fiore@0: fiore@0: @Override fiore@3: public void stopMove(GraphElement ge,DiagramEventSource source) { fiore@3: ge.stopMove(source); fiore@0: } fiore@0: } fiore@0: } fiore@0: fiore@0: }