Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2132ed37c37f5a26b9338486c57841120d88a24a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*******************************************************************************
 * Copyright (c) 2009, 2011 Ericsson, MontaVista Software
 * 
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   Francois Chouinard - Initial API and implementation
 *   Yufen Kuo       (ykuo@mvista.com) - add support to allow user specify trace library path
 *******************************************************************************/

package org.eclipse.linuxtools.lttng.ui.views.project.dialogs;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.linuxtools.lttng.TraceHelper;
import org.eclipse.linuxtools.lttng.exceptions.LttngException;
import org.eclipse.linuxtools.lttng.trace.LTTngTraceVersion;
import org.eclipse.linuxtools.lttng.ui.views.project.handlers.TraceErrorHandler;
import org.eclipse.linuxtools.lttng.ui.views.project.model.LTTngProjectNode;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.internal.wizards.datatransfer.WizardFileSystemResourceImportPage1;

/**
 * <b><u>ImportTraceWizardPage</u></b>
 * <p>
 * TODO: Implement me. Please.
 */
@Deprecated
@SuppressWarnings("restriction")
public class ImportTraceWizardPage extends WizardFileSystemResourceImportPage1 {

    private boolean isContainerSet = false;
    private String initialContainerString = ""; //$NON-NLS-1$
    private String selectedSourceDirectory = ""; //$NON-NLS-1$
    private IProject project;

    public ImportTraceWizardPage(IWorkbench workbench, IStructuredSelection selection) {
	super(workbench, selection);

	project = (IProject) selection.getFirstElement();
	IFolder folder = project.getFolder(LTTngProjectNode.TRACE_FOLDER_NAME);
	if (folder == null) {
        MessageDialog.openError(getShell(), Messages.ImportTrace_ErrorTitle, Messages.ImportTrace_InvalidProject);
	}
	String path = folder.getFullPath().toOSString();

	initialContainerString = path;
	setContainerFieldValue(path);
    }

    public String getTraceDirectory() {
	String tmpPath = ""; //$NON-NLS-1$
	if ((getSourceDirectory() != null) && (getSourceDirectory().getName() != null)) {
	    tmpPath = this.getSourceDirectory().getName().toString();
	}

	return tmpPath;
    }

    public String getInitialContainerString() {
	return initialContainerString;
    }

    public String getTracepath() {
	String tmpPath = ""; //$NON-NLS-1$
	if ((getSourceDirectory() != null) && (getSourceDirectory().getPath() != null)) {
	    tmpPath = this.getSourceDirectory().getPath().toString();
	}

	return tmpPath;
    }

    public String getDestination() {
	String returnPath = null;

	if (getContainerFullPath() != null) {
	    returnPath = getContainerFullPath().toString();
	}
	return returnPath;
    }

    public boolean isSelectedElementsValidLttngTraces() {
	boolean returnedValue = true;

	// We don't want to test until something is selected
	if (selectionGroup.getCheckedElementCount() > 0) {

	    // We don't want to revalidate each time, only want a new directory
	    // is selected
	    if (!selectedSourceDirectory.equals(getSourceDirectory().getAbsolutePath().toString())) {
		try {
		    if (isPathLttngTrace(getSourceDirectory().getAbsolutePath()) == false) {
			returnedValue = false;
			selectedSourceDirectory = ""; //$NON-NLS-1$

			String errMessage[] = { Messages.ImportTraceWizardPage_BadTraceVersion + getSourceDirectory().getAbsolutePath() };
			errMessage = extendErrorMessage(errMessage, ""); //$NON-NLS-1$
			errMessage = extendErrorMessage(errMessage, Messages.ImportTraceWizardPage_BadTraceVersionMsg1);
			errMessage = extendErrorMessage(errMessage, Messages.ImportTraceWizardPage_BadTraceVersionMsg2);
			showVersionErrorPopup(errMessage);
			selectionGroup.setAllSelections(false);
		    } else {
			selectedSourceDirectory = getSourceDirectory().getAbsolutePath();

			if (isContainerSet == false) {
			    isContainerSet = true;

			    if (!getDestination().toString().equals(getInitialContainerString() + "/" + getTraceDirectory())) { //$NON-NLS-1$
				// *** HACK ***
				// Force a sane destination to avoid imported
				// files to end up in the root of the "Traces/"
				// directory
				setContainerFieldValue(getInitialContainerString() + "/" + getTraceDirectory()); //$NON-NLS-1$
			    }
			}
		    }
		} catch (LttngException e) {
		    String[] errorMessages = e.toString().split("\n"); //$NON-NLS-1$
		    String exceptionMessage[] = { "Version check failed for the path : ", this.getTracepath(), "", "Returned error was :" }; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$

		    for (int pos = 0; pos < errorMessages.length; pos++) {
			exceptionMessage = extendErrorMessage(exceptionMessage, errorMessages[pos]);
		    }

		    showVersionErrorPopup(exceptionMessage);
		    selectionGroup.setAllSelections(false);
		    returnedValue = false;
		}
	    }
	}
	isContainerSet = false;

	return returnedValue;
    }

