Fix some findbugs
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / SelectTracesWizardPage.java
CommitLineData
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
13package org.eclipse.linuxtools.tmf.ui.project.wizards;
14
15import java.util.HashMap;
16import java.util.Map;
17import java.util.Set;
18import java.util.Vector;
19
20import org.eclipse.core.resources.IFile;
21import org.eclipse.core.resources.IFolder;
22import org.eclipse.core.resources.IResource;
23import org.eclipse.core.resources.IWorkspace;
24import org.eclipse.core.resources.ResourcesPlugin;
25import org.eclipse.core.runtime.CoreException;
26import org.eclipse.core.runtime.IPath;
27import org.eclipse.core.runtime.QualifiedName;
28import org.eclipse.jface.viewers.CheckboxTableViewer;
29import org.eclipse.jface.wizard.WizardPage;
e12ecd30 30import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
12c155f5
FC
31import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement;
32import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
33import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
34import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
35import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
36import org.eclipse.linuxtools.tmf.ui.project.model.TraceFolderContentProvider;
37import org.eclipse.linuxtools.tmf.ui.project.model.TraceFolderLabelProvider;
38import org.eclipse.swt.SWT;
39import org.eclipse.swt.layout.FormAttachment;
40import org.eclipse.swt.layout.FormData;
41import org.eclipse.swt.layout.FormLayout;
42import org.eclipse.swt.widgets.Composite;
43import org.eclipse.swt.widgets.Table;
44import org.eclipse.swt.widgets.TableColumn;
45
46/**
47 * <b><u>SelectTracesWizardPage</u></b>
48 * <p>
49 */
50public class SelectTracesWizardPage extends WizardPage {
51
52 // ------------------------------------------------------------------------
53 // Attributes
54 // ------------------------------------------------------------------------
55
56 private final TmfProjectElement fProject;
57 private final TmfExperimentElement fExperiment;
58 private HashMap<String, TmfTraceElement> fPreviousTraces;
59 private CheckboxTableViewer fCheckboxTableViewer;
60
61 // ------------------------------------------------------------------------
62 // Constructor
63 // ------------------------------------------------------------------------
64
65 protected SelectTracesWizardPage(TmfProjectElement project, TmfExperimentElement experiment) {
66 super(""); //$NON-NLS-1$
67 setTitle(Messages.SelectTracesWizardPage_WindowTitle);
68 setDescription(Messages.SelectTracesWizardPage_Description);
69 fProject = project;
70 fExperiment = experiment;
71 }
72
73 // ------------------------------------------------------------------------
74 // Dialog
75 // ------------------------------------------------------------------------
76
77 @Override
78 public void createControl(Composite parent) {
79 Composite container = new Composite(parent, SWT.NULL);
80 container.setLayout(new FormLayout());
81 setControl(container);
82
83 fCheckboxTableViewer = CheckboxTableViewer.newCheckList(container, SWT.BORDER);
84 fCheckboxTableViewer.setContentProvider(new TraceFolderContentProvider());
85 fCheckboxTableViewer.setLabelProvider(new TraceFolderLabelProvider());
86
87 final Table table = fCheckboxTableViewer.getTable();
88 final FormData formData = new FormData();
89 formData.bottom = new FormAttachment(100, 0);
90 formData.right = new FormAttachment(100, 0);
91 formData.top = new FormAttachment(0, 0);
92 formData.left = new FormAttachment(0, 0);
93 table.setLayoutData(formData);
94 table.setHeaderVisible(true);
95
96 final TableColumn tableColumn = new TableColumn(table, SWT.NONE);
97 tableColumn.setWidth(200);
98 tableColumn.setText(Messages.SelectTracesWizardPage_TraceColumnHeader);
99
100 // Get the list of traces already part of the experiment
101 fPreviousTraces = new HashMap<String, TmfTraceElement>();
102 for (ITmfProjectModelElement child : fExperiment.getChildren()) {
103 if (child instanceof TmfTraceElement) {
104 TmfTraceElement trace = (TmfTraceElement) child;
105 String name = trace.getResource().getName();
106 fPreviousTraces.put(name, trace);
107 }
108 }
109
110 // Populate the list of traces to choose from
111 Set<String> keys = fPreviousTraces.keySet();
112 TmfTraceFolder traceFolder = fProject.getTracesFolder();
113 fCheckboxTableViewer.setInput(traceFolder);
114
115 // Set the checkbox for the traces already included
116 int index = 0;
117 Object element = fCheckboxTableViewer.getElementAt(index++);
118 while (element != null) {
119 if (element instanceof TmfTraceElement) {
120 TmfTraceElement trace = (TmfTraceElement) element;
121 if (keys.contains(trace.getResource().getName())) {
122 fCheckboxTableViewer.setChecked(element, true);
123 }
124 }
125 element = fCheckboxTableViewer.getElementAt(index++);
126 }
127 }
128
129 public boolean performFinish() {
130
131 IFolder experiment = fExperiment.getResource();
132
133 // Add the selected traces to the experiment
134 Set<String> keys = fPreviousTraces.keySet();
135 TmfTraceElement[] traces = getSelection();
136 for (TmfTraceElement trace : traces) {
137 String name = trace.getResource().getName();
138 if (keys.contains(name)) {
139 fPreviousTraces.remove(name);
140 } else {
141 IResource resource = trace.getResource();
142 IPath location = resource.getLocation();
143 createLink(experiment, trace, resource, location);
144 }
145 }
146
147 // Remove traces that were unchecked (thus left in fPreviousTraces)
148 keys = fPreviousTraces.keySet();
149 for (String key : keys) {
150 fExperiment.removeChild(fPreviousTraces.get(key));
151 IResource resource = experiment.findMember(key);
152 try {
153 resource.delete(true, null);
154 } catch (CoreException e) {
155 e.printStackTrace();
156 }
157 }
158 fProject.refresh();
159
160 return true;
161 }
162
163 /**
164 * Create a link to the actual trace and set the trace type
165 */
166 private void createLink(IFolder experiment, TmfTraceElement trace, IResource resource, IPath location) {
167 IWorkspace workspace = ResourcesPlugin.getWorkspace();
168 try {
169 Map<QualifiedName, String> properties = trace.getResource().getPersistentProperties();
e12ecd30
BH
170 String bundleName = properties.get(TmfCommonConstants.TRACEBUNDLE);
171 String traceType = properties.get(TmfCommonConstants.TRACETYPE);
172 String iconUrl = properties.get(TmfCommonConstants.TRACEICON);
12c155f5
FC
173
174 if (resource instanceof IFolder) {
175 IFolder folder = experiment.getFolder(trace.getName());
176 if (workspace.validateLinkLocation(folder, location).isOK()) {
177 folder.createLink(location, IResource.REPLACE, null);
178 setProperties(folder, bundleName, traceType, iconUrl);
179
180 } else {
181 System.out.println("Invalid Trace Location"); //$NON-NLS-1$
182 }
183 } else {
184 IFile file = experiment.getFile(trace.getName());
185 if (workspace.validateLinkLocation(file, location).isOK()) {
186 file.createLink(location, IResource.REPLACE, null);
187 setProperties(file, bundleName, traceType, iconUrl);
188 } else {
189 System.out.println("Invalid Trace Location"); //$NON-NLS-1$
190 }
191 }
192 } catch (CoreException e) {
193 e.printStackTrace();
194 }
195 }
196
197 private void setProperties(IResource resource, String bundleName, String traceType, String iconUrl) throws CoreException {
e12ecd30
BH
198 resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, bundleName);
199 resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType);
200 resource.setPersistentProperty(TmfCommonConstants.TRACEICON, iconUrl);
12c155f5
FC
201 }
202
203 /**
204 * Get the list of selected traces
205 */
206 private TmfTraceElement[] getSelection() {
207 Vector<TmfTraceElement> traces = new Vector<TmfTraceElement>();
208 Object[] selection = fCheckboxTableViewer.getCheckedElements();
209 for (Object sel : selection) {
210 if (sel instanceof TmfTraceElement)
211 traces.add((TmfTraceElement) sel);
212 }
213 TmfTraceElement[] result = new TmfTraceElement[traces.size()];
214 traces.toArray(result);
215 return result;
216 }
217
218}
This page took 0.035175 seconds and 5 git commands to generate.