1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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
.lttng
.ui
.views
.project
.dialogs
;
15 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
16 import org
.eclipse
.linuxtools
.lttng
.LttngException
;
17 import org
.eclipse
.linuxtools
.lttng
.trace
.LTTngTraceVersion
;
18 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.handlers
.TraceErrorHandler
;
19 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectNode
;
20 import org
.eclipse
.ui
.IWorkbench
;
21 import org
.eclipse
.ui
.internal
.wizards
.datatransfer
.WizardFileSystemResourceImportPage1
;
24 * <b><u>ImportTraceWizardPage</u></b>
26 * TODO: Implement me. Please.
28 @SuppressWarnings("restriction")
29 public class ImportTraceWizardPage
extends WizardFileSystemResourceImportPage1
{
31 private boolean isContainerSet
= false;
32 private String initialContainerString
= ""; //$NON-NLS-1$
33 private String selectedSourceDirectory
= ""; //$NON-NLS-1$
35 public ImportTraceWizardPage(IWorkbench workbench
, IStructuredSelection selection
) {
36 super(workbench
, selection
);
38 LTTngProjectNode folder
= (LTTngProjectNode
) selection
.getFirstElement();
39 String path
= folder
.getTracesFolder().getFolder().getFullPath().toOSString();
41 initialContainerString
= path
;
42 setContainerFieldValue(path
);
46 public String
getTraceDirectory() {
47 String tmpPath
= ""; //$NON-NLS-1$
48 if ( (getSourceDirectory() != null) && (getSourceDirectory().getName() != null) ) {
49 tmpPath
= this.getSourceDirectory().getName().toString();
55 public String
getInitialContainerString() {
56 return initialContainerString
;
59 public String
getTracepath() {
60 String tmpPath
= ""; //$NON-NLS-1$
61 if ( (getSourceDirectory() != null) && (getSourceDirectory().getPath() != null) ) {
62 tmpPath
= this.getSourceDirectory().getPath().toString();
68 public String
getDestination() {
69 String returnPath
= null;
71 if ( getContainerFullPath() != null ) {
72 returnPath
= getContainerFullPath().toString();
77 public boolean isSelectedElementsValidLttngTraces() {
78 boolean returnedValue
= true;
80 // We don't want to test until something is selected
81 if ( selectionGroup
.getCheckedElementCount() > 0 ) {
83 // We don't want to revalidate each time, only want a new directory is selected
84 if ( ! selectedSourceDirectory
.equals(getSourceDirectory().getAbsolutePath().toString()) )
87 if ( isPathLttngTrace( getSourceDirectory().getAbsolutePath() ) == false ) {
88 returnedValue
= false;
89 selectedSourceDirectory
= "";
91 String errMessage
[] = { "Couldn't get LTTng version number for the path : " };
92 errMessage
= extendErrorMessage(errMessage
, getSourceDirectory().getAbsolutePath() );
93 errMessage
= extendErrorMessage(errMessage
, "");
94 errMessage
= extendErrorMessage(errMessage
, "Verify that the directory is a valid LTTng trace directory.");
95 errMessage
= extendErrorMessage(errMessage
, "Make sure the top directory is the trace itself and not any of its parent.");
96 showVersionErrorPopup(errMessage
);
97 selectionGroup
.setAllSelections(false);
100 selectedSourceDirectory
= getSourceDirectory().getAbsolutePath();
102 if ( isContainerSet
== false ) {
103 isContainerSet
= true;
105 if ( ! getDestination().toString().equals( getInitialContainerString() + "/" + getTraceDirectory() ) ) {
107 // Force a sane destination to avoid imported files to end up in the root of the "Traces/" directory
108 setContainerFieldValue(getInitialContainerString() + "/" + getTraceDirectory());
113 catch (LttngException e
) {
114 String
[] errorMessages
= e
.toString().split("\n");
115 String exceptionMessage
[] = { "Version check failed for the path : ", this.getTracepath(), "", "Returned error was :" };
117 for ( int pos
=0; pos
<errorMessages
.length
; pos
++) {
118 exceptionMessage
= extendErrorMessage(exceptionMessage
, errorMessages
[pos
]);
121 showVersionErrorPopup(exceptionMessage
);
122 selectionGroup
.setAllSelections(false);
123 returnedValue
= false;
127 isContainerSet
= false;
129 return returnedValue
;
133 public boolean isPathLttngTrace(String path
) throws LttngException
{
135 boolean returnedValue
= true;
137 // Ask for a LttngTraceVersion for the given path
138 LTTngTraceVersion traceVersion
= new LTTngTraceVersion( path
);
140 // If this is not a valid LTTng trace
141 if ( traceVersion
.isValidLttngTrace() == false ) {
142 returnedValue
= false;
145 return returnedValue
;
149 public String
[] extendErrorMessage(String
[] oldErrorMessage
, String lineToAdd
) {
150 String tmSwapMessage
[] = new String
[oldErrorMessage
.length
+ 1];
151 for ( int pos
= 0; pos
<oldErrorMessage
.length
; pos
++) {
152 tmSwapMessage
[pos
] = oldErrorMessage
[pos
];
154 tmSwapMessage
[oldErrorMessage
.length
] = lineToAdd
;
156 return tmSwapMessage
;
161 * This function will show a version error popup that contain the given message.
164 public void showVersionErrorPopup(String
[] errMessages
) {
165 TraceErrorHandler errorDialog
= new TraceErrorHandler(errMessages
);
167 errorDialog
.execute(null);
169 catch (Exception e
) {
175 // // *** HACK HACK AND HACK ***
176 // // Everything below is a proof of concept on how we could tweak the import wizard to act according to our plan
177 // // Uncomment everything below if you want to test it, but please, does not put any of this into production
178 // @SuppressWarnings({ "unchecked", "rawtypes" })
180 // public boolean finish() {
181 // if (!ensureSourceIsValid()) {
184 // saveWidgetValues();
186 // Iterator resourcesEnum = getSelectedResources().iterator();
187 // List listRealFiles = new ArrayList();
191 // // We need to convert everything into java.io.File because ImportOperation does NOT support FileSystemElement
192 // while (resourcesEnum.hasNext()) {
193 // FileSystemElement tmpFileElement = ((FileSystemElement)resourcesEnum.next());
194 // java.io.File tmpRealFile = new java.io.File(tmpFileElement.getFileSystemObject().toString());
196 // listRealFiles.add(tmpRealFile);
199 // if (listRealFiles.size() > 0) {
200 // // Call import ressources (code is below)
201 // return importResources(listRealFiles);
204 // MessageDialog.openInformation(getContainer().getShell(),
205 // DataTransferMessages.DataTransfer_information,
206 // DataTransferMessages.FileImport_noneSelected);
211 // protected boolean importResources(List fileSystemObjects) {
212 // // *** Explanation of the hackssss
213 // // We want the import wizard to import everything in the form of :
214 // // trace1/ -> tracefiles*
216 // // However, the wizard is too dumb to do the following and will recreate the full architecture the user selected.
217 // // So, depending what the user select, we could end up with something like :
218 // // home/user/somewhere/trace1/ -> tracefiles*
220 // // Since there is nothing to do with that, we need to change the "source" and the "to-import files" to reflect this.
221 // // Basically, from the case above, the "source" should be changed to "trace1/" and "to-import files"
222 // // should have the correct parent so the wizard can still find them
224 // // Let's see how fun it is to do with mr. import wizard.
227 // List listRealFilesShortPath = new ArrayList();
228 // java.io.File newFullSource = getSourceDirectory();
230 // // We will loop for every "to-import full path files" we have and recreate "short path" files
231 // // Mean, the current path of the file is currently something like :
232 // // Path : /home/billybob/mytraces/trace1/metadata_0 Parent : null
233 // // And we want something less dumb like :
234 // // Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
235 // for (int pos=0; pos<fileSystemObjects.size(); pos++) {
236 // java.io.File oldFile = (java.io.File)fileSystemObjects.get(pos);
237 // java.io.File newShortPathFile = oldFile;
240 // // HACK #2 : We need to ajust the source of the files!
241 // // Our current source is probably like :
242 // // (Source) Path : / (or null?)
243 // // (Files) Path : /home/billybob/mytraces/trace1/metadata_0 Parent : null
244 // // We want something like :
245 // // (Source) Path : /home/billybob/mytraces/trace1/
246 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
248 // // *BUG : However, we might need MULTIPLE sources since we could have MULTIPLE traces selected...
249 // // THIS IS NOT HANDLED YET.
251 // // Make a new path like -> /home/billybob/mytraces/trace1/
252 // String newParent = oldFile.getAbsolutePath().substring(0, oldFile.getAbsolutePath().lastIndexOf("/") );
254 // // Create a "short path file" with the good parent from it. This give :
255 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
256 // newShortPathFile = new java.io.File(newParent, oldFile.getName() );
258 // // Create a new "full source" directory -> /home/billybob/mytraces/trace1/
259 // newFullSource = new java.io.File( newParent );
261 // // Add our pretty file to the new List
262 // listRealFilesShortPath.add(newShortPathFile);
267 // // Now that we have everything, we need to AJUST THE DESTINATION
268 // // To do so, we ajust the "ContainerValue" text field.
270 // // Right now we have something like :
271 // // Path -> /where/to/import/
272 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
273 // // We want something like :
274 // // Path -> /where/to/import/trace1/
275 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
278 // // We take the current text field and we add the "full source" name
279 // // Note : the "name" is the last directory name so "trace1" is returned for a path like "/home/billybob/mytraces/trace1/"
280 // setContainerFieldValue(getContainerFullPath() + "/" + newFullSource.getName());
283 // System.out.println("\n\n" + getContainerFullPath());
284 // System.out.println(newFullSource);
285 // System.out.println(FileSystemStructureProvider.INSTANCE);
286 // System.out.println(this.getContainer());
287 // System.out.println(fileSystemObjects);
290 // // Finally import !!
291 // ImportOperation operation = new ImportOperation(getContainerFullPath(), newFullSource, FileSystemStructureProvider.INSTANCE, this, listRealFilesShortPath);
293 // operation.setContext(getShell());
294 // return executeImportOperation(operation);
297 // // This function test if the selected directory are LTTng traces
298 // // This one is made to work with the madness above.
299 // public boolean isSelectedElementsValidLttngTraces() {
300 // boolean returnedValue = true;
302 // String errMessage[] = { "Couldn't get LTTng version number for the path : " };
304 // // We don't want to test until something is selected
305 // if ( selectionGroup.getCheckedElementCount() > 0 ) {
307 // List<MinimizedFileSystemElement> selectionList = selectionGroup.getAllWhiteCheckedItems();
308 // MinimizedFileSystemElement tmpSelectedElement = null;
310 // for ( int x=0; x<selectionList.size(); x++) {
311 // tmpSelectedElement = selectionList.get(x);
314 // // Not sure ALL directory are checked.
315 // if ( tmpSelectedElement.isDirectory() ) {
316 // String tmpPath = tmpSelectedElement.getFileSystemObject().toString();
317 // if ( isPathLttngTrace( tmpPath ) == false ) {
318 // returnedValue = false;
319 // errMessage = extendErrorMessage(errMessage, tmpPath);
324 // if ( returnedValue == false ) {
325 // errMessage = extendErrorMessage(errMessage, "");
326 // errMessage = extendErrorMessage(errMessage, "Verify that the directory is a valid LTTng trace directory.");
327 // showVersionErrorPopup(errMessage);
328 // selectionGroup.setAllSelections(false);
331 // catch (LttngException e) {
332 // String exceptionMessage[] = { "Version check failed for the path : ", this.getTracepath(), "", "Returned error was :", e.toString() };
333 // showVersionErrorPopup(exceptionMessage);
334 // selectionGroup.setAllSelections(false);
335 // returnedValue = false;
339 // return returnedValue;