1 19 20 package ca.mcgill.sable.soot.cfg.editParts; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.eclipse.draw2d.ChopboxAnchor; 29 import org.eclipse.draw2d.ConnectionAnchor; 30 import org.eclipse.draw2d.IFigure; 31 import org.eclipse.draw2d.geometry.Rectangle; 32 import org.eclipse.draw2d.graph.DirectedGraph; 33 import org.eclipse.draw2d.graph.Node; 34 import org.eclipse.gef.ConnectionEditPart; 35 import org.eclipse.gef.EditPartViewer; 36 import org.eclipse.gef.EditPolicy; 37 import org.eclipse.gef.NodeEditPart; 38 import org.eclipse.gef.Request; 39 import org.eclipse.gef.editparts.AbstractGraphicalEditPart; 40 import org.eclipse.swt.widgets.Display; 41 42 import ca.mcgill.sable.soot.cfg.editpolicies.FlowSelectPolicy; 43 import ca.mcgill.sable.soot.cfg.figures.CFGNodeFigure; 44 import ca.mcgill.sable.soot.cfg.model.CFGElement; 45 import ca.mcgill.sable.soot.cfg.model.CFGFlowData; 46 import ca.mcgill.sable.soot.cfg.model.CFGFlowInfo; 47 import ca.mcgill.sable.soot.cfg.model.CFGNode; 48 import ca.mcgill.sable.soot.cfg.model.CFGPartialFlowData; 49 50 public class CFGNodeEditPart 51 extends AbstractGraphicalEditPart 52 implements NodeEditPart, PropertyChangeListener { 53 54 57 public CFGNodeEditPart() { 58 super(); 59 } 60 61 64 protected IFigure createFigure() { 65 return new CFGNodeFigure(); 66 } 67 68 public void contributeNodesToGraph(DirectedGraph graph, HashMap map){ 69 Node node = new Node(this); 70 node.width = getFigure().getBounds().width; int height = 22; 72 if (((CFGNode)getModel()).getBefore() != null){ 73 height += ((CFGNode)getModel()).getBefore().getChildren().size() * 22; 74 } 75 if (((CFGNode)getModel()).getAfter() != null){ 76 height += ((CFGNode)getModel()).getAfter().getChildren().size() * 22; 77 } 78 node.height = height; graph.nodes.add(node); 80 map.put(this, node); 81 } 82 83 public void contributeEdgesToGraph(DirectedGraph graph, HashMap map) { 84 List outgoing = getSourceConnections(); 85 for (int i = 0; i < outgoing.size(); i++){ 86 CFGEdgeEditPart edge = (CFGEdgeEditPart)outgoing.get(i); 87 edge.contributeToGraph(graph, map); 88 } 89 } 90 91 public void applyGraphResults(DirectedGraph graph, HashMap map){ 92 Node node = (Node)map.get(this); 93 ((CFGNodeFigure)getFigure()).setBounds(new Rectangle(node.x, node.y, node.width, node.height)); List outgoing = getSourceConnections(); 95 for (int i = 0; i < outgoing.size(); i++){ 96 CFGEdgeEditPart edge = (CFGEdgeEditPart)outgoing.get(i); 97 edge.applyGraphResults(graph, map); 98 } 99 100 } 101 102 public void resetColors(){ 103 Iterator it = getChildren().iterator(); 104 while (it.hasNext()){ 105 Object next = it.next(); 106 if (next instanceof FlowDataEditPart){ 107 ((FlowDataEditPart)next).resetChildColors(); 108 } 109 else if (next instanceof NodeDataEditPart){ 110 ((NodeDataEditPart)next).resetColors(); 111 112 } 113 } 114 115 ((CFGNodeFigure)getFigure()).removeIndicator(); 116 } 117 118 public CFGNode getNode(){ 119 return (CFGNode)getModel(); 120 } 121 122 125 protected void createEditPolicies() { 126 installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE,new FlowSelectPolicy()); 127 } 128 129 public List getModelSourceConnections(){ 130 return getNode().getOutputs(); 131 } 132 133 public List getModelTargetConnections(){ 134 return getNode().getInputs(); 135 } 136 137 140 public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart arg0) { 141 return new ChopboxAnchor(getFigure()); 142 } 143 144 147 public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart arg0) { 148 return new ChopboxAnchor(getFigure()); 149 } 150 151 154 public ConnectionAnchor getSourceConnectionAnchor(Request arg0) { 155 return new ChopboxAnchor(getFigure()); 156 } 157 158 161 public ConnectionAnchor getTargetConnectionAnchor(Request arg0) { 162 return new ChopboxAnchor(getFigure()); 163 } 164 165 public void activate(){ 166 super.activate(); 167 getNode().addPropertyChangeListener(this); 168 } 169 170 public void deactivate(){ 171 super.deactivate(); 172 getNode().removePropertyChangeListener(this); 173 } 174 175 public List getModelChildren(){ 176 return getNode().getChildren(); 177 } 178 179 public void propertyChange(PropertyChangeEvent event){ 180 if (event.getPropertyName().equals(CFGElement.NODE_DATA)){ 181 refreshChildren(); 182 refreshVisuals(); 183 } 184 else if (event.getPropertyName().equals(CFGElement.BEFORE_INFO)){ 185 refreshChildren(); 186 refreshVisuals(); 187 final EditPartViewer viewer = getViewer(); 188 final CFGNodeEditPart thisPart = this; 189 Display.getDefault().syncExec(new Runnable (){ 190 public void run(){ 191 viewer.reveal(thisPart); 192 }; 193 }); 194 } 195 else if (event.getPropertyName().equals(CFGElement.AFTER_INFO)){ 196 refreshChildren(); 197 refreshVisuals(); 198 final EditPartViewer viewer = getViewer(); 199 final CFGNodeEditPart thisPart = this; 200 Display.getDefault().syncExec(new Runnable (){ 201 public void run(){ 202 viewer.reveal(thisPart); 203 }; 204 }); 205 } 206 else if (event.getPropertyName().equals(CFGElement.INPUTS)){ 207 refreshTargetConnections(); 208 } 209 else if (event.getPropertyName().equals(CFGElement.OUTPUTS)){ 210 refreshSourceConnections(); 211 } 212 else if (event.getPropertyName().equals(CFGElement.REVEAL)){ 213 214 revealThis(); 215 resetColors(); 216 } 217 else if (event.getPropertyName().equals(CFGElement.HIGHLIGHT)){ 218 highlightThis(); 219 revealThis(); 220 } 221 222 } 223 224 private void revealThis(){ 225 final EditPartViewer viewer = getViewer(); 226 final CFGNodeEditPart thisPart = this; 227 Display.getDefault().syncExec(new Runnable (){ 228 public void run(){ 229 viewer.reveal(thisPart); 230 viewer.select(thisPart); 231 }; 232 }); 233 } 234 235 protected void highlightThis(){ 236 if (((CFGNode)getModel()).getBefore() == null){ 237 CFGFlowData data = new CFGFlowData(); 238 CFGFlowInfo info = new CFGFlowInfo(); 239 info.setText(""); 240 CFGPartialFlowData pInfo = new CFGPartialFlowData(); 241 pInfo.addChild(info); 242 data.addChild(pInfo); 243 244 ((CFGNode)getModel()).setBefore(data); 245 } 246 Iterator it = this.getChildren().iterator(); 247 while (it.hasNext()){ 248 Object next = it.next(); 249 if (next instanceof NodeDataEditPart){ 250 ((NodeDataEditPart)next).addIndicator(); 251 } 252 } 253 ((CFGNodeFigure)getFigure()).addIndicator(); 254 } 255 256 protected void refreshVisuals(){ 257 Iterator it = getChildren().iterator(); 258 while (it.hasNext()){ 259 Object next = it.next(); 260 if (next instanceof FlowDataEditPart){ 261 ((CFGNodeFigure)getFigure()).add(((FlowDataEditPart)next).getFigure()); 262 } 263 else if (next instanceof NodeDataEditPart){ 264 ((CFGNodeFigure)getFigure()).add(((NodeDataEditPart)next).getFigure()); 265 } 266 } 267 } 268 269 public void updateSize(AbstractGraphicalEditPart part, IFigure child, Rectangle rect){ 270 this.setLayoutConstraint(part, child, rect); 271 } 272 273 public void updateSize(int width, int height){ 274 275 int w = ((CFGNodeFigure)getFigure()).getBounds().width; 276 int h = ((CFGNodeFigure)getFigure()).getBounds().height; 277 h += height; 278 if (width > w){ 279 ((CFGNodeFigure)getFigure()).setSize(width, h); 280 ((CFGNodeFigure)getFigure()).revalidate(); 281 } 282 } 283 284 285 public void handleClickEvent(Object evt){ 286 ((CFGGraphEditPart)getParent()).handleClickEvent(evt); 287 } 288 289 } 290 | Popular Tags |