1 | package de.uka.ipd.sdq.sensorframework.visualisation.views; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | import java.util.List; |
6 | |
7 | import org.eclipse.jface.viewers.ITreeContentProvider; |
8 | import org.eclipse.jface.viewers.Viewer; |
9 | |
10 | import de.uka.ipd.sdq.sensorframework.SensorFrameworkDataset; |
11 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
12 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
13 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
14 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
15 | import de.uka.ipd.sdq.sensorframework.entities.dao.IExperimentDAO; |
16 | |
17 | /** |
18 | * TODO |
19 | * @author admin |
20 | */ |
21 | public class TreeContentProvider implements ITreeContentProvider { |
22 | |
23 | protected static final int EXPERIMENT_RUNS = 0; |
24 | protected static final int SENSORS = 1; |
25 | |
26 | private List<IDAOFactory> rootEntry; |
27 | |
28 | /* (non-Javadoc) |
29 | * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) |
30 | */ |
31 | public Object[] getElements(Object parent) { |
32 | rootEntry = new ArrayList<IDAOFactory>(); |
33 | rootEntry.addAll(SensorFrameworkDataset.singleton().getDataSources()); |
34 | return getChildren(rootEntry); |
35 | } |
36 | |
37 | /* (non-Javadoc) |
38 | * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) |
39 | */ |
40 | @SuppressWarnings("unchecked") |
41 | public Object[] getChildren(Object parent) { |
42 | |
43 | /** List of root elements (DAO,...) */ |
44 | if (parent instanceof ArrayList) |
45 | return ((ArrayList<IDAOFactory>) parent).toArray(); |
46 | |
47 | /** ExperimentDAO */ |
48 | if (parent instanceof IDAOFactory){ |
49 | IExperimentDAO experimentDAO = ((IDAOFactory)parent).createExperimentDAO(); |
50 | return createExperimentAndDAOList((IDAOFactory)parent,experimentDAO.getExperiments()).toArray(); |
51 | } |
52 | |
53 | /** Experiment */ |
54 | if (parent instanceof ExperimentAndDAO) { |
55 | ExperimentAndDAO experiment = (ExperimentAndDAO) parent; |
56 | Object[] objects = { |
57 | new TreeContainer(experiment.getDatasource(), experiment.getExperiment(), EXPERIMENT_RUNS), |
58 | new TreeContainer(experiment.getDatasource(), experiment.getExperiment(), SENSORS) }; |
59 | return objects; |
60 | } |
61 | |
62 | /** TreeObject - container of Sensor or ExperimentRun */ |
63 | if (parent instanceof TreeObject) { |
64 | TreeObject treeObject = (TreeObject) parent; |
65 | if (treeObject.getObject() instanceof ExperimentRun) { |
66 | ExperimentRun run = (ExperimentRun) treeObject.getObject(); |
67 | Experiment experiment = treeObject.getExperiment(); |
68 | Collection<Sensor> sensors = experiment.getSensors(); |
69 | |
70 | TreeObject[] objects = new TreeObject[sensors.size()]; |
71 | |
72 | int i = 0; |
73 | for (Sensor sensor : sensors) { |
74 | objects[i++] = |
75 | new TreeObject( |
76 | sensor, treeObject.getDatasource(), experiment, |
77 | run); |
78 | } |
79 | return objects; |
80 | |
81 | } |
82 | } |
83 | |
84 | /** TreeContainer - container a collection of TreeObject */ |
85 | if (parent instanceof TreeContainer) |
86 | return ((TreeContainer) parent).getElements().toArray(); |
87 | |
88 | return new Object[0]; |
89 | } |
90 | |
91 | private List<ExperimentAndDAO> createExperimentAndDAOList( |
92 | IDAOFactory dao, Collection<Experiment> experiments){ |
93 | ArrayList<ExperimentAndDAO>result = new ArrayList<ExperimentAndDAO>(); |
94 | for(Experiment e:experiments) |
95 | result.add(new ExperimentAndDAO(dao,e)); |
96 | return result; |
97 | } |
98 | |
99 | /* (non-Javadoc) |
100 | * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) |
101 | */ |
102 | public boolean hasChildren(Object parent) { |
103 | if (parent instanceof TreeObject){ |
104 | TreeObject object = (TreeObject) parent; |
105 | if (object.getObject() instanceof Sensor) |
106 | return false; |
107 | } |
108 | |
109 | if (parent instanceof IDAOFactory) { |
110 | IExperimentDAO experimentDAO = ((IDAOFactory)parent).createExperimentDAO(); |
111 | return !experimentDAO.getExperiments().isEmpty(); |
112 | } |
113 | return true; |
114 | } |
115 | |
116 | /* (non-Javadoc) |
117 | * @see org.eclipse.jface.viewers.IContentProvider#dispose() |
118 | */ |
119 | public void dispose() { |
120 | if (rootEntry != null) |
121 | rootEntry.clear(); |
122 | } |
123 | |
124 | /* (non-Javadoc) |
125 | * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) |
126 | */ |
127 | public Object getParent(Object child) { |
128 | // TODO Auto-generated method stub |
129 | return null; |
130 | } |
131 | |
132 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
133 | // TODO Auto-generated method stub |
134 | } |
135 | } |