1 /*******************************************************************************
2 * Copyright (c) 2009, 2011 Ericsson, MontaVista Software
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 * Yufen Kuo (ykuo@mvista.com) - add support to allow user specify trace library path
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.dialogs
;
16 import org
.eclipse
.core
.resources
.IFolder
;
17 import org
.eclipse
.core
.resources
.IProject
;
18 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
19 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
20 import org
.eclipse
.linuxtools
.lttng
.core
.TraceHelper
;
21 import org
.eclipse
.linuxtools
.lttng
.core
.exceptions
.LttngException
;
22 import org
.eclipse
.linuxtools
.lttng
.core
.trace
.LTTngTraceVersion
;
23 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.handlers
.TraceErrorHandler
;
24 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectNode
;
25 import org
.eclipse
.ui
.IWorkbench
;
26 import org
.eclipse
.ui
.internal
.wizards
.datatransfer
.WizardFileSystemResourceImportPage1
;
29 * <b><u>ImportTraceWizardPage</u></b>
31 * TODO: Implement me. Please.
34 @SuppressWarnings("restriction")
35 public class ImportTraceWizardPage
extends WizardFileSystemResourceImportPage1
{
37 private boolean isContainerSet
= false;
38 private String initialContainerString
= ""; //$NON-NLS-1$
39 private String selectedSourceDirectory
= ""; //$NON-NLS-1$
40 private IProject project
;
42 public ImportTraceWizardPage(IWorkbench workbench
, IStructuredSelection selection
) {
43 super(workbench
, selection
);
45 project
= (IProject
) selection
.getFirstElement();
46 IFolder folder
= project
.getFolder(LTTngProjectNode
.TRACE_FOLDER_NAME
);
48 MessageDialog
.openError(getShell(), Messages
.ImportTrace_ErrorTitle
, Messages
.ImportTrace_InvalidProject
);
50 String path
= folder
.getFullPath().toOSString();
52 initialContainerString
= path
;
53 setContainerFieldValue(path
);
56 public String
getTraceDirectory() {
57 String tmpPath
= ""; //$NON-NLS-1$
58 if ((getSourceDirectory() != null) && (getSourceDirectory().getName() != null)) {
59 tmpPath
= this.getSourceDirectory().getName().toString();
65 public String
getInitialContainerString() {
66 return initialContainerString
;
69 public String
getTracepath() {
70 String tmpPath
= ""; //$NON-NLS-1$
71 if ((getSourceDirectory() != null) && (getSourceDirectory().getPath() != null)) {
72 tmpPath
= this.getSourceDirectory().getPath().toString();
78 public String
getDestination() {
79 String returnPath
= null;
81 if (getContainerFullPath() != null) {
82 returnPath
= getContainerFullPath().toString();
87 public boolean isSelectedElementsValidLttngTraces() {
88 boolean returnedValue
= true;
90 // We don't want to test until something is selected
91 if (selectionGroup
.getCheckedElementCount() > 0) {
93 // We don't want to revalidate each time, only want a new directory
95 if (!selectedSourceDirectory
.equals(getSourceDirectory().getAbsolutePath().toString())) {
97 if (isPathLttngTrace(getSourceDirectory().getAbsolutePath()) == false) {
98 returnedValue
= false;
99 selectedSourceDirectory
= ""; //$NON-NLS-1$
101 String errMessage
[] = { Messages
.ImportTraceWizardPage_BadTraceVersion
+ getSourceDirectory().getAbsolutePath() };
102 errMessage
= extendErrorMessage(errMessage
, ""); //$NON-NLS-1$
103 errMessage
= extendErrorMessage(errMessage
, Messages
.ImportTraceWizardPage_BadTraceVersionMsg1
);
104 errMessage
= extendErrorMessage(errMessage
, Messages
.ImportTraceWizardPage_BadTraceVersionMsg2
);
105 showVersionErrorPopup(errMessage
);
106 selectionGroup
.setAllSelections(false);
108 selectedSourceDirectory
= getSourceDirectory().getAbsolutePath();
110 if (isContainerSet
== false) {
111 isContainerSet
= true;
113 if (!getDestination().toString().equals(getInitialContainerString() + "/" + getTraceDirectory())) { //$NON-NLS-1$
115 // Force a sane destination to avoid imported
116 // files to end up in the root of the "Traces/"
118 setContainerFieldValue(getInitialContainerString() + "/" + getTraceDirectory()); //$NON-NLS-1$
122 } catch (LttngException e
) {
123 String
[] errorMessages
= e
.toString().split("\n"); //$NON-NLS-1$
124 String exceptionMessage
[] = { "Version check failed for the path : ", this.getTracepath(), "", "Returned error was :" }; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
126 for (int pos
= 0; pos
< errorMessages
.length
; pos
++) {
127 exceptionMessage
= extendErrorMessage(exceptionMessage
, errorMessages
[pos
]);
130 showVersionErrorPopup(exceptionMessage
);
131 selectionGroup
.setAllSelections(false);
132 returnedValue
= false;
136 isContainerSet
= false;
138 return returnedValue
;
141 public boolean isPathLttngTrace(String path
) throws LttngException
{
143 boolean returnedValue
= true;
144 String traceLibPath
= TraceHelper
.getTraceLibDirFromProject(project
);
145 // Ask for a LttngTraceVersion for the given path
146 LTTngTraceVersion traceVersion
= new LTTngTraceVersion(path
, traceLibPath
);
148 // If this is not a valid LTTng trace
149 if (traceVersion
.isValidLttngTrace() == false) {
150 returnedValue
= false;
153 return returnedValue
;
156 public String
[] extendErrorMessage(String
[] oldErrorMessage
, String lineToAdd
) {
157 String tmSwapMessage
[] = new String
[oldErrorMessage
.length
+ 1];
158 for (int pos
= 0; pos
< oldErrorMessage
.length
; pos
++) {
159 tmSwapMessage
[pos
] = oldErrorMessage
[pos
];
161 tmSwapMessage
[oldErrorMessage
.length
] = lineToAdd
;
163 return tmSwapMessage
;
167 * This function will show a version error popup that contain the given
171 public void showVersionErrorPopup(String
[] errMessages
) {
172 TraceErrorHandler errorDialog
= new TraceErrorHandler(errMessages
);
174 errorDialog
.execute(null);
175 } catch (Exception e
) {
180 // // *** HACK HACK AND HACK ***
181 // // Everything below is a proof of concept on how we could tweak the
182 // import wizard to act according to our plan
183 // // Uncomment everything below if you want to test it, but please, does
184 // not put any of this into production
185 // @SuppressWarnings({ "unchecked", "rawtypes" })
187 // public boolean finish() {
188 // if (!ensureSourceIsValid()) {
191 // saveWidgetValues();
193 // Iterator resourcesEnum = getSelectedResources().iterator();
194 // List listRealFiles = new ArrayList();
198 // // We need to convert everything into java.io.File because
199 // ImportOperation does NOT support FileSystemElement
200 // while (resourcesEnum.hasNext()) {
201 // FileSystemElement tmpFileElement =
202 // ((FileSystemElement)resourcesEnum.next());
203 // java.io.File tmpRealFile = new
204 // java.io.File(tmpFileElement.getFileSystemObject().toString());
206 // listRealFiles.add(tmpRealFile);
209 // if (listRealFiles.size() > 0) {
210 // // Call import ressources (code is below)
211 // return importResources(listRealFiles);
214 // MessageDialog.openInformation(getContainer().getShell(),
215 // DataTransferMessages.DataTransfer_information,
216 // DataTransferMessages.FileImport_noneSelected);
221 // protected boolean importResources(List fileSystemObjects) {
222 // // *** Explanation of the hackssss
223 // // We want the import wizard to import everything in the form of :
224 // // trace1/ -> tracefiles*
226 // // However, the wizard is too dumb to do the following and will recreate
227 // the full architecture the user selected.
228 // // So, depending what the user select, we could end up with something
230 // // home/user/somewhere/trace1/ -> tracefiles*
232 // // Since there is nothing to do with that, we need to change the "source"
233 // and the "to-import files" to reflect this.
234 // // Basically, from the case above, the "source" should be changed to
235 // "trace1/" and "to-import files"
236 // // should have the correct parent so the wizard can still find them
238 // // Let's see how fun it is to do with mr. import wizard.
241 // List listRealFilesShortPath = new ArrayList();
242 // java.io.File newFullSource = getSourceDirectory();
244 // // We will loop for every "to-import full path files" we have and
245 // recreate "short path" files
246 // // Mean, the current path of the file is currently something like :
247 // // Path : /home/billybob/mytraces/trace1/metadata_0 Parent : null
248 // // And we want something less dumb like :
249 // // Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
250 // for (int pos=0; pos<fileSystemObjects.size(); pos++) {
251 // java.io.File oldFile = (java.io.File)fileSystemObjects.get(pos);
252 // java.io.File newShortPathFile = oldFile;
255 // // HACK #2 : We need to ajust the source of the files!
256 // // Our current source is probably like :
257 // // (Source) Path : / (or null?)
258 // // (Files) Path : /home/billybob/mytraces/trace1/metadata_0 Parent : null
259 // // We want something like :
260 // // (Source) Path : /home/billybob/mytraces/trace1/
261 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
263 // // *BUG : However, we might need MULTIPLE sources since we could have
264 // MULTIPLE traces selected...
265 // // THIS IS NOT HANDLED YET.
267 // // Make a new path like -> /home/billybob/mytraces/trace1/
268 // String newParent = oldFile.getAbsolutePath().substring(0,
269 // oldFile.getAbsolutePath().lastIndexOf("/") );
271 // // Create a "short path file" with the good parent from it. This give :
272 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
273 // newShortPathFile = new java.io.File(newParent, oldFile.getName() );
275 // // Create a new "full source" directory ->
276 // /home/billybob/mytraces/trace1/
277 // newFullSource = new java.io.File( newParent );
279 // // Add our pretty file to the new List
280 // listRealFilesShortPath.add(newShortPathFile);
285 // // Now that we have everything, we need to AJUST THE DESTINATION
286 // // To do so, we ajust the "ContainerValue" text field.
288 // // Right now we have something like :
289 // // Path -> /where/to/import/
290 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
291 // // We want something like :
292 // // Path -> /where/to/import/trace1/
293 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
296 // // We take the current text field and we add the "full source" name
297 // // Note : the "name" is the last directory name so "trace1" is returned
298 // for a path like "/home/billybob/mytraces/trace1/"
299 // setContainerFieldValue(getContainerFullPath() + "/" +
300 // newFullSource.getName());
303 // System.out.println("\n\n" + getContainerFullPath());
304 // System.out.println(newFullSource);
305 // System.out.println(FileSystemStructureProvider.INSTANCE);
306 // System.out.println(this.getContainer());
307 // System.out.println(fileSystemObjects);
310 // // Finally import !!
311 // ImportOperation operation = new ImportOperation(getContainerFullPath(),
312 // newFullSource, FileSystemStructureProvider.INSTANCE, this,
313 // listRealFilesShortPath);
315 // operation.setContext(getShell());
316 // return executeImportOperation(operation);
319 // // This function test if the selected directory are LTTng traces
320 // // This one is made to work with the madness above.
321 // public boolean isSelectedElementsValidLttngTraces() {
322 // boolean returnedValue = true;
324 // String errMessage[] = {
325 // "Couldn't get LTTng version number for the path : " };
327 // // We don't want to test until something is selected
328 // if ( selectionGroup.getCheckedElementCount() > 0 ) {
330 // List<MinimizedFileSystemElement> selectionList =
331 // selectionGroup.getAllWhiteCheckedItems();
332 // MinimizedFileSystemElement tmpSelectedElement = null;
334 // for ( int x=0; x<selectionList.size(); x++) {
335 // tmpSelectedElement = selectionList.get(x);
338 // // Not sure ALL directory are checked.
339 // if ( tmpSelectedElement.isDirectory() ) {
340 // String tmpPath = tmpSelectedElement.getFileSystemObject().toString();
341 // if ( isPathLttngTrace( tmpPath ) == false ) {
342 // returnedValue = false;
343 // errMessage = extendErrorMessage(errMessage, tmpPath);
348 // if ( returnedValue == false ) {
349 // errMessage = extendErrorMessage(errMessage, "");
350 // errMessage = extendErrorMessage(errMessage,
351 // "Verify that the directory is a valid LTTng trace directory.");
352 // showVersionErrorPopup(errMessage);
353 // selectionGroup.setAllSelections(false);
356 // catch (LttngException e) {
357 // String exceptionMessage[] = { "Version check failed for the path : ",
358 // this.getTracepath(), "", "Returned error was :", e.toString() };
359 // showVersionErrorPopup(exceptionMessage);
360 // selectionGroup.setAllSelections(false);
361 // returnedValue = false;
365 // return returnedValue;