(no commit message)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / project / dialogs / ImportTraceWizardPage.java
CommitLineData
6e512b93
ASL
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 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.lttng.ui.views.project.dialogs;
14
15import org.eclipse.jface.viewers.IStructuredSelection;
16import org.eclipse.linuxtools.lttng.LttngException;
17import org.eclipse.linuxtools.lttng.trace.LTTngTraceVersion;
18import org.eclipse.linuxtools.lttng.ui.views.project.handlers.TraceErrorHandler;
19import org.eclipse.linuxtools.lttng.ui.views.project.model.LTTngProjectNode;
20import org.eclipse.ui.IWorkbench;
21import org.eclipse.ui.internal.wizards.datatransfer.WizardFileSystemResourceImportPage1;
22
23/**
24 * <b><u>ImportTraceWizardPage</u></b>
25 * <p>
26 * TODO: Implement me. Please.
27 */
28@SuppressWarnings("restriction")
29public class ImportTraceWizardPage extends WizardFileSystemResourceImportPage1 {
30
31 private boolean isContainerSet = false;
32 private String initialContainerString = "";
33 private String selectedSourceDirectory = "";
34
35 public ImportTraceWizardPage(IWorkbench workbench, IStructuredSelection selection) {
36 super(workbench, selection);
37
38 LTTngProjectNode folder = (LTTngProjectNode) selection.getFirstElement();
39 String path = folder.getTracesFolder().getFolder().getFullPath().toOSString();
40
41 initialContainerString = path;
42 setContainerFieldValue(path);
43 }
44
45
46 public String getTraceDirectory() {
47 String tmpPath = "";
48 if ( (getSourceDirectory() != null) && (getSourceDirectory().getName() != null) ) {
49 tmpPath = this.getSourceDirectory().getName().toString();
50 }
51
52 return tmpPath;
53 }
54
55 public String getInitialContainerString() {
56 return initialContainerString;
57 }
58
59 public String getTracepath() {
60 String tmpPath = "";
61 if ( (getSourceDirectory() != null) && (getSourceDirectory().getPath() != null) ) {
62 tmpPath = this.getSourceDirectory().getPath().toString();
63 }
64
65 return tmpPath;
66 }
67
68 public String getDestination() {
69 String returnPath = null;
70
71 if ( getContainerFullPath() != null ) {
72 returnPath = getContainerFullPath().toString();
73 }
74 return returnPath;
75 }
76
77 public boolean isSelectedElementsValidLttngTraces() {
78 boolean returnedValue = true;
79
80 // We don't want to test until something is selected
81 if ( selectionGroup.getCheckedElementCount() > 0 ) {
82
83 // We don't want to revalidate each time, only want a new directory is selected
84 if ( ! selectedSourceDirectory.equals(getSourceDirectory().getAbsolutePath().toString()) )
85 {
86 try {
87 if ( isPathLttngTrace( getSourceDirectory().getAbsolutePath() ) == false ) {
88 returnedValue = false;
89 selectedSourceDirectory = "";
90
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);
98 }
99 else {
100 selectedSourceDirectory = getSourceDirectory().getAbsolutePath();
101
102 if ( isContainerSet == false ) {
103 isContainerSet = true;
104
105 if ( ! getDestination().toString().equals( getInitialContainerString() + "/" + getTraceDirectory() ) ) {
106 // *** HACK ***
107 // Force a sane destination to avoid imported files to end up in the root of the "Traces/" directory
108 setContainerFieldValue(getInitialContainerString() + "/" + getTraceDirectory());
109 }
110 }
111 }
112 }
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 :" };
116
117 for ( int pos=0; pos<errorMessages.length; pos++) {
118 exceptionMessage = extendErrorMessage(exceptionMessage, errorMessages[pos]);
119 }
120
121 showVersionErrorPopup(exceptionMessage);
122 selectionGroup.setAllSelections(false);
123 returnedValue = false;
124 }
125 }
126 }
127 isContainerSet = false;
128
129 return returnedValue;
130 }
131
132
133 public boolean isPathLttngTrace(String path) throws LttngException {
134
135 boolean returnedValue = true;
136
137 // Ask for a LttngTraceVersion for the given path
138 LTTngTraceVersion traceVersion = new LTTngTraceVersion( path );
139
140 // If this is not a valid LTTng trace
141 if ( traceVersion.isValidLttngTrace() == false ) {
142 returnedValue = false;
143 }
144
145 return returnedValue;
146 }
147
148
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];
153 }
154 tmSwapMessage[oldErrorMessage.length] = lineToAdd;
155
156 return tmSwapMessage;
157 }
158
159
160 /**
161 * This function will show a version error popup that contain the given message.
162 *
163 */
164 public void showVersionErrorPopup(String[] errMessages) {
165 TraceErrorHandler errorDialog = new TraceErrorHandler(errMessages);
166 try {
167 errorDialog.execute(null);
168 }
169 catch (Exception e) {
170 e.printStackTrace();
171 }
172 }
173
174
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" })
179// @Override
180// public boolean finish() {
181// if (!ensureSourceIsValid()) {
182// return false;
183// }
184// saveWidgetValues();
185//
186// Iterator resourcesEnum = getSelectedResources().iterator();
187// List listRealFiles = new ArrayList();
188//
189// // ****
190// // HACK #1 :
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());
195//
196// listRealFiles.add(tmpRealFile);
197// }
198//
199// if (listRealFiles.size() > 0) {
200// // Call import ressources (code is below)
201// return importResources(listRealFiles);
202// }
203//
204// MessageDialog.openInformation(getContainer().getShell(),
205// DataTransferMessages.DataTransfer_information,
206// DataTransferMessages.FileImport_noneSelected);
207// return false;
208// }
209//
210// @Override
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*
215// //
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*
219// //
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
223// //
224// // Let's see how fun it is to do with mr. import wizard.
225//
226//
227// List listRealFilesShortPath = new ArrayList();
228// java.io.File newFullSource = getSourceDirectory();
229//
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;
238//
239// // ***
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/
247// //
248// // *BUG : However, we might need MULTIPLE sources since we could have MULTIPLE traces selected...
249// // THIS IS NOT HANDLED YET.
250//
251// // Make a new path like -> /home/billybob/mytraces/trace1/
252// String newParent = oldFile.getAbsolutePath().substring(0, oldFile.getAbsolutePath().lastIndexOf("/") );
253//
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() );
257//
258// // Create a new "full source" directory -> /home/billybob/mytraces/trace1/
259// newFullSource = new java.io.File( newParent );
260//
261// // Add our pretty file to the new List
262// listRealFilesShortPath.add(newShortPathFile);
263// }
264//
265// // ***
266// // HACK #3
267// // Now that we have everything, we need to AJUST THE DESTINATION
268// // To do so, we ajust the "ContainerValue" text field.
269// //
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/
276// //
277//
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());
281//
282// /*
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);
288// */
289//
290// // Finally import !!
291// ImportOperation operation = new ImportOperation(getContainerFullPath(), newFullSource, FileSystemStructureProvider.INSTANCE, this, listRealFilesShortPath);
292//
293// operation.setContext(getShell());
294// return executeImportOperation(operation);
295// }
296//
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;
301//
302// String errMessage[] = { "Couldn't get LTTng version number for the path : " };
303//
304// // We don't want to test until something is selected
305// if ( selectionGroup.getCheckedElementCount() > 0 ) {
306// try {
307// List<MinimizedFileSystemElement> selectionList = selectionGroup.getAllWhiteCheckedItems();
308// MinimizedFileSystemElement tmpSelectedElement = null;
309//
310// for ( int x=0; x<selectionList.size(); x++) {
311// tmpSelectedElement = selectionList.get(x);
312//
313// // *** VERIFY ***
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);
320// }
321// }
322// }
323//
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);
329// }
330// }
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;
336// }
337// }
338//
339// return returnedValue;
340// }
341
342}
This page took 0.038613 seconds and 5 git commands to generate.