annotate java/src/uk/ac/qmul/eecs/ccmi/gui/Diagram.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
rev   line source
f@0 1 /*
f@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0 3
f@0 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 5
f@0 6 This program is free software: you can redistribute it and/or modify
f@0 7 it under the terms of the GNU General Public License as published by
f@0 8 the Free Software Foundation, either version 3 of the License, or
f@0 9 (at your option) any later version.
f@0 10
f@0 11 This program is distributed in the hope that it will be useful,
f@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 14 GNU General Public License for more details.
f@0 15
f@0 16 You should have received a copy of the GNU General Public License
f@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 18 */
f@0 19 package uk.ac.qmul.eecs.ccmi.gui;
f@0 20
f@0 21 import java.awt.geom.Point2D;
f@0 22 import java.util.Set;
f@0 23
f@0 24 import uk.ac.qmul.eecs.ccmi.diagrammodel.CollectionModel;
f@0 25 import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramElement;
f@0 26 import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramModel;
f@0 27 import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramTreeNode;
f@0 28 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties;
f@0 29 import uk.ac.qmul.eecs.ccmi.diagrammodel.TreeModel;
f@0 30 import uk.ac.qmul.eecs.ccmi.gui.persistence.PrototypePersistenceDelegate;
f@0 31 import uk.ac.qmul.eecs.ccmi.network.AwarenessMessage;
f@0 32 import uk.ac.qmul.eecs.ccmi.network.DiagramEventActionSource;
f@0 33
f@0 34 /**
f@0 35 * The {@code Diagram} class holds all the data needed for a representation of the diagram. It is used by component classes
f@0 36 * such as {@link GraphPanel} and {@link DiagramTree} to draw the diagram by accessing the diagram model or by
f@0 37 * {@link EditorTabbedPane} to assign a title to the tabs out of the diagram name.
f@0 38 *
f@0 39 */
f@0 40 public abstract class Diagram implements Cloneable {
f@0 41
f@0 42 /**
f@0 43 * Crates a new instance of a Diagram. The diagram created through this method is not shared with any peer via
f@0 44 * a server.
f@0 45 * @param name the name of the diagram.
f@0 46 * @param nodes an array of node prototypes. Nodes inserted by users in the diagram will be created by cloning these nodes.
f@0 47 * @param edges an array of edge prototypes. Edges inserted by users in the diagram will be created by cloning these edges.
f@0 48 * @param prototypePersistenceDelegate a delegate class to handle nodes and edges persistence.
f@0 49 * @return a new instance of {@code Diagram}
f@0 50 */
f@0 51 public static Diagram newInstance(String name, Node[] nodes, Edge[] edges, PrototypePersistenceDelegate prototypePersistenceDelegate){
f@0 52 return new LocalDiagram(name,nodes,edges,prototypePersistenceDelegate);
f@0 53 }
f@0 54
f@0 55 /**
f@0 56 * Returns the name of the diagram. The name identifies the diagram uniquely in the editor. There cannot
f@0 57 * be two diagrams with the same name open at the same time. This makes things easier when sharing diagrams
f@0 58 * with other users via the network.
f@0 59 *
f@0 60 * @return the name of the diagram
f@0 61 */
f@0 62 public abstract String getName();
f@0 63
f@0 64 /**
f@0 65 * Assign this diagram a new name.
f@0 66 * @param name the new name of the diagram
f@0 67 */
f@0 68 public abstract void setName(String name);
f@0 69
f@0 70 /**
f@0 71 * Returns an array with the node prototypes. Node prototypes are used when creating new node
f@0 72 * instances via the {@code clone()} method.
f@0 73 *
f@0 74 * @return an array of nodes
f@0 75 */
f@0 76 public abstract Node[] getNodePrototypes();
f@0 77
f@0 78 /**
f@0 79 * Returns an array with the edge prototypes. Edge prototypes are used when creating new edge
f@0 80 * instances via the {@code clone()} method.
f@0 81 *
f@0 82 * @return an array of edges
f@0 83 */
f@0 84 public abstract Edge[] getEdgePrototypes();
f@0 85
f@0 86 /**
f@0 87 * Returns the tree model of this diagram. Note that each diagram holds a {@code DiagramModel}
f@0 88 * which has two sub-models ({@code TreeModel} and {@code CollectionModel}). Changes on one
f@0 89 * sub-model will affect the other model as well.
f@0 90 *
f@0 91 * @return the tree model of this diagram
f@0 92 *
f@0 93 * @see uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramModel uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramModel
f@0 94 */
f@0 95 public abstract TreeModel<Node,Edge> getTreeModel();
f@0 96
f@0 97 /**
f@0 98 * Returns the collection model of this diagram. Note that each diagram holds a {@code DiagramModel}
f@0 99 * which has two sub-models ({@code TreeModel} and {@code CollectionModel}). Changes on one
f@0 100 * sub-model will affect the other model as well.
f@0 101 *
f@0 102 * @return the tree model of this diagram
f@0 103 *
f@0 104 * @see uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramModel uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramModel
f@0 105 */
f@0 106 public abstract CollectionModel<Node,Edge> getCollectionModel();
f@0 107
f@0 108 /**
f@0 109 * Returns the model updater of this diagram. The model updater is the delegate for all the
f@0 110 * update operations affecting the diagram model.
f@0 111 *
f@0 112 * @return the model updater for this diagram
f@0 113 */
f@0 114 public abstract DiagramModelUpdater getModelUpdater();
f@0 115
f@0 116 /**
f@0 117 * Returns the label of the diagram. The label is slightly different from the name as it's the string
f@0 118 * appearing in the tabbed pane of the editor. It includes asterisk character at the end when the {@code DiagramModel}
f@0 119 * of this class has been changed and not yet saved on hard disk.
f@0 120 *
f@0 121 * @return a label for this diagram
f@0 122 */
f@0 123 public abstract String getLabel();
f@0 124
f@0 125 /**
f@0 126 * Returns the delegates for this diagram for nodes and edges prototypes persistence.
f@0 127 * When saving a diagram to an xml file each node and edge of the prototypes is encoded
f@0 128 * in the xml file. Indeed the template of a diagram is made of of its prototypes.
f@0 129 * In the template is held the general attributes common to all the nodes and edges, like
f@0 130 * for instance the type of a node but not its current position.
f@0 131 *
f@0 132 * @return the PrototypePersistenceDelegate for this diagram
f@0 133 */
f@0 134 public abstract PrototypePersistenceDelegate getPrototypePersistenceDelegate();
f@0 135
f@0 136 @Override
f@0 137 public Object clone(){
f@0 138 try {
f@0 139 return super.clone();
f@0 140 } catch (CloneNotSupportedException e) {
f@0 141 throw new RuntimeException(e);
f@0 142 }
f@0 143 }
f@0 144
f@0 145 public static class LocalDiagram extends Diagram {
f@0 146
f@0 147 protected LocalDiagram(String name, Node[] nodes, Edge[] edges,PrototypePersistenceDelegate prototypePersistenceDelegate){
f@0 148 this.name = name;
f@0 149 this.nodes = nodes;
f@0 150 this.edges = edges;
f@0 151 this.prototypePersistenceDelegate = prototypePersistenceDelegate;
f@0 152 diagramModel = new DiagramModel<Node,Edge>(nodes,edges);
f@0 153 innerModelUpdater = new InnerModelUpdater();
f@0 154 }
f@0 155
f@0 156 @Override
f@0 157 public String getName(){
f@0 158 return name;
f@0 159 }
f@0 160
f@0 161 @Override
f@0 162 public void setName(String name){
f@0 163 this.name = name;
f@0 164 }
f@0 165
f@0 166 @Override
f@0 167 public Node[] getNodePrototypes(){
f@0 168 return nodes;
f@0 169 }
f@0 170
f@0 171 @Override
f@0 172 public Edge[] getEdgePrototypes(){
f@0 173 return edges;
f@0 174 }
f@0 175
f@0 176 @Override
f@0 177 public TreeModel<Node,Edge> getTreeModel(){
f@0 178 return diagramModel.getTreeModel();
f@0 179 }
f@0 180
f@0 181 @Override
f@0 182 public CollectionModel<Node,Edge> getCollectionModel(){
f@0 183 return diagramModel.getDiagramCollection();
f@0 184 }
f@0 185
f@0 186 @Override
f@0 187 public String getLabel(){
f@0 188 return name;
f@0 189 }
f@0 190
f@0 191 @Override
f@0 192 public DiagramModelUpdater getModelUpdater(){
f@0 193 return innerModelUpdater;
f@0 194 }
f@0 195
f@0 196 @Override
f@0 197 public String toString(){
f@0 198 return name;
f@0 199 }
f@0 200
f@0 201 @Override
f@0 202 public PrototypePersistenceDelegate getPrototypePersistenceDelegate(){
f@0 203 return prototypePersistenceDelegate;
f@0 204 }
f@0 205
f@0 206 /**
f@0 207 * Creates a new {@code Diagram} by clonation.
f@0 208 */
f@0 209 @Override
f@0 210 public Object clone(){
f@0 211 LocalDiagram clone = (LocalDiagram)super.clone();
f@0 212 clone.name = getName();
f@0 213 clone.nodes = getNodePrototypes();
f@0 214 clone.edges = getEdgePrototypes();
f@0 215 /* constructor with no args makes just a dummy wrapper */
f@0 216 clone.diagramModel = new DiagramModel<Node,Edge>(nodes,edges);
f@0 217 clone.innerModelUpdater = clone.new InnerModelUpdater();
f@0 218 return clone;
f@0 219 }
f@0 220
f@0 221 private DiagramModel<Node,Edge> diagramModel;
f@0 222 private InnerModelUpdater innerModelUpdater;
f@0 223 private PrototypePersistenceDelegate prototypePersistenceDelegate;
f@0 224 private String name;
f@0 225 private Node[] nodes;
f@0 226 private Edge[] edges;
f@0 227
f@0 228 private class InnerModelUpdater implements DiagramModelUpdater {
f@0 229
f@0 230 @Override
f@0 231 public boolean getLock(DiagramTreeNode treeNode, Lock lock, DiagramEventActionSource source) {
f@0 232 /* using a non shared diagram requires no actual lock, therefore the answer is always yes */
f@0 233 return true;
f@0 234 }
f@0 235
f@0 236 @Override
f@0 237 public void yieldLock(DiagramTreeNode treeNode, Lock lock, DiagramEventActionSource actionSource) {}
f@0 238
f@0 239 @Override
f@0 240 public void sendAwarenessMessage(AwarenessMessage.Name awMsgName, Object source){}
f@0 241
f@0 242 @Override
f@0 243 public void insertInCollection(DiagramElement element,DiagramEventSource source) {
f@0 244 if(element instanceof Node)
f@0 245 diagramModel.getDiagramCollection().insert((Node)element,source);
f@0 246 else
f@0 247 diagramModel.getDiagramCollection().insert((Edge)element,source);
f@0 248 }
f@0 249
f@0 250 @Override
f@0 251 public void insertInTree(DiagramElement element) {
f@0 252 if(element instanceof Node)
f@0 253 diagramModel.getTreeModel().insertTreeNode((Node)element,DiagramEventSource.TREE);
f@0 254 else
f@0 255 diagramModel.getTreeModel().insertTreeNode((Edge)element,DiagramEventSource.TREE);
f@0 256 }
f@0 257
f@0 258 @Override
f@0 259 public void takeOutFromCollection(DiagramElement element, DiagramEventSource source) {
f@0 260 diagramModel.getDiagramCollection().takeOut(element,source);
f@0 261 }
f@0 262
f@0 263 @Override
f@0 264 public void takeOutFromTree(DiagramElement element) {
f@0 265 diagramModel.getTreeModel().takeTreeNodeOut(element,DiagramEventSource.TREE);
f@0 266 }
f@0 267
f@0 268 @Override
f@0 269 public void setName(DiagramElement element, String name,DiagramEventSource source) {
f@0 270 element.setName(name,source);
f@0 271 }
f@0 272
f@0 273 @Override
f@0 274 public void setNotes(DiagramTreeNode treeNode, String notes,DiagramEventSource source) {
f@0 275 diagramModel.getTreeModel().setNotes(treeNode, notes,source);
f@0 276 }
f@0 277
f@0 278 @Override
f@0 279 public void setProperty(Node node, String type, int index,
f@0 280 String value,DiagramEventSource source) {
f@0 281 node.setProperty(type, index, value,source);
f@0 282 }
f@0 283
f@0 284 @Override
f@0 285 public void setProperties(Node node, NodeProperties properties,DiagramEventSource source) {
f@0 286 node.setProperties(properties,source);
f@0 287 }
f@0 288
f@0 289 @Override
f@0 290 public void clearProperties(Node node,DiagramEventSource source) {
f@0 291 node.clearProperties(source);
f@0 292 }
f@0 293
f@0 294 @Override
f@0 295 public void addProperty(Node node, String type, String value,DiagramEventSource source) {
f@0 296 node.addProperty(type, value,source);
f@0 297 }
f@0 298
f@0 299 @Override
f@0 300 public void removeProperty(Node node, String type, int index,DiagramEventSource source) {
f@0 301 node.removeProperty(type, index,source);
f@0 302 }
f@0 303
f@0 304 @Override
f@0 305 public void setModifiers(Node node, String type, int index,
f@0 306 Set<Integer> modifiers,DiagramEventSource source) {
f@0 307 node.setModifierIndexes(type, index, modifiers,source);
f@0 308 }
f@0 309
f@0 310 @Override
f@0 311 public void setEndLabel(Edge edge, Node node, String label,DiagramEventSource source) {
f@0 312 edge.setEndLabel(node, label,source);
f@0 313 }
f@0 314
f@0 315 @Override
f@0 316 public void setEndDescription(Edge edge, Node node,
f@0 317 int index,DiagramEventSource source) {
f@0 318 edge.setEndDescription(node, index,source);
f@0 319 }
f@0 320
f@0 321 @Override
f@0 322 public void translate(GraphElement ge, Point2D p, double x, double y,DiagramEventSource source) {
f@0 323 ge.translate(p, x, y,source);
f@0 324 }
f@0 325
f@0 326 @Override
f@0 327 public void startMove(GraphElement ge, Point2D p,DiagramEventSource source) {
f@0 328 ge.startMove(p,source);
f@0 329 }
f@0 330
f@0 331 @Override
f@0 332 public void bend(Edge edge, Point2D p,DiagramEventSource source) {
f@0 333 edge.bend(p,source);
f@0 334 }
f@0 335
f@0 336 @Override
f@0 337 public void stopMove(GraphElement ge,DiagramEventSource source) {
f@0 338 ge.stopMove(source);
f@0 339 }
f@0 340 }
f@0 341 }
f@0 342
f@0 343 }