1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 Ericsson
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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
;
15 import java
.util
.HashMap
;
18 import java
.util
.Vector
;
20 import org
.eclipse
.core
.resources
.IFile
;
21 import org
.eclipse
.core
.resources
.IFolder
;
22 import org
.eclipse
.core
.resources
.IResource
;
23 import org
.eclipse
.core
.resources
.IWorkspace
;
24 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
25 import org
.eclipse
.core
.runtime
.CoreException
;
26 import org
.eclipse
.core
.runtime
.IPath
;
27 import org
.eclipse
.core
.runtime
.QualifiedName
;
28 import org
.eclipse
.jface
.viewers
.CheckboxTableViewer
;
29 import org
.eclipse
.jface
.wizard
.WizardPage
;
30 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TraceFolderContentProvider
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TraceFolderLabelProvider
;
39 import org
.eclipse
.swt
.SWT
;
40 import org
.eclipse
.swt
.layout
.FormAttachment
;
41 import org
.eclipse
.swt
.layout
.FormData
;
42 import org
.eclipse
.swt
.layout
.FormLayout
;
43 import org
.eclipse
.swt
.widgets
.Composite
;
44 import org
.eclipse
.swt
.widgets
.Table
;
45 import org
.eclipse
.swt
.widgets
.TableColumn
;
48 * Implementation of a wizard page for selecting trace for an experiment.
52 * @author Francois Chouinard
54 public class SelectTracesWizardPage
extends WizardPage
{
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 private final TmfProjectElement fProject
;
61 private final TmfExperimentElement fExperiment
;
62 private Map
<String
, TmfTraceElement
> fPreviousTraces
;
63 private CheckboxTableViewer fCheckboxTableViewer
;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 * @param project The project model element.
71 * @param experiment The experiment model experiment.
73 protected SelectTracesWizardPage(TmfProjectElement project
, TmfExperimentElement experiment
) {
74 super(""); //$NON-NLS-1$
75 setTitle(Messages
.SelectTracesWizardPage_WindowTitle
);
76 setDescription(Messages
.SelectTracesWizardPage_Description
);
78 fExperiment
= experiment
;
81 // ------------------------------------------------------------------------
83 // ------------------------------------------------------------------------
86 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
89 public void createControl(Composite parent
) {
90 Composite container
= new Composite(parent
, SWT
.NULL
);
91 container
.setLayout(new FormLayout());
92 setControl(container
);
94 fCheckboxTableViewer
= CheckboxTableViewer
.newCheckList(container
, SWT
.BORDER
);
95 fCheckboxTableViewer
.setContentProvider(new TraceFolderContentProvider());
96 fCheckboxTableViewer
.setLabelProvider(new TraceFolderLabelProvider());
98 final Table table
= fCheckboxTableViewer
.getTable();
99 final FormData formData
= new FormData();
100 formData
.bottom
= new FormAttachment(100, 0);
101 formData
.right
= new FormAttachment(100, 0);
102 formData
.top
= new FormAttachment(0, 0);
103 formData
.left
= new FormAttachment(0, 0);
104 table
.setLayoutData(formData
);
105 table
.setHeaderVisible(true);
107 final TableColumn tableColumn
= new TableColumn(table
, SWT
.NONE
);
108 tableColumn
.setWidth(200);
109 tableColumn
.setText(Messages
.SelectTracesWizardPage_TraceColumnHeader
);
111 // Get the list of traces already part of the experiment
112 fPreviousTraces
= new HashMap
<String
, TmfTraceElement
>();
113 for (ITmfProjectModelElement child
: fExperiment
.getChildren()) {
114 if (child
instanceof TmfTraceElement
) {
115 TmfTraceElement trace
= (TmfTraceElement
) child
;
116 String name
= trace
.getResource().getName();
117 fPreviousTraces
.put(name
, trace
);
121 // Populate the list of traces to choose from
122 Set
<String
> keys
= fPreviousTraces
.keySet();
123 TmfTraceFolder traceFolder
= fProject
.getTracesFolder();
124 fCheckboxTableViewer
.setInput(traceFolder
);
126 // Set the checkbox for the traces already included
128 Object element
= fCheckboxTableViewer
.getElementAt(index
++);
129 while (element
!= null) {
130 if (element
instanceof TmfTraceElement
) {
131 TmfTraceElement trace
= (TmfTraceElement
) element
;
132 if (keys
.contains(trace
.getResource().getName())) {
133 fCheckboxTableViewer
.setChecked(element
, true);
136 element
= fCheckboxTableViewer
.getElementAt(index
++);
141 * Method to finalize the select operation.
142 * @return <code>true</code> if successful else <code>false</code>
144 public boolean performFinish() {
146 IFolder experiment
= fExperiment
.getResource();
148 // Add the selected traces to the experiment
149 Set
<String
> keys
= fPreviousTraces
.keySet();
150 TmfTraceElement
[] traces
= getSelection();
151 for (TmfTraceElement trace
: traces
) {
152 String name
= trace
.getResource().getName();
153 if (keys
.contains(name
)) {
154 fPreviousTraces
.remove(name
);
156 IResource resource
= trace
.getResource();
157 IPath location
= resource
.getLocation();
158 createLink(experiment
, trace
, resource
, location
);
162 // Remove traces that were unchecked (thus left in fPreviousTraces)
163 keys
= fPreviousTraces
.keySet();
164 for (String key
: keys
) {
165 fExperiment
.removeChild(fPreviousTraces
.get(key
));
166 IResource resource
= experiment
.findMember(key
);
168 resource
.delete(true, null);
169 } catch (CoreException e
) {
170 Activator
.getDefault().logError("Error selecting traces for experiment " + experiment
.getName(), e
); //$NON-NLS-1$
179 * Create a link to the actual trace and set the trace type
181 private static void createLink(IFolder experiment
, TmfTraceElement trace
,
182 IResource resource
, IPath location
) {
183 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
185 Map
<QualifiedName
, String
> properties
= trace
.getResource().getPersistentProperties();
186 String bundleName
= properties
.get(TmfCommonConstants
.TRACEBUNDLE
);
187 String traceType
= properties
.get(TmfCommonConstants
.TRACETYPE
);
188 String iconUrl
= properties
.get(TmfCommonConstants
.TRACEICON
);
190 if (resource
instanceof IFolder
) {
191 IFolder folder
= experiment
.getFolder(trace
.getName());
192 if (workspace
.validateLinkLocation(folder
, location
).isOK()) {
193 folder
.createLink(location
, IResource
.REPLACE
, null);
194 setProperties(folder
, bundleName
, traceType
, iconUrl
);
197 Activator
.getDefault().logError("Error creating link. Invalid trace location " + location
); //$NON-NLS-1$
200 IFile file
= experiment
.getFile(trace
.getName());
201 if (workspace
.validateLinkLocation(file
, location
).isOK()) {
202 file
.createLink(location
, IResource
.REPLACE
, null);
203 setProperties(file
, bundleName
, traceType
, iconUrl
);
205 Activator
.getDefault().logError("Error creating link. Invalid trace location " + location
); //$NON-NLS-1$
208 } catch (CoreException e
) {
209 Activator
.getDefault().logError("Error creating link to location " + location
, e
); //$NON-NLS-1$
213 private static void setProperties(IResource resource
, String bundleName
,
214 String traceType
, String iconUrl
) throws CoreException
{
215 resource
.setPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
, bundleName
);
216 resource
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceType
);
217 resource
.setPersistentProperty(TmfCommonConstants
.TRACEICON
, iconUrl
);
221 * Get the list of selected traces
223 private TmfTraceElement
[] getSelection() {
224 Vector
<TmfTraceElement
> traces
= new Vector
<TmfTraceElement
>();
225 Object
[] selection
= fCheckboxTableViewer
.getCheckedElements();
226 for (Object sel
: selection
) {
227 if (sel
instanceof TmfTraceElement
) {
228 traces
.add((TmfTraceElement
) sel
);
231 TmfTraceElement
[] result
= new TmfTraceElement
[traces
.size()];
232 traces
.toArray(result
);