1 | package de.uka.ipd.sdq.sensorframework.visualisation.tabs.sensors; |
2 | |
3 | import java.util.Observable; |
4 | import java.util.Observer; |
5 | |
6 | import org.eclipse.jface.dialogs.Dialog; |
7 | import org.eclipse.jface.viewers.CellEditor; |
8 | import org.eclipse.jface.viewers.DialogCellEditor; |
9 | import org.eclipse.jface.viewers.ISelection; |
10 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
11 | import org.eclipse.jface.viewers.IStructuredSelection; |
12 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
13 | import org.eclipse.jface.viewers.TableViewer; |
14 | import org.eclipse.jface.window.Window; |
15 | import org.eclipse.swt.SWT; |
16 | import org.eclipse.swt.events.SelectionAdapter; |
17 | import org.eclipse.swt.events.SelectionEvent; |
18 | import org.eclipse.swt.layout.FormAttachment; |
19 | import org.eclipse.swt.layout.FormData; |
20 | import org.eclipse.swt.layout.GridData; |
21 | import org.eclipse.swt.widgets.Composite; |
22 | import org.eclipse.swt.widgets.Control; |
23 | import org.eclipse.swt.widgets.Table; |
24 | import org.eclipse.swt.widgets.TableColumn; |
25 | import org.eclipse.swt.widgets.ToolBar; |
26 | import org.eclipse.swt.widgets.ToolItem; |
27 | import org.eclipse.ui.IWorkbenchPart; |
28 | import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection; |
29 | import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage; |
30 | |
31 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
32 | import de.uka.ipd.sdq.sensorframework.visualisation.VisualisationImages; |
33 | import de.uka.ipd.sdq.sensorframework.visualisation.VisualisationPlugin; |
34 | import de.uka.ipd.sdq.sensorframework.visualisation.dialogs.ExperimentRunsDialog; |
35 | import de.uka.ipd.sdq.sensorframework.visualisation.dialogs.SensorsDialog; |
36 | import de.uka.ipd.sdq.sensorframework.visualisation.editor.AbstractReportView; |
37 | import de.uka.ipd.sdq.sensorframework.visualisation.editor.ConfigEditorInput; |
38 | import de.uka.ipd.sdq.sensorframework.visualisation.editor.ConfigEntry; |
39 | import de.uka.ipd.sdq.sensorframework.visualisation.views.TreeObject; |
40 | |
41 | /** |
42 | * @author Roman Andrej |
43 | */ |
44 | public class SensorsPropertySection extends AbstractPropertySection implements |
45 | Observer { |
46 | |
47 | /* (non-Javadoc) |
48 | * @see java.util.Observer#update(java.util.Observable, java.lang.Object) |
49 | */ |
50 | public void update(Observable subject, Object signal) { |
51 | if (subject instanceof ConfigEditorInput) |
52 | refresh(); |
53 | } |
54 | |
55 | private ConfigEditorInput configObject; |
56 | private ConfigEntry selectedEntry; |
57 | private TableViewer viewer; |
58 | private ToolItem deleteRunItem; |
59 | |
60 | public static final int ICON_COLUMN_INDEX = 0; |
61 | public static final int CONTEXT_COLUMN_INDEX = 1; |
62 | public static final int RUN_COLUMN_INDEX = 2; |
63 | public static final int SENSORS_COLUMN_INDEX = 3; |
64 | |
65 | /** |
66 | * Columns of a table, which is used into ParameterEditDialog |
67 | */ |
68 | public final static String ICON_COLUMN = ""; |
69 | public final static String CONTEXT_COLUMN = "Context"; |
70 | public final static String RUN_COLUMN = "Experiment Datetime"; |
71 | public final static String SENSORS_COLUMN = "Sensors"; |
72 | |
73 | /** ToolBar width. */ |
74 | private final int toolbarWidth = 24; |
75 | |
76 | // Set column names of Tabele |
77 | protected static String[] columnNames = new String[] { ICON_COLUMN, |
78 | CONTEXT_COLUMN, RUN_COLUMN, SENSORS_COLUMN }; |
79 | |
80 | // style the style of table to construct |
81 | int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL |
82 | | SWT.FULL_SELECTION | SWT.HIDE_SELECTION; |
83 | |
84 | /* (non-Javadoc) |
85 | * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, |
86 | * org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage) |
87 | */ |
88 | @Override |
89 | public void createControls(Composite parent, |
90 | TabbedPropertySheetPage aTabbedPropertySheetPage) { |
91 | super.createControls(parent, aTabbedPropertySheetPage); |
92 | |
93 | Composite composite = getWidgetFactory() |
94 | .createFlatFormComposite(parent); |
95 | composite.getParent().setLayoutData( |
96 | new GridData(SWT.FILL, SWT.FILL, true, true)); |
97 | |
98 | /** |
99 | * Create the cell editors for Name, Type column |
100 | */ |
101 | CellEditor[] editors = new CellEditor[columnNames.length]; |
102 | |
103 | Table table = new Table(composite, style); |
104 | final FormData fd_table = new FormData(); |
105 | fd_table.bottom = new FormAttachment(100, 0); |
106 | fd_table.left = new FormAttachment(0, 0); |
107 | fd_table.top = new FormAttachment(0, 0); |
108 | table.setLayoutData(fd_table); |
109 | table.setLinesVisible(true); |
110 | table.setHeaderVisible(true); |
111 | |
112 | viewer = new TableViewer(table); |
113 | |
114 | viewer.setColumnProperties(columnNames); |
115 | viewer.setContentProvider(new SensorsTabContentProvider()); |
116 | viewer.setLabelProvider(new SensorsTabLabelProvider()); |
117 | viewer.setCellModifier(new SensorsTabCellModifier()); |
118 | viewer.addSelectionChangedListener(new ISelectionChangedListener() { |
119 | public void selectionChanged(SelectionChangedEvent event) { |
120 | Object object = ((IStructuredSelection) viewer.getSelection()) |
121 | .getFirstElement(); |
122 | selectedEntry = (ConfigEntry) object; |
123 | |
124 | if (selectedEntry != null) |
125 | deleteRunItem.setEnabled(true); |
126 | else deleteRunItem.setEnabled(false); |
127 | } |
128 | }); |
129 | editors[SENSORS_COLUMN_INDEX] = new DialogCellEditor(table) { |
130 | |
131 | @Override |
132 | protected Object openDialogBox(Control cellEditorWindow) { |
133 | SensorsDialog dialog = new SensorsDialog(cellEditorWindow |
134 | .getShell(), selectedEntry); |
135 | |
136 | if (dialog.open() == Dialog.OK) |
137 | viewer.refresh(); |
138 | return null; |
139 | } |
140 | }; |
141 | // Assign the cell editors to the viewer |
142 | viewer.setCellEditors(editors); |
143 | |
144 | // Definere the table columns |
145 | final TableColumn zeroColumn = new TableColumn(table, SWT.NONE); |
146 | zeroColumn.setResizable(false); |
147 | zeroColumn.setWidth(30); |
148 | |
149 | final TableColumn contextColumn = new TableColumn(table, SWT.NONE); |
150 | contextColumn.setWidth(100); |
151 | contextColumn.setText(CONTEXT_COLUMN); |
152 | |
153 | final TableColumn runColumn = new TableColumn(table, SWT.NONE); |
154 | runColumn.setWidth(140); |
155 | runColumn.setText(RUN_COLUMN); |
156 | |
157 | final TableColumn sensorsColumn = new TableColumn(table, SWT.NONE); |
158 | sensorsColumn.setWidth(200); |
159 | sensorsColumn.setText(SENSORS_COLUMN); |
160 | |
161 | final ToolBar toolBar = new ToolBar(composite, SWT.VERTICAL); |
162 | fd_table.right = new FormAttachment(toolBar, 0, SWT.LEFT); |
163 | final FormData fd_toolBar = new FormData(); |
164 | fd_toolBar.left = new FormAttachment(100, - toolbarWidth); |
165 | fd_toolBar.bottom = new FormAttachment(100, 0); |
166 | fd_toolBar.right = new FormAttachment(100, 0); |
167 | fd_toolBar.top = new FormAttachment(0, 0); |
168 | toolBar.setLayoutData(fd_toolBar); |
169 | |
170 | ToolItem addRunItem = new ToolItem(toolBar, SWT.PUSH); |
171 | addRunItem.setImage(VisualisationImages.imageRegistry.get(VisualisationImages.ADD)); |
172 | addRunItem.addSelectionListener(new SelectionAdapter() { |
173 | |
174 | /* (non-Javadoc) |
175 | * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
176 | */ |
177 | @Override |
178 | public void widgetSelected(SelectionEvent e) { |
179 | ExperimentRunsDialog dialog = new ExperimentRunsDialog( |
180 | e.display.getActiveShell()); |
181 | if (dialog.open() == Window.OK && dialog.getResult() != null) { |
182 | TreeObject object = dialog.getResult(); |
183 | ConfigEntry configEntry = new ConfigEntry( |
184 | object.getDatasource(), |
185 | (ExperimentRun) object.getObject(), object |
186 | .getExperiment(), null); |
187 | configObject.addConfigEntry(configEntry); |
188 | viewer.refresh(); |
189 | } |
190 | } |
191 | }); |
192 | |
193 | deleteRunItem = new ToolItem(toolBar, SWT.PUSH); |
194 | deleteRunItem.setImage(VisualisationImages.imageRegistry.get(VisualisationImages.DELETE)); |
195 | deleteRunItem.addSelectionListener(new SelectionAdapter(){ |
196 | |
197 | /* (non-Javadoc) |
198 | * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
199 | */ |
200 | @Override |
201 | public void widgetSelected(SelectionEvent e) { |
202 | configObject.removeConfigEntry(selectedEntry); |
203 | } |
204 | |
205 | }); |
206 | deleteRunItem.setEnabled(false); |
207 | |
208 | /** set Observer to the ConfigObject */ |
209 | AbstractReportView view = (AbstractReportView) VisualisationPlugin.getDefault() |
210 | .getWorkbench().getActiveWorkbenchWindow().getActivePage() |
211 | .getActiveEditor(); |
212 | configObject = (ConfigEditorInput) view.getEditorInput(); |
213 | configObject.addObserver(this); |
214 | } |
215 | |
216 | /* (non-Javadoc) |
217 | * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#getSelection() |
218 | */ |
219 | @Override |
220 | public ISelection getSelection() { |
221 | // TODO Auto-generated method stub |
222 | return super.getSelection(); |
223 | } |
224 | |
225 | /* (non-Javadoc) |
226 | * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#refresh() |
227 | */ |
228 | @Override |
229 | public void refresh() { |
230 | viewer.refresh(); |
231 | } |
232 | |
233 | /* (non-Javadoc) |
234 | * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#setInput(org.eclipse.ui.IWorkbenchPart, |
235 | * org.eclipse.jface.viewers.ISelection) |
236 | */ |
237 | @Override |
238 | public void setInput(IWorkbenchPart part, ISelection selection) { |
239 | super.setInput(part, selection); |
240 | |
241 | if (part instanceof AbstractReportView) { |
242 | AbstractReportView view = (AbstractReportView) part; |
243 | configObject = (ConfigEditorInput) view.getEditorInput(); |
244 | viewer.setInput(configObject); |
245 | } |
246 | } |
247 | } |