annotate java/src/uk/ac/qmul/eecs/ccmi/diagrammodel/DiagramElement.java @ 0:9418ab7b7f3f

Initial import
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Fri, 16 Dec 2011 17:35:51 +0000
parents
children 9e67171477bc
rev   line source
fiore@0 1 /*
fiore@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
fiore@0 3
fiore@0 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
fiore@0 5
fiore@0 6 This program is free software: you can redistribute it and/or modify
fiore@0 7 it under the terms of the GNU General Public License as published by
fiore@0 8 the Free Software Foundation, either version 3 of the License, or
fiore@0 9 (at your option) any later version.
fiore@0 10
fiore@0 11 This program is distributed in the hope that it will be useful,
fiore@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
fiore@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fiore@0 14 GNU General Public License for more details.
fiore@0 15
fiore@0 16 You should have received a copy of the GNU General Public License
fiore@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
fiore@0 18 */
fiore@0 19 package uk.ac.qmul.eecs.ccmi.diagrammodel;
fiore@0 20
fiore@0 21 import java.io.InputStream;
fiore@0 22
fiore@0 23 /**
fiore@0 24 * A Diagram Element is either a node or an edge of the diagram. It's an abstract
fiore@0 25 * class which is extended by DiagramEdge and DiagramNode.
fiore@0 26 *
fiore@0 27 */
fiore@0 28 @SuppressWarnings("serial")
fiore@0 29 public abstract class DiagramElement extends DiagramModelTreeNode implements Cloneable{
fiore@0 30
fiore@0 31 protected DiagramElement(){
fiore@0 32 name = "";
fiore@0 33 id = NO_ID;
fiore@0 34 notifier = DUMMY_NOTIFIER; // initially set to no effect notifier
fiore@0 35 }
fiore@0 36
fiore@0 37 /**
fiore@0 38 * Returns the type of this diagram element. The type is the same for all the elements
fiore@0 39 * which fall under the same type, whereas a different name can be assigned to each
fiore@0 40 * instance of such elements.
fiore@0 41 * @return the type of this element
fiore@0 42 */
fiore@0 43 public String getType(){
fiore@0 44 return type;
fiore@0 45 }
fiore@0 46
fiore@0 47 /**
fiore@0 48 * Set the type of this diagram element. This method should be called as soon as the object is created
fiore@0 49 * and should not be called anymore on this object.
fiore@0 50 * @param type the type of this element
fiore@0 51 */
fiore@0 52 protected void setType(String type){
fiore@0 53 this.type = type;
fiore@0 54 }
fiore@0 55
fiore@0 56 /**
fiore@0 57 * Notifies the model of a changed that has happened on this element. If this element is not
fiore@0 58 * held by any model than this method will have no effect.
fiore@0 59 * @param evt an event representing the fact that the element is changed
fiore@0 60 */
fiore@0 61 protected void notifyChange(ElementChangedEvent evt){
fiore@0 62 notifier.notifyChange(evt);
fiore@0 63 }
fiore@0 64
fiore@0 65 /**
fiore@0 66 * returns the tree node name "as it is", without any decoration such as notes, bookmarks or cardinality.
fiore@0 67 * Unlike the String returned by toString
fiore@0 68 * @return the tree node name
fiore@0 69 */
fiore@0 70 public String getName(){
fiore@0 71 if(name.isEmpty() && id != NO_ID)
fiore@0 72 return "new " + getType() + " " + id;
fiore@0 73 return name;
fiore@0 74 }
fiore@0 75
fiore@0 76 /**
fiore@0 77 * Sets the name of this element instance.
fiore@0 78 *
fiore@0 79 * @param the string to set as the name
fiore@0 80 */
fiore@0 81 public void setName(String s){
fiore@0 82 String name = s;
fiore@0 83 /* if the user enters an empty string we go back to the default name */
fiore@0 84 if(s.isEmpty() && id != NO_ID){
fiore@0 85 name = "new " + getType() + " " + id;
fiore@0 86 }
fiore@0 87 setUserObject(name);
fiore@0 88 this.name = name;
fiore@0 89 notifyChange(new ElementChangedEvent(this,this,"name"));
fiore@0 90 }
fiore@0 91
fiore@0 92 /**
fiore@0 93 * Returns an InputStream to a sound file with the sound of this element
fiore@0 94 * @return an InputStream
fiore@0 95 */
fiore@0 96 public abstract InputStream getSound();
fiore@0 97
fiore@0 98 /**
fiore@0 99 * Sets the if for this element. The id is a number which uniquely identifies this instance
fiore@0 100 * within a DiagramModel.
fiore@0 101 * Unlike the name, which can be the same for two different instances.
fiore@0 102 * @param id a long number which must be greater than 0
fiore@0 103 * @throws IllegalArgumentException id the id passe as argument is lower or equal to 0.
fiore@0 104 */
fiore@0 105 public void setId(long id){
fiore@0 106 if (id < NO_ID)
fiore@0 107 throw new IllegalArgumentException();
fiore@0 108 else
fiore@0 109 this.id = id;
fiore@0 110 if(name.isEmpty() && id != NO_ID){
fiore@0 111 String s = "new " + getType() + " " + id;
fiore@0 112 this.name = s;
fiore@0 113 setUserObject(s);
fiore@0 114 }
fiore@0 115 }
fiore@0 116
fiore@0 117 /**
fiore@0 118 * Returns the id of this instance of DiagramElement.
fiore@0 119 * @return a long representing the id of this instance of the element
fiore@0 120 * or NO_ID if it hasn't got one.
fiore@0 121 */
fiore@0 122 public long getId(){
fiore@0 123 return id;
fiore@0 124 }
fiore@0 125
fiore@0 126 /**
fiore@0 127 * Sets the notifier to be used for notification
fiore@0 128 * following an internal change of the node
fiore@0 129 * @param notifier the notifier call the notify method(s) on
fiore@0 130 */
fiore@0 131 public void setNotifier(ElementNotifier notifier){
fiore@0 132 this.notifier = notifier;
fiore@0 133 }
fiore@0 134
fiore@0 135 @Override
fiore@0 136 public Object clone(){
fiore@0 137 DiagramElement clone = (DiagramElement)super.clone();
fiore@0 138 clone.name = "";
fiore@0 139 clone.id = NO_ID;
fiore@0 140 return clone;
fiore@0 141 }
fiore@0 142
fiore@0 143 /**
fiore@0 144 * Returns a description of the DiagramElement passed as argument, suitable
fiore@0 145 * for logging purposes.
fiore@0 146 *
fiore@0 147 * @param de the diagram element to log stuff about
fiore@0 148 * @return a log entry describing the element passed as agument
fiore@0 149 */
fiore@0 150 public static String toLogString(DiagramElement de){
fiore@0 151 StringBuilder builder = new StringBuilder(de.getName());
fiore@0 152 builder.append('(');
fiore@0 153 if(de.getId() == DiagramElement.NO_ID)
fiore@0 154 builder.append("no id");
fiore@0 155 else
fiore@0 156 builder.append(de.getId());
fiore@0 157 builder.append(')');
fiore@0 158 return builder.toString();
fiore@0 159 }
fiore@0 160
fiore@0 161 private long id = NO_ID;
fiore@0 162 private ElementNotifier notifier;
fiore@0 163 private String type;
fiore@0 164 private String name;
fiore@0 165 private static final ElementNotifier DUMMY_NOTIFIER = new ElementNotifier(){
fiore@0 166 @Override
fiore@0 167 public void notifyChange(ElementChangedEvent evt) {}
fiore@0 168 };
fiore@0 169 /**
fiore@0 170 * The value returned by getId() if the element instance has not been assigned any id
fiore@0 171 */
fiore@0 172 public static final long NO_ID = 0;
fiore@0 173 }