001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2005 Jennifer Lhotak
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package ca.mcgill.sable.graph.editparts;
021:
022: import org.eclipse.gef.editparts.AbstractConnectionEditPart;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.graphics.Font;
025: import org.eclipse.draw2d.*;
026: import org.eclipse.draw2d.graph.*;
027: import java.util.*;
028: import java.beans.*;
029:
030: import org.eclipse.draw2d.*;
031:
032: import ca.mcgill.sable.graph.model.Element;
033:
034: public class EdgeEditPart extends AbstractConnectionEditPart implements
035: PropertyChangeListener {
036: Font f = new Font(null, "Arial", 8, SWT.NORMAL);
037:
038: public EdgeEditPart() {
039: super ();
040: }
041:
042: /* (non-Javadoc)
043: * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
044: */
045: protected void createEditPolicies() {
046: }
047:
048: protected IFigure createFigure() {
049: PolylineConnection conn = new PolylineConnection();
050:
051: conn.setTargetDecoration(new PolygonDecoration());
052: conn.setConnectionRouter(new BendpointConnectionRouter());
053: if (((ca.mcgill.sable.graph.model.Edge) getModel()).getLabel() != null) {
054: Label connLabel = new Label(
055: ((ca.mcgill.sable.graph.model.Edge) getModel())
056: .getLabel());
057: connLabel.setFont(f);
058: conn.add(connLabel);
059: conn.getLayoutManager().setConstraint(connLabel,
060: new MidpointLocator(conn, 0));
061: }
062: return conn;
063: }
064:
065: public void contributeToGraph(DirectedGraph graph, HashMap map) {
066: Node source = (Node) map.get(getSource());
067: Node target = (Node) map.get(getTarget());
068: if (!source.equals(target)) {
069: Edge e = new Edge(this , source, target);
070: graph.edges.add(e);
071: map.put(this , e);
072: }
073: }
074:
075: public void applyGraphResults(DirectedGraph graph, HashMap map) {
076: Edge e = (Edge) map.get(this );
077: if (e != null) {
078: NodeList nl = e.vNodes;
079: PolylineConnection conn = (PolylineConnection) getConnectionFigure();
080: if (nl != null) {
081: ArrayList bends = new ArrayList();
082: for (int i = 0; i < nl.size(); i++) {
083: Node n = nl.getNode(i);
084: int x = n.x;
085: int y = n.y;
086: if (e.isFeedback) {
087: bends
088: .add(new AbsoluteBendpoint(x, y
089: + n.height));
090: bends.add(new AbsoluteBendpoint(x, y));
091: } else {
092: bends.add(new AbsoluteBendpoint(x, y));
093: bends
094: .add(new AbsoluteBendpoint(x, y
095: + n.height));
096: }
097: }
098: conn.setRoutingConstraint(bends);
099: } else {
100: conn.setRoutingConstraint(Collections.EMPTY_LIST);
101: }
102: }
103:
104: }
105:
106: public Edge getEdge() {
107: return (Edge) getModel();
108: }
109:
110: public void propertyChange(PropertyChangeEvent event) {
111: if (event.getPropertyName().equals(Element.EDGE_LABEL)) {
112: refreshVisuals();
113: }
114: }
115:
116: public void refreshVisuals() {
117: if (((ca.mcgill.sable.graph.model.Edge) getModel()).getLabel() != null) {
118: Label connLabel = new Label(
119: ((ca.mcgill.sable.graph.model.Edge) getModel())
120: .getLabel());
121: connLabel.setFont(f);
122: ((PolylineConnection) getFigure()).add(connLabel);
123: ((PolylineConnection) getFigure())
124: .getLayoutManager()
125: .setConstraint(
126: connLabel,
127: new MidpointLocator(
128: (PolylineConnection) getFigure(), 0));
129: }
130: getFigure().revalidate();
131: }
132:
133: public void activate() {
134: super .activate();
135: ((ca.mcgill.sable.graph.model.Edge) getModel())
136: .addPropertyChangeListener(this );
137: }
138:
139: public void deactivate() {
140: super .deactivate();
141: ((ca.mcgill.sable.graph.model.Edge) getModel())
142: .removePropertyChangeListener(this);
143: }
144: }
|