1 19 20 21 package ca.mcgill.sable.soot.cfg.editParts; 22 23 import org.eclipse.draw2d.*; 24 import org.eclipse.gef.editparts.*; 25 import org.eclipse.draw2d.graph.*; 26 import org.eclipse.gef.*; 27 import org.eclipse.draw2d.geometry.*; 28 import java.util.*; 29 import ca.mcgill.sable.soot.cfg.model.*; 30 import java.beans.*; 31 32 33 public class CFGGraphEditPart extends AbstractGraphicalEditPart 34 implements PropertyChangeListener { 35 36 private int figureWidth = 20000; 37 private int figureHeight = 20000; 38 39 public CFGGraphEditPart() { 40 super(); 41 } 42 43 46 protected IFigure createFigure() { 47 IFigure f = new Figure() { 48 public void setBound(Rectangle rect){ 49 int x = bounds.x; 50 int y = bounds.y; 51 52 boolean resize = (rect.width != bounds.width) || (rect.height != bounds.height), 53 translate = (rect.x != x) || (rect.y != y); 54 55 if (isVisible() && (resize || translate)) 56 erase(); 57 if (translate) { 58 int dx = rect.x - x; 59 int dy = rect.y - y; 60 primTranslate(dx, dy); 61 } 62 bounds.width = rect.width; 63 bounds.height = rect.height; 64 65 if (resize || translate) { 66 fireMoved(); 67 repaint(); 68 } 69 } 70 }; 71 f.setLayoutManager(new CFGGraphLayoutManager(this)); 72 return f; 73 } 74 75 protected void setFigure(IFigure figure){ 76 figure.getBounds().setSize(getFigureWidth(),getFigureHeight()); 77 super.setFigure(figure); 78 } 79 80 83 protected void createEditPolicies() { 84 } 85 86 public void contributeNodesToGraph(DirectedGraph graph, HashMap map){ 87 Iterator it = getChildren().iterator(); 88 while (it.hasNext()){ 89 Object next = it.next(); 90 if (next instanceof CFGNodeEditPart){ 91 ((CFGNodeEditPart)next).contributeNodesToGraph(graph, map); 92 } 93 } 94 95 } 96 public void contributeEdgesToGraph(DirectedGraph graph, HashMap map){ 97 Iterator it = getChildren().iterator(); 98 while (it.hasNext()){ 99 Object next = it.next(); 100 if (next instanceof CFGNodeEditPart){ 101 ((CFGNodeEditPart)next).contributeEdgesToGraph(graph, map); 102 } 103 } 104 105 } 106 107 public void applyGraphResults(DirectedGraph graph, HashMap map){ 108 Iterator it = getChildren().iterator(); 109 while (it.hasNext()){ 110 Object next = it.next(); 111 if (next instanceof CFGNodeEditPart){ 112 ((CFGNodeEditPart)next).applyGraphResults(graph, map); 113 } 114 } 115 determineGraphBounds(graph); 116 } 117 118 public void resetChildColors(){ 119 Iterator it = getChildren().iterator(); 120 while (it.hasNext()){ 121 Object next = it.next(); 122 if (next instanceof CFGNodeEditPart){ 123 ((CFGNodeEditPart)next).resetColors(); 124 } 125 } 126 } 127 128 private void determineGraphBounds(DirectedGraph graph){ 129 Iterator it = graph.nodes.iterator(); 130 int width = 0; 131 int height = 0; 132 while (it.hasNext()){ 133 Node n = (Node)it.next(); 134 if (width < n.x){ 135 width = n.x + 300; 136 } 137 height = max(height, n.height); 138 } 139 setFigureWidth(width); 140 setFigureHeight(height); 141 } 142 143 private int max(int i, int j){ 144 return i < j ? j : i; 145 } 146 147 public CFGGraph getGraph(){ 148 return (CFGGraph)getModel(); 149 } 150 151 public List getModelChildren(){ 152 return getGraph().getChildren(); 153 } 154 155 public void activate(){ 156 super.activate(); 157 getGraph().addPropertyChangeListener(this); 158 } 159 160 public void deactivate(){ 161 super.deactivate(); 162 getGraph().removePropertyChangeListener(this); 163 } 164 165 public void propertyChange(PropertyChangeEvent event){ 166 if (event.getPropertyName().equals(CFGElement.CHILDREN)){ 167 refreshChildren(); 168 } 169 else if (event.getPropertyName().equals(CFGElement.NEW_FLOW_DATA)){ 170 resetChildColors(); 171 } 172 ((GraphicalEditPart)(getViewer().getContents())).getFigure().revalidate(); 173 } 174 177 public int getFigureHeight() { 178 return figureHeight; 179 } 180 181 184 public int getFigureWidth() { 185 return figureWidth; 186 } 187 188 191 public void setFigureHeight(int i) { 192 figureHeight = i; 193 } 194 195 198 public void setFigureWidth(int i) { 199 figureWidth = i; 200 } 201 202 public void handleClickEvent(Object evt){ 203 ((CFGGraph)getModel()).handleClickEvent(evt); 204 } 205 } 206 | Popular Tags |