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 java.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024:
025: import org.eclipse.draw2d.*;
026: import org.eclipse.gef.*;
027: import org.eclipse.gef.editparts.*;
028: import org.eclipse.draw2d.graph.*;
029: import java.util.*;
030: import ca.mcgill.sable.graph.model.*;
031: import ca.mcgill.sable.graph.figures.*;
032: import org.eclipse.draw2d.geometry.*;
033: import org.eclipse.swt.graphics.RGB;
034: import org.eclipse.swt.widgets.Display;
035: import ca.mcgill.sable.graph.editpolicies.*;
036: import ca.mcgill.sable.graph.*;
037:
038: public class SimpleNodeEditPart extends AbstractGraphicalEditPart
039: implements NodeEditPart, PropertyChangeListener {
040:
041: private Object data;
042:
043: public SimpleNodeEditPart() {
044: super ();
045: }
046:
047: /* (non-Javadoc)
048: * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
049: */
050: protected IFigure createFigure() {
051: RectangleFigure rect = new RectangleFigure();
052: ToolbarLayout layout = new ToolbarLayout();
053: layout.setMinorAlignment(ToolbarLayout.ALIGN_CENTER);
054: rect.setLayoutManager(layout);
055:
056: Label label = new Label();
057: label.getInsets().top = 1;
058: label.getInsets().bottom = 1;
059: label.getInsets().right = 1;
060: label.getInsets().left = 1;
061: rect.add(label);
062:
063: return rect;
064: }
065:
066: public void contributeNodesToGraph(DirectedGraph graph, HashMap map) {
067: Node node = new Node(this );
068: node.width = getFigure().getBounds().width;//getNode().getWidth();
069: node.height = getFigure().getBounds().height;
070: graph.nodes.add(node);
071: map.put(this , node);
072: }
073:
074: public void contributeEdgesToGraph(DirectedGraph graph, HashMap map) {
075: List outgoing = getSourceConnections();
076: for (int i = 0; i < outgoing.size(); i++) {
077: EdgeEditPart edge = (EdgeEditPart) outgoing.get(i);
078: edge.contributeToGraph(graph, map);
079: }
080: }
081:
082: public void applyGraphResults(DirectedGraph graph, HashMap map) {
083: Node node = (Node) map.get(this );
084: if (node != null) {
085: getFigure().setBounds(
086: new Rectangle(node.x + 10, node.y, node.width,
087: node.height));//getFigure().getBounds().height));//getFigure().getBounds().height));
088: List outgoing = getSourceConnections();
089: for (int i = 0; i < outgoing.size(); i++) {
090: EdgeEditPart edge = (EdgeEditPart) outgoing.get(i);
091: edge.applyGraphResults(graph, map);
092: }
093: }
094:
095: }
096:
097: public SimpleNode getNode() {
098: return (SimpleNode) getModel();
099: }
100:
101: /* (non-Javadoc)
102: * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
103: */
104: protected void createEditPolicies() {
105: }
106:
107: public List getModelSourceConnections() {
108: return getNode().getOutputs();
109: }
110:
111: public List getModelTargetConnections() {
112: return getNode().getInputs();
113: }
114:
115: /* (non-Javadoc)
116: * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
117: */
118: public ConnectionAnchor getSourceConnectionAnchor(
119: ConnectionEditPart arg0) {
120: return new ChopboxAnchor(getFigure());
121: }
122:
123: /* (non-Javadoc)
124: * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
125: */
126: public ConnectionAnchor getTargetConnectionAnchor(
127: ConnectionEditPart arg0) {
128: return new ChopboxAnchor(getFigure());
129: }
130:
131: /* (non-Javadoc)
132: * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.Request)
133: */
134: public ConnectionAnchor getSourceConnectionAnchor(Request arg0) {
135: return new ChopboxAnchor(getFigure());
136: }
137:
138: /* (non-Javadoc)
139: * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.Request)
140: */
141: public ConnectionAnchor getTargetConnectionAnchor(Request arg0) {
142: return new ChopboxAnchor(getFigure());
143: }
144:
145: public void activate() {
146: super .activate();
147: getNode().addPropertyChangeListener(this );
148: }
149:
150: public void deactivate() {
151: super .deactivate();
152: getNode().removePropertyChangeListener(this );
153: }
154:
155: public void propertyChange(PropertyChangeEvent event) {
156: if (event.getPropertyName().equals(Element.DATA)) {
157: //System.out.println("new val: "+event.getNewValue());
158: setData(event.getNewValue());
159: refreshVisuals();
160: } else if (event.getPropertyName().equals(Element.INPUTS)) {
161: refreshTargetConnections();
162: } else if (event.getPropertyName().equals(Element.OUTPUTS)) {
163: refreshSourceConnections();
164: }
165:
166: }
167:
168: public List getModelChildren() {
169: return getNode().getChildren();
170: }
171:
172: protected void refreshVisuals() {
173: Iterator it = getFigure().getChildren().iterator();
174: while (it.hasNext()) {
175: Object next = it.next();
176: if (next instanceof Label) {
177: ((Label) next).setText(getData().toString());
178: if (getData() != null) {
179: getFigure()
180: .setSize(
181: (getData().toString().length() * 7) + 10,
182: ((((Label) next).getBounds().height / 2) + 10));
183: getFigure().revalidate();
184: ((GraphEditPart) getParent()).getFigure()
185: .revalidate();
186: }
187: }
188: }
189: }
190:
191: private boolean expanded;
192: private boolean topLevel;
193: private boolean bottomLevel;
194:
195: public void switchToComplex() {
196: GraphPlugin.getDefault().getGenerator().expandGraph(getNode());
197:
198: }
199:
200: /**
201: * @return
202: */
203: public Object getData() {
204: return data;
205: }
206:
207: /**
208: * @param string
209: */
210: public void setData(Object obj) {
211: data = obj;
212: }
213:
214: /**
215: * @return
216: */
217: public boolean isBottomLevel() {
218: return bottomLevel;
219: }
220:
221: /**
222: * @return
223: */
224: public boolean isExpanded() {
225: return expanded;
226: }
227:
228: /**
229: * @return
230: */
231: public boolean isTopLevel() {
232: return topLevel;
233: }
234:
235: /**
236: * @param b
237: */
238: public void setBottomLevel(boolean b) {
239: bottomLevel = b;
240: }
241:
242: /**
243: * @param b
244: */
245: public void setExpanded(boolean b) {
246: expanded = b;
247: }
248:
249: /**
250: * @param b
251: */
252: public void setTopLevel(boolean b) {
253: topLevel = b;
254: }
255:
256: }
|