KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.graph.model.*;
30 import java.beans.*;
31
32 public class GraphEditPart extends AbstractGraphicalEditPart
33     implements PropertyChangeListener {
34
35     private int figureWidth = 20000;
36     private int figureHeight = 20000;
37     
38
39     public GraphEditPart() {
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 GraphLayoutManager(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 SimpleNodeEditPart){
91                 SimpleNodeEditPart child = (SimpleNodeEditPart)next;
92                 child.contributeNodesToGraph(graph, map);
93         
94             }
95         }
96         
97     }
98     
99     
100     public void contributeEdgesToGraph(DirectedGraph graph, HashMap map){
101         Iterator it = getChildren().iterator();
102         while (it.hasNext()){
103             Object JavaDoc next = it.next();
104             if (next instanceof SimpleNodeEditPart){
105                 ((SimpleNodeEditPart)next).contributeEdgesToGraph(graph, map);
106
107             }
108         }
109         
110     }
111
112     public void applyGraphResults(DirectedGraph graph, HashMap map){
113         Iterator it = getChildren().iterator();
114         while (it.hasNext()){
115             Object JavaDoc next = it.next();
116             if (next instanceof SimpleNodeEditPart){
117                 ((SimpleNodeEditPart)next).applyGraphResults(graph, map);
118             }
119         }
120         determineGraphBounds(graph);
121     }
122     
123     
124     
125     private void determineGraphBounds(DirectedGraph graph){
126         Iterator it = graph.nodes.iterator();
127         int width = 0;
128         int height = 0;
129         while (it.hasNext()){
130             Node n = (Node)it.next();
131             if (width < n.x){
132                 width = n.x + 300;
133             }
134             height = max(height, n.height);
135         }
136         setFigureWidth(width);
137         setFigureHeight(height);
138     }
139     
140     private int max(int i, int j){
141         return i < j ? j : i;
142     }
143     
144     public Graph getGraph(){
145         return (Graph)getModel();
146     }
147     
148     public List getModelChildren(){
149         return getGraph().getChildren();
150     }
151
152     public void activate(){
153         super.activate();
154         getGraph().addPropertyChangeListener(this);
155     }
156     
157     public void deactivate(){
158         super.deactivate();
159         getGraph().removePropertyChangeListener(this);
160     }
161     
162     public void propertyChange(PropertyChangeEvent event){
163         if (event.getPropertyName().equals(Element.GRAPH_CHILD)){
164             refreshChildren();
165         }
166         
167         getFigure().revalidate();
168         ((GraphicalEditPart)(getViewer().getContents())).getFigure().revalidate();
169     
170     }
171     /**
172      * @return
173      */

174     public int getFigureHeight() {
175         return figureHeight;
176     }
177
178     /**
179      * @return
180      */

181     public int getFigureWidth() {
182         return figureWidth;
183     }
184
185     /**
186      * @param i
187      */

188     public void setFigureHeight(int i) {
189         figureHeight = i;
190     }
191
192     /**
193      * @param i
194      */

195     public void setFigureWidth(int i) {
196         figureWidth = i;
197     }
198
199 }
200
Popular Tags