1 | package de.uka.ipd.sdq.sensorframework.visualisation.editor; |
2 | |
3 | import java.util.ArrayList; |
4 | |
5 | import org.eclipse.core.runtime.IConfigurationElement; |
6 | import org.eclipse.core.runtime.Platform; |
7 | import org.eclipse.jface.dialogs.MessageDialog; |
8 | import org.eclipse.jface.viewers.LabelProvider; |
9 | import org.eclipse.swt.widgets.Shell; |
10 | |
11 | import de.uka.ipd.sdq.sensorframework.adapter.AdapterRegistry; |
12 | import de.uka.ipd.sdq.sensorframework.adapter.IAdapterFactory; |
13 | import de.uka.ipd.sdq.sensorframework.entities.SensorAndMeasurements; |
14 | import de.uka.ipd.sdq.sensorframework.visualisation.VisualisationPlugin; |
15 | import de.uka.ipd.sdq.sensorframework.visualisation.dialogs.ActionListSelectionDialog; |
16 | import de.uka.ipd.sdq.sensorframework.visualisation.dialogs.ViewAndAdapterFactory; |
17 | |
18 | /** |
19 | * This class offers the methods, which it for validating possible a |
20 | * Sensor->View makes. Sensor->View - that only Views for sensors can be |
21 | * selected that can represent this sensor |
22 | * |
23 | * @author Roman Andrej |
24 | * |
25 | */ |
26 | public class SensorValidationToView { |
27 | |
28 | /** |
29 | * @return - all view, which can represent the sensor |
30 | */ |
31 | public static Object[] findViews(SensorAndMeasurements sensorAndMeasurements) { |
32 | ArrayList<ViewAndAdapterFactory> result = new ArrayList<ViewAndAdapterFactory>(); |
33 | IConfigurationElement[] elements = getConfigurationElements(); |
34 | |
35 | for (IConfigurationElement element : elements) { // Iterate over all registered views |
36 | String executableObject = element.getAttribute("acceptsData"); |
37 | try { |
38 | Class<?> viewerAcceptsClass = Class.forName(executableObject); |
39 | if (AdapterRegistry.singleton().canAdapt(sensorAndMeasurements,viewerAcceptsClass)) { |
40 | // there is at least one adapter which makes the combination work |
41 | for (IAdapterFactory f : AdapterRegistry.singleton().getAllAvailableFactories(sensorAndMeasurements, viewerAcceptsClass)) { |
42 | result.add(new ViewAndAdapterFactory(element,f)); |
43 | } |
44 | } |
45 | } catch (ClassNotFoundException e) { |
46 | // catch exception for TimeSpanSensor |
47 | } catch (Exception e) { |
48 | e.printStackTrace(); |
49 | } |
50 | } |
51 | return result.toArray(); |
52 | } |
53 | |
54 | public static boolean canViewSensor( |
55 | SensorAndMeasurements sensorAndMeasurements) { |
56 | |
57 | String activeEditorId = VisualisationPlugin.getDefault().getWorkbench() |
58 | .getActiveWorkbenchWindow().getActivePage().getActiveEditor() |
59 | .getSite().getId(); |
60 | Object[] views = findViews(sensorAndMeasurements); |
61 | |
62 | for (int i = 0; i < views.length; i++) { |
63 | ViewAndAdapterFactory viewer = (ViewAndAdapterFactory) views[i]; |
64 | String editorId = viewer.getView().getAttribute("editorID"); |
65 | if (activeEditorId.equals(editorId)) |
66 | return true; |
67 | } |
68 | return false; |
69 | } |
70 | |
71 | /** |
72 | * @return - actions, which are present in Menu/Visualization |
73 | */ |
74 | public static IConfigurationElement[] getConfigurationElements() { |
75 | IConfigurationElement[] configurationElements = Platform |
76 | .getExtensionRegistry().getConfigurationElementsFor( |
77 | "de.uka.ipd.sdq.sensorframework.visualisation"); |
78 | return configurationElements; |
79 | } |
80 | |
81 | /** |
82 | * show message use the MessageDialog. DialogTitle is a curent name of |
83 | * active editor |
84 | */ |
85 | public static void showMessage(Shell shell) { |
86 | String msg = "This View do not support the representation of the selected sensor!"; |
87 | |
88 | String editorName = VisualisationPlugin.getDefault().getWorkbench() |
89 | .getActiveWorkbenchWindow().getActivePage().getActiveEditor() |
90 | .getTitle(); |
91 | |
92 | MessageDialog.openInformation(shell, editorName, msg); |
93 | } |
94 | |
95 | /** |
96 | * The method get a selected action from 'ActionListSelectionDialog'. |
97 | * |
98 | * @return - choose Action from ActionListSelectionDialog |
99 | */ |
100 | public static ViewAndAdapterFactory getSelectedAction(Shell shell, |
101 | Object[] elements) { |
102 | ActionListSelectionDialog dialog = new ActionListSelectionDialog(shell, |
103 | new DialogLabelProvider()); |
104 | |
105 | dialog.setElements(elements); |
106 | dialog.open(); |
107 | Object[] results = dialog.getResult(); |
108 | if (results != null) |
109 | return (ViewAndAdapterFactory) results[0]; |
110 | else |
111 | return null; |
112 | } |
113 | } |
114 | |
115 | /** The Class define the LabelProvider for ActionListSelectionDialog. */ |
116 | class DialogLabelProvider extends LabelProvider { |
117 | |
118 | /* (non-Javadoc) |
119 | * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object) |
120 | */ |
121 | @Override |
122 | public String getText(Object element) { |
123 | if (element instanceof ViewAndAdapterFactory) { |
124 | ViewAndAdapterFactory viewAndAdapter = (ViewAndAdapterFactory) element; |
125 | String displayName = viewAndAdapter.getView().getAttribute("displayName"); |
126 | return displayName.replace("{0}",viewAndAdapter.getFactory() == null ? "" : viewAndAdapter.getFactory().getMetricLabel()); |
127 | } |
128 | return super.getText(element); |
129 | } |
130 | } |