Method: defaultSClassDefinition(SClassDefinition)

1: /*
2: * #%~
3: * Combinatorial Testing
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.plugins.combinatorialtesting.views;
23:
24: import java.io.IOException;
25: import java.util.HashMap;
26: import java.util.HashSet;
27: import java.util.List;
28: import java.util.Map;
29: import java.util.Set;
30: import java.util.Vector;
31:
32: import org.eclipse.jface.viewers.IStructuredContentProvider;
33: import org.eclipse.jface.viewers.ITreeContentProvider;
34: import org.eclipse.jface.viewers.Viewer;
35: import org.eclipse.ui.part.ViewPart;
36: import org.overture.ast.analysis.AnalysisAdaptor;
37: import org.overture.ast.analysis.AnalysisException;
38: import org.overture.ast.definitions.ANamedTraceDefinition;
39: import org.overture.ast.definitions.PDefinition;
40: import org.overture.ast.definitions.SClassDefinition;
41: import org.overture.ast.modules.AModuleModules;
42: import org.overture.ast.node.INode;
43: import org.overture.ct.utils.TraceHelperNotInitializedException;
44: import org.overture.ide.core.resources.IVdmProject;
45: import org.overture.ide.plugins.combinatorialtesting.views.treeView.ITreeNode;
46: import org.overture.ide.plugins.combinatorialtesting.views.treeView.NotYetReadyTreeNode;
47: import org.overture.ide.plugins.combinatorialtesting.views.treeView.ProjectTreeNode;
48: import org.overture.ide.plugins.combinatorialtesting.views.treeView.TraceTreeNode;
49: import org.overture.ide.plugins.combinatorialtesting.views.treeView.TreeParent;
50: import org.xml.sax.SAXException;
51:
52: public class ViewContentProvider implements IStructuredContentProvider,
53:                 ITreeContentProvider
54: {
55:         private TreeParent invisibleRoot;
56:         ViewPart viewer;
57:         Map<INode, List<TraceTreeNode>> containerNodes = new HashMap<INode, List<TraceTreeNode>>();
58:
59:         public ViewContentProvider(ViewPart p)
60:         {
61:                 // this.traceHelpers = trs;
62:                 viewer = p;
63:         }
64:
65:         public void inputChanged(Viewer v, Object oldInput, Object newInput)
66:         {
67:
68:         }
69:
70:         public void resetCache(IVdmProject project)
71:         {
72:                 Set<INode> containers = TraceAstUtility.getTraceContainers(project);
73:                 for (INode iNode : containers)
74:                 {
75:                         containerNodes.remove(iNode);
76:                 }
77:         }
78:
79:         public void dispose()
80:         {
81:         }
82:
83:         public void addChild(ProjectTreeNode project)
84:         {
85:                 invisibleRoot.addChild(project);
86:         }
87:
88:         public Object[] getElements(Object parent)
89:         {
90:                 if (invisibleRoot == null)
91:                 {
92:                         initialize();
93:                 }
94:                 return getChildren(invisibleRoot);
95:         }
96:
97:         public Object getParent(Object child)
98:         {
99:                 if (child instanceof ITreeNode)
100:                 {
101:                         return ((ITreeNode) child).getParent();
102:                 }
103:                 return null;
104:         }
105:
106:         public Object[] getChildren(Object parent)
107:         {
108:                 if (parent instanceof ProjectTreeNode)
109:                 {
110:                         Set<INode> containers = TraceAstUtility.getTraceContainers(((ProjectTreeNode) parent).project);
111:                         return containers.toArray();
112:                 }
113:                 if (parent instanceof ITreeNode)
114:                 {
115:                         return ((ITreeNode) parent).getChildren().toArray();
116:                 }
117:                 if (parent instanceof SClassDefinition
118:                                 || parent instanceof AModuleModules)
119:                 {
120:                         List<TraceTreeNode> children = new Vector<TraceTreeNode>();
121:
122:                         List<ANamedTraceDefinition> traceDefs = TraceAstUtility.getTraceDefinitions((INode) parent);
123:                         if (containerNodes.containsKey(parent)
124:                                         && containerNodes.get(parent).size() == traceDefs.size())
125:                         {
126:                                 return containerNodes.get(parent).toArray();
127:                         } else
128:                         {
129:
130:                                 for (ANamedTraceDefinition def : traceDefs)
131:                                 {
132:                                         // ITracesHelper tr = traceHelpers.get(TraceAstUtility.getProject(def));
133:                                         try
134:                                         {
135:                                                 children.add(new TraceTreeNode(def));
136:                                         } catch (SAXException e)
137:                                         {
138:                                                 // TODO Auto-generated catch block
139:                                                 e.printStackTrace();
140:                                         } catch (IOException e)
141:                                         {
142:                                                 // TODO Auto-generated catch block
143:                                                 e.printStackTrace();
144:                                         } catch (ClassNotFoundException e)
145:                                         {
146:                                                 // TODO Auto-generated catch block
147:                                                 e.printStackTrace();
148:                                         } catch (TraceHelperNotInitializedException e)
149:                                         {
150:                                                 // TODO Auto-generated catch block
151:                                                 e.printStackTrace();
152:                                         }
153:                                 }
154:                                 containerNodes.put((INode) parent, children);
155:                         }
156:
157:                         return children.toArray();
158:                 }
159:
160:                 return new Object[0];
161:         }
162:
163:         public boolean hasChildren(Object parent)
164:         {
165:                 if (parent instanceof ProjectTreeNode)
166:                 {
167:                         HasTraceAnalysis analysis = new HasTraceAnalysis();
168:                         for (INode node : ((ProjectTreeNode) parent).project.getModel().getRootElementList())
169:                         {
170:                                 if (analysis.hasTrace(node))
171:                                 {
172:                                         return true;
173:                                 }
174:
175:                         }
176:                         return false;
177:
178:                 } else if (parent instanceof NotYetReadyTreeNode)
179:                 {
180:                         return false;
181:                 }
182:                 if (parent instanceof ITreeNode)
183:                 {
184:                         return ((ITreeNode) parent).hasChildren();
185:                 } else if (parent instanceof SClassDefinition
186:                                 || parent instanceof AModuleModules)
187:                 {
188:                         return true;
189:                 }
190:                 return false;
191:         }
192:
193:         static class HasTraceAnalysis extends AnalysisAdaptor
194:         {
195:                 boolean hasTrace = false;
196:
197:                 public boolean hasTrace(INode node)
198:                 {
199:                         hasTrace = false;
200:
201:                         try
202:                         {
203:                                 node.apply(this);
204:                         } catch (Throwable e)
205:                         {
206:                         }
207:
208:                         return hasTrace;
209:                 }
210:
211:                 @Override
212:                 public void defaultSClassDefinition(SClassDefinition node)
213:                                 throws AnalysisException
214:                 {
215:•                        for (PDefinition def : node.getDefinitions())
216:                         {
217:•                                if (def instanceof ANamedTraceDefinition)
218:                                 {
219:                                         hasTrace = true;
220:                                         throw new AnalysisException("stop search");
221:                                 }
222:                         }
223:                 }
224:
225:                 @Override
226:                 public void caseAModuleModules(AModuleModules node)
227:                                 throws AnalysisException
228:                 {
229:                         for (PDefinition def : node.getDefs())
230:                         {
231:                                 if (def instanceof ANamedTraceDefinition)
232:                                 {
233:                                         hasTrace = true;
234:                                         throw new AnalysisException("stop search");
235:                                 }
236:                         }
237:                 }
238:
239:         }
240:
241:         static class TraceContainerSearch extends AnalysisAdaptor
242:         {
243:                 Set<INode> containers = new HashSet<INode>();
244:
245:                 public Set<INode> getTraceContainers(INode node)
246:                 {
247:                         containers.clear();
248:
249:                         try
250:                         {
251:                                 node.apply(this);
252:                         } catch (Throwable e)
253:                         {
254:
255:                         }
256:
257:                         return containers;
258:                 }
259:
260:                 @Override
261:                 public void defaultSClassDefinition(SClassDefinition node)
262:                                 throws AnalysisException
263:                 {
264:                         for (PDefinition def : node.getDefinitions())
265:                         {
266:                                 if (def instanceof ANamedTraceDefinition)
267:                                 {
268:                                         containers.add(node);
269:                                         throw new AnalysisException("stop search");
270:                                 }
271:                         }
272:                 }
273:
274:                 @Override
275:                 public void caseAModuleModules(AModuleModules node)
276:                                 throws AnalysisException
277:                 {
278:                         for (PDefinition def : node.getDefs())
279:                         {
280:                                 if (def instanceof ANamedTraceDefinition)
281:                                 {
282:                                         containers.add(node);
283:                                         throw new AnalysisException("stop search");
284:                                 }
285:                         }
286:                 }
287:
288:         }
289:
290:         static class TraceSearch extends AnalysisAdaptor
291:         {
292:                 List<ANamedTraceDefinition> containers = new Vector<ANamedTraceDefinition>();
293:
294:                 public List<ANamedTraceDefinition> getTraces(INode node)
295:                 {
296:                         containers.clear();
297:
298:                         try
299:                         {
300:                                 node.apply(this);
301:                         } catch (Throwable e)
302:                         {
303:
304:                         }
305:
306:                         return containers;
307:                 }
308:
309:                 @Override
310:                 public void defaultSClassDefinition(SClassDefinition node)
311:                 {
312:                         for (PDefinition def : node.getDefinitions())
313:                         {
314:                                 if (def instanceof ANamedTraceDefinition)
315:                                 {
316:                                         containers.add((ANamedTraceDefinition) def);
317:                                 }
318:                         }
319:                 }
320:
321:                 @Override
322:                 public void caseAModuleModules(AModuleModules node)
323:                 {
324:                         for (PDefinition def : node.getDefs())
325:                         {
326:                                 if (def instanceof ANamedTraceDefinition)
327:                                 {
328:                                         containers.add((ANamedTraceDefinition) def);
329:                                 }
330:                         }
331:                 }
332:
333:         }
334:
335:         /*
336:          * We will set up a dummy model to initialize tree heararchy. In a real code, you will connect to a real model and
337:          * expose its hierarchy.
338:          */
339:         private void initialize()
340:         {
341:                 invisibleRoot = new TreeParent("");
342:
343:                 ProjectTreeNode projectTreeNode;
344:
345:                 for (IVdmProject project : TraceAstUtility.getProjects())
346:                 {
347:                         HasTraceAnalysis analysis = new HasTraceAnalysis();
348:                         for (INode node : project.getModel().getRootElementList())
349:                         {
350:                                 if (analysis.hasTrace(node))
351:                                 {
352:                                         projectTreeNode = new ProjectTreeNode(project);
353:                                         invisibleRoot.addChild(projectTreeNode);
354:                                         break;
355:                                 }
356:                         }
357:                 }
358:
359:         }
360: }