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.testing;
021:
022: import org.eclipse.ui.*;
023: import org.eclipse.jface.action.*;
024: import org.eclipse.jface.viewers.*;
025: import ca.mcgill.sable.graph.*;
026: import ca.mcgill.sable.graph.model.*;
027: import org.eclipse.core.runtime.*;
028: import java.util.*;
029: import java.lang.reflect.*;
030:
031: public class GraphGenerator implements IWorkbenchWindowActionDelegate {
032:
033: private ArrayList children = new ArrayList();
034: private Graph graph;
035:
036: public GraphGenerator() {
037: }
038:
039: public void run(IAction action) {
040: IWorkbenchPage page = GraphPlugin.getDefault().getWorkbench()
041: .getActiveWorkbenchWindow().getActivePage();
042: try {
043: setGraph(new Graph());
044: IEditorPart part = page.openEditor(graph,
045: "ca.mcgill.sable.graph.GraphEditor");
046: handleChildren();
047: } catch (PartInitException e3) {
048: e3.printStackTrace();
049: } catch (Exception e2) {
050: e2.printStackTrace();
051: }
052: }
053:
054: public void buildModel() {
055:
056: GraphPlugin.getDefault().setGenerator(this );
057: TestNode p1 = new TestNode();
058: p1.setData("P1");
059:
060: TestNode p2 = new TestNode();
061: p2.setData("P2");
062:
063: p1.addOutput(p2);
064:
065: TestNode c1 = new TestNode();
066: TestNode c2 = new TestNode();
067: TestNode c3 = new TestNode();
068:
069: p1.addChild(c1);
070: p1.addChild(c2);
071: p1.addChild(c3);
072:
073: c1.addOutput(c2);
074: c1.addOutput(c3);
075: c1.addOutput(p2);
076: c3.addOutput(p2);
077:
078: c1.setData("C1");
079: c2.setData("C2");
080: c3.setData("C3");
081:
082: TestNode c4 = new TestNode();
083: TestNode c5 = new TestNode();
084: p2.addChild(c4);
085: p2.addChild(c5);
086:
087: c4.addOutput(c5);
088: c1.addOutput(c4);
089: c3.addOutput(c4);
090: p1.addOutput(c4);
091:
092: c4.setData("C4");
093: c5.setData("C5");
094:
095: getChildren().add(p1);
096: getChildren().add(p2);
097:
098: handleChildren();
099: }
100:
101: HashMap map;
102:
103: private void handleChildren() {
104: getGraph().removeAllChildren();
105: map = new HashMap();
106: Iterator it = getChildren().iterator();
107: while (it.hasNext()) {
108: TestNode tn = (TestNode) it.next();
109:
110: SimpleNode sn = new SimpleNode();
111: getGraph().addChild(sn);
112: sn.setData(tn.getData());
113: sn.setChildren(tn.getChildren());
114:
115: map.put(tn, sn);
116: }
117:
118: Iterator it2 = getChildren().iterator();
119: while (it2.hasNext()) {
120: TestNode tn = (TestNode) it2.next();
121:
122: SimpleNode src = (SimpleNode) map.get(tn);
123: Iterator eIt = tn.getOutputs().iterator();
124:
125: while (eIt.hasNext()) {
126: Object endTn = eIt.next();
127: SimpleNode tgt = (SimpleNode) map.get(endTn);
128: if (tgt != null) {
129: Edge e = new Edge(src, tgt);
130: }
131: }
132: }
133: }
134:
135: public void expandGraph(SimpleNode node) {
136: Iterator it = getChildren().iterator();
137: TestNode tn = null;
138: while (it.hasNext()) {
139: tn = (TestNode) it.next();
140: if (map.get(tn).equals(node))
141: break;
142: }
143: if (tn != null) {
144: if (tn.getChildren().size() > 0) {
145: getChildren().remove(tn);
146: }
147: }
148: Iterator childIt = node.getChildren().iterator();
149: while (childIt.hasNext()) {
150: TestNode test = (TestNode) childIt.next();
151: getChildren().add(test);
152: }
153: handleChildren();
154: }
155:
156: public void dispose() {
157: }
158:
159: public void selectionChanged(IAction action, ISelection sel) {
160: }
161:
162: public void init(IWorkbenchWindow window) {
163: }
164:
165: /**
166: * @return
167: */
168: public ArrayList getChildren() {
169: return children;
170: }
171:
172: /**
173: * @param list
174: */
175: public void setChildren(ArrayList list) {
176: children = list;
177: }
178:
179: /**
180: * @return
181: */
182: public Graph getGraph() {
183: return graph;
184: }
185:
186: /**
187: * @param graph
188: */
189: public void setGraph(Graph graph) {
190: this.graph = graph;
191: }
192:
193: }
|