fiore@0: /* fiore@0: CCmI Diagram Editor for Android fiore@0: fiore@0: Copyright (C) 2012 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) fiore@0: fiore@0: This program is free software: you can redistribute it and/or modify fiore@0: it under the terms of the GNU General Public License as published by fiore@0: the Free Software Foundation, either version 3 of the License, or fiore@0: (at your option) any later version. fiore@0: fiore@0: This program is distributed in the hope that it will be useful, fiore@0: but WITHOUT ANY WARRANTY; without even the implied warranty of fiore@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fiore@0: GNU General Public License for more details. fiore@0: fiore@0: You should have received a copy of the GNU General Public License fiore@0: along with this program. If not, see . fiore@0: */ fiore@0: package uk.ac.qmul.eecs.ccmi.xmlparser; fiore@0: fiore@0: import java.util.Iterator; fiore@0: import java.util.List; fiore@0: fiore@0: /** fiore@0: * An implementation of {@code DiagramUpdater} that immediately execute the commands on the local instance fiore@0: * of {@code Diagram}. fiore@0: */ fiore@0: public class LocalDiagramUpdater implements DiagramUpdater { fiore@0: Diagram diagram; fiore@0: fiore@0: /** fiore@0: * Constructor fiore@0: * fiore@0: * @param diagram the diagram the commands will be executed on fiore@0: */ fiore@0: public LocalDiagramUpdater(Diagram diagram){ fiore@0: this.diagram = diagram; fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void addNode(Node node){ fiore@0: diagram.getComponents().getNodes().add(node); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void rename(HierarchyItem item, String newName) { fiore@0: if(item instanceof Node){ fiore@0: ((Node)item).setName(newName); fiore@0: }else if(item instanceof Edge){ fiore@0: ((Edge)item).setName(newName); fiore@0: }else if(item instanceof PropertyValue){ fiore@0: ((PropertyValue)item).setValue(newName); fiore@0: } fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void delete(HierarchyItem item) { fiore@0: if(item instanceof Node){ fiore@0: diagram.getComponents().getNodes().remove(item); fiore@0: Node node = (Node)item; fiore@0: Iterator iterator = diagram.getComponents().getEdges().iterator(); fiore@0: /* we are deleting this node, so we need to remove it from * fiore@0: * attached nodes list of the edges it's attached to */ fiore@0: while(iterator.hasNext()){ fiore@0: Edge edge = iterator.next(); fiore@0: boolean removed = false; fiore@0: for(EdgeNode edgeNode : edge.getAttachedNodes()){ fiore@0: if(edgeNode.getId() == node.getId()){ fiore@0: edge.getAttachedNodes().remove(edgeNode); fiore@0: removed = true; fiore@0: break; fiore@0: } fiore@0: } fiore@0: /* there cannot be an edge with one node only. If the edge had two nodes one * fiore@0: * of which was the deleted one, then the edge itself must be deleted */ fiore@0: if(removed && edge.getAttachedNodes().size() == 1) fiore@0: iterator.remove(); fiore@0: } fiore@0: fiore@0: }else if(item instanceof Edge){ fiore@0: diagram.getComponents().getEdges().remove(item); fiore@0: }else if(item instanceof PropertyValue){ fiore@0: for(Node node: diagram.getComponents().getNodes()){ fiore@0: for(NodeProperty property : node.getProperties()){ fiore@0: if(property.getValues().remove(item)){ fiore@0: return; fiore@0: } fiore@0: } fiore@0: } fiore@0: } fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void addEdge(Edge edge) { fiore@0: diagram.getComponents().getEdges().add(edge); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void addProperty(Node node, NodeProperty property, String propertyValue) { fiore@0: property.getValues().add(new PropertyValue(propertyValue)); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void setLabel(EdgeNode edgeNode, String label) { fiore@0: edgeNode.setLabel(label); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void setArrowHead(EdgeNode edgeNode, String arrowHead) { fiore@0: edgeNode.setHead(arrowHead); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void setModifiers(PropertyValue propertyValue, List modifiers) { fiore@0: propertyValue.setModifiersIndexes(modifiers); fiore@0: } fiore@0: fiore@0: }