1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 Ericsson, École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Francois Chouinard - Initial API and implementation
11 * Geneviève Bastien - Moved the add and remove code to the experiment class
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
;
16 import java
.util
.HashMap
;
19 import java
.util
.Vector
;
21 import org
.eclipse
.core
.resources
.IFolder
;
22 import org
.eclipse
.core
.runtime
.CoreException
;
23 import org
.eclipse
.jface
.viewers
.CheckboxTableViewer
;
24 import org
.eclipse
.jface
.wizard
.WizardPage
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
26 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TraceFolderContentProvider
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TraceFolderLabelProvider
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.layout
.FormAttachment
;
35 import org
.eclipse
.swt
.layout
.FormData
;
36 import org
.eclipse
.swt
.layout
.FormLayout
;
37 import org
.eclipse
.swt
.widgets
.Composite
;
38 import org
.eclipse
.swt
.widgets
.Table
;
39 import org
.eclipse
.swt
.widgets
.TableColumn
;
42 * Implementation of a wizard page for selecting trace for an experiment.
46 * @author Francois Chouinard
48 public class SelectTracesWizardPage
extends WizardPage
{
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 private final TmfProjectElement fProject
;
55 private final TmfExperimentElement fExperiment
;
56 private Map
<String
, TmfTraceElement
> fPreviousTraces
;
57 private CheckboxTableViewer fCheckboxTableViewer
;
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
64 * @param project The project model element.
65 * @param experiment The experiment model experiment.
67 protected SelectTracesWizardPage(TmfProjectElement project
, TmfExperimentElement experiment
) {
68 super(""); //$NON-NLS-1$
69 setTitle(Messages
.SelectTracesWizardPage_WindowTitle
);
70 setDescription(Messages
.SelectTracesWizardPage_Description
);
72 fExperiment
= experiment
;
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
80 public void createControl(Composite parent
) {
81 Composite container
= new Composite(parent
, SWT
.NULL
);
82 container
.setLayout(new FormLayout());
83 setControl(container
);
85 fCheckboxTableViewer
= CheckboxTableViewer
.newCheckList(container
, SWT
.BORDER
);
86 fCheckboxTableViewer
.setContentProvider(new TraceFolderContentProvider());
87 fCheckboxTableViewer
.setLabelProvider(new TraceFolderLabelProvider());
89 final Table table
= fCheckboxTableViewer
.getTable();
90 final FormData formData
= new FormData();
91 formData
.bottom
= new FormAttachment(100, 0);
92 formData
.right
= new FormAttachment(100, 0);
93 formData
.top
= new FormAttachment(0, 0);
94 formData
.left
= new FormAttachment(0, 0);
95 table
.setLayoutData(formData
);
96 table
.setHeaderVisible(true);
98 final TableColumn tableColumn
= new TableColumn(table
, SWT
.NONE
);
99 tableColumn
.setWidth(200);
100 tableColumn
.setText(Messages
.SelectTracesWizardPage_TraceColumnHeader
);
102 // Get the list of traces already part of the experiment
103 fPreviousTraces
= new HashMap
<>();
104 for (ITmfProjectModelElement child
: fExperiment
.getChildren()) {
105 if (child
instanceof TmfTraceElement
) {
106 TmfTraceElement trace
= (TmfTraceElement
) child
;
107 String name
= trace
.getResource().getName();
108 fPreviousTraces
.put(name
, trace
);
112 // Populate the list of traces to choose from
113 Set
<String
> keys
= fPreviousTraces
.keySet();
114 TmfTraceFolder traceFolder
= fProject
.getTracesFolder();
115 fCheckboxTableViewer
.setInput(traceFolder
);
117 // Set the checkbox for the traces already included
119 Object element
= fCheckboxTableViewer
.getElementAt(index
++);
120 while (element
!= null) {
121 if (element
instanceof TmfTraceElement
) {
122 TmfTraceElement trace
= (TmfTraceElement
) element
;
123 if (keys
.contains(trace
.getResource().getName())) {
124 fCheckboxTableViewer
.setChecked(element
, true);
127 element
= fCheckboxTableViewer
.getElementAt(index
++);
132 * Method to finalize the select operation.
133 * @return <code>true</code> if successful else <code>false</code>
135 public boolean performFinish() {
137 IFolder experiment
= fExperiment
.getResource();
138 boolean changed
= false;
140 // Add the selected traces to the experiment
141 Set
<String
> keys
= fPreviousTraces
.keySet();
142 TmfTraceElement
[] traces
= getSelection();
143 for (TmfTraceElement trace
: traces
) {
144 String name
= trace
.getResource().getName();
145 if (keys
.contains(name
)) {
146 fPreviousTraces
.remove(name
);
148 fExperiment
.addTrace(trace
);
153 // Remove traces that were unchecked (thus left in fPreviousTraces)
154 keys
= fPreviousTraces
.keySet();
155 for (String key
: keys
) {
157 fExperiment
.removeTrace(fPreviousTraces
.get(key
));
158 } catch (CoreException e
) {
159 Activator
.getDefault().logError("Error selecting traces for experiment " + experiment
.getName(), e
); //$NON-NLS-1$
165 fExperiment
.deleteSupplementaryResources();
166 fExperiment
.closeEditors();
173 * Get the list of selected traces
175 private TmfTraceElement
[] getSelection() {
176 Vector
<TmfTraceElement
> traces
= new Vector
<>();
177 Object
[] selection
= fCheckboxTableViewer
.getCheckedElements();
178 for (Object sel
: selection
) {
179 if (sel
instanceof TmfTraceElement
) {
180 traces
.add((TmfTraceElement
) sel
);
183 TmfTraceElement
[] result
= new TmfTraceElement
[traces
.size()];
184 traces
.toArray(result
);