annotate java/src/uk/ac/qmul/eecs/ccmi/gui/Node.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) 2002 Cay S. Horstmann (http://horstmann.com)
f@0 5 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 6
f@0 7 This program is free software: you can redistribute it and/or modify
f@0 8 it under the terms of the GNU General Public License as published by
f@0 9 the Free Software Foundation, either version 3 of the License, or
f@0 10 (at your option) any later version.
f@0 11
f@0 12 This program is distributed in the hope that it will be useful,
f@0 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 15 GNU General Public License for more details.
f@0 16
f@0 17 You should have received a copy of the GNU General Public License
f@0 18 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 19 */
f@0 20 package uk.ac.qmul.eecs.ccmi.gui;
f@0 21
f@0 22 import java.awt.Color;
f@0 23 import java.awt.Graphics2D;
f@0 24 import java.awt.Shape;
f@0 25 import java.awt.geom.Point2D;
f@0 26 import java.awt.geom.Rectangle2D;
f@0 27 import java.io.IOException;
f@0 28 import java.util.ArrayList;
f@0 29 import java.util.LinkedHashSet;
f@0 30 import java.util.List;
f@0 31 import java.util.Set;
f@0 32
f@0 33 import org.w3c.dom.Document;
f@0 34 import org.w3c.dom.Element;
f@0 35 import org.w3c.dom.NodeList;
f@0 36
f@0 37 import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramEdge;
f@0 38 import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramNode;
f@0 39 import uk.ac.qmul.eecs.ccmi.diagrammodel.ElementChangedEvent;
f@0 40 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties;
f@0 41 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties.Modifiers;
f@0 42 import uk.ac.qmul.eecs.ccmi.gui.persistence.PersistenceManager;
f@0 43
f@0 44 /**
f@0 45 * An node in a graph. {@code Node} objects are used in a {@code GraphPanel} to render diagram nodes visually.
f@0 46 * {@code Node} objects are used in the tree representation of the diagram as well, as they're
f@0 47 * subclasses of {@link DiagramNode}
f@0 48 *
f@0 49 */
f@0 50 @SuppressWarnings("serial")
f@0 51 public abstract class Node extends DiagramNode implements GraphElement{
f@0 52
f@0 53 /**
f@0 54 * Constructor to be called by sub classes
f@0 55 *
f@0 56 * @param type the type of the new node. All nodes with this type will be
f@0 57 * put under the same tree node in the tree representation
f@0 58 * @param properties the properties of this node
f@0 59 */
f@0 60 public Node(String type, NodeProperties properties){
f@0 61 super(type,properties);
f@0 62 attachedEdges = new ArrayList<Edge>();
f@0 63 }
f@0 64
f@0 65 /* --- DiagramNode abstract methods implementation --- */
f@0 66 @Override
f@0 67 public int getEdgesNum(){
f@0 68 return attachedEdges.size();
f@0 69 }
f@0 70
f@0 71 @Override
f@0 72 public Edge getEdgeAt(int index){
f@0 73 return attachedEdges.get(index);
f@0 74 }
f@0 75
f@0 76 @Override
f@0 77 public boolean addEdge(DiagramEdge e){
f@0 78 return attachedEdges.add((Edge)e);
f@0 79 }
f@0 80
f@0 81 @Override
f@0 82 public boolean removeEdge(DiagramEdge e){
f@0 83 return attachedEdges.remove((Edge)e);
f@0 84 }
f@0 85
f@0 86 @Override
f@0 87 public Node getExternalNode(){
f@0 88 return null;
f@0 89 }
f@0 90
f@0 91 @Override
f@0 92 public void setExternalNode(DiagramNode node){
f@0 93 throw new UnsupportedOperationException();
f@0 94 }
f@0 95
f@0 96 @Override
f@0 97 public Node getInternalNodeAt(int i){
f@0 98 throw new UnsupportedOperationException();
f@0 99 }
f@0 100
f@0 101 @Override
f@0 102 public int getInternalNodesNum(){
f@0 103 return 0;
f@0 104 }
f@0 105
f@0 106 @Override
f@0 107 public void addInternalNode(DiagramNode node){
f@0 108 throw new UnsupportedOperationException();
f@0 109 }
f@0 110
f@0 111 @Override
f@0 112 public void removeInternalNode(DiagramNode node){
f@0 113 throw new UnsupportedOperationException();
f@0 114 }
f@0 115
f@0 116 @Override
f@0 117 public void stopMove(Object source){
f@0 118 notifyChange(new ElementChangedEvent(this,this,"stop_move",source));
f@0 119 /* edges can change as a result of nodes motion thus we call the method for all the edges
f@0 120 * of the node regardless what the mouse point is */
f@0 121 for(int i = 0; i < getEdgesNum();i++){
f@0 122 getEdgeAt(i).stopMove(source);
f@0 123 }
f@0 124 }
f@0 125
f@0 126 @Override
f@0 127 public void translate( Point2D p , double dx, double dy, Object source){
f@0 128 translateImplementation( p, dx, dy);
f@0 129 for(int i=0; i< getInternalNodesNum();i++){
f@0 130 getInternalNodeAt(i).translate(p, dx, dy,source);
f@0 131 }
f@0 132 notifyChange(new ElementChangedEvent(this, this, "translate", source));
f@0 133 }
f@0 134
f@0 135 @Override
f@0 136 protected void setNotes(String notes,Object source){
f@0 137 this.notes = notes;
f@0 138 notifyChange(new ElementChangedEvent(this,this,"notes",source));
f@0 139 }
f@0 140
f@0 141 /**
f@0 142 * The actual implementation of {@code translate()}. The {@code translate} method
f@0 143 * when called will, in turn, call this method, and then call all the registered
f@0 144 * change listeners in order to notify them that the node has been translated.
f@0 145 *
f@0 146 * @param p the point we are translating from
f@0 147 * @param dx the amount to translate in the x-direction
f@0 148 * @param dy the amount to translate in the y-direction
f@0 149 */
f@0 150 protected abstract void translateImplementation(Point2D p , double dx, double dy);
f@0 151
f@0 152 /**
f@0 153 * Tests whether the node contains a point.
f@0 154 * @param aPoint the point to test
f@0 155 * @return true if this node contains aPoint
f@0 156 */
f@0 157 public abstract boolean contains(Point2D aPoint);
f@0 158
f@0 159 @Override
f@0 160 public abstract Rectangle2D getBounds();
f@0 161
f@0 162 @Override
f@0 163 public void startMove(Point2D p,Object source){
f@0 164 /* useless, here just to comply with the GraphElement interface */
f@0 165 }
f@0 166
f@0 167 @Override
f@0 168 public abstract Point2D getConnectionPoint(Direction d);
f@0 169
f@0 170 @Override
f@0 171 public void draw(Graphics2D g2){
f@0 172 if(!"".equals(getNotes())){
f@0 173 Rectangle2D bounds = getBounds();
f@0 174 Color oldColor = g2.getColor();
f@0 175 g2.setColor(GraphPanel.GRABBER_COLOR);
f@0 176 g2.fill(new Rectangle2D.Double(bounds.getX() - MARKER_SIZE / 2, bounds.getY() - MARKER_SIZE / 2, MARKER_SIZE, MARKER_SIZE));
f@0 177 g2.setColor(oldColor);
f@0 178 }
f@0 179 }
f@0 180
f@0 181 /**
f@0 182 * Returns the geometric shape of this node
f@0 183 *
f@0 184 * @return the shape of this node
f@0 185 */
f@0 186 public abstract Shape getShape();
f@0 187
f@0 188 /**
f@0 189 * Encodes the internal data of this node (position, name, properties, modifiers) in XML format.
f@0 190 *
f@0 191 * The saved data can be retrieved and set back via {@code decode}.
f@0 192 *
f@0 193 * @param doc An XMl document
f@0 194 * @param parent the parent XML tag this node tag will be nested in
f@0 195 */
f@0 196 public void encode(Document doc, Element parent){
f@0 197 parent.setAttribute(PersistenceManager.NAME,getName());
f@0 198
f@0 199 Element positionTag = doc.createElement(PersistenceManager.POSITION);
f@0 200 Rectangle2D bounds = getBounds();
f@0 201 positionTag.setAttribute(PersistenceManager.X, String.valueOf(bounds.getX()));
f@0 202 positionTag.setAttribute(PersistenceManager.Y, String.valueOf(bounds.getY()));
f@0 203 parent.appendChild(positionTag);
f@0 204
f@0 205 if(getProperties().isEmpty())
f@0 206 return;
f@0 207
f@0 208 Element propertiesTag = doc.createElement(PersistenceManager.PROPERTIES);
f@0 209 parent.appendChild(propertiesTag);
f@0 210 for(String type : getProperties().getTypes()){
f@0 211 List<String> values = getProperties().getValues(type);
f@0 212 if(values.isEmpty())
f@0 213 continue;
f@0 214 Element propertyTag = doc.createElement(PersistenceManager.PROPERTY);
f@0 215 propertiesTag.appendChild(propertyTag);
f@0 216
f@0 217 Element typeTag = doc.createElement(PersistenceManager.TYPE);
f@0 218 typeTag.appendChild(doc.createTextNode(type));
f@0 219 propertyTag.appendChild(typeTag);
f@0 220
f@0 221 int index = 0;
f@0 222 for(String value : values){
f@0 223 Element elementTag = doc.createElement(PersistenceManager.ELEMENT);
f@0 224 propertyTag.appendChild(elementTag);
f@0 225
f@0 226 Element valueTag = doc.createElement(PersistenceManager.VALUE);
f@0 227 valueTag.appendChild(doc.createTextNode(value));
f@0 228 elementTag.appendChild(valueTag);
f@0 229
f@0 230
f@0 231 Set<Integer> modifierIndexes = getProperties().getModifiers(type).getIndexes(index);
f@0 232 if(!modifierIndexes.isEmpty()){
f@0 233 Element modifiersTag = doc.createElement(PersistenceManager.MODIFIERS);
f@0 234 StringBuilder builder = new StringBuilder();
f@0 235 for(Integer i : modifierIndexes )
f@0 236 builder.append(i).append(' ');
f@0 237 builder.deleteCharAt(builder.length()-1);//remove last space
f@0 238 modifiersTag.appendChild(doc.createTextNode(builder.toString()));
f@0 239 elementTag.appendChild(modifiersTag);
f@0 240 }
f@0 241 index++;
f@0 242 }
f@0 243 }
f@0 244 }
f@0 245
f@0 246 /**
f@0 247 * Sets the internal data of this node (position, name, properties, modifiers) from an XML file
f@0 248 * node tag previously encoded via {@code encode}
f@0 249 *
f@0 250 * @param doc An XMl document
f@0 251 * @param nodeTag the XML {@code PersistenceManager.NODE } tag with data for this node
f@0 252 * @throws IOException if something goes wrong when reading the document. E.g. when the file is corrupted
f@0 253 *
f@0 254 * @see uk.ac.qmul.eecs.ccmi.gui.persistence
f@0 255 */
f@0 256 public void decode(Document doc, Element nodeTag) throws IOException{
f@0 257 setName(nodeTag.getAttribute(PersistenceManager.NAME),DiagramEventSource.PERS);
f@0 258 try{
f@0 259 setId(Integer.parseInt(nodeTag.getAttribute(PersistenceManager.ID)));
f@0 260 }catch(NumberFormatException nfe){
f@0 261 throw new IOException();
f@0 262 }
f@0 263
f@0 264 if(nodeTag.getElementsByTagName(PersistenceManager.POSITION).item(0) == null)
f@0 265 throw new IOException();
f@0 266 Element positionTag = (Element)nodeTag.getElementsByTagName(PersistenceManager.POSITION).item(0);
f@0 267 double dx,dy;
f@0 268 try{
f@0 269 dx = Double.parseDouble(positionTag.getAttribute(PersistenceManager.X));
f@0 270 dy = Double.parseDouble(positionTag.getAttribute(PersistenceManager.Y));
f@0 271 }catch(NumberFormatException nfe){
f@0 272 throw new IOException();
f@0 273 }
f@0 274 Rectangle2D bounds = getBounds();
f@0 275 translate(new Point2D.Double(0,0), dx - bounds.getX(), dy - bounds.getY(),DiagramEventSource.PERS);
f@0 276
f@0 277 NodeList propList = nodeTag.getElementsByTagName(PersistenceManager.PROPERTY);
f@0 278 NodeProperties properties = getProperties();
f@0 279 for(int j=0; j<propList.getLength();j++){
f@0 280 Element propertyTag = (Element)propList.item(j);
f@0 281
f@0 282 if(propertyTag.getElementsByTagName(PersistenceManager.TYPE).item(0) == null)
f@0 283 throw new IOException();
f@0 284 Element pTypeTag = (Element)propertyTag.getElementsByTagName(PersistenceManager.TYPE).item(0);
f@0 285 String propertyType = pTypeTag.getTextContent();
f@0 286
f@0 287 /* scan all the <Element> of the current <Property>*/
f@0 288 NodeList elemValueList = propertyTag.getElementsByTagName(PersistenceManager.ELEMENT);
f@0 289 for(int h=0; h<elemValueList.getLength(); h++){
f@0 290
f@0 291 Element elemTag = (Element)elemValueList.item(h);
f@0 292 /* get the <value> */
f@0 293 if(elemTag.getElementsByTagName(PersistenceManager.VALUE).item(0) == null)
f@0 294 throw new IOException();
f@0 295 Element valueTag = (Element)elemTag.getElementsByTagName(PersistenceManager.VALUE).item(0);
f@0 296 String value = valueTag.getTextContent();
f@0 297
f@0 298 /* <modifiers>. need to go back on the prototypes because the content of <modifier> is a list */
f@0 299 /* of int index pointing to the modifiers type, defined just in the prototypes */
f@0 300 Element prototypesTag = (Element)doc.getElementsByTagName(PersistenceManager.PROTOTYPES).item(0);
f@0 301 Modifiers modifiers = null;
f@0 302 try {
f@0 303 modifiers = properties.getModifiers(propertyType);
f@0 304 }catch(IllegalArgumentException iae){
f@0 305 throw new IOException(iae);
f@0 306 }
f@0 307 if(!modifiers.isNull()){
f@0 308 Element modifiersTag = (Element)((Element)elemValueList.item(h)).getElementsByTagName(PersistenceManager.MODIFIERS).item(0);
f@0 309 if(modifiersTag != null){ //else there are no modifiers specified for this property value
f@0 310 Set<Integer> indexesToAdd = new LinkedHashSet<Integer>();
f@0 311 String indexesString = modifiersTag.getTextContent();
f@0 312 String[] indexes = indexesString.split(" ");
f@0 313 for(String s : indexes){
f@0 314 try{
f@0 315 int index = Integer.parseInt(s);
f@0 316 NodeList templatePropList = prototypesTag.getElementsByTagName(PersistenceManager.PROPERTY);
f@0 317 String modifiersType = null;
f@0 318 /* look at the property prototypes to see which modifier the index is referring to. *
f@0 319 * The index is in fact the id attribute of the <Modifier> tag in the prototypes section */
f@0 320 for(int k=0; k<templatePropList.getLength();k++){
f@0 321 Element prototypePropTag = (Element)templatePropList.item(k);
f@0 322 Element prototypePropTypeTag = (Element)prototypePropTag.getElementsByTagName(PersistenceManager.TYPE).item(0);
f@0 323
f@0 324 if(propertyType.equals(prototypePropTypeTag.getTextContent())){
f@0 325 NodeList proptotypeModifierList = prototypePropTag.getElementsByTagName(PersistenceManager.MODIFIER);
f@0 326 for(int m = 0 ; m<proptotypeModifierList.getLength();m++){
f@0 327 if(index == Integer.parseInt(((Element)proptotypeModifierList.item(m)).getAttribute(PersistenceManager.ID))){
f@0 328 Element modifierTypeTag = (Element)((Element)proptotypeModifierList.item(m)).getElementsByTagName(PersistenceManager.TYPE).item(0);
f@0 329 modifiersType = modifierTypeTag.getTextContent();
f@0 330 }
f@0 331 }
f@0 332 }
f@0 333 }
f@0 334 if(modifiersType == null) // the index must point to a valid modifier's id
f@0 335 throw new IOException();
f@0 336 indexesToAdd.add(Integer.valueOf(modifiers.getTypes().indexOf(modifiersType)));
f@0 337 }catch(NumberFormatException nfe){
f@0 338 throw new IOException(nfe);
f@0 339 }
f@0 340 }
f@0 341 addProperty(propertyType, value,DiagramEventSource.PERS);//whether propertyType actually exist in the prototypes has been already checked
f@0 342 setModifierIndexes(propertyType, h, indexesToAdd,DiagramEventSource.PERS);
f@0 343 }else
f@0 344 addProperty(propertyType, value,DiagramEventSource.PERS);
f@0 345 }else
f@0 346 addProperty(propertyType, value,DiagramEventSource.PERS);
f@0 347 }
f@0 348 }
f@0 349 }
f@0 350
f@0 351 @SuppressWarnings("unchecked")
f@0 352 @Override
f@0 353 public Object clone(){
f@0 354 Node clone = (Node)super.clone();
f@0 355 clone.attachedEdges = (ArrayList<Edge>) attachedEdges.clone();
f@0 356 return clone;
f@0 357 }
f@0 358
f@0 359 /**
f@0 360 * An array of references to the edges attached to this node
f@0 361 */
f@0 362 protected ArrayList<Edge> attachedEdges;
f@0 363
f@0 364 private final int MARKER_SIZE = 7;
f@0 365 /**
f@0 366 * The shadow color of nodes
f@0 367 */
f@0 368 protected static final Color SHADOW_COLOR = Color.LIGHT_GRAY;
f@0 369 public static final int SHADOW_GAP = 2;
f@0 370
f@0 371 }