1 /*******************************************************************************
2 * Copyright (c) 2009, 2012, 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 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
83 public void createControl(Composite parent
) {
84 Composite container
= new Composite(parent
, SWT
.NULL
);
85 container
.setLayout(new FormLayout());
86 setControl(container
);
88 fCheckboxTableViewer
= CheckboxTableViewer
.newCheckList(container
, SWT
.BORDER
);
89 fCheckboxTableViewer
.setContentProvider(new TraceFolderContentProvider());
90 fCheckboxTableViewer
.setLabelProvider(new TraceFolderLabelProvider());
92 final Table table
= fCheckboxTableViewer
.getTable();
93 final FormData formData
= new FormData();
94 formData
.bottom
= new FormAttachment(100, 0);
95 formData
.right
= new FormAttachment(100, 0);
96 formData
.top
= new FormAttachment(0, 0);
97 formData
.left
= new FormAttachment(0, 0);
98 table
.setLayoutData(formData
);
99 table
.setHeaderVisible(true);
101 final TableColumn tableColumn
= new TableColumn(table
, SWT
.NONE
);
102 tableColumn
.setWidth(200);
103 tableColumn
.setText(Messages
.SelectTracesWizardPage_TraceColumnHeader
);
105 // Get the list of traces already part of the experiment
106 fPreviousTraces
= new HashMap
<String
, TmfTraceElement
>();
107 for (ITmfProjectModelElement child
: fExperiment
.getChildren()) {
108 if (child
instanceof TmfTraceElement
) {
109 TmfTraceElement trace
= (TmfTraceElement
) child
;
110 String name
= trace
.getResource().getName();
111 fPreviousTraces
.put(name
, trace
);
115 // Populate the list of traces to choose from
116 Set
<String
> keys
= fPreviousTraces
.keySet();
117 TmfTraceFolder traceFolder
= fProject
.getTracesFolder();
118 fCheckboxTableViewer
.setInput(traceFolder
);
120 // Set the checkbox for the traces already included
122 Object element
= fCheckboxTableViewer
.getElementAt(index
++);
123 while (element
!= null) {
124 if (element
instanceof TmfTraceElement
) {
125 TmfTraceElement trace
= (TmfTraceElement
) element
;
126 if (keys
.contains(trace
.getResource().getName())) {
127 fCheckboxTableViewer
.setChecked(element
, true);
130 element
= fCheckboxTableViewer
.getElementAt(index
++);
135 * Method to finalize the select operation.
136 * @return <code>true</code> if successful else <code>false</code>
138 public boolean performFinish() {
140 IFolder experiment
= fExperiment
.getResource();
142 // Add the selected traces to the experiment
143 Set
<String
> keys
= fPreviousTraces
.keySet();
144 TmfTraceElement
[] traces
= getSelection();
145 for (TmfTraceElement trace
: traces
) {
146 String name
= trace
.getResource().getName();
147 if (keys
.contains(name
)) {
148 fPreviousTraces
.remove(name
);
150 fExperiment
.addTrace(trace
);
154 // Remove traces that were unchecked (thus left in fPreviousTraces)
155 keys
= fPreviousTraces
.keySet();
156 for (String key
: keys
) {
158 fExperiment
.removeTrace(fPreviousTraces
.get(key
));
159 } catch (CoreException e
) {
160 Activator
.getDefault().logError("Error selecting traces for experiment " + experiment
.getName(), e
); //$NON-NLS-1$
169 * Get the list of selected traces
171 private TmfTraceElement
[] getSelection() {
172 Vector
<TmfTraceElement
> traces
= new Vector
<TmfTraceElement
>();
173 Object
[] selection
= fCheckboxTableViewer
.getCheckedElements();
174 for (Object sel
: selection
) {
175 if (sel
instanceof TmfTraceElement
) {
176 traces
.add((TmfTraceElement
) sel
);
179 TmfTraceElement
[] result
= new TmfTraceElement
[traces
.size()];
180 traces
.toArray(result
);