view src/uk/ac/qmul/eecs/ccmi/xmlparser/LocalDiagramUpdater.java @ 0:e0ee6ac3a45f

first import
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Thu, 13 Dec 2012 20:00:21 +0000
parents
children
line wrap: on
line source
/*  
 CCmI Diagram Editor for Android 
  
 Copyright (C) 2012  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package uk.ac.qmul.eecs.ccmi.xmlparser;

import java.util.Iterator;
import java.util.List;

/**
 * An implementation of {@code DiagramUpdater} that immediately execute the commands on the local instance
 * of {@code Diagram}. 
 */
public class LocalDiagramUpdater implements DiagramUpdater {
	Diagram diagram;

	/**
	 * Constructor 
	 * 
	 * @param diagram the diagram the commands will be executed on
	 */
	public LocalDiagramUpdater(Diagram diagram){
		this.diagram = diagram;
	}
	
	@Override
	public void addNode(Node node){
		diagram.getComponents().getNodes().add(node);
	}

	@Override
	public void rename(HierarchyItem item, String newName) {
		if(item instanceof Node){
			((Node)item).setName(newName);
		}else if(item instanceof Edge){
			((Edge)item).setName(newName);
		}else if(item instanceof PropertyValue){
			((PropertyValue)item).setValue(newName);
		}
	}

	@Override
	public void delete(HierarchyItem item) {
		if(item instanceof Node){
			diagram.getComponents().getNodes().remove(item);
			Node node = (Node)item;
			Iterator<Edge> iterator = diagram.getComponents().getEdges().iterator(); 
			/* we are deleting this node, so we need to remove it from  * 
			 * attached nodes list of the edges it's attached to        */
			while(iterator.hasNext()){
				Edge edge = iterator.next();
				boolean removed = false;
				for(EdgeNode edgeNode : edge.getAttachedNodes()){
					if(edgeNode.getId() == node.getId()){
						edge.getAttachedNodes().remove(edgeNode);
						removed = true;
						break;
					}
				}
				/* there cannot be an edge with one node only. If the edge had two nodes one * 
				 * of which was the deleted one, then the edge itself must be deleted        */
				if(removed && edge.getAttachedNodes().size() == 1)
					iterator.remove();
			}
			
		}else if(item instanceof Edge){
			diagram.getComponents().getEdges().remove(item);
		}else if(item instanceof PropertyValue){
			for(Node node: diagram.getComponents().getNodes()){
				for(NodeProperty property : node.getProperties()){
					if(property.getValues().remove(item)){
						return;
					}
				}
			}
		}
	}

	@Override
	public void addEdge(Edge edge) {
		diagram.getComponents().getEdges().add(edge);
	}

	@Override
	public void addProperty(Node node, NodeProperty property, String propertyValue) {
		property.getValues().add(new PropertyValue(propertyValue));
	}

	@Override
	public void setLabel(EdgeNode edgeNode, String label) {
		edgeNode.setLabel(label);
	}

	@Override
	public void setArrowHead(EdgeNode edgeNode, String arrowHead) {
		edgeNode.setHead(arrowHead);
	}

	@Override
	public void setModifiers(PropertyValue propertyValue, List<Integer> modifiers) {
		propertyValue.setModifiersIndexes(modifiers);		
	}

}