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