Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
beb19106 | 2 | * Copyright (c) 2009, 2012, 2013 Ericsson, École Polytechnique de Montréal |
abbdd66a | 3 | * |
12c155f5 FC |
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 | |
abbdd66a | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
beb19106 | 11 | * Geneviève Bastien - Moved the add and remove code to the experiment class |
12c155f5 FC |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.tmf.ui.project.wizards; | |
15 | ||
16 | import java.util.HashMap; | |
17 | import java.util.Map; | |
18 | import java.util.Set; | |
19 | import java.util.Vector; | |
20 | ||
12c155f5 | 21 | import org.eclipse.core.resources.IFolder; |
12c155f5 | 22 | import org.eclipse.core.runtime.CoreException; |
12c155f5 FC |
23 | import org.eclipse.jface.viewers.CheckboxTableViewer; |
24 | import org.eclipse.jface.wizard.WizardPage; | |
8fd82db5 | 25 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
12c155f5 FC |
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; | |
40 | ||
41 | /** | |
b544077e | 42 | * Implementation of a wizard page for selecting trace for an experiment. |
12c155f5 | 43 | * <p> |
abbdd66a | 44 | * |
b544077e BH |
45 | * @version 1.0 |
46 | * @author Francois Chouinard | |
12c155f5 FC |
47 | */ |
48 | public class SelectTracesWizardPage extends WizardPage { | |
49 | ||
50 | // ------------------------------------------------------------------------ | |
51 | // Attributes | |
52 | // ------------------------------------------------------------------------ | |
53 | ||
54 | private final TmfProjectElement fProject; | |
55 | private final TmfExperimentElement fExperiment; | |
9fa32496 | 56 | private Map<String, TmfTraceElement> fPreviousTraces; |
12c155f5 FC |
57 | private CheckboxTableViewer fCheckboxTableViewer; |
58 | ||
59 | // ------------------------------------------------------------------------ | |
60 | // Constructor | |
61 | // ------------------------------------------------------------------------ | |
b544077e BH |
62 | /** |
63 | * Constructor | |
64 | * @param project The project model element. | |
65 | * @param experiment The experiment model experiment. | |
66 | */ | |
12c155f5 FC |
67 | protected SelectTracesWizardPage(TmfProjectElement project, TmfExperimentElement experiment) { |
68 | super(""); //$NON-NLS-1$ | |
69 | setTitle(Messages.SelectTracesWizardPage_WindowTitle); | |
70 | setDescription(Messages.SelectTracesWizardPage_Description); | |
71 | fProject = project; | |
72 | fExperiment = experiment; | |
73 | } | |
74 | ||
75 | // ------------------------------------------------------------------------ | |
76 | // Dialog | |
77 | // ------------------------------------------------------------------------ | |
b544077e BH |
78 | /* |
79 | * (non-Javadoc) | |
80 | * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite) | |
81 | */ | |
12c155f5 FC |
82 | @Override |
83 | public void createControl(Composite parent) { | |
84 | Composite container = new Composite(parent, SWT.NULL); | |
85 | container.setLayout(new FormLayout()); | |
86 | setControl(container); | |
87 | ||
88 | fCheckboxTableViewer = CheckboxTableViewer.newCheckList(container, SWT.BORDER); | |
89 | fCheckboxTableViewer.setContentProvider(new TraceFolderContentProvider()); | |
90 | fCheckboxTableViewer.setLabelProvider(new TraceFolderLabelProvider()); | |
91 | ||
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); | |
100 | ||
101 | final TableColumn tableColumn = new TableColumn(table, SWT.NONE); | |
102 | tableColumn.setWidth(200); | |
103 | tableColumn.setText(Messages.SelectTracesWizardPage_TraceColumnHeader); | |
104 | ||
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); | |
112 | } | |
113 | } | |
114 | ||
115 | // Populate the list of traces to choose from | |
116 | Set<String> keys = fPreviousTraces.keySet(); | |
117 | TmfTraceFolder traceFolder = fProject.getTracesFolder(); | |
118 | fCheckboxTableViewer.setInput(traceFolder); | |
119 | ||
120 | // Set the checkbox for the traces already included | |
121 | int index = 0; | |
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); | |
128 | } | |
129 | } | |
130 | element = fCheckboxTableViewer.getElementAt(index++); | |
131 | } | |
132 | } | |
133 | ||
b544077e BH |
134 | /** |
135 | * Method to finalize the select operation. | |
136 | * @return <code>true</code> if successful else <code>false</code> | |
137 | */ | |
12c155f5 FC |
138 | public boolean performFinish() { |
139 | ||
140 | IFolder experiment = fExperiment.getResource(); | |
141 | ||
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); | |
149 | } else { | |
beb19106 | 150 | fExperiment.addTrace(trace); |
12c155f5 FC |
151 | } |
152 | } | |
153 | ||
154 | // Remove traces that were unchecked (thus left in fPreviousTraces) | |
155 | keys = fPreviousTraces.keySet(); | |
156 | for (String key : keys) { | |
12c155f5 | 157 | try { |
beb19106 | 158 | fExperiment.removeTrace(fPreviousTraces.get(key)); |
12c155f5 | 159 | } catch (CoreException e) { |
8fd82db5 | 160 | Activator.getDefault().logError("Error selecting traces for experiment " + experiment.getName(), e); //$NON-NLS-1$ |
12c155f5 FC |
161 | } |
162 | } | |
163 | fProject.refresh(); | |
164 | ||
165 | return true; | |
166 | } | |
167 | ||
12c155f5 FC |
168 | /** |
169 | * Get the list of selected traces | |
170 | */ | |
171 | private TmfTraceElement[] getSelection() { | |
172 | Vector<TmfTraceElement> traces = new Vector<TmfTraceElement>(); | |
173 | Object[] selection = fCheckboxTableViewer.getCheckedElements(); | |
174 | for (Object sel : selection) { | |
abbdd66a | 175 | if (sel instanceof TmfTraceElement) { |
12c155f5 | 176 | traces.add((TmfTraceElement) sel); |
abbdd66a | 177 | } |
12c155f5 FC |
178 | } |
179 | TmfTraceElement[] result = new TmfTraceElement[traces.size()]; | |
180 | traces.toArray(result); | |
181 | return result; | |
182 | } | |
183 | ||
184 | } |