1 19 package org.lucane.applications.whiteboard.operations; 20 21 import org.lucane.applications.whiteboard.graph.GraphUtils; 22 import org.lucane.applications.whiteboard.graph.MyGraph; 23 import org.lucane.applications.whiteboard.operations.changers.*; 24 25 import java.util.*; 26 import org.jgraph.event.GraphModelEvent; 27 import org.jgraph.graph.*; 28 29 public class GraphEdit implements GraphOperation 30 { 31 private Object [] changed; 32 private Map oldAttributes; 33 private Map newAttributes; 34 35 private ConnectionSet connectionSet; 36 private ParentMap parentMap; 37 38 public void init(GraphModelEvent gme) 39 { 40 GraphModel model = (GraphModel)gme.getSource(); 41 GraphModelEvent.GraphModelChange change = (GraphModelEvent.GraphModelChange)gme.getChange(); 42 43 this.oldAttributes = change.getAttributes(); 44 this.newAttributes = change.getPreviousAttributes(); 45 this.changed = change.getChanged(); 46 47 this.connectionSet = change.getPreviousConnectionSet(); 48 this.parentMap = change.getPreviousParentMap(); 49 } 50 51 public void apply(MyGraph graph) 52 { 53 GraphModel model = graph.getModel(); 54 HashMap attributes = new HashMap(); 55 56 for(int i=0;i<changed.length;i++) 57 { 58 DefaultGraphCell cell = (DefaultGraphCell)changed[i]; 59 60 AttributeMap allCellAttributes = cell.getAttributes(); 61 Map oldCellAttributes = (Map)oldAttributes.get(cell); 62 if(oldCellAttributes != null) 63 allCellAttributes.putAll(oldCellAttributes); 64 cell.setAttributes(allCellAttributes); 65 cell.setUserObject(allCellAttributes.get("value")); 66 67 DefaultGraphCell correspondingCell = GraphUtils.findCorrespondingCell(graph, cell); 68 if(correspondingCell == null) 69 correspondingCell = cell; 70 else 71 { 72 Changer changer = ChangerFactory.getChanger(cell); 73 changer.change(graph, correspondingCell, cell); 74 } 75 76 attributes.put(correspondingCell, newAttributes.get(cell)); 77 } 78 79 model.edit(attributes, connectionSet, parentMap, null); 80 } 81 } | Popular Tags |