annotate java/src/uk/ac/qmul/eecs/ccmi/diagrammodel/DiagramElement.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.diagrammodel;
f@0 20
f@0 21 import java.io.InputStream;
f@0 22 import java.util.concurrent.locks.ReentrantLock;
f@0 23
f@0 24
f@0 25 /**
f@0 26 * A Diagram Element is either a node or an edge of the diagram. It's an abstract
f@0 27 * class which is extended by DiagramEdge and DiagramNode.
f@0 28 *
f@0 29 */
f@0 30 @SuppressWarnings("serial")
f@0 31 public abstract class DiagramElement extends DiagramTreeNode implements Cloneable{
f@0 32
f@0 33 protected DiagramElement(){
f@0 34 name = "";
f@0 35 id = NO_ID;
f@0 36 notifier = DUMMY_NOTIFIER; // initially set to no effect notifier
f@0 37 }
f@0 38
f@0 39 /**
f@0 40 * Returns the type of this diagram element. The type is like the category this element belongs to.
f@0 41 * For instance in a public transport diagram one might have three types of diagram element: tube, train
f@0 42 * and busses.
f@0 43 *
f@0 44 * @return the type of this element
f@0 45 */
f@0 46 public String getType(){
f@0 47 return type;
f@0 48 }
f@0 49
f@0 50 /**
f@0 51 * Set the type of this diagram element. This method should be called as soon as the object is created
f@0 52 * and should not be called anymore on this object.
f@0 53 *
f@0 54 * @param type the type of this element
f@0 55 */
f@0 56 protected void setType(String type){
f@0 57 this.type = type;
f@0 58 }
f@0 59
f@0 60 /**
f@0 61 * Notifies the model of a changed that has happened on this element. If this element is not
f@0 62 * held by any model than this method will have no effect.
f@0 63 * @param evt an event representing the fact that the element is changed
f@0 64 */
f@0 65 protected void notifyChange(ElementChangedEvent evt){
f@0 66 notifier.notifyChange(evt);
f@0 67 }
f@0 68
f@0 69 /**
f@0 70 * returns the tree node name "as it is", without any decoration such as notes, bookmarks or cardinality.
f@0 71 * Unlike the String returned by toString
f@0 72 * @return the tree node name
f@0 73 */
f@0 74 public String getName(){
f@0 75 if(name.isEmpty() && id != NO_ID)
f@0 76 return "new " + getType() + " " + id;
f@0 77 return name;
f@0 78 }
f@0 79
f@0 80 /**
f@0 81 * Sets the name of this element instance.
f@0 82 *
f@0 83 * @param s the string to set as the name
f@0 84 * @param source the source of this action
f@0 85 */
f@0 86 public void setName(String s, Object source){
f@0 87 String name = s;
f@0 88 /* if the user enters an empty string we go back to the default name */
f@0 89 if(s.isEmpty() && id != NO_ID){
f@0 90 name = "new " + getType() + " " + id;
f@0 91 }
f@0 92 setUserObject(name);
f@0 93 this.name = name;
f@0 94 notifyChange(new ElementChangedEvent(this,this,"name",source));
f@0 95 }
f@0 96
f@0 97 /**
f@0 98 * Returns an InputStream to a sound file with the sound of this element
f@0 99 * @return an InputStream
f@0 100 */
f@0 101 public abstract InputStream getSound();
f@0 102
f@0 103 /**
f@0 104 * Sets the if for this element. The id is a number which uniquely identifies this instance
f@0 105 * within a DiagramModel.
f@0 106 * Unlike the name, which can be the same for two different instances.
f@0 107 * @param id a long number which must be greater than 0
f@0 108 * @throws IllegalArgumentException id the id passe as argument is lower or equal to 0.
f@0 109 */
f@0 110 public void setId(long id){
f@0 111 if (id < NO_ID)
f@0 112 throw new IllegalArgumentException();
f@0 113 else
f@0 114 this.id = id;
f@0 115 if(name.isEmpty() && id != NO_ID){
f@0 116 String s = "new " + getType() + " " + id;
f@0 117 this.name = s;
f@0 118 setUserObject(s);
f@0 119 }
f@0 120 }
f@0 121
f@0 122 /**
f@0 123 * Returns the id of this instance of DiagramElement.
f@0 124 * @return a long representing the id of this instance of the element
f@0 125 * or NO_ID if it hasn't got one.
f@0 126 */
f@0 127 public long getId(){
f@0 128 return id;
f@0 129 }
f@0 130
f@0 131 public ReentrantLock getMonitor(){
f@0 132 if(notifier == null)
f@0 133 return null;
f@0 134 return (ReentrantLock)notifier;
f@0 135 }
f@0 136
f@0 137 /**
f@0 138 * Sets the notifier to be used for notification
f@0 139 * following an internal change of the node
f@0 140 * @param notifier the notifier call the notify method(s) on
f@0 141 */
f@0 142 <N extends DiagramNode,E extends DiagramEdge> void setNotifier(DiagramModel<N,E>.ReentrantLockNotifier notifier){
f@0 143 this.notifier = notifier;
f@0 144 }
f@0 145
f@0 146 @Override
f@0 147 public Object clone(){
f@0 148 DiagramElement clone = (DiagramElement)super.clone();
f@0 149 clone.name = "";
f@0 150 clone.id = NO_ID;
f@0 151 return clone;
f@0 152 }
f@0 153
f@0 154 /**
f@0 155 * Returns a description of the DiagramElement passed as argument, suitable
f@0 156 * for logging purposes.
f@0 157 *
f@0 158 * @param de the diagram element to log stuff about
f@0 159 * @return a log entry describing the element passed as agument
f@0 160 */
f@0 161 public static String toLogString(DiagramElement de){
f@0 162 StringBuilder builder = new StringBuilder(de.getName());
f@0 163 builder.append('(');
f@0 164 if(de.getId() == DiagramElement.NO_ID)
f@0 165 builder.append("no id");
f@0 166 else
f@0 167 builder.append(de.getId());
f@0 168 builder.append(')');
f@0 169 return builder.toString();
f@0 170 }
f@0 171
f@0 172 private long id = NO_ID;
f@0 173 private ElementNotifier notifier;
f@0 174 private String type;
f@0 175 private String name;
f@0 176 private static final ElementNotifier DUMMY_NOTIFIER = new ElementNotifier(){
f@0 177 @Override
f@0 178 public void notifyChange(ElementChangedEvent evt) {}
f@0 179 };
f@0 180 /**
f@0 181 * The value returned by getId() if the element instance has not been assigned any id
f@0 182 */
f@0 183 public static final long NO_ID = 0;
f@0 184 }