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
.exceptions
.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
);
45 public String
getTraceDirectory() {
46 String tmpPath
= ""; //$NON-NLS-1$
47 if ((getSourceDirectory() != null) && (getSourceDirectory().getName() != null)) {
48 tmpPath
= this.getSourceDirectory().getName().toString();
54 public String
getInitialContainerString() {
55 return initialContainerString
;
58 public String
getTracepath() {
59 String tmpPath
= ""; //$NON-NLS-1$
60 if ((getSourceDirectory() != null) && (getSourceDirectory().getPath() != null)) {
61 tmpPath
= this.getSourceDirectory().getPath().toString();
67 public String
getDestination() {
68 String returnPath
= null;
70 if (getContainerFullPath() != null) {
71 returnPath
= getContainerFullPath().toString();
76 public boolean isSelectedElementsValidLttngTraces() {
77 boolean returnedValue
= true;
79 // We don't want to test until something is selected
80 if (selectionGroup
.getCheckedElementCount() > 0) {
82 // We don't want to revalidate each time, only want a new directory
84 if (!selectedSourceDirectory
.equals(getSourceDirectory().getAbsolutePath().toString())) {
86 if (isPathLttngTrace(getSourceDirectory().getAbsolutePath()) == false) {
87 returnedValue
= false;
88 selectedSourceDirectory
= ""; //$NON-NLS-1$
90 String errMessage
[] = { Messages
.ImportTraceWizardPage_BadTraceVersion
+ getSourceDirectory().getAbsolutePath() };
91 errMessage
= extendErrorMessage(errMessage
, ""); //$NON-NLS-1$
92 errMessage
= extendErrorMessage(errMessage
, Messages
.ImportTraceWizardPage_BadTraceVersionMsg1
);
93 errMessage
= extendErrorMessage(errMessage
, Messages
.ImportTraceWizardPage_BadTraceVersionMsg2
);
94 showVersionErrorPopup(errMessage
);
95 selectionGroup
.setAllSelections(false);
97 selectedSourceDirectory
= getSourceDirectory().getAbsolutePath();
99 if (isContainerSet
== false) {
100 isContainerSet
= true;
102 if (!getDestination().toString().equals(getInitialContainerString() + "/" + getTraceDirectory())) { //$NON-NLS-1$
104 // Force a sane destination to avoid imported
105 // files to end up in the root of the "Traces/"
107 setContainerFieldValue(getInitialContainerString() + "/" + getTraceDirectory()); //$NON-NLS-1$
111 } catch (LttngException e
) {
112 String
[] errorMessages
= e
.toString().split("\n"); //$NON-NLS-1$
113 String exceptionMessage
[] = { "Version check failed for the path : ", this.getTracepath(), "", "Returned error was :" }; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
115 for (int pos
= 0; pos
< errorMessages
.length
; pos
++) {
116 exceptionMessage
= extendErrorMessage(exceptionMessage
, errorMessages
[pos
]);
119 showVersionErrorPopup(exceptionMessage
);
120 selectionGroup
.setAllSelections(false);
121 returnedValue
= false;
125 isContainerSet
= false;
127 return returnedValue
;
130 public boolean isPathLttngTrace(String path
) throws LttngException
{
132 boolean returnedValue
= true;
134 // Ask for a LttngTraceVersion for the given path
135 LTTngTraceVersion traceVersion
= new LTTngTraceVersion(path
);
137 // If this is not a valid LTTng trace
138 if (traceVersion
.isValidLttngTrace() == false) {
139 returnedValue
= false;
142 return returnedValue
;
145 public String
[] extendErrorMessage(String
[] oldErrorMessage
, String lineToAdd
) {
146 String tmSwapMessage
[] = new String
[oldErrorMessage
.length
+ 1];
147 for (int pos
= 0; pos
< oldErrorMessage
.length
; pos
++) {
148 tmSwapMessage
[pos
] = oldErrorMessage
[pos
];
150 tmSwapMessage
[oldErrorMessage
.length
] = lineToAdd
;
152 return tmSwapMessage
;
156 * This function will show a version error popup that contain the given
160 public void showVersionErrorPopup(String
[] errMessages
) {
161 TraceErrorHandler errorDialog
= new TraceErrorHandler(errMessages
);
163 errorDialog
.execute(null);
164 } catch (Exception e
) {
169 // // *** HACK HACK AND HACK ***
170 // // Everything below is a proof of concept on how we could tweak the
171 // import wizard to act according to our plan
172 // // Uncomment everything below if you want to test it, but please, does
173 // not put any of this into production
174 // @SuppressWarnings({ "unchecked", "rawtypes" })
176 // public boolean finish() {
177 // if (!ensureSourceIsValid()) {
180 // saveWidgetValues();
182 // Iterator resourcesEnum = getSelectedResources().iterator();
183 // List listRealFiles = new ArrayList();
187 // // We need to convert everything into java.io.File because
188 // ImportOperation does NOT support FileSystemElement
189 // while (resourcesEnum.hasNext()) {
190 // FileSystemElement tmpFileElement =
191 // ((FileSystemElement)resourcesEnum.next());
192 // java.io.File tmpRealFile = new
193 // java.io.File(tmpFileElement.getFileSystemObject().toString());
195 // listRealFiles.add(tmpRealFile);
198 // if (listRealFiles.size() > 0) {
199 // // Call import ressources (code is below)
200 // return importResources(listRealFiles);
203 // MessageDialog.openInformation(getContainer().getShell(),
204 // DataTransferMessages.DataTransfer_information,
205 // DataTransferMessages.FileImport_noneSelected);
210 // protected boolean importResources(List fileSystemObjects) {
211 // // *** Explanation of the hackssss
212 // // We want the import wizard to import everything in the form of :
213 // // trace1/ -> tracefiles*
215 // // However, the wizard is too dumb to do the following and will recreate
216 // the full architecture the user selected.
217 // // So, depending what the user select, we could end up with something
219 // // home/user/somewhere/trace1/ -> tracefiles*
221 // // Since there is nothing to do with that, we need to change the "source"
222 // and the "to-import files" to reflect this.
223 // // Basically, from the case above, the "source" should be changed to
224 // "trace1/" and "to-import files"
225 // // should have the correct parent so the wizard can still find them
227 // // Let's see how fun it is to do with mr. import wizard.
230 // List listRealFilesShortPath = new ArrayList();
231 // java.io.File newFullSource = getSourceDirectory();
233 // // We will loop for every "to-import full path files" we have and
234 // recreate "short path" files
235 // // Mean, the current path of the file is currently something like :
236 // // Path : /home/billybob/mytraces/trace1/metadata_0 Parent : null
237 // // And we want something less dumb like :
238 // // Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
239 // for (int pos=0; pos<fileSystemObjects.size(); pos++) {
240 // java.io.File oldFile = (java.io.File)fileSystemObjects.get(pos);
241 // java.io.File newShortPathFile = oldFile;
244 // // HACK #2 : We need to ajust the source of the files!
245 // // Our current source is probably like :
246 // // (Source) Path : / (or null?)
247 // // (Files) Path : /home/billybob/mytraces/trace1/metadata_0 Parent : null
248 // // We want something like :
249 // // (Source) Path : /home/billybob/mytraces/trace1/
250 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
252 // // *BUG : However, we might need MULTIPLE sources since we could have
253 // MULTIPLE traces selected...
254 // // THIS IS NOT HANDLED YET.
256 // // Make a new path like -> /home/billybob/mytraces/trace1/
257 // String newParent = oldFile.getAbsolutePath().substring(0,
258 // oldFile.getAbsolutePath().lastIndexOf("/") );
260 // // Create a "short path file" with the good parent from it. This give :
261 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
262 // newShortPathFile = new java.io.File(newParent, oldFile.getName() );
264 // // Create a new "full source" directory ->
265 // /home/billybob/mytraces/trace1/
266 // newFullSource = new java.io.File( newParent );
268 // // Add our pretty file to the new List
269 // listRealFilesShortPath.add(newShortPathFile);
274 // // Now that we have everything, we need to AJUST THE DESTINATION
275 // // To do so, we ajust the "ContainerValue" text field.
277 // // Right now we have something like :
278 // // Path -> /where/to/import/
279 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
280 // // We want something like :
281 // // Path -> /where/to/import/trace1/
282 // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
285 // // We take the current text field and we add the "full source" name
286 // // Note : the "name" is the last directory name so "trace1" is returned
287 // for a path like "/home/billybob/mytraces/trace1/"
288 // setContainerFieldValue(getContainerFullPath() + "/" +
289 // newFullSource.getName());
292 // System.out.println("\n\n" + getContainerFullPath());
293 // System.out.println(newFullSource);
294 // System.out.println(FileSystemStructureProvider.INSTANCE);
295 // System.out.println(this.getContainer());
296 // System.out.println(fileSystemObjects);
299 // // Finally import !!
300 // ImportOperation operation = new ImportOperation(getContainerFullPath(),
301 // newFullSource, FileSystemStructureProvider.INSTANCE, this,
302 // listRealFilesShortPath);
304 // operation.setContext(getShell());
305 // return executeImportOperation(operation);
308 // // This function test if the selected directory are LTTng traces
309 // // This one is made to work with the madness above.
310 // public boolean isSelectedElementsValidLttngTraces() {
311 // boolean returnedValue = true;
313 // String errMessage[] = {
314 // "Couldn't get LTTng version number for the path : " };
316 // // We don't want to test until something is selected
317 // if ( selectionGroup.getCheckedElementCount() > 0 ) {
319 // List<MinimizedFileSystemElement> selectionList =
320 // selectionGroup.getAllWhiteCheckedItems();
321 // MinimizedFileSystemElement tmpSelectedElement = null;
323 // for ( int x=0; x<selectionList.size(); x++) {
324 // tmpSelectedElement = selectionList.get(x);
327 // // Not sure ALL directory are checked.
328 // if ( tmpSelectedElement.isDirectory() ) {
329 // String tmpPath = tmpSelectedElement.getFileSystemObject().toString();
330 // if ( isPathLttngTrace( tmpPath ) == false ) {
331 // returnedValue = false;
332 // errMessage = extendErrorMessage(errMessage, tmpPath);
337 // if ( returnedValue == false ) {
338 // errMessage = extendErrorMessage(errMessage, "");
339 // errMessage = extendErrorMessage(errMessage,
340 // "Verify that the directory is a valid LTTng trace directory.");
341 // showVersionErrorPopup(errMessage);
342 // selectionGroup.setAllSelections(false);
345 // catch (LttngException e) {
346 // String exceptionMessage[] = { "Version check failed for the path : ",
347 // this.getTracepath(), "", "Returned error was :", e.toString() };
348 // showVersionErrorPopup(exceptionMessage);
349 // selectionGroup.setAllSelections(false);
350 // returnedValue = false;
354 // return returnedValue;