KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > graph > editparts > EdgeEditPart


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2005 Jennifer Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

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     /* (non-Javadoc)
46      * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
47      */

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