KickJava   Java API By Example, From Geeks To Geeks.

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


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 package ca.mcgill.sable.soot.cfg.editParts;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
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 JavaDoc {
53
54     /**
55      *
56      */

57     public CFGNodeEditPart() {
58         super();
59     }
60
61     /* (non-Javadoc)
62      * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
63      */

64     protected IFigure createFigure() {
65         return new CFGNodeFigure();
66     }
67
68     public void contributeNodesToGraph(DirectedGraph graph, HashMap JavaDoc map){
69         Node node = new Node(this);
70         node.width = getFigure().getBounds().width;//getNode().getWidth();
71
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;//getFigure().getBounds().height;
79
graph.nodes.add(node);
80         map.put(this, node);
81     }
82     
83     public void contributeEdgesToGraph(DirectedGraph graph, HashMap JavaDoc map) {
84         List JavaDoc 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 JavaDoc map){
92         Node node = (Node)map.get(this);
93         ((CFGNodeFigure)getFigure()).setBounds(new Rectangle(node.x, node.y, node.width, node.height));//getFigure().getBounds().height));//getFigure().getBounds().height));
94
List JavaDoc 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 JavaDoc it = getChildren().iterator();
104         while (it.hasNext()){
105             Object JavaDoc 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     /* (non-Javadoc)
123      * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
124      */

125     protected void createEditPolicies() {
126         installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE,new FlowSelectPolicy());
127     }
128
129     public List JavaDoc getModelSourceConnections(){
130         return getNode().getOutputs();
131     }
132     
133     public List JavaDoc getModelTargetConnections(){
134         return getNode().getInputs();
135     }
136     
137     /* (non-Javadoc)
138      * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
139      */

140     public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart arg0) {
141         return new ChopboxAnchor(getFigure());
142     }
143
144     /* (non-Javadoc)
145      * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
146      */

147     public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart arg0) {
148         return new ChopboxAnchor(getFigure());
149     }
150
151     /* (non-Javadoc)
152      * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.Request)
153      */

154     public ConnectionAnchor getSourceConnectionAnchor(Request arg0) {
155         return new ChopboxAnchor(getFigure());
156     }
157
158     /* (non-Javadoc)
159      * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.Request)
160      */

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 JavaDoc getModelChildren(){
176         return getNode().getChildren();
177     }
178     
179     public void propertyChange(PropertyChangeEvent JavaDoc 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 JavaDoc(){
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 JavaDoc(){
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 JavaDoc(){
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 JavaDoc it = this.getChildren().iterator();
247         while (it.hasNext()){
248             Object JavaDoc 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 JavaDoc it = getChildren().iterator();
258         while (it.hasNext()){
259             Object JavaDoc 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 JavaDoc evt){
286         ((CFGGraphEditPart)getParent()).handleClickEvent(evt);
287     }
288     
289 }
290
Popular Tags