annotate java/src/uk/ac/qmul/eecs/ccmi/diagrammodel/DiagramNode.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.util.List;
f@0 22 import java.util.Set;
f@0 23
f@0 24 import uk.ac.qmul.eecs.ccmi.diagrammodel.ElementChangedEvent.PropertyChangeArgs;
f@0 25 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties.Modifiers;
f@0 26
f@0 27 /**
f@0 28 * This class represents a node in the diagram.
f@0 29 *
f@0 30 */
f@0 31 @SuppressWarnings("serial")
f@0 32 public abstract class DiagramNode extends DiagramElement {
f@0 33 /**
f@0 34 * Constructor to be called by sub classes
f@0 35 *
f@0 36 * @param type the type of the new node. All nodes with this type will be
f@0 37 * put under the same tree node in the tree representation
f@0 38 * @param properties the properties of this node
f@0 39 */
f@0 40 public DiagramNode(String type, NodeProperties properties){
f@0 41 setType(type);
f@0 42 this.properties = properties;
f@0 43 }
f@0 44
f@0 45 /**
f@0 46 * Returns the properties of this node. Be aware that what is returned is the reference
f@0 47 * to the actual NodeProperties object inside this DiagramNode. Thereforemodifying the returned
f@0 48 * object will affect this node.
f@0 49 * @return the properties of this node
f@0 50 */
f@0 51 public NodeProperties getProperties(){
f@0 52 return properties;
f@0 53 }
f@0 54
f@0 55 /**
f@0 56 * Returns a copy of the properties of this node. The modifying the returned object
f@0 57 * won't affect this Node.
f@0 58 * @return Returns a copy of the properties of this node
f@0 59 */
f@0 60 public NodeProperties getPropertiesCopy(){
f@0 61 NodeProperties p = (NodeProperties)properties.clone();
f@0 62 for(String type : properties.getTypes()){
f@0 63 Modifiers modifiers = properties.getModifiers(type);
f@0 64 int index = 0;
f@0 65 for(String value : properties.getValues(type)){
f@0 66 if(properties.getModifiers(type).isNull())
f@0 67 p.addValue(type, value);
f@0 68 else
f@0 69 p.addValue(type, value, modifiers.getIndexes(index++));
f@0 70 }
f@0 71 }
f@0 72 return p;
f@0 73 }
f@0 74
f@0 75 /**
f@0 76 * Set the NodeProperties of this node
f@0 77 * @param properties the properties to set for this node
f@0 78 * @param source the source of the action that triggered this method
f@0 79 */
f@0 80 public void setProperties(NodeProperties properties, Object source){
f@0 81 this.properties = properties;
f@0 82 notifyChange(new ElementChangedEvent(this,this.properties,"properties",source));
f@0 83 }
f@0 84
f@0 85 /**
f@0 86 * Add a property to the NodeProperties of this node
f@0 87 * @see NodeProperties#addValue(String, String)
f@0 88 *
f@0 89 * @param propertyType the type of the property to add
f@0 90 * @param propertyValue the property to add
f@0 91 * @param source the source of the action that triggered this method
f@0 92 */
f@0 93 public void addProperty(String propertyType, String propertyValue, Object source){
f@0 94 getProperties().addValue(propertyType, propertyValue);
f@0 95 int index = getProperties().getValues(propertyType).size() - 1;
f@0 96 notifyChange(new ElementChangedEvent(this,new PropertyChangeArgs(propertyType,index,""),"property.add",source));
f@0 97 }
f@0 98
f@0 99 /**
f@0 100 * Removes a property from the NodeProperties of this node
f@0 101 * @see NodeProperties#removeValue(String, int)
f@0 102 *
f@0 103 * @param propertyType the type of the property to add
f@0 104 * @param valueIndex the index of the property to remove
f@0 105 * @param source the source of the action that triggered this method
f@0 106 */
f@0 107 public void removeProperty(String propertyType, int valueIndex, Object source){
f@0 108 String oldValue = getProperties().getValues(propertyType).get(valueIndex);
f@0 109 getProperties().removeValue(propertyType, valueIndex);
f@0 110 notifyChange(new ElementChangedEvent(this,new PropertyChangeArgs(propertyType,valueIndex,oldValue),"property.remove",source));
f@0 111 }
f@0 112
f@0 113 /**
f@0 114 * Set a property on the NodeProperties of this node to a new value
f@0 115 * @see NodeProperties#setValue(String, int, String)
f@0 116 *
f@0 117 * @param propertyType the type of the property to add
f@0 118 * @param valueIndex the index of the property to remove
f@0 119 * @param newValue the new value for this property
f@0 120 * @param source the source of the action that triggered this method
f@0 121 */
f@0 122 public void setProperty(String propertyType, int valueIndex, String newValue, Object source){
f@0 123 String oldValue = getProperties().getValues(propertyType).get(valueIndex);
f@0 124 getProperties().setValue(propertyType, valueIndex, newValue);
f@0 125 notifyChange(new ElementChangedEvent(this,new PropertyChangeArgs(propertyType,valueIndex,oldValue),"property.set",source));
f@0 126 }
f@0 127
f@0 128 /**
f@0 129 * Removes all the values in the NodeProperties of this node
f@0 130 * @see NodeProperties#clear()
f@0 131 *
f@0 132 * @param source the source of the action that triggered this method
f@0 133 */
f@0 134 public void clearProperties(Object source){
f@0 135 getProperties().clear();
f@0 136 notifyChange(new ElementChangedEvent(this,this,"properties.clear",source));
f@0 137 }
f@0 138
f@0 139 /**
f@0 140 * set the modifier indexes in the NodeProperties of this node
f@0 141 * @see Modifiers#setIndexes(int, Set)
f@0 142 *
f@0 143 * @param propertyType the type of the property to add
f@0 144 * @param propertyValueIndex the index of the property value whose modifiers
f@0 145 * are to be changed
f@0 146 * @param modifierIndexes the new modifiers (identified by their index )
f@0 147 * for this property value
f@0 148 * @param source the source of the action that triggered this method
f@0 149 */
f@0 150 public void setModifierIndexes(String propertyType, int propertyValueIndex, Set<Integer> modifierIndexes,Object source){
f@0 151 StringBuilder oldIndexes = new StringBuilder();
f@0 152 List<String> modifierTypes = getProperties().getModifiers(propertyType).getTypes();
f@0 153 Set<Integer> indexes = getProperties().getModifiers(propertyType).getIndexes(propertyValueIndex);
f@0 154 for(Integer I : indexes){
f@0 155 oldIndexes.append(modifierTypes.get(I)).append(' ');
f@0 156 }
f@0 157 getProperties().getModifiers(propertyType).setIndexes(propertyValueIndex, modifierIndexes);
f@0 158 notifyChange(new ElementChangedEvent(this,new PropertyChangeArgs(propertyType,propertyValueIndex,oldIndexes.toString()),"property.modifiers",source));
f@0 159 }
f@0 160
f@0 161 /**
f@0 162 * Returns a more detailed description of the node than {@link #spokenText()}.
f@0 163 * the description includes which how many properties the node has and
f@0 164 * how many edges are attached to it.
f@0 165 *
f@0 166 * @return a description of the node
f@0 167 */
f@0 168 @Override
f@0 169 public String detailedSpokenText(){
f@0 170 StringBuilder builder = new StringBuilder(getType());
f@0 171 builder.append(' ');
f@0 172 builder.append(getName());
f@0 173 builder.append('.').append(' ');
f@0 174 for(int i=0; i<getChildCount();i++){
f@0 175 DiagramTreeNode treeNode = (DiagramTreeNode) getChildAt(i);
f@0 176 if(treeNode.getChildCount() > 0){
f@0 177 builder.append(treeNode.getChildCount())
f@0 178 .append(' ')
f@0 179 .append(treeNode.getName())
f@0 180 .append(';')
f@0 181 .append(' ');
f@0 182 }
f@0 183 }
f@0 184 return builder.toString();
f@0 185 }
f@0 186
f@0 187 /**
f@0 188 * Returns the number of attached edges
f@0 189 * @return the number of attached edges
f@0 190 */
f@0 191 public abstract int getEdgesNum();
f@0 192
f@0 193 /**
f@0 194 * Returns the attached edge at the specified index
f@0 195 * @param index an index into edge's list
f@0 196 * @return the attached edge at the specified index
f@0 197 */
f@0 198 public abstract DiagramEdge getEdgeAt(int index);
f@0 199
f@0 200 /**
f@0 201 * add an edge to the attached edges list
f@0 202 * @param e the edge to be added
f@0 203 *
f@0 204 * @return {@code true} if internal collection of edges changed as a result of the call
f@0 205 */
f@0 206 public abstract boolean addEdge(DiagramEdge e);
f@0 207
f@0 208 /**
f@0 209 * Removes an edge from the attached edges list
f@0 210 * @param e the edge to be removed
f@0 211 *
f@0 212 * @return {@code true} if internal collection of edges changed as a result of the call
f@0 213 */
f@0 214 public abstract boolean removeEdge(DiagramEdge e);
f@0 215
f@0 216 /**
f@0 217 * Gets the node this node is within.
f@0 218 * @return the parent node, or null if the node
f@0 219 * has no parent
f@0 220 */
f@0 221 public abstract DiagramNode getExternalNode();
f@0 222
f@0 223 /**
f@0 224 * Sets the node this node is within.
f@0 225 * @param node the parent node, or null if the node
f@0 226 * has no parent
f@0 227 */
f@0 228 public abstract void setExternalNode(DiagramNode node);
f@0 229
f@0 230 /**
f@0 231 * Returns the number of internal nodes
f@0 232 * @return the number of internal nodes
f@0 233 */
f@0 234 public abstract int getInternalNodesNum();
f@0 235
f@0 236 /**
f@0 237 * Gets the internal node at the specified index
f@0 238 * @param index an index into internal nodes list
f@0 239 * @return the internal node at the specified index
f@0 240 */
f@0 241 public abstract DiagramNode getInternalNodeAt(int index);
f@0 242
f@0 243 /**
f@0 244 * Adds a node to the internal nodes list.
f@0 245 * @param node the internal node to add
f@0 246 */
f@0 247 public abstract void addInternalNode(DiagramNode node);
f@0 248
f@0 249 /**
f@0 250 * Removes a node from the internal nodes list.
f@0 251 * @param node the internal node to remove
f@0 252 */
f@0 253 public abstract void removeInternalNode(DiagramNode node);
f@0 254
f@0 255 @Override
f@0 256 public Object clone(){
f@0 257 DiagramNode clone = (DiagramNode)super.clone();
f@0 258 clone.properties = (NodeProperties)properties.clone();
f@0 259 return clone;
f@0 260 }
f@0 261
f@0 262 private NodeProperties properties;
f@0 263 }