1 19 20 21 package ca.mcgill.sable.graph.editparts; 22 23 import org.eclipse.gef.editparts.AbstractConnectionEditPart; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.graphics.Font; 26 import org.eclipse.draw2d.*; 27 import org.eclipse.draw2d.graph.*; 28 import java.util.*; 29 import java.beans.*; 30 31 import org.eclipse.draw2d.*; 32 33 import ca.mcgill.sable.graph.model.Element; 34 35 public class EdgeEditPart extends AbstractConnectionEditPart 36 implements PropertyChangeListener 37 { 38 Font f = new Font(null, "Arial", 8, SWT.NORMAL); 39 40 41 public EdgeEditPart() { 42 super(); 43 } 44 45 48 protected void createEditPolicies() { 49 } 50 51 protected IFigure createFigure(){ 52 PolylineConnection conn = new PolylineConnection(); 53 54 conn.setTargetDecoration(new PolygonDecoration()); 55 conn.setConnectionRouter(new BendpointConnectionRouter()); 56 if (((ca.mcgill.sable.graph.model.Edge)getModel()).getLabel() != null){ 57 Label connLabel = new Label(((ca.mcgill.sable.graph.model.Edge)getModel()).getLabel()); 58 connLabel.setFont(f); 59 conn.add(connLabel); 60 conn.getLayoutManager().setConstraint(connLabel, new MidpointLocator(conn, 0)); 61 } 62 return conn; 63 } 64 65 public void contributeToGraph(DirectedGraph graph, HashMap map){ 66 Node source = (Node)map.get(getSource()); 67 Node target = (Node)map.get(getTarget()); 68 if (!source.equals(target)){ 69 Edge e = new Edge(this, source, target); 70 graph.edges.add(e); 71 map.put(this, e); 72 } 73 } 74 75 public void applyGraphResults(DirectedGraph graph, HashMap map){ 76 Edge e = (Edge)map.get(this); 77 if (e != null) { 78 NodeList nl = e.vNodes; 79 PolylineConnection conn = (PolylineConnection)getConnectionFigure(); 80 if (nl != null){ 81 ArrayList bends = new ArrayList(); 82 for (int i = 0; i < nl.size(); i++){ 83 Node n = nl.getNode(i); 84 int x = n.x; 85 int y = n.y; 86 if (e.isFeedback){ 87 bends.add(new AbsoluteBendpoint(x, y + n.height)); 88 bends.add(new AbsoluteBendpoint(x, y)); 89 } 90 else { 91 bends.add(new AbsoluteBendpoint(x, y)); 92 bends.add(new AbsoluteBendpoint(x, y + n.height)); 93 } 94 } 95 conn.setRoutingConstraint(bends); 96 } 97 else { 98 conn.setRoutingConstraint(Collections.EMPTY_LIST); 99 } 100 } 101 102 } 103 104 105 public Edge getEdge(){ 106 return (Edge)getModel(); 107 } 108 109 public void propertyChange(PropertyChangeEvent event){ 110 if (event.getPropertyName().equals(Element.EDGE_LABEL)){ 111 refreshVisuals(); 112 } 113 } 114 115 public void refreshVisuals(){ 116 if (((ca.mcgill.sable.graph.model.Edge)getModel()).getLabel() != null){ 117 Label connLabel = new Label(((ca.mcgill.sable.graph.model.Edge)getModel()).getLabel()); 118 connLabel.setFont(f); 119 ((PolylineConnection)getFigure()).add(connLabel); 120 ((PolylineConnection)getFigure()).getLayoutManager().setConstraint(connLabel, new MidpointLocator((PolylineConnection)getFigure(), 0)); 121 } 122 getFigure().revalidate(); 123 } 124 125 public void activate(){ 126 super.activate(); 127 ((ca.mcgill.sable.graph.model.Edge)getModel()).addPropertyChangeListener(this); 128 } 129 130 public void deactivate(){ 131 super.deactivate(); 132 ((ca.mcgill.sable.graph.model.Edge)getModel()).removePropertyChangeListener(this); 133 } 134 } 135 | Popular Tags |