KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > cfg > editParts > CFGGraphEditPart


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 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.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     /* (non-Javadoc)
44      * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
45      */

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

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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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     /**
175      * @return
176      */

177     public int getFigureHeight() {
178         return figureHeight;
179     }
180
181     /**
182      * @return
183      */

184     public int getFigureWidth() {
185         return figureWidth;
186     }
187
188     /**
189      * @param i
190      */

191     public void setFigureHeight(int i) {
192         figureHeight = i;
193     }
194
195     /**
196      * @param i
197      */

198     public void setFigureWidth(int i) {
199         figureWidth = i;
200     }
201
202     public void handleClickEvent(Object JavaDoc evt){
203         ((CFGGraph)getModel()).handleClickEvent(evt);
204     }
205 }
206
Popular Tags