1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011 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
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TraceFolderContentProvider
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TraceFolderLabelProvider
;
37 import org
.eclipse
.swt
.SWT
;
38 import org
.eclipse
.swt
.layout
.FormAttachment
;
39 import org
.eclipse
.swt
.layout
.FormData
;
40 import org
.eclipse
.swt
.layout
.FormLayout
;
41 import org
.eclipse
.swt
.widgets
.Composite
;
42 import org
.eclipse
.swt
.widgets
.Table
;
43 import org
.eclipse
.swt
.widgets
.TableColumn
;
46 * <b><u>SelectTracesWizardPage</u></b>
49 public class SelectTracesWizardPage
extends WizardPage
{
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
55 private final TmfProjectElement fProject
;
56 private final TmfExperimentElement fExperiment
;
57 private HashMap
<String
, TmfTraceElement
> fPreviousTraces
;
58 private CheckboxTableViewer fCheckboxTableViewer
;
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
64 protected SelectTracesWizardPage(TmfProjectElement project
, TmfExperimentElement experiment
) {
65 super(""); //$NON-NLS-1$
66 setTitle(Messages
.SelectTracesWizardPage_WindowTitle
);
67 setDescription(Messages
.SelectTracesWizardPage_Description
);
69 fExperiment
= experiment
;
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
77 public void createControl(Composite parent
) {
78 Composite container
= new Composite(parent
, SWT
.NULL
);
79 container
.setLayout(new FormLayout());
80 setControl(container
);
82 fCheckboxTableViewer
= CheckboxTableViewer
.newCheckList(container
, SWT
.BORDER
);
83 fCheckboxTableViewer
.setContentProvider(new TraceFolderContentProvider());
84 fCheckboxTableViewer
.setLabelProvider(new TraceFolderLabelProvider());
86 final Table table
= fCheckboxTableViewer
.getTable();
87 final FormData formData
= new FormData();
88 formData
.bottom
= new FormAttachment(100, 0);
89 formData
.right
= new FormAttachment(100, 0);
90 formData
.top
= new FormAttachment(0, 0);
91 formData
.left
= new FormAttachment(0, 0);
92 table
.setLayoutData(formData
);
93 table
.setHeaderVisible(true);
95 final TableColumn tableColumn
= new TableColumn(table
, SWT
.NONE
);
96 tableColumn
.setWidth(200);
97 tableColumn
.setText(Messages
.SelectTracesWizardPage_TraceColumnHeader
);
99 // Get the list of traces already part of the experiment
100 fPreviousTraces
= new HashMap
<String
, TmfTraceElement
>();
101 for (ITmfProjectModelElement child
: fExperiment
.getChildren()) {
102 if (child
instanceof TmfTraceElement
) {
103 TmfTraceElement trace
= (TmfTraceElement
) child
;
104 String name
= trace
.getResource().getName();
105 fPreviousTraces
.put(name
, trace
);
109 // Populate the list of traces to choose from
110 Set
<String
> keys
= fPreviousTraces
.keySet();
111 TmfTraceFolder traceFolder
= fProject
.getTracesFolder();
112 fCheckboxTableViewer
.setInput(traceFolder
);
114 // Set the checkbox for the traces already included
116 Object element
= fCheckboxTableViewer
.getElementAt(index
++);
117 while (element
!= null) {
118 if (element
instanceof TmfTraceElement
) {
119 TmfTraceElement trace
= (TmfTraceElement
) element
;
120 if (keys
.contains(trace
.getResource().getName())) {
121 fCheckboxTableViewer
.setChecked(element
, true);
124 element
= fCheckboxTableViewer
.getElementAt(index
++);
128 public boolean performFinish() {
130 IFolder experiment
= fExperiment
.getResource();
132 // Add the selected traces to the experiment
133 Set
<String
> keys
= fPreviousTraces
.keySet();
134 TmfTraceElement
[] traces
= getSelection();
135 for (TmfTraceElement trace
: traces
) {
136 String name
= trace
.getResource().getName();
137 if (keys
.contains(name
)) {
138 fPreviousTraces
.remove(name
);
140 IResource resource
= trace
.getResource();
141 IPath location
= resource
.getLocation();
142 createLink(experiment
, trace
, resource
, location
);
146 // Remove traces that were unchecked (thus left in fPreviousTraces)
147 keys
= fPreviousTraces
.keySet();
148 for (String key
: keys
) {
149 fExperiment
.removeChild(fPreviousTraces
.get(key
));
150 IResource resource
= experiment
.findMember(key
);
152 resource
.delete(true, null);
153 } catch (CoreException e
) {
163 * Create a link to the actual trace and set the trace type
165 private void createLink(IFolder experiment
, TmfTraceElement trace
, IResource resource
, IPath location
) {
166 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
168 Map
<QualifiedName
, String
> properties
= trace
.getResource().getPersistentProperties();
169 String bundleName
= properties
.get(TmfTraceElement
.TRACEBUNDLE
);
170 String traceType
= properties
.get(TmfTraceElement
.TRACETYPE
);
171 String iconUrl
= properties
.get(TmfTraceElement
.TRACEICON
);
173 if (resource
instanceof IFolder
) {
174 IFolder folder
= experiment
.getFolder(trace
.getName());
175 if (workspace
.validateLinkLocation(folder
, location
).isOK()) {
176 folder
.createLink(location
, IResource
.REPLACE
, null);
177 setProperties(folder
, bundleName
, traceType
, iconUrl
);
180 System
.out
.println("Invalid Trace Location"); //$NON-NLS-1$
183 IFile file
= experiment
.getFile(trace
.getName());
184 if (workspace
.validateLinkLocation(file
, location
).isOK()) {
185 file
.createLink(location
, IResource
.REPLACE
, null);
186 setProperties(file
, bundleName
, traceType
, iconUrl
);
188 System
.out
.println("Invalid Trace Location"); //$NON-NLS-1$
191 } catch (CoreException e
) {
196 private void setProperties(IResource resource
, String bundleName
, String traceType
, String iconUrl
) throws CoreException
{
197 resource
.setPersistentProperty(TmfTraceElement
.TRACEBUNDLE
, bundleName
);
198 resource
.setPersistentProperty(TmfTraceElement
.TRACETYPE
, traceType
);
199 resource
.setPersistentProperty(TmfTraceElement
.TRACEICON
, iconUrl
);
203 * Get the list of selected traces
205 private TmfTraceElement
[] getSelection() {
206 Vector
<TmfTraceElement
> traces
= new Vector
<TmfTraceElement
>();
207 Object
[] selection
= fCheckboxTableViewer
.getCheckedElements();
208 for (Object sel
: selection
) {
209 if (sel
instanceof TmfTraceElement
)
210 traces
.add((TmfTraceElement
) sel
);
212 TmfTraceElement
[] result
= new TmfTraceElement
[traces
.size()];
213 traces
.toArray(result
);