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.draw2d.*;
023: import org.eclipse.gef.editparts.*;
024: import org.eclipse.draw2d.graph.*;
025: import org.eclipse.gef.*;
026: import org.eclipse.draw2d.geometry.*;
027: import java.util.*;
028: import ca.mcgill.sable.graph.model.*;
029: import java.beans.*;
030:
031: public class GraphEditPart extends AbstractGraphicalEditPart implements
032: PropertyChangeListener {
033:
034: private int figureWidth = 20000;
035: private int figureHeight = 20000;
036:
037: public GraphEditPart() {
038: super ();
039: }
040:
041: /* (non-Javadoc)
042: * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
043: */
044: protected IFigure createFigure() {
045: IFigure f = new Figure() {
046: public void setBound(Rectangle rect) {
047: int x = bounds.x;
048: int y = bounds.y;
049:
050: boolean resize = (rect.width != bounds.width)
051: || (rect.height != bounds.height), translate = (rect.x != x)
052: || (rect.y != y);
053:
054: if (isVisible() && (resize || translate))
055: erase();
056: if (translate) {
057: int dx = rect.x - x;
058: int dy = rect.y - y;
059: primTranslate(dx, dy);
060: }
061: bounds.width = rect.width;
062: bounds.height = rect.height;
063:
064: if (resize || translate) {
065: fireMoved();
066: repaint();
067: }
068: }
069: };
070: f.setLayoutManager(new GraphLayoutManager(this ));
071: return f;
072: }
073:
074: protected void setFigure(IFigure figure) {
075: figure.getBounds().setSize(getFigureWidth(), getFigureHeight());
076: super .setFigure(figure);
077: }
078:
079: /* (non-Javadoc)
080: * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
081: */
082: protected void createEditPolicies() {
083: }
084:
085: public void contributeNodesToGraph(DirectedGraph graph, HashMap map) {
086: Iterator it = getChildren().iterator();
087: while (it.hasNext()) {
088: Object next = it.next();
089: if (next instanceof SimpleNodeEditPart) {
090: SimpleNodeEditPart child = (SimpleNodeEditPart) next;
091: child.contributeNodesToGraph(graph, map);
092:
093: }
094: }
095:
096: }
097:
098: public void contributeEdgesToGraph(DirectedGraph graph, HashMap map) {
099: Iterator it = getChildren().iterator();
100: while (it.hasNext()) {
101: Object next = it.next();
102: if (next instanceof SimpleNodeEditPart) {
103: ((SimpleNodeEditPart) next).contributeEdgesToGraph(
104: graph, map);
105:
106: }
107: }
108:
109: }
110:
111: public void applyGraphResults(DirectedGraph graph, HashMap map) {
112: Iterator it = getChildren().iterator();
113: while (it.hasNext()) {
114: Object next = it.next();
115: if (next instanceof SimpleNodeEditPart) {
116: ((SimpleNodeEditPart) next).applyGraphResults(graph,
117: map);
118: }
119: }
120: determineGraphBounds(graph);
121: }
122:
123: private void determineGraphBounds(DirectedGraph graph) {
124: Iterator it = graph.nodes.iterator();
125: int width = 0;
126: int height = 0;
127: while (it.hasNext()) {
128: Node n = (Node) it.next();
129: if (width < n.x) {
130: width = n.x + 300;
131: }
132: height = max(height, n.height);
133: }
134: setFigureWidth(width);
135: setFigureHeight(height);
136: }
137:
138: private int max(int i, int j) {
139: return i < j ? j : i;
140: }
141:
142: public Graph getGraph() {
143: return (Graph) getModel();
144: }
145:
146: public List getModelChildren() {
147: return getGraph().getChildren();
148: }
149:
150: public void activate() {
151: super .activate();
152: getGraph().addPropertyChangeListener(this );
153: }
154:
155: public void deactivate() {
156: super .deactivate();
157: getGraph().removePropertyChangeListener(this );
158: }
159:
160: public void propertyChange(PropertyChangeEvent event) {
161: if (event.getPropertyName().equals(Element.GRAPH_CHILD)) {
162: refreshChildren();
163: }
164:
165: getFigure().revalidate();
166: ((GraphicalEditPart) (getViewer().getContents())).getFigure()
167: .revalidate();
168:
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: }
|