Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010, 2011 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.project.wizards; | |
14 | ||
15 | import java.util.HashMap; | |
16 | import java.util.Map; | |
17 | import java.util.Set; | |
18 | import java.util.Vector; | |
19 | ||
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; | |
44 | ||
45 | /** | |
46 | * <b><u>SelectTracesWizardPage</u></b> | |
47 | * <p> | |
48 | */ | |
49 | public class SelectTracesWizardPage extends WizardPage { | |
50 | ||
51 | // ------------------------------------------------------------------------ | |
52 | // Attributes | |
53 | // ------------------------------------------------------------------------ | |
54 | ||
55 | private final TmfProjectElement fProject; | |
56 | private final TmfExperimentElement fExperiment; | |
57 | private HashMap<String, TmfTraceElement> fPreviousTraces; | |
58 | private CheckboxTableViewer fCheckboxTableViewer; | |
59 | ||
60 | // ------------------------------------------------------------------------ | |
61 | // Constructor | |
62 | // ------------------------------------------------------------------------ | |
63 | ||
64 | protected SelectTracesWizardPage(TmfProjectElement project, TmfExperimentElement experiment) { | |
65 | super(""); //$NON-NLS-1$ | |
66 | setTitle(Messages.SelectTracesWizardPage_WindowTitle); | |
67 | setDescription(Messages.SelectTracesWizardPage_Description); | |
68 | fProject = project; | |
69 | fExperiment = experiment; | |
70 | } | |
71 | ||
72 | // ------------------------------------------------------------------------ | |
73 | // Dialog | |
74 | // ------------------------------------------------------------------------ | |
75 | ||
76 | @Override | |
77 | public void createControl(Composite parent) { | |
78 | Composite container = new Composite(parent, SWT.NULL); | |
79 | container.setLayout(new FormLayout()); | |
80 | setControl(container); | |
81 | ||
82 | fCheckboxTableViewer = CheckboxTableViewer.newCheckList(container, SWT.BORDER); | |
83 | fCheckboxTableViewer.setContentProvider(new TraceFolderContentProvider()); | |
84 | fCheckboxTableViewer.setLabelProvider(new TraceFolderLabelProvider()); | |
85 | ||
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); | |
94 | ||
95 | final TableColumn tableColumn = new TableColumn(table, SWT.NONE); | |
96 | tableColumn.setWidth(200); | |
97 | tableColumn.setText(Messages.SelectTracesWizardPage_TraceColumnHeader); | |
98 | ||
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); | |
106 | } | |
107 | } | |
108 | ||
109 | // Populate the list of traces to choose from | |
110 | Set<String> keys = fPreviousTraces.keySet(); | |
111 | TmfTraceFolder traceFolder = fProject.getTracesFolder(); | |
112 | fCheckboxTableViewer.setInput(traceFolder); | |
113 | ||
114 | // Set the checkbox for the traces already included | |
115 | int index = 0; | |
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); | |
122 | } | |
123 | } | |
124 | element = fCheckboxTableViewer.getElementAt(index++); | |
125 | } | |
126 | } | |
127 | ||
128 | public boolean performFinish() { | |
129 | ||
130 | IFolder experiment = fExperiment.getResource(); | |
131 | ||
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); | |
139 | } else { | |
140 | IResource resource = trace.getResource(); | |
141 | IPath location = resource.getLocation(); | |
142 | createLink(experiment, trace, resource, location); | |
143 | } | |
144 | } | |
145 | ||
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); | |
151 | try { | |
152 | resource.delete(true, null); | |
153 | } catch (CoreException e) { | |
154 | e.printStackTrace(); | |
155 | } | |
156 | } | |
157 | fProject.refresh(); | |
158 | ||
159 | return true; | |
160 | } | |
161 | ||
162 | /** | |
163 | * Create a link to the actual trace and set the trace type | |
164 | */ | |
165 | private void createLink(IFolder experiment, TmfTraceElement trace, IResource resource, IPath location) { | |
166 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); | |
167 | try { | |
168 | Map<QualifiedName, String> properties = trace.getResource().getPersistentProperties(); | |
0f0d17bb AM |
169 | String bundleName = properties.get(TmfTraceElement.TRACEBUNDLE); |
170 | String traceType = properties.get(TmfTraceElement.TRACETYPE); | |
171 | String iconUrl = properties.get(TmfTraceElement.TRACEICON); | |
12c155f5 FC |
172 | |
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); | |
178 | ||
179 | } else { | |
180 | System.out.println("Invalid Trace Location"); //$NON-NLS-1$ | |
181 | } | |
182 | } else { | |
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); | |
187 | } else { | |
188 | System.out.println("Invalid Trace Location"); //$NON-NLS-1$ | |
189 | } | |
190 | } | |
191 | } catch (CoreException e) { | |
192 | e.printStackTrace(); | |
193 | } | |
194 | } | |
195 | ||
196 | private void setProperties(IResource resource, String bundleName, String traceType, String iconUrl) throws CoreException { | |
0f0d17bb AM |
197 | resource.setPersistentProperty(TmfTraceElement.TRACEBUNDLE, bundleName); |
198 | resource.setPersistentProperty(TmfTraceElement.TRACETYPE, traceType); | |
199 | resource.setPersistentProperty(TmfTraceElement.TRACEICON, iconUrl); | |
12c155f5 FC |
200 | } |
201 | ||
202 | /** | |
203 | * Get the list of selected traces | |
204 | */ | |
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); | |
211 | } | |
212 | TmfTraceElement[] result = new TmfTraceElement[traces.size()]; | |
213 | traces.toArray(result); | |
214 | return result; | |
215 | } | |
216 | ||
217 | } |