Fix for Bug354541 - TraceLibPath handling + JUnits
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / project / dialogs / ImportTraceWizardPage.java
CommitLineData
6e512b93 1/*******************************************************************************
a3767fd9 2 * Copyright (c) 2009, 2011 Ericsson, MontaVista Software
6e512b93
ASL
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
a3767fd9 11 * Yufen Kuo (ykuo@mvista.com) - add support to allow user specify trace library path
6e512b93
ASL
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.lttng.ui.views.project.dialogs;
15
a3767fd9 16import org.eclipse.core.resources.IProject;
6e512b93 17import org.eclipse.jface.viewers.IStructuredSelection;
c392540b 18import org.eclipse.linuxtools.lttng.exceptions.LttngException;
8e30d685 19import org.eclipse.linuxtools.lttng.trace.LTTngTraceVersion;
a3767fd9 20import org.eclipse.linuxtools.lttng.ui.TraceHelper;
8e30d685 21import org.eclipse.linuxtools.lttng.ui.views.project.handlers.TraceErrorHandler;
2fbfb67b 22import org.eclipse.linuxtools.lttng.ui.views.project.model.LTTngProjectNode;
6e512b93
ASL
23import org.eclipse.ui.IWorkbench;
24import org.eclipse.ui.internal.wizards.datatransfer.WizardFileSystemResourceImportPage1;
25
26/**
27 * <b><u>ImportTraceWizardPage</u></b>
28 * <p>
29 * TODO: Implement me. Please.
30 */
31@SuppressWarnings("restriction")
32public class ImportTraceWizardPage extends WizardFileSystemResourceImportPage1 {
a2e53983
FC
33
34 private boolean isContainerSet = false;
35 private String initialContainerString = ""; //$NON-NLS-1$
36 private String selectedSourceDirectory = ""; //$NON-NLS-1$
a3767fd9 37 private IProject project;
a2e53983
FC
38
39 public ImportTraceWizardPage(IWorkbench workbench, IStructuredSelection selection) {
40 super(workbench, selection);
41
42 LTTngProjectNode folder = (LTTngProjectNode) selection.getFirstElement();
a3767fd9 43 project = folder.getProject();
a2e53983
FC
44 String path = folder.getTracesFolder().getFolder().getFullPath().toOSString();
45
46 initialContainerString = path;
47 setContainerFieldValue(path);
48 }
49
50 public String getTraceDirectory() {
51 String tmpPath = ""; //$NON-NLS-1$
52 if ((getSourceDirectory() != null) && (getSourceDirectory().getName() != null)) {
53 tmpPath = this.getSourceDirectory().getName().toString();
8e30d685 54 }
a2e53983
FC
55
56 return tmpPath;
57 }
58
59 public String getInitialContainerString() {
60 return initialContainerString;
61 }
62
63 public String getTracepath() {
64 String tmpPath = ""; //$NON-NLS-1$
65 if ((getSourceDirectory() != null) && (getSourceDirectory().getPath() != null)) {
66 tmpPath = this.getSourceDirectory().getPath().toString();
8e30d685 67 }
a2e53983
FC
68
69 return tmpPath;
70 }
71
72 public String getDestination() {
73 String returnPath = null;
74
75 if (getContainerFullPath() != null) {
76 returnPath = getContainerFullPath().toString();
8e30d685 77 }
a2e53983
FC
78 return returnPath;
79 }
80
81 public boolean isSelectedElementsValidLttngTraces() {
82 boolean returnedValue = true;
83
84 // We don't want to test until something is selected
85 if (selectionGroup.getCheckedElementCount() > 0) {
86
87 // We don't want to revalidate each time, only want a new directory
88 // is selected
89 if (!selectedSourceDirectory.equals(getSourceDirectory().getAbsolutePath().toString())) {
90 try {
91 if (isPathLttngTrace(getSourceDirectory().getAbsolutePath()) == false) {
92 returnedValue = false;
93 selectedSourceDirectory = ""; //$NON-NLS-1$
94
95 String errMessage[] = { Messages.ImportTraceWizardPage_BadTraceVersion + getSourceDirectory().getAbsolutePath() };
96 errMessage = extendErrorMessage(errMessage, ""); //$NON-NLS-1$
97 errMessage = extendErrorMessage(errMessage, Messages.ImportTraceWizardPage_BadTraceVersionMsg1);
98 errMessage = extendErrorMessage(errMessage, Messages.ImportTraceWizardPage_BadTraceVersionMsg2);
99 showVersionErrorPopup(errMessage);
100 selectionGroup.setAllSelections(false);
101 } else {
102 selectedSourceDirectory = getSourceDirectory().getAbsolutePath();
103
104 if (isContainerSet == false) {
105 isContainerSet = true;
106
107 if (!getDestination().toString().equals(getInitialContainerString() + "/" + getTraceDirectory())) { //$NON-NLS-1$
108 // *** HACK ***
109 // Force a sane destination to avoid imported
110 // files to end up in the root of the "Traces/"
111 // directory
112 setContainerFieldValue(getInitialContainerString() + "/" + getTraceDirectory()); //$NON-NLS-1$
113 }
8e30d685 114 }
a2e53983
FC
115 }
116 } catch (LttngException e) {
117 String[] errorMessages = e.toString().split("\n"); //$NON-NLS-1$
118 String exceptionMessage[] = { "Version check failed for the path : ", this.getTracepath(), "", "Returned error was :" }; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
119
120 for (int pos = 0; pos < errorMessages.length; pos++) {
121 exceptionMessage = extendErrorMessage(exceptionMessage, errorMessages[pos]);
122 }
123
124 showVersionErrorPopup(exceptionMessage);
125 selectionGroup.setAllSelections(false);
126 returnedValue = false;
8e30d685 127 }
a2e53983 128 }
8e30d685 129 }
a2e53983
FC
130 isContainerSet = false;
131
132 return returnedValue;
133 }
134
135 public boolean isPathLttngTrace(String path) throws LttngException {
136
137 boolean returnedValue = true;
a3767fd9 138 String traceLibPath = TraceHelper.getTraceLibDirFromProject(project);
a2e53983 139 // Ask for a LttngTraceVersion for the given path
a3767fd9 140 LTTngTraceVersion traceVersion = new LTTngTraceVersion(path, traceLibPath);
a2e53983
FC
141
142 // If this is not a valid LTTng trace
143 if (traceVersion.isValidLttngTrace() == false) {
144 returnedValue = false;
8e30d685 145 }
a2e53983
FC
146
147 return returnedValue;
148 }
149
150 public String[] extendErrorMessage(String[] oldErrorMessage, String lineToAdd) {
151 String tmSwapMessage[] = new String[oldErrorMessage.length + 1];
152 for (int pos = 0; pos < oldErrorMessage.length; pos++) {
153 tmSwapMessage[pos] = oldErrorMessage[pos];
8e30d685 154 }
a2e53983
FC
155 tmSwapMessage[oldErrorMessage.length] = lineToAdd;
156
157 return tmSwapMessage;
158 }
159
8e30d685 160 /**
a2e53983
FC
161 * This function will show a version error popup that contain the given
162 * message.
8e30d685
WB
163 *
164 */
165 public void showVersionErrorPopup(String[] errMessages) {
a2e53983
FC
166 TraceErrorHandler errorDialog = new TraceErrorHandler(errMessages);
167 try {
168 errorDialog.execute(null);
169 } catch (Exception e) {
170 e.printStackTrace();
171 }
8e30d685 172 }
a2e53983
FC
173
174 // // *** HACK HACK AND HACK ***
175 // // Everything below is a proof of concept on how we could tweak the
176 // import wizard to act according to our plan
177 // // Uncomment everything below if you want to test it, but please, does
178 // not put any of this into production
179 // @SuppressWarnings({ "unchecked", "rawtypes" })
180 // @Override
181 // public boolean finish() {
182 // if (!ensureSourceIsValid()) {
183 // return false;
184 // }
185 // saveWidgetValues();
186 //
187 // Iterator resourcesEnum = getSelectedResources().iterator();
188 // List listRealFiles = new ArrayList();
189 //
190 // // ****
191 // // HACK #1 :
192 // // We need to convert everything into java.io.File because
193 // ImportOperation does NOT support FileSystemElement
194 // while (resourcesEnum.hasNext()) {
195 // FileSystemElement tmpFileElement =
196 // ((FileSystemElement)resourcesEnum.next());
197 // java.io.File tmpRealFile = new
198 // java.io.File(tmpFileElement.getFileSystemObject().toString());
199 //
200 // listRealFiles.add(tmpRealFile);
201 // }
202 //
203 // if (listRealFiles.size() > 0) {
204 // // Call import ressources (code is below)
205 // return importResources(listRealFiles);
206 // }
207 //
208 // MessageDialog.openInformation(getContainer().getShell(),
209 // DataTransferMessages.DataTransfer_information,
210 // DataTransferMessages.FileImport_noneSelected);
211 // return false;
212 // }
213 //
214 // @Override
215 // protected boolean importResources(List fileSystemObjects) {
216 // // *** Explanation of the hackssss
217 // // We want the import wizard to import everything in the form of :
218 // // trace1/ -> tracefiles*
219 // //
220 // // However, the wizard is too dumb to do the following and will recreate
221 // the full architecture the user selected.
222 // // So, depending what the user select, we could end up with something
223 // like :
224 // // home/user/somewhere/trace1/ -> tracefiles*
225 // //
226 // // Since there is nothing to do with that, we need to change the "source"
227 // and the "to-import files" to reflect this.
228 // // Basically, from the case above, the "source" should be changed to
229 // "trace1/" and "to-import files"
230 // // should have the correct parent so the wizard can still find them
231 // //
232 // // Let's see how fun it is to do with mr. import wizard.
233 //
234 //
235 // List listRealFilesShortPath = new ArrayList();
236 // java.io.File newFullSource = getSourceDirectory();
237 //
238 // // We will loop for every "to-import full path files" we have and
239 // recreate "short path" files
240 // // Mean, the current path of the file is currently something like :
241 // // Path : /home/billybob/mytraces/trace1/metadata_0 Parent : null
242 // // And we want something less dumb like :
243 // // Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
244 // for (int pos=0; pos<fileSystemObjects.size(); pos++) {
245 // java.io.File oldFile = (java.io.File)fileSystemObjects.get(pos);
246 // java.io.File newShortPathFile = oldFile;
247 //
248 // // ***
249 // // HACK #2 : We need to ajust the source of the files!
250 // // Our current source is probably like :
251 // // (Source) Path : / (or null?)
252 // // (Files) Path : /home/billybob/mytraces/trace1/metadata_0 Parent : null
253 // // We want something like :
254 // // (Source) Path : /home/billybob/mytraces/trace1/
255 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
256 // //
257 // // *BUG : However, we might need MULTIPLE sources since we could have
258 // MULTIPLE traces selected...
259 // // THIS IS NOT HANDLED YET.
260 //
261 // // Make a new path like -> /home/billybob/mytraces/trace1/
262 // String newParent = oldFile.getAbsolutePath().substring(0,
263 // oldFile.getAbsolutePath().lastIndexOf("/") );
264 //
265 // // Create a "short path file" with the good parent from it. This give :
266 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
267 // newShortPathFile = new java.io.File(newParent, oldFile.getName() );
268 //
269 // // Create a new "full source" directory ->
270 // /home/billybob/mytraces/trace1/
271 // newFullSource = new java.io.File( newParent );
272 //
273 // // Add our pretty file to the new List
274 // listRealFilesShortPath.add(newShortPathFile);
275 // }
276 //
277 // // ***
278 // // HACK #3
279 // // Now that we have everything, we need to AJUST THE DESTINATION
280 // // To do so, we ajust the "ContainerValue" text field.
281 // //
282 // // Right now we have something like :
283 // // Path -> /where/to/import/
284 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
285 // // We want something like :
286 // // Path -> /where/to/import/trace1/
287 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
288 // //
289 //
290 // // We take the current text field and we add the "full source" name
291 // // Note : the "name" is the last directory name so "trace1" is returned
292 // for a path like "/home/billybob/mytraces/trace1/"
293 // setContainerFieldValue(getContainerFullPath() + "/" +
294 // newFullSource.getName());
295 //
296 // /*
297 // System.out.println("\n\n" + getContainerFullPath());
298 // System.out.println(newFullSource);
299 // System.out.println(FileSystemStructureProvider.INSTANCE);
300 // System.out.println(this.getContainer());
301 // System.out.println(fileSystemObjects);
302 // */
303 //
304 // // Finally import !!
305 // ImportOperation operation = new ImportOperation(getContainerFullPath(),
306 // newFullSource, FileSystemStructureProvider.INSTANCE, this,
307 // listRealFilesShortPath);
308 //
309 // operation.setContext(getShell());
310 // return executeImportOperation(operation);
311 // }
312 //
313 // // This function test if the selected directory are LTTng traces
314 // // This one is made to work with the madness above.
315 // public boolean isSelectedElementsValidLttngTraces() {
316 // boolean returnedValue = true;
317 //
318 // String errMessage[] = {
319 // "Couldn't get LTTng version number for the path : " };
320 //
321 // // We don't want to test until something is selected
322 // if ( selectionGroup.getCheckedElementCount() > 0 ) {
323 // try {
324 // List<MinimizedFileSystemElement> selectionList =
325 // selectionGroup.getAllWhiteCheckedItems();
326 // MinimizedFileSystemElement tmpSelectedElement = null;
327 //
328 // for ( int x=0; x<selectionList.size(); x++) {
329 // tmpSelectedElement = selectionList.get(x);
330 //
331 // // *** VERIFY ***
332 // // Not sure ALL directory are checked.
333 // if ( tmpSelectedElement.isDirectory() ) {
334 // String tmpPath = tmpSelectedElement.getFileSystemObject().toString();
335 // if ( isPathLttngTrace( tmpPath ) == false ) {
336 // returnedValue = false;
337 // errMessage = extendErrorMessage(errMessage, tmpPath);
338 // }
339 // }
340 // }
341 //
342 // if ( returnedValue == false ) {
343 // errMessage = extendErrorMessage(errMessage, "");
344 // errMessage = extendErrorMessage(errMessage,
345 // "Verify that the directory is a valid LTTng trace directory.");
346 // showVersionErrorPopup(errMessage);
347 // selectionGroup.setAllSelections(false);
348 // }
349 // }
350 // catch (LttngException e) {
351 // String exceptionMessage[] = { "Version check failed for the path : ",
352 // this.getTracepath(), "", "Returned error was :", e.toString() };
353 // showVersionErrorPopup(exceptionMessage);
354 // selectionGroup.setAllSelections(false);
355 // returnedValue = false;
356 // }
357 // }
358 //
359 // return returnedValue;
360 // }
361
6e512b93 362}
This page took 0.043208 seconds and 5 git commands to generate.