diff java/src/uk/ac/qmul/eecs/ccmi/gui/Diagram.java @ 0:78b7fc5391a2

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