1 19 package org.lucane.applications.whiteboard.operations; 20 21 import java.util.Date ; 22 import java.util.Hashtable ; 23 import java.util.Map ; 24 25 import org.jgraph.event.GraphModelEvent; 26 import org.jgraph.graph.*; 27 import org.lucane.applications.whiteboard.graph.GraphUtils; 28 import org.lucane.applications.whiteboard.graph.MyGraph; 29 30 public class GraphLayoutEdit implements GraphOperation 31 { 32 public static final int BRING_TO_FRONT = 0; 33 public static final int BRING_TO_BACK = 1; 34 public static final int GROUP = 2; 35 36 private Object [] changed; 37 private int operation; 38 private Date timestamp; 39 40 public void init(GraphModelEvent gme) {} 41 42 public void init(Object [] cells, int operation) 43 { 44 init(cells, operation, null); 45 } 46 47 public void init(Object [] cells, int operation, Date timestamp) 48 { 49 this.changed = cells; 50 this.operation = operation; 51 this.timestamp = timestamp; 52 } 53 54 public void apply(MyGraph graph) 55 { 56 GraphModel model = graph.getModel(); 57 Object [] cells = new Object [changed.length]; 58 for(int i=0;i<changed.length;i++) 59 { 60 DefaultGraphCell cell = (DefaultGraphCell)changed[i]; 61 cells[i] = GraphUtils.findCorrespondingCell(graph, cell); 62 } 63 64 if(operation == BRING_TO_FRONT) 65 bringToFront(graph, cells); 66 else if(operation == BRING_TO_BACK) 67 bringToBack(graph, cells); 68 else if(operation == GROUP) 69 group(graph, cells); 70 } 71 72 private void bringToFront(MyGraph graph, Object [] cells) 73 { 74 graph.getGraphLayoutCache().toFront(cells); 75 } 76 77 private void bringToBack(MyGraph graph, Object [] cells) 78 { 79 graph.getGraphLayoutCache().toBack(cells); 80 } 81 82 private void group(MyGraph graph, Object [] cells) 83 { 84 DefaultGraphCell group = new DefaultGraphCell("Group"); 85 AttributeMap attrs = new AttributeMap(); 86 attrs.put("timestamp", timestamp); 87 Map attributes = new Hashtable (); 88 attributes.put(group, attrs); 89 90 ParentMap map = new ParentMap(); 91 for (int i = cells.length-1; i >=0; i--) 92 map.addEntry(cells[i], group); 93 94 graph.getGraphLayoutCache().insert( 95 new Object [] {group}, attributes, null, map, null); 96 } 97 } | Popular Tags |