    public boolean isPathLttngTrace(String path) throws LttngException {

	boolean returnedValue = true;
    String traceLibPath = TraceHelper.getTraceLibDirFromProject(project);
	// Ask for a LttngTraceVersion for the given path
	LTTngTraceVersion traceVersion = new LTTngTraceVersion(path, traceLibPath);

	// If this is not a valid LTTng trace
	if (traceVersion.isValidLttngTrace() == false) {
	    returnedValue = false;
	}

	return returnedValue;
    }

    public String[] extendErrorMessage(String[] oldErrorMessage, String lineToAdd) {
	String tmSwapMessage[] = new String[oldErrorMessage.length + 1];
	for (int pos = 0; pos < oldErrorMessage.length; pos++) {
	    tmSwapMessage[pos] = oldErrorMessage[pos];
	}
	tmSwapMessage[oldErrorMessage.length] = lineToAdd;

	return tmSwapMessage;
    }

    /**
     * This function will show a version error popup that contain the given
     * message.
     * 
     */
    public void showVersionErrorPopup(String[] errMessages) {
	TraceErrorHandler errorDialog = new TraceErrorHandler(errMessages);
	try {
	    errorDialog.execute(null);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    // // *** HACK HACK AND HACK ***
    // // Everything below is a proof of concept on how we could tweak the
    // import wizard to act according to our plan
    // // Uncomment everything below if you want to test it, but please, does
    // not put any of this into production
    // @SuppressWarnings({ "unchecked", "rawtypes" })
    // @Override
    // public boolean finish() {
    // if (!ensureSourceIsValid()) {
    // return false;
    // }
    // saveWidgetValues();
    //
    // Iterator resourcesEnum = getSelectedResources().iterator();
    // List listRealFiles = new ArrayList();
    //
    // // ****
    // // HACK #1 :
    // // We need to convert everything into java.io.File because
    // ImportOperation does NOT support FileSystemElement
    // while (resourcesEnum.hasNext()) {
    // FileSystemElement tmpFileElement =
    // ((FileSystemElement)resourcesEnum.next());
    // java.io.File tmpRealFile = new
    // java.io.File(tmpFileElement.getFileSystemObject().toString());
    //
    // listRealFiles.add(tmpRealFile);
    // }
    //
    // if (listRealFiles.size() > 0) {
    // // Call import ressources (code is below)
    // return importResources(listRealFiles);
    // }
    //
    // MessageDialog.openInformation(getContainer().getShell(),
    // DataTransferMessages.DataTransfer_information,
    // DataTransferMessages.FileImport_noneSelected);
    // return false;
    // }
    //
    // @Override
    // protected boolean importResources(List fileSystemObjects) {
    // // *** Explanation of the hackssss
    // // We want the import wizard to import everything in the form of :
    // // trace1/ -> tracefiles*
    // //
    // // However, the wizard is too dumb to do the following and will recreate
    // the full architecture the user selected.
    // // So, depending what the user select, we could end up with something
    // like :
    // // home/user/somewhere/trace1/ -> tracefiles*
    // //
    // // Since there is nothing to do with that, we need to change the "source"
    // and the "to-import files" to reflect this.
    // // Basically, from the case above, the "source" should be changed to
    // "trace1/" and "to-import files"
    // // should have the correct parent so the wizard can still find them
    // //
    // // Let's see how fun it is to do with mr. import wizard.
    //
    //
    // List listRealFilesShortPath = new ArrayList();
    // java.io.File newFullSource = getSourceDirectory();
    //
    // // We will loop for every "to-import full path files" we have and
    // recreate "short path" files
    // // Mean, the current path of the file is currently something like :
    // // Path : /home/billybob/mytraces/trace1/metadata_0 Parent : null
    // // And we want something less dumb like :
    // // Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
    // for (int pos=0; pos<fileSystemObjects.size(); pos++) {
    // java.io.File oldFile = (java.io.File)fileSystemObjects.get(pos);
    // java.io.File newShortPathFile = oldFile;
    //
    // // ***
    // // HACK #2 : We need to ajust the source of the files!
    // // Our current source is probably like :
    // // (Source) Path : / (or null?)
    // // (Files) Path : /home/billybob/mytraces/trace1/metadata_0 Parent : null
    // // We want something like :
    // // (Source) Path : /home/billybob/mytraces/trace1/
    // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
    // //
    // // *BUG : However, we might need MULTIPLE sources since we could have
    // MULTIPLE traces selected...
    // // THIS IS NOT HANDLED YET.
    //
    // // Make a new path like -> /home/billybob/mytraces/trace1/
    // String newParent = oldFile.getAbsolutePath().substring(0,
    // oldFile.getAbsolutePath().lastIndexOf("/") );
    //
    // // Create a "short path file" with the good parent from it. This give :
    // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
    // newShortPathFile = new java.io.File(newParent, oldFile.getName() );
    //
    // // Create a new "full source" directory ->
    // /home/billybob/mytraces/trace1/
    // newFullSource = new java.io.File( newParent );
    //
    // // Add our pretty file to the new List
    // listRealFilesShortPath.add(newShortPathFile);
    // }
    //
    // // ***
    // // HACK #3
    // // Now that we have everything, we need to AJUST THE DESTINATION
    // // To do so, we ajust the "ContainerValue" text field.
    // //
    // // Right now we have something like :
    // // Path -> /where/to/import/
    // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
    // // We want something like :
    // // Path -> /where/to/import/trace1/
    // // (Files) Path : metadata_0 Parent : /home/billybob/mytraces/trace1/
    // //
    //
    // // We take the current text field and we add the "full source" name
    // // Note : the "name" is the last directory name so "trace1" is returned
    // for a path like "/home/billybob/mytraces/trace1/"
    // setContainerFieldValue(getContainerFullPath() + "/" +
    // newFullSource.getName());
    //
    // /*
    // System.out.println("\n\n" + getContainerFullPath());
    // System.out.println(newFullSource);
    // System.out.println(FileSystemStructureProvider.INSTANCE);
    // System.out.println(this.getContainer());
    // System.out.println(fileSystemObjects);
    // */
    //
    // // Finally import !!
    // ImportOperation operation = new ImportOperation(getContainerFullPath(),
    // newFullSource, FileSystemStructureProvider.INSTANCE, this,
    // listRealFilesShortPath);
    //
    // operation.setContext(getShell());
    // return executeImportOperation(operation);
    // }
    //
    // // This function test if the selected directory are LTTng traces
    // // This one is made to work with the madness above.
    // public boolean isSelectedElementsValidLttngTraces() {
    // boolean returnedValue = true;
    //
    // String errMessage[] = {
    // "Couldn't get LTTng version number for the path : " };
    //
    // // We don't want to test until something is selected
    // if ( selectionGroup.getCheckedElementCount() > 0 ) {
    // try {
    // List<MinimizedFileSystemElement> selectionList =
    // selectionGroup.getAllWhiteCheckedItems();
    // MinimizedFileSystemElement tmpSelectedElement = null;
    //
    // for ( int x=0; x<selectionList.size(); x++) {
    // tmpSelectedElement = selectionList.get(x);
    //
    // // *** VERIFY ***
    // // Not sure ALL directory are checked.
    // if ( tmpSelectedElement.isDirectory() ) {
    // String tmpPath = tmpSelectedElement.getFileSystemObject().toString();
    // if ( isPathLttngTrace( tmpPath ) == false ) {
    // returnedValue = false;
    // errMessage = extendErrorMessage(errMessage, tmpPath);
    // }
    // }
    // }
    //
    // if ( returnedValue == false ) {
    // errMessage = extendErrorMessage(errMessage, "");
    // errMessage = extendErrorMessage(errMessage,
    // "Verify that the directory is a valid LTTng trace directory.");
    // showVersionErrorPopup(errMessage);
    // selectionGroup.setAllSelections(false);
    // }
    // }
    // catch (LttngException e) {
    // String exceptionMessage[] = { "Version check failed for the path : ",
    // this.getTracepath(), "", "Returned error was :", e.toString() };
    // showVersionErrorPopup(exceptionMessage);
    // selectionGroup.setAllSelections(false);
    // returnedValue = false;
    // }
    // }
    //
    // return returnedValue;
    // }

}

Back to the top