| 1 | package de.uka.ipd.sdq.sensorframework.visualisation.views; |
| 2 | |
| 3 | import java.io.BufferedWriter; |
| 4 | import java.io.FileWriter; |
| 5 | import java.io.File; |
| 6 | import java.util.Collection; |
| 7 | import java.util.Iterator; |
| 8 | import java.util.logging.Level; |
| 9 | import java.util.logging.Logger; |
| 10 | |
| 11 | import org.eclipse.jface.action.IMenuListener; |
| 12 | import org.eclipse.jface.action.IMenuManager; |
| 13 | import org.eclipse.jface.action.IToolBarManager; |
| 14 | import org.eclipse.jface.action.MenuManager; |
| 15 | import org.eclipse.jface.action.Separator; |
| 16 | import org.eclipse.jface.dialogs.MessageDialog; |
| 17 | import org.eclipse.jface.preference.IPreferenceNode; |
| 18 | import org.eclipse.jface.preference.IPreferencePage; |
| 19 | import org.eclipse.jface.preference.PreferenceDialog; |
| 20 | import org.eclipse.jface.preference.PreferenceManager; |
| 21 | import org.eclipse.jface.preference.PreferenceNode; |
| 22 | import org.eclipse.jface.util.LocalSelectionTransfer; |
| 23 | import org.eclipse.jface.viewers.ISelection; |
| 24 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 25 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 26 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 27 | import org.eclipse.jface.viewers.TreeViewer; |
| 28 | import org.eclipse.jface.viewers.ViewerSorter; |
| 29 | import org.eclipse.jface.wizard.WizardDialog; |
| 30 | import org.eclipse.jface.action.Action; |
| 31 | |
| 32 | import org.eclipse.swt.SWT; |
| 33 | import org.eclipse.swt.dnd.DND; |
| 34 | import org.eclipse.swt.dnd.Transfer; |
| 35 | import org.eclipse.swt.widgets.Composite; |
| 36 | import org.eclipse.swt.widgets.Menu; |
| 37 | |
| 38 | import org.eclipse.ui.PlatformUI; |
| 39 | import org.eclipse.ui.IActionBars; |
| 40 | import org.eclipse.ui.IWorkbenchActionConstants; |
| 41 | import org.eclipse.ui.part.EditorInputTransfer; |
| 42 | import org.eclipse.ui.part.ViewPart; |
| 43 | |
| 44 | import de.uka.ipd.sdq.sensorframework.SensorFrameworkDataset; |
| 45 | import de.uka.ipd.sdq.sensorframework.dao.file.FileDAOFactory; |
| 46 | import de.uka.ipd.sdq.sensorframework.dialogs.dataset.AddNewDatasourceWizard; |
| 47 | import de.uka.ipd.sdq.sensorframework.dialogs.dataset.ConfigureDatasourceDialog; |
| 48 | import de.uka.ipd.sdq.sensorframework.dialogs.dataset.OpenDatasourceWizard; |
| 49 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
| 50 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
| 51 | import de.uka.ipd.sdq.sensorframework.entities.Measurement; |
| 52 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
| 53 | import de.uka.ipd.sdq.sensorframework.entities.SensorAndMeasurements; |
| 54 | import de.uka.ipd.sdq.sensorframework.entities.StateMeasurement; |
| 55 | import de.uka.ipd.sdq.sensorframework.entities.TimeSpanMeasurement; |
| 56 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
| 57 | import de.uka.ipd.sdq.sensorframework.visualisation.VisualisationPlugin; |
| 58 | import de.uka.ipd.sdq.sensorframework.visualisation.dialogs.CSVSettingsDialog; |
| 59 | import de.uka.ipd.sdq.sensorframework.visualisation.dialogs.DialogType; |
| 60 | |
| 61 | /** |
| 62 | * The view shows data obtained from the 'SensorFactory' model. The view is connected to the model |
| 63 | * using a content provider. |
| 64 | * |
| 65 | * @author Roman Andrej |
| 66 | */ |
| 67 | public class ExperimentsView extends ViewPart { |
| 68 | private TreeViewer viewer; |
| 69 | |
| 70 | /** Define elements, which can be deleted. */ |
| 71 | private IDAOFactory selectedFactory = null; |
| 72 | private Experiment selectedExperiment = null; |
| 73 | |
| 74 | /** Define the actions for menu manager. */ |
| 75 | private Action reloadView; |
| 76 | private Action saveAsCSV; |
| 77 | private Action collapseAll; |
| 78 | private Action expandAll; |
| 79 | private Action newDataSet; |
| 80 | private Action openDataSet; |
| 81 | private Action deleteDataSet; |
| 82 | private Action properties; |
| 83 | |
| 84 | private static Logger logger = Logger |
| 85 | .getLogger("de.uka.ipd.sdq.sensorframework.visualisation.views.ExperimentsView.log"); |
| 86 | |
| 87 | public ExperimentsView() { |
| 88 | } |
| 89 | |
| 90 | class NameSorter extends ViewerSorter { |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * (non-Javadoc) |
| 95 | * |
| 96 | * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets .Composite) |
| 97 | */ |
| 98 | @Override |
| 99 | public void createPartControl(Composite parent) { |
| 100 | int ops = DND.DROP_COPY | DND.DROP_MOVE; |
| 101 | |
| 102 | viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); |
| 103 | viewer.setContentProvider(new TreeContentProvider()); |
| 104 | viewer.setLabelProvider(new TreeLabelProvider()); |
| 105 | viewer.setSorter(new NameSorter()); |
| 106 | viewer.setInput(getViewSite()); |
| 107 | |
| 108 | Transfer[] transfers = new Transfer[] { EditorInputTransfer.getInstance(), |
| 109 | LocalSelectionTransfer.getTransfer() }; |
| 110 | viewer.addDragSupport(ops, transfers, new TreeDragSourceListener(viewer)); |
| 111 | viewer.addDoubleClickListener(new DoubleClickListener()); |
| 112 | viewer.addSelectionChangedListener(new ISelectionChangedListener() { |
| 113 | /* |
| 114 | * (non-Javadoc) |
| 115 | * |
| 116 | * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged |
| 117 | * (org.eclipse.jface.viewers.SelectionChangedEvent) |
| 118 | */ |
| 119 | public void selectionChanged(SelectionChangedEvent event) { |
| 120 | IStructuredSelection selection = (IStructuredSelection) event.getSelection(); |
| 121 | Object selectedObject = selection.getFirstElement(); |
| 122 | setSelectedElement(selectedObject); |
| 123 | } |
| 124 | }); |
| 125 | |
| 126 | makeActions(); |
| 127 | hookContextMenu(); |
| 128 | contributeToActionBars(); |
| 129 | } |
| 130 | |
| 131 | private void hookContextMenu() { |
| 132 | MenuManager menuMgr = new MenuManager("#PopupMenu"); |
| 133 | menuMgr.setRemoveAllWhenShown(true); |
| 134 | menuMgr.addMenuListener(new IMenuListener() { |
| 135 | public void menuAboutToShow(IMenuManager manager) { |
| 136 | ExperimentsView.this.fillContextMenu(manager); |
| 137 | } |
| 138 | }); |
| 139 | Menu menu = menuMgr.createContextMenu(viewer.getControl()); |
| 140 | viewer.getControl().setMenu(menu); |
| 141 | getSite().registerContextMenu(menuMgr, viewer); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Contributes actions to the action bars. |
| 146 | */ |
| 147 | private void contributeToActionBars() { |
| 148 | IActionBars bars = getViewSite().getActionBars(); |
| 149 | fillLocalPullDown(bars.getMenuManager()); |
| 150 | fillLocalToolBar(bars.getToolBarManager()); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Fills the action bar menu with the list of resolvers to be selected |
| 155 | * |
| 156 | * @param manager |
| 157 | * - The menu manager to add the actions to. |
| 158 | */ |
| 159 | private void fillLocalPullDown(IMenuManager manager) { |
| 160 | manager.add(newDataSet); |
| 161 | manager.add(openDataSet); |
| 162 | manager.add(deleteDataSet); |
| 163 | manager.add(new Separator()); |
| 164 | manager.add(reloadView); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Fill context for the tree view pop-up menu. |
| 169 | * |
| 170 | * @param menu |
| 171 | * - the menu manager. |
| 172 | */ |
| 173 | private void fillContextMenu(IMenuManager manager) { |
| 174 | manager.add(deleteDataSet); |
| 175 | manager.add(new Separator()); |
| 176 | manager.add(reloadView); |
| 177 | manager.add(new Separator()); |
| 178 | manager.add(properties); |
| 179 | manager.add(new Separator()); |
| 180 | manager.add(saveAsCSV); |
| 181 | // Other plug-ins can contribute there actions here |
| 182 | manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); |
| 183 | |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Adds actions to the local toolbar. |
| 188 | * |
| 189 | * @param manager |
| 190 | * - the local toolbar manager. |
| 191 | */ |
| 192 | private void fillLocalToolBar(IToolBarManager manager) { |
| 193 | manager.add(newDataSet); |
| 194 | manager.add(openDataSet); |
| 195 | manager.add(deleteDataSet); |
| 196 | manager.add(new Separator()); |
| 197 | manager.add(reloadView); |
| 198 | manager.add(new Separator()); |
| 199 | manager.add(collapseAll); |
| 200 | manager.add(expandAll); |
| 201 | manager.add(new Separator()); |
| 202 | } |
| 203 | |
| 204 | private void makeActions() { |
| 205 | |
| 206 | /** Reload viewer action */ |
| 207 | reloadView = new Action() { |
| 208 | @Override |
| 209 | public void run() { |
| 210 | try { |
| 211 | SensorFrameworkDataset.singleton().reload(); |
| 212 | } catch (Exception ex) { |
| 213 | MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow() |
| 214 | .getShell(), "Reloading the Sensor Dataset Failed", |
| 215 | "Reloading the Sensor Dataset Failed. Error Message given: " |
| 216 | + ex.getMessage()); |
| 217 | } |
| 218 | viewer.refresh(); |
| 219 | } |
| 220 | }; |
| 221 | reloadView.setText("Reload View"); |
| 222 | reloadView.setToolTipText("Reload View"); |
| 223 | reloadView.setImageDescriptor(VisualisationPlugin |
| 224 | .getImageDescriptor("/icons/db_reload_obj.gif")); |
| 225 | |
| 226 | /** Collapse all action */ |
| 227 | collapseAll = new Action() { |
| 228 | @Override |
| 229 | public void run() { |
| 230 | viewer.collapseAll(); |
| 231 | } |
| 232 | }; |
| 233 | collapseAll.setText("Collapse All"); |
| 234 | collapseAll.setToolTipText("Collapse All"); |
| 235 | collapseAll.setImageDescriptor(VisualisationPlugin |
| 236 | .getImageDescriptor("/icons/collapseall.gif")); |
| 237 | |
| 238 | /** Expand all action */ |
| 239 | expandAll = new Action() { |
| 240 | @Override |
| 241 | public void run() { |
| 242 | viewer.expandAll(); |
| 243 | } |
| 244 | }; |
| 245 | expandAll.setText("Expand All"); |
| 246 | expandAll.setToolTipText("Expand All"); |
| 247 | expandAll |
| 248 | .setImageDescriptor(VisualisationPlugin.getImageDescriptor("/icons/expandall.gif")); |
| 249 | |
| 250 | /** New DataSet action. */ |
| 251 | newDataSet = new Action() { |
| 252 | @Override |
| 253 | public void run() { |
| 254 | AddNewDatasourceWizard wizard = new AddNewDatasourceWizard(); |
| 255 | // Instantiates the wizard container with the wizard and opens |
| 256 | // it |
| 257 | WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard); |
| 258 | dialog.create(); |
| 259 | dialog.setTitle(ConfigureDatasourceDialog.ADD_WIZARD_TITLE); |
| 260 | dialog.open(); |
| 261 | viewer.refresh(); |
| 262 | } |
| 263 | }; |
| 264 | newDataSet.setText("New Data Source"); |
| 265 | newDataSet.setToolTipText("New Data Source"); |
| 266 | newDataSet.setImageDescriptor(VisualisationPlugin.getImageDescriptor("/icons/add_obj.gif")); |
| 267 | |
| 268 | /** Open DataSet action */ |
| 269 | openDataSet = new Action() { |
| 270 | @Override |
| 271 | public void run() { |
| 272 | |
| 273 | OpenDatasourceWizard wizard = new OpenDatasourceWizard(); |
| 274 | |
| 275 | // Instantiates the wizard container with the wizard and opens |
| 276 | // it |
| 277 | WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard); |
| 278 | dialog.create(); |
| 279 | |
| 280 | dialog.setTitle(ConfigureDatasourceDialog.OPEN_WISARD_TITLE); |
| 281 | dialog.open(); |
| 282 | viewer.refresh(); |
| 283 | } |
| 284 | }; |
| 285 | openDataSet.setText("Open"); |
| 286 | openDataSet.setToolTipText("Open a Data Source"); |
| 287 | openDataSet.setImageDescriptor(VisualisationPlugin |
| 288 | .getImageDescriptor("/icons/data_source_folder.gif")); |
| 289 | |
| 290 | /** Delete DataSet/Experiment action. */ |
| 291 | deleteDataSet = new Action() { |
| 292 | @Override |
| 293 | public void run() { |
| 294 | |
| 295 | // selected element in 'ExperimentView' DAOFactory |
| 296 | if (selectedFactory != null) { |
| 297 | SensorFrameworkDataset.singleton().removeDataSource(selectedFactory); |
| 298 | viewer.refresh(); |
| 299 | } |
| 300 | // selected element in 'ExperimentView' is Experiment |
| 301 | if (selectedExperiment != null && selectedFactory != null) { |
| 302 | selectedFactory.createExperimentDAO() |
| 303 | .removeExperiment(selectedExperiment, true); |
| 304 | viewer.refresh(); |
| 305 | } |
| 306 | } |
| 307 | }; |
| 308 | deleteDataSet.setText("Delete"); |
| 309 | deleteDataSet.setToolTipText("Delete a Data Source/Experiment"); |
| 310 | deleteDataSet.setImageDescriptor(VisualisationPlugin |
| 311 | .getImageDescriptor("/icons/remove_obj.gif")); |
| 312 | deleteDataSet.setEnabled(false); |
| 313 | |
| 314 | /** Properties action. */ |
| 315 | properties = new Action() { |
| 316 | @Override |
| 317 | public void run() { |
| 318 | |
| 319 | IPreferencePage page = new DAOFactoryPreferencePage(selectedFactory); |
| 320 | page.setTitle("General Information"); |
| 321 | PreferenceManager mgr = new PreferenceManager(); |
| 322 | IPreferenceNode node = new PreferenceNode("1", page); |
| 323 | mgr.addToRoot(node); |
| 324 | PreferenceDialog dialog = new PreferenceDialog(getSite().getShell(), mgr); |
| 325 | dialog.create(); |
| 326 | dialog.setMessage(page.getTitle()); |
| 327 | dialog.open(); |
| 328 | |
| 329 | } |
| 330 | }; |
| 331 | properties.setText("Properties"); |
| 332 | properties.setEnabled(false); |
| 333 | |
| 334 | /** Save As CSV action */ |
| 335 | saveAsCSV = new Action() { |
| 336 | @Override |
| 337 | public void run() { |
| 338 | exportSelectedDataToCSV(); |
| 339 | } |
| 340 | }; |
| 341 | saveAsCSV.setText("Save as CSV"); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Export the measurement of one sensor to a CSV file. |
| 346 | * |
| 347 | * @author David Scherr |
| 348 | * @param fileName |
| 349 | * Name of the CSV file. |
| 350 | * @param measurement |
| 351 | * The collection of measurements of the selected sensor. |
| 352 | * @param isHeader |
| 353 | * If the value is true, then the CSV file will integrate a superscription. |
| 354 | * @param separator |
| 355 | * At the moment there are 3 possibilities to separate the CSV data: (1) Semicolon |
| 356 | * (2) Comma (3) Tabulator. One of these is stored in this parameter as the |
| 357 | * corresponding character, but still in String format (maybe in the future someone |
| 358 | * will add a separator with more than one character). |
| 359 | */ |
| 360 | private void exportSensorMeasurementToCSV(String fileName, Collection<Measurement> measurement, |
| 361 | boolean isHeader, String separator) { |
| 362 | |
| 363 | FileWriter fileWriter; |
| 364 | BufferedWriter bufferedWriter; |
| 365 | try { |
| 366 | fileWriter = new FileWriter(fileName); |
| 367 | bufferedWriter = new BufferedWriter(fileWriter); |
| 368 | if (measurement.iterator().next() instanceof StateMeasurement) { |
| 369 | // If you have activated the header in the dialog (CSVSettingsDialog), then you get |
| 370 | // a superscription of the measurement types. |
| 371 | if (isHeader) { |
| 372 | bufferedWriter.append("Event Time" + separator + "State" + "\n"); |
| 373 | } |
| 374 | // Get each element of Event Time and Sensor State as a pair which will be save as |
| 375 | // one CSV line into the buffer. |
| 376 | for (Iterator<Measurement> iterator = measurement.iterator(); iterator.hasNext();) { |
| 377 | // The instance of the Measurement is known as StateMeasurement and through the |
| 378 | // casting you get the special data of this type of Measurement. |
| 379 | StateMeasurement data = ((StateMeasurement) iterator.next()); |
| 380 | // Write one pair of data per line with the separator, which you can choose in |
| 381 | // the dialog (CSVSettingsDialog). |
| 382 | bufferedWriter.append(data.getEventTime() + separator |
| 383 | + data.getSensorState().getStateLiteral() + "\n"); |
| 384 | } |
| 385 | } else if (measurement.iterator().next() instanceof TimeSpanMeasurement) { |
| 386 | // If you have activated the header in the dialog (CSVSettingsDialog), then you get |
| 387 | // a superscription of the measurement types. |
| 388 | if (isHeader) { |
| 389 | bufferedWriter.append("Event Time" + separator + "Time Span" + "\n"); |
| 390 | } |
| 391 | // Get each element of Event Time and Time Span as a pair which will be save as one |
| 392 | // CSV line into the buffer. |
| 393 | for (Iterator<Measurement> iterator = measurement.iterator(); iterator.hasNext();) { |
| 394 | // The instance of the Measurement is known as TimeSpanMeasurement and through |
| 395 | // the casting you get the special data of this type of Measurement. |
| 396 | TimeSpanMeasurement data = ((TimeSpanMeasurement) iterator.next()); |
| 397 | // Write one pair of data per line with the separator, which you can choose in |
| 398 | // the dialog (CSVSettingsDialog). |
| 399 | bufferedWriter.append(data.getEventTime() + separator + data.getTimeSpan() |
| 400 | + "\n"); |
| 401 | } |
| 402 | } else { |
| 403 | // The type of measurement is unknown. |
| 404 | logger.log(Level.SEVERE, "It is not possible to export this type of measurement " |
| 405 | + "to the CSV format. At the moment only instances of StateMeasurement " |
| 406 | + "and TimeSpanMeasurement are suitable."); |
| 407 | throw new IllegalArgumentException(); |
| 408 | } |
| 409 | bufferedWriter.close(); |
| 410 | fileWriter.close(); |
| 411 | } catch (Exception e) { |
| 412 | e.printStackTrace(); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Export an experiment run to a corresponding folder with all subordinated runs as CSV files. |
| 418 | * |
| 419 | * @author David Scherr |
| 420 | * @param pathDir |
| 421 | * Path in which you find the <code>ExperimentRun</code> directory. |
| 422 | * @param sensors |
| 423 | * Sensors of Measurement. |
| 424 | * @param run |
| 425 | * <code>ExperimentRun</code> with all measurements. |
| 426 | * @param isHeader |
| 427 | * If the value is true, then the CSV file will integrate a superscription. |
| 428 | * @param separator |
| 429 | * At the moment there are 3 possibilities to separate the CSV data: (1) Semicolon |
| 430 | * (2) Comma (3) Tabulator. One of these is stored in this parameter as the |
| 431 | * corresponding character, but still in String format (maybe in the future someone |
| 432 | * will add a separator with more than one character). |
| 433 | */ |
| 434 | private void exportExperimentRunToCSV(String pathDir, Collection<Sensor> sensors, |
| 435 | ExperimentRun run, boolean isHeader, String separator) { |
| 436 | SensorAndMeasurements sensorAndMeasurements; |
| 437 | String fileName; |
| 438 | String pathFile; |
| 439 | for (Iterator<Sensor> iterator = sensors.iterator(); iterator.hasNext();) { |
| 440 | Sensor sensor = iterator.next(); |
| 441 | sensorAndMeasurements = run.getMeasurementsOfSensor(sensor); |
| 442 | fileName = sanitizeFileName(sensor.getSensorName().toString() + ".csv"); |
| 443 | pathFile = pathDir + File.separatorChar + fileName; |
| 444 | exportSensorMeasurementToCSV(pathFile, sensorAndMeasurements.getMeasurements(), |
| 445 | isHeader, separator); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Get the Data which are associated with the selected TreeView element and open a save dialog |
| 451 | * with settings for the export to CSV files. |
| 452 | * |
| 453 | * @author David Scherr |
| 454 | */ |
| 455 | private void exportSelectedDataToCSV() { |
| 456 | |
| 457 | ISelection selection = viewer.getSelection(); |
| 458 | IStructuredSelection structuredSelection = (IStructuredSelection) selection; |
| 459 | Object object = structuredSelection.getFirstElement(); |
| 460 | |
| 461 | if (object instanceof FileDAOFactory) { |
| 462 | |
| 463 | } else if (object instanceof ExperimentAndDAO) { |
| 464 | |
| 465 | } else if (object instanceof TreeContainer) { |
| 466 | TreeContainer treeContainer = (TreeContainer) object; |
| 467 | String dirAllExperimentRuns = treeContainer.getName(); |
| 468 | CSVSettingsDialog dialog = new CSVSettingsDialog("", dirAllExperimentRuns, "", |
| 469 | DialogType.DIRECTORY); |
| 470 | |
| 471 | // If the returned path of the dialog is valid, then continue the export. |
| 472 | if (dialog.getValidPath()) { |
| 473 | exportAllExperimentRunsToCSV(dialog.getPath(), treeContainer.getElements(), dialog |
| 474 | .isHeader(), dialog.getSeparator()); |
| 475 | } |
| 476 | |
| 477 | } else if (object instanceof TreeObject) { |
| 478 | TreeObject treeObject = (TreeObject) object; |
| 479 | Object innerObject = treeObject.getObject(); |
| 480 | |
| 481 | if (innerObject instanceof Sensor) { |
| 482 | // Get the measurement of a special selected sensor of the TreeObject. |
| 483 | Sensor sensor = (Sensor) innerObject; |
| 484 | SensorAndMeasurements sensorAndMeasurements = treeObject.getRun() |
| 485 | .getMeasurementsOfSensor(sensor); |
| 486 | String fileName = sanitizeFileName(sensorAndMeasurements.getSensor() |
| 487 | .getSensorName().toString() |
| 488 | + ".csv"); |
| 489 | // Please note that the dialog type is a FileDialog. |
| 490 | CSVSettingsDialog dialog = new CSVSettingsDialog("", fileName, "*.csv", |
| 491 | DialogType.FILE); |
| 492 | |
| 493 | if (dialog.getValidPath()) { |
| 494 | exportSensorMeasurementToCSV(dialog.getPath(), sensorAndMeasurements |
| 495 | .getMeasurements(), dialog.isHeader(), dialog.getSeparator()); |
| 496 | } |
| 497 | } else if (innerObject instanceof ExperimentRun) { |
| 498 | // Get the measurements of all sensors of the selected TreeObject. |
| 499 | ExperimentRun run = (ExperimentRun) innerObject; |
| 500 | String dirExperiment = sanitizeFileName(run.getExperimentDateTime()); |
| 501 | // Please note that the dialog type is a DirectoryDialog. |
| 502 | CSVSettingsDialog dialog = new CSVSettingsDialog("", dirExperiment, "", |
| 503 | DialogType.DIRECTORY); |
| 504 | |
| 505 | if (dialog.getValidPath()) { |
| 506 | exportExperimentRunToCSV(dialog.getPath(), treeObject.getExperiment() |
| 507 | .getSensors(), run, dialog.isHeader(), dialog.getSeparator()); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Export all experiment runs to the corresponding folders which contains sub-folders with the |
| 515 | * particular runs as CSV files. |
| 516 | * |
| 517 | * @author David Scherr |
| 518 | * @param pathDir |
| 519 | * Path in which you find the directory of all Experiment Runs. |
| 520 | * @param allExperimentRuns |
| 521 | * Collection of the TreeObjects, which contains Experiment Runs. |
| 522 | * @param isHeader |
| 523 | * If the value is true, then the CSV file will integrate a superscription. |
| 524 | * @param separator |
| 525 | * At the moment there are 3 possibilities to separate the CSV data: (1) Semicolon |
| 526 | * (2) Comma (3) Tabulator. One of these is stored in this parameter as the |
| 527 | * corresponding character, but still in String format (maybe in the future someone |
| 528 | * will add a separator with more than one character). |
| 529 | */ |
| 530 | private void exportAllExperimentRunsToCSV(String pathDir, |
| 531 | Collection<TreeObject> allExperimentRuns, boolean isHeader, String separator) { |
| 532 | // Save all Experiments |
| 533 | for (Iterator<TreeObject> iterator = allExperimentRuns.iterator(); iterator.hasNext();) { |
| 534 | TreeObject treeObject = (TreeObject) iterator.next(); |
| 535 | Object innerObject = treeObject.getObject(); |
| 536 | |
| 537 | // Save all ExperimentRuns |
| 538 | if (innerObject instanceof ExperimentRun) { |
| 539 | // Get the measurements of all sensors of the selected TreeObject. |
| 540 | ExperimentRun run = (ExperimentRun) innerObject; |
| 541 | String dirExperiment = sanitizeFileName(run.getExperimentDateTime()); |
| 542 | String pathAllExperimentRuns = pathDir + File.separatorChar + dirExperiment; |
| 543 | new File(pathAllExperimentRuns).mkdirs(); |
| 544 | exportExperimentRunToCSV(pathAllExperimentRuns, treeObject.getExperiment() |
| 545 | .getSensors(), run, isHeader, separator); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Some characters are not allowed for file and directory names, e.g. : , ", *, etc. which will |
| 552 | * be replaced by sanitizeFileName(). |
| 553 | * |
| 554 | * @author David Scherr |
| 555 | * @param fileOrDirName |
| 556 | * The name of a file or directory. |
| 557 | * @return The sanitized file name, which is free of not allowed characters. |
| 558 | */ |
| 559 | private String sanitizeFileName(String fileOrDirName) { |
| 560 | |
| 561 | // Replace all chars, which can't be a part of a valid windows filename. |
| 562 | fileOrDirName = fileOrDirName.replace('\\', '-'); |
| 563 | fileOrDirName = fileOrDirName.replace('/', '-'); |
| 564 | fileOrDirName = fileOrDirName.replace(':', '-'); |
| 565 | fileOrDirName = fileOrDirName.replace('*', '+'); |
| 566 | fileOrDirName = fileOrDirName.replace('?', '!'); |
| 567 | fileOrDirName = fileOrDirName.replace('"', ' '); |
| 568 | fileOrDirName = fileOrDirName.replace('<', '('); |
| 569 | fileOrDirName = fileOrDirName.replace('>', ')'); |
| 570 | fileOrDirName = fileOrDirName.replace('|', ','); |
| 571 | |
| 572 | return fileOrDirName; |
| 573 | } |
| 574 | |
| 575 | /** Set a instance of, in viewer selected element. */ |
| 576 | private void setSelectedElement(Object selected) { |
| 577 | |
| 578 | selectedFactory = null; |
| 579 | selectedExperiment = null; |
| 580 | |
| 581 | if (selected instanceof IDAOFactory) { |
| 582 | selectedFactory = (IDAOFactory) selected; |
| 583 | deleteDataSet.setEnabled(true); |
| 584 | properties.setEnabled(true); |
| 585 | saveAsCSV.setEnabled(false); |
| 586 | } else if (selected instanceof ExperimentAndDAO) { |
| 587 | ExperimentAndDAO experimentAndDAO = (ExperimentAndDAO) selected; |
| 588 | selectedExperiment = (Experiment) experimentAndDAO.getExperiment(); |
| 589 | selectedFactory = experimentAndDAO.getDatasource(); |
| 590 | deleteDataSet.setEnabled(true); |
| 591 | properties.setEnabled(false); |
| 592 | saveAsCSV.setEnabled(false); |
| 593 | } else { |
| 594 | deleteDataSet.setEnabled(false); |
| 595 | properties.setEnabled(false); |
| 596 | saveAsCSV.setEnabled(true); |
| 597 | |
| 598 | if ((selected instanceof TreeContainer)) { |
| 599 | if (((TreeContainer) selected).getName().equals("Sensors")) { |
| 600 | saveAsCSV.setEnabled(false); |
| 601 | } |
| 602 | } else if (selected instanceof TreeObject) { |
| 603 | if (((TreeObject) selected).getObject() instanceof ExperimentRun) { |
| 604 | |
| 605 | } else { |
| 606 | if (((TreeObject) selected).getRun() == null) { |
| 607 | saveAsCSV.setEnabled(false); |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Passing the focus request to the viewer's control. |
| 616 | */ |
| 617 | @Override |
| 618 | public void setFocus() { |
| 619 | viewer.getControl().setFocus(); |
| 620 | } |
| 621 | } |