Refactor the Histogram View (Bug352885)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / project / dialogs / ImportTraceWizardPage.java
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
13 package org.eclipse.linuxtools.lttng.ui.views.project.dialogs;
14
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;
22
23 /**
24 * <b><u>ImportTraceWizardPage</u></b>
25 * <p>
26 * TODO: Implement me. Please.
27 */
28 @SuppressWarnings("restriction")
29 public class ImportTraceWizardPage extends WizardFileSystemResourceImportPage1 {
30
31 private boolean isContainerSet = false;
32 private String initialContainerString = ""; //$NON-NLS-1$
33 private String selectedSourceDirectory = ""; //$NON-NLS-1$
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 public String getTraceDirectory() {
46 String tmpPath = ""; //$NON-NLS-1$
47 if ((getSourceDirectory() != null) && (getSourceDirectory().getName() != null)) {
48 tmpPath = this.getSourceDirectory().getName().toString();
49 }
50
51 return tmpPath;
52 }
53
54 public String getInitialContainerString() {
55 return initialContainerString;
56 }
57
58 public String getTracepath() {
59 String tmpPath = ""; //$NON-NLS-1$
60 if ((getSourceDirectory() != null) && (getSourceDirectory().getPath() != null)) {
61 tmpPath = this.getSourceDirectory().getPath().toString();
62 }
63
64 return tmpPath;
65 }
66
67 public String getDestination() {
68 String returnPath = null;
69
70 if (getContainerFullPath() != null) {
71 returnPath = getContainerFullPath().toString();
72 }
73 return returnPath;
74 }
75
76 public boolean isSelectedElementsValidLttngTraces() {
77 boolean returnedValue = true;
78
79 // We don't want to test until something is selected
80 if (selectionGroup.getCheckedElementCount() > 0) {
81
82 // We don't want to revalidate each time, only want a new directory
83 // is selected
84 if (!selectedSourceDirectory.equals(getSourceDirectory().getAbsolutePath().toString())) {
85 try {
86 if (isPathLttngTrace(getSourceDirectory().getAbsolutePath()) == false) {
87 returnedValue = false;
88 selectedSourceDirectory = ""; //$NON-NLS-1$
89
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);
96 } else {
97 selectedSourceDirectory = getSourceDirectory().getAbsolutePath();
98
99 if (isContainerSet == false) {
100 isContainerSet = true;
101
102 if (!getDestination().toString().equals(getInitialContainerString() + "/" + getTraceDirectory())) { //$NON-NLS-1$
103 // *** HACK ***
104 // Force a sane destination to avoid imported
105 // files to end up in the root of the "Traces/"
106 // directory
107 setContainerFieldValue(getInitialContainerString() + "/" + getTraceDirectory()); //$NON-NLS-1$
108 }
109 }
110 }
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$
114
115 for (int pos = 0; pos < errorMessages.length; pos++) {
116 exceptionMessage = extendErrorMessage(exceptionMessage, errorMessages[pos]);
117 }
118
119 showVersionErrorPopup(exceptionMessage);
120 selectionGroup.setAllSelections(false);
121 returnedValue = false;
122 }
123 }
124 }
125 isContainerSet = false;
126
127 return returnedValue;
128 }
129
130 public boolean isPathLttngTrace(String path) throws LttngException {
131
132 boolean returnedValue = true;
133
134 // Ask for a LttngTraceVersion for the given path
135 LTTngTraceVersion traceVersion = new LTTngTraceVersion(path);
136
137 // If this is not a valid LTTng trace
138 if (traceVersion.isValidLttngTrace() == false) {
139 returnedValue = false;
140 }
141
142 return returnedValue;
143 }
144
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];
149 }
150 tmSwapMessage[oldErrorMessage.length] = lineToAdd;
151
152 return tmSwapMessage;
153 }
154
155 /**
156 * This function will show a version error popup that contain the given
157 * message.
158 *
159 */
160 public void showVersionErrorPopup(String[] errMessages) {
161 TraceErrorHandler errorDialog = new TraceErrorHandler(errMessages);
162 try {
163 errorDialog.execute(null);
164 } catch (Exception e) {
165 e.printStackTrace();
166 }
167 }
168
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" })
175 // @Override
176 // public boolean finish() {
177 // if (!ensureSourceIsValid()) {
178 // return false;
179 // }
180 // saveWidgetValues();
181 //
182 // Iterator resourcesEnum = getSelectedResources().iterator();
183 // List listRealFiles = new ArrayList();
184 //
185 // // ****
186 // // HACK #1 :
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());
194 //
195 // listRealFiles.add(tmpRealFile);
196 // }
197 //
198 // if (listRealFiles.size() > 0) {
199 // // Call import ressources (code is below)
200 // return importResources(listRealFiles);
201 // }
202 //
203 // MessageDialog.openInformation(getContainer().getShell(),
204 // DataTransferMessages.DataTransfer_information,
205 // DataTransferMessages.FileImport_noneSelected);
206 // return false;
207 // }
208 //
209 // @Override
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*
214 // //
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
218 // like :
219 // // home/user/somewhere/trace1/ -> tracefiles*
220 // //
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
226 // //
227 // // Let's see how fun it is to do with mr. import wizard.
228 //
229 //
230 // List listRealFilesShortPath = new ArrayList();
231 // java.io.File newFullSource = getSourceDirectory();
232 //
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;
242 //
243 // // ***
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/
251 // //
252 // // *BUG : However, we might need MULTIPLE sources since we could have
253 // MULTIPLE traces selected...
254 // // THIS IS NOT HANDLED YET.
255 //
256 // // Make a new path like -> /home/billybob/mytraces/trace1/
257 // String newParent = oldFile.getAbsolutePath().substring(0,
258 // oldFile.getAbsolutePath().lastIndexOf("/") );
259 //
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() );
263 //
264 // // Create a new "full source" directory ->
265 // /home/billybob/mytraces/trace1/
266 // newFullSource = new java.io.File( newParent );
267 //
268 // // Add our pretty file to the new List
269 // listRealFilesShortPath.add(newShortPathFile);
270 // }
271 //
272 // // ***
273 // // HACK #3
274 // // Now that we have everything, we need to AJUST THE DESTINATION
275 // // To do so, we ajust the "ContainerValue" text field.
276 // //
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/
283 // //
284 //
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());
290 //
291 // /*
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);
297 // */
298 //
299 // // Finally import !!
300 // ImportOperation operation = new ImportOperation(getContainerFullPath(),
301 // newFullSource, FileSystemStructureProvider.INSTANCE, this,
302 // listRealFilesShortPath);
303 //
304 // operation.setContext(getShell());
305 // return executeImportOperation(operation);
306 // }
307 //
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;
312 //
313 // String errMessage[] = {
314 // "Couldn't get LTTng version number for the path : " };
315 //
316 // // We don't want to test until something is selected
317 // if ( selectionGroup.getCheckedElementCount() > 0 ) {
318 // try {
319 // List<MinimizedFileSystemElement> selectionList =
320 // selectionGroup.getAllWhiteCheckedItems();
321 // MinimizedFileSystemElement tmpSelectedElement = null;
322 //
323 // for ( int x=0; x<selectionList.size(); x++) {
324 // tmpSelectedElement = selectionList.get(x);
325 //
326 // // *** VERIFY ***
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);
333 // }
334 // }
335 // }
336 //
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);
343 // }
344 // }
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;
351 // }
352 // }
353 //
354 // return returnedValue;
355 // }
356
357 }
This page took 0.039058 seconds and 5 git commands to generate.