Commit | Line | Data |
---|---|---|
002f9f07 | 1 | /******************************************************************************* |
252c602c | 2 | * Copyright (c) 2009, 2014 Ericsson and others. |
002f9f07 BH |
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 | * Francois Chouinard - Got rid of dependency on internal platform class | |
12 | * Francois Chouinard - Complete re-design | |
13 | * Anna Dushistova(Montavista) - [383047] NPE while importing a CFT trace | |
14 | * Matthew Khouzam - Moved out some common functions | |
15 | * Patrick Tasse - Add sorting of file system elements | |
252c602c | 16 | * Bernd Hufmann - Re-design of trace selection and trace validation |
0e1d517e | 17 | * Marc-Andre Laperle - Preserve folder structure on import |
002f9f07 BH |
18 | *******************************************************************************/ |
19 | ||
1de10308 | 20 | package org.eclipse.linuxtools.internal.tmf.ui.project.wizards.importtrace; |
002f9f07 BH |
21 | |
22 | import java.io.File; | |
16f91bd5 | 23 | import java.io.InputStream; |
002f9f07 BH |
24 | import java.lang.reflect.InvocationTargetException; |
25 | import java.util.ArrayList; | |
29ceb238 | 26 | import java.util.Collection; |
002f9f07 BH |
27 | import java.util.HashMap; |
28 | import java.util.Iterator; | |
002f9f07 BH |
29 | import java.util.List; |
30 | import java.util.Map; | |
002f9f07 BH |
31 | |
32 | import org.eclipse.core.resources.IContainer; | |
33 | import org.eclipse.core.resources.IFolder; | |
34 | import org.eclipse.core.resources.IProject; | |
35 | import org.eclipse.core.resources.IResource; | |
36 | import org.eclipse.core.resources.ResourcesPlugin; | |
37 | import org.eclipse.core.runtime.CoreException; | |
38 | import org.eclipse.core.runtime.IPath; | |
252c602c | 39 | import org.eclipse.core.runtime.IProgressMonitor; |
002f9f07 | 40 | import org.eclipse.core.runtime.IStatus; |
252c602c | 41 | import org.eclipse.core.runtime.NullProgressMonitor; |
002f9f07 BH |
42 | import org.eclipse.core.runtime.Path; |
43 | import org.eclipse.core.runtime.Platform; | |
252c602c BH |
44 | import org.eclipse.core.runtime.Status; |
45 | import org.eclipse.core.runtime.SubMonitor; | |
46 | import org.eclipse.core.runtime.SubProgressMonitor; | |
89730b51 | 47 | import org.eclipse.core.runtime.URIUtil; |
252c602c | 48 | import org.eclipse.jface.dialogs.IDialogSettings; |
16f91bd5 | 49 | import org.eclipse.jface.dialogs.MessageDialog; |
252c602c BH |
50 | import org.eclipse.jface.operation.IRunnableWithProgress; |
51 | import org.eclipse.jface.operation.ModalContext; | |
282218e9 MAL |
52 | import org.eclipse.jface.viewers.CheckStateChangedEvent; |
53 | import org.eclipse.jface.viewers.ICheckStateListener; | |
002f9f07 BH |
54 | import org.eclipse.jface.viewers.IStructuredSelection; |
55 | import org.eclipse.jface.viewers.ITreeContentProvider; | |
56 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
252c602c | 57 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
002f9f07 | 58 | import org.eclipse.linuxtools.tmf.core.TmfProjectNature; |
252c602c | 59 | import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceImportException; |
002f9f07 BH |
60 | import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType; |
61 | import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper; | |
6ca50a1d | 62 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement; |
002f9f07 | 63 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry; |
16f91bd5 | 64 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
002f9f07 BH |
65 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
66 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceTypeUIUtils; | |
339d539c | 67 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTracesFolder; |
16f91bd5 | 68 | import org.eclipse.osgi.util.NLS; |
002f9f07 BH |
69 | import org.eclipse.swt.SWT; |
70 | import org.eclipse.swt.custom.BusyIndicator; | |
71 | import org.eclipse.swt.events.FocusEvent; | |
72 | import org.eclipse.swt.events.FocusListener; | |
73 | import org.eclipse.swt.events.KeyEvent; | |
74 | import org.eclipse.swt.events.KeyListener; | |
2f7b3dd7 BH |
75 | import org.eclipse.swt.events.ModifyEvent; |
76 | import org.eclipse.swt.events.ModifyListener; | |
002f9f07 BH |
77 | import org.eclipse.swt.events.SelectionAdapter; |
78 | import org.eclipse.swt.events.SelectionEvent; | |
002f9f07 BH |
79 | import org.eclipse.swt.layout.GridData; |
80 | import org.eclipse.swt.layout.GridLayout; | |
81 | import org.eclipse.swt.widgets.Button; | |
82 | import org.eclipse.swt.widgets.Combo; | |
83 | import org.eclipse.swt.widgets.Composite; | |
84 | import org.eclipse.swt.widgets.DirectoryDialog; | |
85 | import org.eclipse.swt.widgets.Event; | |
86 | import org.eclipse.swt.widgets.Group; | |
87 | import org.eclipse.swt.widgets.Label; | |
88 | import org.eclipse.ui.IWorkbench; | |
89 | import org.eclipse.ui.dialogs.FileSystemElement; | |
252c602c | 90 | import org.eclipse.ui.dialogs.IOverwriteQuery; |
002f9f07 | 91 | import org.eclipse.ui.dialogs.WizardResourceImportPage; |
282218e9 | 92 | import org.eclipse.ui.internal.ide.DialogUtil; |
29ceb238 | 93 | import org.eclipse.ui.internal.ide.dialogs.IElementFilter; |
252c602c | 94 | import org.eclipse.ui.model.AdaptableList; |
002f9f07 | 95 | import org.eclipse.ui.model.WorkbenchContentProvider; |
282218e9 MAL |
96 | import org.eclipse.ui.model.WorkbenchLabelProvider; |
97 | import org.eclipse.ui.model.WorkbenchViewerComparator; | |
002f9f07 BH |
98 | import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider; |
99 | import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider; | |
100 | import org.eclipse.ui.wizards.datatransfer.ImportOperation; | |
101 | ||
102 | /** | |
252c602c BH |
103 | * A variant of the standard resource import wizard for importing traces |
104 | * to given tracing project. If no project or tracing project was selected | |
105 | * the wizard imports it to the default tracing project which is created | |
106 | * if necessary. | |
107 | * | |
108 | * In our case traces could be files or a directory structure. This wizard | |
109 | * supports both cases. It imports traces for a selected trace type or, if | |
110 | * no trace type is selected, it tries to detect the trace type automatically. | |
111 | * However, the automatic detection is a best-effort and cannot guarantee | |
112 | * that the detection is successful. The reason for this is that there might | |
113 | * be multiple trace types that can be assigned to a single trace. | |
114 | * | |
002f9f07 | 115 | * |
002f9f07 BH |
116 | * @author Francois Chouinard |
117 | * @since 2.0 | |
118 | */ | |
29ceb238 | 119 | @SuppressWarnings("restriction") |
002f9f07 BH |
120 | public class ImportTraceWizardPage extends WizardResourceImportPage { |
121 | ||
002f9f07 BH |
122 | // ------------------------------------------------------------------------ |
123 | // Constants | |
124 | // ------------------------------------------------------------------------ | |
002f9f07 | 125 | private static final String IMPORT_WIZARD_PAGE = "ImportTraceWizardPage"; //$NON-NLS-1$ |
6781d5b9 | 126 | private static final String IMPORT_WIZARD_ROOT_DIRECTORY_ID = IMPORT_WIZARD_PAGE + ".import_root_directory_id"; //$NON-NLS-1$ |
252c602c | 127 | private static final String IMPORT_WIZARD_IMPORT_UNRECOGNIZED_ID = IMPORT_WIZARD_PAGE + ".import_unrecognized_traces_id"; //$NON-NLS-1$ |
ff13a025 | 128 | private static final String IMPORT_WIZARD_PRESERVE_FOLDERS_ID = IMPORT_WIZARD_PAGE + ".import_preserve_folders_id"; //$NON-NLS-1$ |
252c602c BH |
129 | private static final String SEPARATOR = ":"; //$NON-NLS-1$ |
130 | private static final String AUTO_DETECT = Messages.ImportTraceWizard_AutoDetection; | |
002f9f07 BH |
131 | |
132 | // ------------------------------------------------------------------------ | |
133 | // Attributes | |
134 | // ------------------------------------------------------------------------ | |
135 | ||
002f9f07 BH |
136 | // Target import directory ('Traces' folder) |
137 | private IFolder fTargetFolder; | |
16f91bd5 BH |
138 | // Target Trace folder element |
139 | private TmfTraceFolder fTraceFolderElement; | |
252c602c BH |
140 | // Flag to handle destination folder change event |
141 | private Boolean fIsDestinationChanged = false; | |
142 | // Combo box containing trace types | |
143 | private Combo fTraceTypes; | |
144 | // Button to ignore unrecognized traces or not | |
145 | private Button fImportUnrecognizedButton; | |
146 | // Button to overwrite existing resources or not | |
147 | private Button fOverwriteExistingResourcesCheckbox; | |
148 | // Button to link or copy traces to workspace | |
149 | private Button fCreateLinksInWorkspaceButton; | |
0e1d517e MAL |
150 | // Button to preserve folder structure |
151 | private Button fPreserveFolderStructureButton; | |
252c602c BH |
152 | private boolean entryChanged = false; |
153 | /** The directory name field */ | |
154 | protected Combo directoryNameField; | |
155 | /** The directory browse button. */ | |
156 | protected Button directoryBrowseButton; | |
002f9f07 | 157 | |
282218e9 MAL |
158 | /** |
159 | * ResourceTreeAndListGroup was internal in Kepler and we referenced it. It | |
160 | * is now removed in Luna. To keep our builds compatible with Kepler, we | |
161 | * need to have our own version of this class. Once we stop supporting | |
162 | * Kepler, we can delete this class and use the public one from the | |
163 | * platform. | |
164 | */ | |
165 | private ResourceTreeAndListGroup fSelectionGroup; | |
166 | ||
002f9f07 BH |
167 | // ------------------------------------------------------------------------ |
168 | // Constructors | |
169 | // ------------------------------------------------------------------------ | |
170 | ||
171 | /** | |
172 | * Constructor. Creates the trace wizard page. | |
173 | * | |
174 | * @param name | |
175 | * The name of the page. | |
176 | * @param selection | |
177 | * The current selection | |
178 | */ | |
179 | protected ImportTraceWizardPage(String name, IStructuredSelection selection) { | |
180 | super(name, selection); | |
181 | } | |
182 | ||
282218e9 MAL |
183 | /** |
184 | * Create the import source selection widget. (Copied from WizardResourceImportPage | |
185 | * but instead always uses the internal ResourceTreeAndListGroup to keep compatibility | |
186 | * with Kepler) | |
187 | */ | |
188 | @Override | |
189 | protected void createFileSelectionGroup(Composite parent) { | |
190 | ||
191 | //Just create with a dummy root. | |
192 | fSelectionGroup = new ResourceTreeAndListGroup(parent, | |
193 | new FileSystemElement("Dummy", null, true),//$NON-NLS-1$ | |
194 | getFolderProvider(), new WorkbenchLabelProvider(), | |
195 | getFileProvider(), new WorkbenchLabelProvider(), SWT.NONE, | |
196 | DialogUtil.inRegularFontMode(parent)); | |
197 | ||
198 | ICheckStateListener listener = new ICheckStateListener() { | |
199 | @Override | |
200 | public void checkStateChanged(CheckStateChangedEvent event) { | |
201 | updateWidgetEnablements(); | |
202 | } | |
203 | }; | |
204 | ||
205 | WorkbenchViewerComparator comparator = new WorkbenchViewerComparator(); | |
206 | fSelectionGroup.setTreeComparator(comparator); | |
207 | fSelectionGroup.setListComparator(comparator); | |
208 | fSelectionGroup.addCheckStateListener(listener); | |
209 | ||
210 | } | |
211 | ||
002f9f07 BH |
212 | /** |
213 | * Constructor | |
214 | * | |
215 | * @param workbench | |
216 | * The workbench reference. | |
217 | * @param selection | |
218 | * The current selection | |
219 | */ | |
220 | public ImportTraceWizardPage(IWorkbench workbench, IStructuredSelection selection) { | |
221 | this(IMPORT_WIZARD_PAGE, selection); | |
222 | setTitle(Messages.ImportTraceWizard_FileSystemTitle); | |
223 | setDescription(Messages.ImportTraceWizard_ImportTrace); | |
224 | ||
225 | // Locate the target trace folder | |
226 | IFolder traceFolder = null; | |
227 | Object element = selection.getFirstElement(); | |
228 | ||
229 | if (element instanceof TmfTraceFolder) { | |
16f91bd5 | 230 | fTraceFolderElement = (TmfTraceFolder) element; |
16f91bd5 | 231 | traceFolder = fTraceFolderElement.getResource(); |
002f9f07 BH |
232 | } else if (element instanceof IProject) { |
233 | IProject project = (IProject) element; | |
234 | try { | |
235 | if (project.hasNature(TmfProjectNature.ID)) { | |
6ca50a1d BH |
236 | TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true); |
237 | fTraceFolderElement = projectElement.getTracesFolder(); | |
339d539c | 238 | traceFolder = project.getFolder(TmfTracesFolder.TRACES_FOLDER_NAME); |
002f9f07 BH |
239 | } |
240 | } catch (CoreException e) { | |
241 | } | |
242 | } | |
243 | ||
6ca50a1d BH |
244 | // If no tracing project was selected or trace folder doesn't exist use |
245 | // default tracing project | |
252c602c BH |
246 | if (traceFolder == null) { |
247 | IProject project = TmfProjectRegistry.createProject( | |
248 | TmfCommonConstants.DEFAULT_TRACE_PROJECT_NAME, null, new NullProgressMonitor()); | |
6ca50a1d BH |
249 | TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true); |
250 | fTraceFolderElement = projectElement.getTracesFolder(); | |
339d539c | 251 | traceFolder = project.getFolder(TmfTracesFolder.TRACES_FOLDER_NAME); |
252c602c BH |
252 | } |
253 | ||
002f9f07 BH |
254 | // Set the target trace folder |
255 | if (traceFolder != null) { | |
256 | fTargetFolder = traceFolder; | |
1d6c9022 | 257 | String path = traceFolder.getFullPath().toString(); |
002f9f07 BH |
258 | setContainerFieldValue(path); |
259 | } | |
260 | } | |
261 | ||
262 | // ------------------------------------------------------------------------ | |
263 | // WizardResourceImportPage | |
264 | // ------------------------------------------------------------------------ | |
265 | ||
002f9f07 BH |
266 | @Override |
267 | protected void createSourceGroup(Composite parent) { | |
268 | createDirectorySelectionGroup(parent); | |
269 | createFileSelectionGroup(parent); | |
270 | createTraceTypeGroup(parent); | |
271 | validateSourceGroup(); | |
272 | } | |
273 | ||
274 | @Override | |
252c602c BH |
275 | protected ITreeContentProvider getFileProvider() { |
276 | return new WorkbenchContentProvider() { | |
002f9f07 | 277 | @Override |
252c602c BH |
278 | public Object[] getChildren(Object object) { |
279 | if (object instanceof TraceFileSystemElement) { | |
280 | TraceFileSystemElement element = (TraceFileSystemElement) object; | |
281 | return element.getFiles().getChildren(element); | |
002f9f07 | 282 | } |
252c602c | 283 | return new Object[0]; |
002f9f07 | 284 | } |
252c602c | 285 | }; |
002f9f07 BH |
286 | } |
287 | ||
288 | @Override | |
289 | protected ITreeContentProvider getFolderProvider() { | |
002f9f07 BH |
290 | return new WorkbenchContentProvider() { |
291 | @Override | |
292 | public Object[] getChildren(Object o) { | |
252c602c BH |
293 | if (o instanceof TraceFileSystemElement) { |
294 | TraceFileSystemElement element = (TraceFileSystemElement) o; | |
295 | return element.getFolders().getChildren(); | |
002f9f07 BH |
296 | } |
297 | return new Object[0]; | |
298 | } | |
002f9f07 | 299 | |
252c602c BH |
300 | @Override |
301 | public boolean hasChildren(Object o) { | |
302 | if (o instanceof TraceFileSystemElement) { | |
303 | TraceFileSystemElement element = (TraceFileSystemElement) o; | |
304 | if (element.isPopulated()) { | |
305 | return getChildren(element).length > 0; | |
306 | } | |
307 | //If we have not populated then wait until asked | |
308 | return true; | |
002f9f07 | 309 | } |
252c602c | 310 | return false; |
002f9f07 | 311 | } |
252c602c | 312 | }; |
002f9f07 BH |
313 | } |
314 | ||
315 | // ------------------------------------------------------------------------ | |
316 | // Directory Selection Group (forked WizardFileSystemResourceImportPage1) | |
317 | // ------------------------------------------------------------------------ | |
318 | ||
002f9f07 BH |
319 | /** |
320 | * creates the directory selection group. | |
321 | * | |
322 | * @param parent | |
323 | * the parent composite | |
324 | */ | |
325 | protected void createDirectorySelectionGroup(Composite parent) { | |
326 | ||
327 | Composite directoryContainerGroup = new Composite(parent, SWT.NONE); | |
328 | GridLayout layout = new GridLayout(); | |
329 | layout.numColumns = 3; | |
330 | directoryContainerGroup.setLayout(layout); | |
331 | directoryContainerGroup.setFont(parent.getFont()); | |
332 | directoryContainerGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); | |
333 | ||
334 | // Label ("Trace directory:") | |
335 | Label groupLabel = new Label(directoryContainerGroup, SWT.NONE); | |
336 | groupLabel.setText(Messages.ImportTraceWizard_DirectoryLocation); | |
337 | groupLabel.setFont(parent.getFont()); | |
338 | ||
339 | // Directory name entry field | |
340 | directoryNameField = new Combo(directoryContainerGroup, SWT.BORDER); | |
341 | GridData data = new GridData(SWT.FILL, SWT.FILL, true, false); | |
342 | data.widthHint = SIZING_TEXT_FIELD_WIDTH; | |
343 | directoryNameField.setLayoutData(data); | |
344 | directoryNameField.setFont(parent.getFont()); | |
345 | ||
346 | directoryNameField.addSelectionListener(new SelectionAdapter() { | |
347 | @Override | |
348 | public void widgetSelected(SelectionEvent e) { | |
349 | updateFromSourceField(); | |
350 | } | |
351 | }); | |
352 | ||
353 | directoryNameField.addKeyListener(new KeyListener() { | |
354 | @Override | |
355 | public void keyPressed(KeyEvent e) { | |
002f9f07 BH |
356 | if (e.character == SWT.CR) { // Windows... |
357 | entryChanged = false; | |
358 | updateFromSourceField(); | |
359 | } | |
360 | } | |
361 | ||
362 | @Override | |
363 | public void keyReleased(KeyEvent e) { | |
364 | } | |
365 | }); | |
366 | ||
367 | directoryNameField.addFocusListener(new FocusListener() { | |
368 | @Override | |
369 | public void focusGained(FocusEvent e) { | |
370 | // Do nothing when getting focus | |
371 | } | |
002f9f07 BH |
372 | @Override |
373 | public void focusLost(FocusEvent e) { | |
374 | // Clear the flag to prevent constant update | |
375 | if (entryChanged) { | |
376 | entryChanged = false; | |
377 | updateFromSourceField(); | |
378 | } | |
379 | } | |
380 | }); | |
381 | ||
2f7b3dd7 BH |
382 | directoryNameField.addModifyListener(new ModifyListener() { |
383 | @Override | |
384 | public void modifyText(ModifyEvent e) { | |
385 | entryChanged = true; | |
386 | } | |
387 | }); | |
388 | ||
002f9f07 BH |
389 | // Browse button |
390 | directoryBrowseButton = new Button(directoryContainerGroup, SWT.PUSH); | |
391 | directoryBrowseButton.setText(Messages.ImportTraceWizard_BrowseButton); | |
392 | directoryBrowseButton.addListener(SWT.Selection, this); | |
393 | directoryBrowseButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); | |
394 | directoryBrowseButton.setFont(parent.getFont()); | |
395 | setButtonLayoutData(directoryBrowseButton); | |
396 | } | |
397 | ||
398 | // ------------------------------------------------------------------------ | |
399 | // Browse for the source directory | |
400 | // ------------------------------------------------------------------------ | |
401 | ||
402 | @Override | |
403 | public void handleEvent(Event event) { | |
404 | if (event.widget == directoryBrowseButton) { | |
405 | handleSourceDirectoryBrowseButtonPressed(); | |
406 | } | |
252c602c BH |
407 | |
408 | // Avoid overwriting destination path without repeatedly trigger | |
409 | // call of handleEvent(); | |
410 | synchronized (fIsDestinationChanged) { | |
411 | if (fIsDestinationChanged == false) { | |
412 | event.display.asyncExec(new Runnable() { | |
413 | @Override | |
414 | public void run() { | |
415 | synchronized (fIsDestinationChanged) { | |
416 | fIsDestinationChanged = true; | |
1d6c9022 | 417 | String path = fTargetFolder.getFullPath().toString(); |
252c602c BH |
418 | setContainerFieldValue(path); |
419 | } | |
420 | } | |
421 | }); | |
422 | } else { | |
423 | fIsDestinationChanged = false; | |
424 | } | |
425 | } | |
002f9f07 BH |
426 | super.handleEvent(event); |
427 | } | |
428 | ||
252c602c BH |
429 | @Override |
430 | protected void handleContainerBrowseButtonPressed() { | |
431 | // Do nothing so that destination directory cannot be changed. | |
432 | } | |
433 | ||
002f9f07 BH |
434 | /** |
435 | * Handle the button pressed event | |
436 | */ | |
437 | protected void handleSourceDirectoryBrowseButtonPressed() { | |
438 | String currentSource = directoryNameField.getText(); | |
439 | DirectoryDialog dialog = new DirectoryDialog(directoryNameField.getShell(), SWT.SAVE | SWT.SHEET); | |
440 | dialog.setText(Messages.ImportTraceWizard_SelectTraceDirectoryTitle); | |
441 | dialog.setMessage(Messages.ImportTraceWizard_SelectTraceDirectoryMessage); | |
442 | dialog.setFilterPath(getSourceDirectoryName(currentSource)); | |
443 | ||
444 | String selectedDirectory = dialog.open(); | |
445 | if (selectedDirectory != null) { | |
446 | // Just quit if the directory is not valid | |
447 | if ((getSourceDirectory(selectedDirectory) == null) || selectedDirectory.equals(currentSource)) { | |
448 | return; | |
449 | } | |
450 | // If it is valid then proceed to populate | |
451 | setErrorMessage(null); | |
452 | setSourceName(selectedDirectory); | |
453 | } | |
454 | } | |
455 | ||
456 | private File getSourceDirectory() { | |
457 | return getSourceDirectory(directoryNameField.getText()); | |
458 | } | |
459 | ||
460 | private static File getSourceDirectory(String path) { | |
461 | File sourceDirectory = new File(getSourceDirectoryName(path)); | |
462 | if (!sourceDirectory.exists() || !sourceDirectory.isDirectory()) { | |
463 | return null; | |
464 | } | |
465 | ||
466 | return sourceDirectory; | |
467 | } | |
468 | ||
469 | private static String getSourceDirectoryName(String sourceName) { | |
470 | IPath result = new Path(sourceName.trim()); | |
471 | if (result.getDevice() != null && result.segmentCount() == 0) { | |
472 | result = result.addTrailingSeparator(); | |
473 | } else { | |
474 | result = result.removeTrailingSeparator(); | |
475 | } | |
476 | return result.toOSString(); | |
477 | } | |
478 | ||
002f9f07 BH |
479 | private void updateFromSourceField() { |
480 | setSourceName(directoryNameField.getText()); | |
481 | updateWidgetEnablements(); | |
482 | } | |
483 | ||
484 | private void setSourceName(String path) { | |
485 | if (path.length() > 0) { | |
486 | String[] currentItems = directoryNameField.getItems(); | |
487 | int selectionIndex = -1; | |
488 | for (int i = 0; i < currentItems.length; i++) { | |
489 | if (currentItems[i].equals(path)) { | |
490 | selectionIndex = i; | |
491 | } | |
492 | } | |
493 | if (selectionIndex < 0) { | |
494 | int oldLength = currentItems.length; | |
495 | String[] newItems = new String[oldLength + 1]; | |
496 | System.arraycopy(currentItems, 0, newItems, 0, oldLength); | |
497 | newItems[oldLength] = path; | |
498 | directoryNameField.setItems(newItems); | |
499 | selectionIndex = oldLength; | |
500 | } | |
501 | directoryNameField.select(selectionIndex); | |
502 | } | |
503 | resetSelection(); | |
504 | } | |
505 | ||
506 | // ------------------------------------------------------------------------ | |
507 | // File Selection Group (forked WizardFileSystemResourceImportPage1) | |
508 | // ------------------------------------------------------------------------ | |
002f9f07 | 509 | private void resetSelection() { |
252c602c | 510 | TraceFileSystemElement root = getFileSystemTree(); |
282218e9 | 511 | fSelectionGroup.setRoot(root); |
002f9f07 BH |
512 | } |
513 | ||
252c602c | 514 | private TraceFileSystemElement getFileSystemTree() { |
002f9f07 BH |
515 | File sourceDirectory = getSourceDirectory(); |
516 | if (sourceDirectory == null) { | |
517 | return null; | |
518 | } | |
519 | return selectFiles(sourceDirectory, FileSystemStructureProvider.INSTANCE); | |
520 | } | |
521 | ||
252c602c BH |
522 | private TraceFileSystemElement selectFiles(final Object rootFileSystemObject, |
523 | final IImportStructureProvider structureProvider) { | |
524 | final TraceFileSystemElement[] results = new TraceFileSystemElement[1]; | |
002f9f07 BH |
525 | BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() { |
526 | @Override | |
527 | public void run() { | |
528 | // Create the root element from the supplied file system object | |
529 | results[0] = createRootElement(rootFileSystemObject, structureProvider); | |
530 | } | |
531 | }); | |
532 | return results[0]; | |
533 | } | |
534 | ||
252c602c | 535 | private static TraceFileSystemElement createRootElement(Object fileSystemObject, |
002f9f07 BH |
536 | IImportStructureProvider provider) { |
537 | ||
538 | boolean isContainer = provider.isFolder(fileSystemObject); | |
539 | String elementLabel = provider.getLabel(fileSystemObject); | |
540 | ||
252c602c BH |
541 | // Use an empty label so that display of the element's full name |
542 | // doesn't include a confusing label | |
543 | TraceFileSystemElement dummyParent = new TraceFileSystemElement("", null, true);//$NON-NLS-1$ | |
16f91bd5 | 544 | dummyParent.setFileSystemObject(((File)fileSystemObject).getParentFile()); |
252c602c BH |
545 | dummyParent.setPopulated(); |
546 | TraceFileSystemElement result = new TraceFileSystemElement( | |
547 | elementLabel, dummyParent, isContainer); | |
548 | result.setFileSystemObject(fileSystemObject); | |
002f9f07 | 549 | |
252c602c BH |
550 | //Get the files for the element so as to build the first level |
551 | result.getFiles(); | |
002f9f07 BH |
552 | |
553 | return dummyParent; | |
554 | } | |
555 | ||
556 | // ------------------------------------------------------------------------ | |
557 | // Trace Type Group | |
558 | // ------------------------------------------------------------------------ | |
002f9f07 BH |
559 | private final void createTraceTypeGroup(Composite parent) { |
560 | Composite composite = new Composite(parent, SWT.NONE); | |
561 | GridLayout layout = new GridLayout(); | |
562 | layout.numColumns = 3; | |
563 | layout.makeColumnsEqualWidth = false; | |
564 | composite.setLayout(layout); | |
565 | composite.setFont(parent.getFont()); | |
566 | GridData buttonData = new GridData(SWT.FILL, SWT.FILL, true, false); | |
567 | composite.setLayoutData(buttonData); | |
568 | ||
569 | // Trace type label ("Trace Type:") | |
570 | Label typeLabel = new Label(composite, SWT.NONE); | |
571 | typeLabel.setText(Messages.ImportTraceWizard_TraceType); | |
572 | typeLabel.setFont(parent.getFont()); | |
573 | ||
574 | // Trace type combo | |
252c602c | 575 | fTraceTypes = new Combo(composite, SWT.BORDER | SWT.READ_ONLY); |
002f9f07 BH |
576 | GridData data = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1); |
577 | fTraceTypes.setLayoutData(data); | |
578 | fTraceTypes.setFont(parent.getFont()); | |
579 | ||
580 | String[] availableTraceTypes = TmfTraceType.getInstance().getAvailableTraceTypes(); | |
252c602c BH |
581 | String[] traceTypeList = new String[availableTraceTypes.length + 1]; |
582 | traceTypeList[0] = AUTO_DETECT; | |
583 | for (int i = 0; i < availableTraceTypes.length; i++) { | |
584 | traceTypeList[i + 1] = availableTraceTypes[i]; | |
585 | } | |
586 | fTraceTypes.setItems(traceTypeList); | |
e2a50e26 | 587 | fTraceTypes.addSelectionListener(new SelectionAdapter() { |
002f9f07 BH |
588 | @Override |
589 | public void widgetSelected(SelectionEvent e) { | |
590 | updateWidgetEnablements(); | |
e2a50e26 BH |
591 | boolean enabled = fTraceTypes.getText().equals(AUTO_DETECT); |
592 | fImportUnrecognizedButton.setEnabled(enabled); | |
002f9f07 BH |
593 | } |
594 | }); | |
252c602c BH |
595 | fTraceTypes.select(0); |
596 | ||
16f91bd5 | 597 | // Unrecognized checkbox |
252c602c BH |
598 | fImportUnrecognizedButton = new Button(composite, SWT.CHECK); |
599 | fImportUnrecognizedButton.setSelection(true); | |
600 | fImportUnrecognizedButton.setText(Messages.ImportTraceWizard_ImportUnrecognized); | |
002f9f07 BH |
601 | } |
602 | ||
603 | // ------------------------------------------------------------------------ | |
604 | // Options | |
605 | // ------------------------------------------------------------------------ | |
606 | ||
002f9f07 BH |
607 | @Override |
608 | protected void createOptionsGroupButtons(Group optionsGroup) { | |
609 | ||
610 | // Overwrite checkbox | |
252c602c BH |
611 | fOverwriteExistingResourcesCheckbox = new Button(optionsGroup, SWT.CHECK); |
612 | fOverwriteExistingResourcesCheckbox.setFont(optionsGroup.getFont()); | |
613 | fOverwriteExistingResourcesCheckbox.setText(Messages.ImportTraceWizard_OverwriteExistingTrace); | |
614 | fOverwriteExistingResourcesCheckbox.setSelection(false); | |
002f9f07 BH |
615 | |
616 | // Create links checkbox | |
252c602c BH |
617 | fCreateLinksInWorkspaceButton = new Button(optionsGroup, SWT.CHECK); |
618 | fCreateLinksInWorkspaceButton.setFont(optionsGroup.getFont()); | |
619 | fCreateLinksInWorkspaceButton.setText(Messages.ImportTraceWizard_CreateLinksInWorkspace); | |
620 | fCreateLinksInWorkspaceButton.setSelection(true); | |
002f9f07 | 621 | |
252c602c | 622 | fCreateLinksInWorkspaceButton.addSelectionListener(new SelectionAdapter() { |
002f9f07 BH |
623 | @Override |
624 | public void widgetSelected(SelectionEvent e) { | |
625 | updateWidgetEnablements(); | |
626 | } | |
627 | }); | |
628 | ||
0e1d517e MAL |
629 | fPreserveFolderStructureButton = new Button(optionsGroup, SWT.CHECK); |
630 | fPreserveFolderStructureButton.setFont(optionsGroup.getFont()); | |
631 | fPreserveFolderStructureButton.setText(Messages.ImportTraceWizard_PreserveFolderStructure); | |
632 | fPreserveFolderStructureButton.setSelection(true); | |
633 | ||
002f9f07 BH |
634 | updateWidgetEnablements(); |
635 | } | |
636 | ||
637 | // ------------------------------------------------------------------------ | |
638 | // Determine if the finish button can be enabled | |
639 | // ------------------------------------------------------------------------ | |
002f9f07 BH |
640 | @Override |
641 | public boolean validateSourceGroup() { | |
642 | ||
643 | File sourceDirectory = getSourceDirectory(); | |
644 | if (sourceDirectory == null) { | |
645 | setMessage(Messages.ImportTraceWizard_SelectTraceSourceEmpty); | |
646 | return false; | |
647 | } | |
648 | ||
649 | if (sourceConflictsWithDestination(new Path(sourceDirectory.getPath()))) { | |
650 | setMessage(null); | |
651 | setErrorMessage(getSourceConflictMessage()); | |
652 | return false; | |
653 | } | |
654 | ||
282218e9 | 655 | if (fSelectionGroup.getCheckedElementCount() == 0) { |
002f9f07 BH |
656 | setMessage(null); |
657 | setErrorMessage(Messages.ImportTraceWizard_SelectTraceNoneSelected); | |
658 | return false; | |
659 | } | |
660 | ||
661 | IContainer container = getSpecifiedContainer(); | |
662 | if (container != null && container.isVirtual()) { | |
663 | if (Platform.getPreferencesService().getBoolean(Activator.PLUGIN_ID, ResourcesPlugin.PREF_DISABLE_LINKING, false, null)) { | |
664 | setMessage(null); | |
665 | setErrorMessage(Messages.ImportTraceWizard_CannotImportFilesUnderAVirtualFolder); | |
666 | return false; | |
667 | } | |
252c602c | 668 | if (fCreateLinksInWorkspaceButton == null || !fCreateLinksInWorkspaceButton.getSelection()) { |
002f9f07 BH |
669 | setMessage(null); |
670 | setErrorMessage(Messages.ImportTraceWizard_HaveToCreateLinksUnderAVirtualFolder); | |
671 | return false; | |
672 | } | |
673 | } | |
674 | ||
002f9f07 BH |
675 | setErrorMessage(null); |
676 | return true; | |
677 | } | |
678 | ||
6781d5b9 BH |
679 | @Override |
680 | protected void restoreWidgetValues() { | |
681 | super.restoreWidgetValues(); | |
682 | ||
683 | IDialogSettings settings = getDialogSettings(); | |
684 | boolean value; | |
685 | if (settings.get(IMPORT_WIZARD_IMPORT_UNRECOGNIZED_ID) == null) { | |
686 | value = true; | |
687 | } else { | |
688 | value = settings.getBoolean(IMPORT_WIZARD_IMPORT_UNRECOGNIZED_ID); | |
689 | } | |
690 | fImportUnrecognizedButton.setSelection(value); | |
691 | ||
692 | if (settings.get(IMPORT_WIZARD_PRESERVE_FOLDERS_ID) == null) { | |
693 | value = true; | |
694 | } else { | |
695 | value = settings.getBoolean(IMPORT_WIZARD_PRESERVE_FOLDERS_ID); | |
696 | } | |
697 | fPreserveFolderStructureButton.setSelection(value); | |
698 | ||
699 | String[] directoryNames = settings.getArray(IMPORT_WIZARD_ROOT_DIRECTORY_ID); | |
700 | if ((directoryNames != null) && (directoryNames.length != 0)) { | |
701 | for (int i = 0; i < directoryNames.length; i++) { | |
702 | directoryNameField.add(directoryNames[i]); | |
703 | } | |
704 | } | |
705 | } | |
706 | ||
707 | @Override | |
708 | protected void saveWidgetValues() { | |
709 | // Persist dialog settings | |
710 | IDialogSettings settings = getDialogSettings(); | |
711 | settings.put(IMPORT_WIZARD_IMPORT_UNRECOGNIZED_ID, fImportUnrecognizedButton.getSelection()); | |
712 | settings.put(IMPORT_WIZARD_PRESERVE_FOLDERS_ID, fPreserveFolderStructureButton.getSelection()); | |
713 | ||
714 | // update directory names history | |
715 | String[] directoryNames = settings.getArray(IMPORT_WIZARD_ROOT_DIRECTORY_ID); | |
716 | if (directoryNames == null) { | |
717 | directoryNames = new String[0]; | |
718 | } | |
719 | ||
720 | String items[] = directoryNameField.getItems(); | |
721 | for (int i = 0; i < items.length; i++) { | |
722 | directoryNames = addToHistory(directoryNames, items[i]); | |
723 | } | |
724 | settings.put(IMPORT_WIZARD_ROOT_DIRECTORY_ID, directoryNames); | |
725 | } | |
726 | ||
002f9f07 BH |
727 | // ------------------------------------------------------------------------ |
728 | // Import the trace(s) | |
729 | // ------------------------------------------------------------------------ | |
730 | ||
731 | /** | |
732 | * Finish the import. | |
733 | * | |
252c602c | 734 | * @return <code>true</code> if successful else <code>false</code> |
002f9f07 BH |
735 | */ |
736 | public boolean finish() { | |
252c602c BH |
737 | String traceTypeName = fTraceTypes.getText(); |
738 | String traceId = null; | |
739 | if (!AUTO_DETECT.equals(traceTypeName)) { | |
740 | String tokens[] = traceTypeName.split(SEPARATOR, 2); | |
741 | if (tokens.length < 2) { | |
742 | return false; | |
743 | } | |
744 | traceId = TmfTraceType.getInstance().getTraceTypeId(tokens[0], tokens[1]); | |
002f9f07 BH |
745 | } |
746 | ||
6781d5b9 BH |
747 | // Save dialog settings |
748 | saveWidgetValues(); | |
002f9f07 | 749 | |
0e1d517e MAL |
750 | IPath baseSourceContainerPath = new Path(getSourceDirectory().getAbsolutePath()); |
751 | final TraceValidateAndImportOperation operation = new TraceValidateAndImportOperation(traceId, baseSourceContainerPath, getContainerFullPath(), | |
752 | fImportUnrecognizedButton.getSelection(), fOverwriteExistingResourcesCheckbox.getSelection(), fCreateLinksInWorkspaceButton.getSelection(), fPreserveFolderStructureButton.getSelection()); | |
002f9f07 | 753 | |
252c602c BH |
754 | IStatus status = Status.OK_STATUS; |
755 | try { | |
756 | getContainer().run(true, true, new IRunnableWithProgress() { | |
757 | @Override | |
758 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
759 | operation.run(monitor); | |
760 | monitor.done(); | |
002f9f07 | 761 | } |
252c602c | 762 | }); |
002f9f07 | 763 | |
252c602c BH |
764 | status = operation.getStatus(); |
765 | } catch (InvocationTargetException e) { | |
766 | status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ImportTraceWizard_ImportProblem, e); | |
767 | } catch (InterruptedException e) { | |
768 | status = Status.CANCEL_STATUS; | |
769 | } finally { | |
770 | if (!status.isOK()) { | |
771 | if (status.getSeverity() == IStatus.CANCEL) { | |
772 | setMessage(Messages.ImportTraceWizard_ImportOperationCancelled); | |
773 | setErrorMessage(null); | |
002f9f07 | 774 | } else { |
252c602c BH |
775 | if (status.getException() != null) { |
776 | displayErrorDialog(status.getMessage() + ": " + status.getException()); //$NON-NLS-1$ | |
777 | } | |
002f9f07 | 778 | setMessage(null); |
252c602c | 779 | setErrorMessage(Messages.ImportTraceWizard_ImportProblem); |
002f9f07 | 780 | } |
252c602c BH |
781 | return false; |
782 | } | |
783 | } | |
252c602c BH |
784 | setErrorMessage(null); |
785 | return true; | |
786 | } | |
002f9f07 | 787 | |
252c602c BH |
788 | |
789 | // ------------------------------------------------------------------------ | |
790 | // Classes | |
791 | // ------------------------------------------------------------------------ | |
792 | ||
16f91bd5 | 793 | private class TraceValidateAndImportOperation { |
252c602c | 794 | private IStatus fStatus; |
252c602c | 795 | private String fTraceType; |
0e1d517e MAL |
796 | private IPath fDestinationContainerPath; |
797 | private IPath fBaseSourceContainerPath; | |
252c602c | 798 | private boolean fImportUnrecognizedTraces; |
252c602c | 799 | private boolean fLink; |
0e1d517e | 800 | private boolean fPreserveFolderStructure; |
16f91bd5 | 801 | private ImportConfirmation fConfirmationMode = ImportConfirmation.SKIP; |
252c602c | 802 | |
0e1d517e | 803 | private TraceValidateAndImportOperation(String traceId, IPath baseSourceContainerPath, IPath destinationContainerPath, boolean doImport, boolean overwrite, boolean link, boolean preserveFolderStructure) { |
252c602c | 804 | fTraceType = traceId; |
0e1d517e MAL |
805 | fBaseSourceContainerPath = baseSourceContainerPath; |
806 | fDestinationContainerPath = destinationContainerPath; | |
252c602c | 807 | fImportUnrecognizedTraces = doImport; |
16f91bd5 BH |
808 | if (overwrite) { |
809 | fConfirmationMode = ImportConfirmation.OVERWRITE_ALL; | |
810 | } | |
252c602c | 811 | fLink = link; |
0e1d517e | 812 | fPreserveFolderStructure = preserveFolderStructure; |
252c602c BH |
813 | } |
814 | ||
815 | public void run(IProgressMonitor progressMonitor) { | |
816 | String currentPath = null; | |
16f91bd5 | 817 | final Map<String, TraceFileSystemElement> folderElements = new HashMap<>(); |
252c602c | 818 | try { |
29ceb238 BH |
819 | |
820 | final ArrayList<TraceFileSystemElement> fileSystemElements = new ArrayList<>(); | |
821 | IElementFilter passThroughFilter = new IElementFilter() { | |
822 | ||
823 | @Override | |
824 | public void filterElements(Collection elements, IProgressMonitor monitor) { | |
825 | fileSystemElements.addAll(elements); | |
826 | } | |
827 | @Override | |
828 | public void filterElements(Object[] elements, IProgressMonitor monitor) { | |
829 | for (int i = 0; i < elements.length; i++) { | |
830 | fileSystemElements.add((TraceFileSystemElement)elements[i]); | |
831 | } | |
832 | } | |
833 | }; | |
834 | ||
835 | // List fileSystemElements will be filled using the passThroughFilter | |
836 | SubMonitor subMonitor = SubMonitor.convert(progressMonitor, 1); | |
282218e9 | 837 | fSelectionGroup.getAllCheckedListItems(passThroughFilter, subMonitor); |
29ceb238 BH |
838 | |
839 | // Check if operation was cancelled. | |
840 | ModalContext.checkCanceled(subMonitor); | |
841 | ||
16f91bd5 | 842 | Iterator<TraceFileSystemElement> fileSystemElementsIter = fileSystemElements.iterator(); |
29ceb238 | 843 | subMonitor = SubMonitor.convert(progressMonitor, fileSystemElements.size()); |
252c602c | 844 | |
16f91bd5 | 845 | while (fileSystemElementsIter.hasNext()) { |
252c602c BH |
846 | ModalContext.checkCanceled(progressMonitor); |
847 | currentPath = null; | |
16f91bd5 BH |
848 | TraceFileSystemElement element = fileSystemElementsIter.next(); |
849 | File fileResource = (File) element.getFileSystemObject(); | |
850 | String resourcePath = fileResource.getAbsolutePath(); | |
0e1d517e MAL |
851 | element.setDestinationContainerPath(computeDestinationContainerPath(new Path(resourcePath))); |
852 | ||
252c602c BH |
853 | currentPath = resourcePath; |
854 | SubMonitor sub = subMonitor.newChild(1); | |
16f91bd5 BH |
855 | if (element.isDirectory()) { |
856 | if (!folderElements.containsKey(resourcePath)) { | |
857 | if (isDirectoryTrace(element)) { | |
858 | folderElements.put(resourcePath, element); | |
c659283d | 859 | validateAndImportTrace(element, sub); |
252c602c BH |
860 | } |
861 | } | |
862 | } else { | |
16f91bd5 BH |
863 | TraceFileSystemElement parentElement = (TraceFileSystemElement)element.getParent(); |
864 | File parentFile = (File) parentElement.getFileSystemObject(); | |
865 | String parentPath = parentFile.getAbsolutePath(); | |
0e1d517e | 866 | parentElement.setDestinationContainerPath(computeDestinationContainerPath(new Path(parentPath))); |
252c602c | 867 | currentPath = parentPath; |
16f91bd5 BH |
868 | if (!folderElements.containsKey(parentPath)) { |
869 | if (isDirectoryTrace(parentElement)) { | |
870 | folderElements.put(parentPath, parentElement); | |
c659283d | 871 | validateAndImportTrace(parentElement, sub); |
252c602c | 872 | } else { |
16f91bd5 | 873 | if (fileResource.exists()) { |
c659283d | 874 | validateAndImportTrace(element, sub); |
002f9f07 | 875 | } |
002f9f07 BH |
876 | } |
877 | } | |
878 | } | |
879 | } | |
252c602c BH |
880 | setStatus(Status.OK_STATUS); |
881 | } catch (InterruptedException e) { | |
882 | setStatus(Status.CANCEL_STATUS); | |
883 | } catch (Exception e) { | |
0e1d517e MAL |
884 | String errorMessage = Messages.ImportTraceWizard_ImportProblem + ": " + //$NON-NLS-1$ |
885 | (currentPath != null ? currentPath : ""); //$NON-NLS-1$ | |
886 | Activator.getDefault().logError(errorMessage, e); | |
887 | setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, errorMessage , e)); | |
888 | } | |
889 | } | |
890 | ||
891 | private IPath computeDestinationContainerPath(Path resourcePath) { | |
892 | IPath destinationContainerPath = fDestinationContainerPath; | |
893 | ||
894 | // We need to figure out the new destination path relative to the selected "base" source directory. | |
895 | // Here for example, the selected source directory is /home/user | |
896 | if (fPreserveFolderStructure) { | |
897 | // /home/user/bar/foo/trace -> /home/user/bar/foo | |
898 | IPath sourceContainerPath = resourcePath.removeLastSegments(1); | |
899 | if (fBaseSourceContainerPath.equals(resourcePath)) { | |
900 | // Use resourcePath directory if fBaseSourceContainerPath points to a directory trace | |
901 | sourceContainerPath = resourcePath; | |
902 | } | |
903 | // /home/user/bar/foo, /home/user -> bar/foo | |
904 | IPath relativeContainerPath = sourceContainerPath.makeRelativeTo(fBaseSourceContainerPath); | |
905 | // project/Traces + bar/foo -> project/Traces/bar/foo | |
906 | destinationContainerPath = fDestinationContainerPath.append(relativeContainerPath); | |
002f9f07 | 907 | } |
0e1d517e | 908 | return destinationContainerPath; |
002f9f07 BH |
909 | } |
910 | ||
c659283d | 911 | private void validateAndImportTrace(TraceFileSystemElement fileSystemElement, IProgressMonitor monitor) |
252c602c | 912 | throws TmfTraceImportException, CoreException, InvocationTargetException, InterruptedException { |
16f91bd5 | 913 | File file = (File) fileSystemElement.getFileSystemObject(); |
252c602c | 914 | String path = file.getAbsolutePath(); |
350cae41 | 915 | TraceTypeHelper traceTypeHelper = null; |
c659283d | 916 | |
252c602c | 917 | if (fTraceType == null) { |
c659283d | 918 | // Auto Detection |
350cae41 PT |
919 | try { |
920 | traceTypeHelper = TmfTraceTypeUIUtils.selectTraceType(path, null, null); | |
921 | } catch (TmfTraceImportException e) { | |
922 | // the trace did not match any trace type | |
923 | } | |
924 | if (traceTypeHelper == null) { | |
925 | if (fImportUnrecognizedTraces) { | |
98bb2f6a | 926 | importResource(fileSystemElement, monitor); |
350cae41 PT |
927 | } |
928 | return; | |
929 | } | |
252c602c | 930 | } else { |
c659283d BH |
931 | boolean isDirectoryTraceType = TmfTraceType.getInstance().isDirectoryTraceType(fTraceType); |
932 | if (fileSystemElement.isDirectory() != isDirectoryTraceType) { | |
252c602c BH |
933 | return; |
934 | } | |
252c602c | 935 | traceTypeHelper = TmfTraceType.getInstance().getTraceType(fTraceType); |
16f91bd5 | 936 | |
350cae41 | 937 | if (traceTypeHelper == null) { |
c659283d BH |
938 | // Trace type not found |
939 | throw new TmfTraceImportException(Messages.ImportTraceWizard_TraceTypeNotFound); | |
252c602c | 940 | } |
c659283d | 941 | |
a8ddd783 | 942 | if (!traceTypeHelper.validate(path).isOK()) { |
c659283d | 943 | // Trace type exist but doesn't validate for given trace. |
350cae41 PT |
944 | return; |
945 | } | |
252c602c | 946 | } |
002f9f07 | 947 | |
c659283d | 948 | // Finally import trace |
0e1d517e MAL |
949 | IResource importedResource = importResource(fileSystemElement, monitor); |
950 | if (importedResource != null) { | |
951 | TmfTraceTypeUIUtils.setTraceType(importedResource, traceTypeHelper); | |
002f9f07 | 952 | } |
002f9f07 | 953 | |
252c602c | 954 | } |
002f9f07 | 955 | |
16f91bd5 BH |
956 | /** |
957 | * Imports a trace resource to project. In case of name collision the | |
958 | * user will be asked to confirm overwriting the existing trace, | |
959 | * overwriting or skipping the trace to be imported. | |
960 | * | |
961 | * @param fileSystemElement | |
962 | * trace file system object to import | |
963 | * @param monitor | |
964 | * a progress monitor | |
0e1d517e | 965 | * @return the imported resource or null if no resource was imported |
16f91bd5 BH |
966 | * |
967 | * @throws InvocationTargetException | |
968 | * if problems during import operation | |
969 | * @throws InterruptedException | |
970 | * if cancelled | |
971 | * @throws CoreException | |
972 | * if problems with workspace | |
973 | */ | |
0e1d517e | 974 | private IResource importResource(TraceFileSystemElement fileSystemElement, IProgressMonitor monitor) |
16f91bd5 | 975 | throws InvocationTargetException, InterruptedException, CoreException { |
0e1d517e | 976 | |
16f91bd5 BH |
977 | ImportConfirmation mode = checkForNameClashes(fileSystemElement); |
978 | switch (mode) { | |
979 | case RENAME: | |
980 | case RENAME_ALL: | |
981 | rename(fileSystemElement); | |
982 | break; | |
983 | case OVERWRITE: | |
984 | case OVERWRITE_ALL: | |
985 | delete(fileSystemElement, monitor); | |
986 | break; | |
987 | case CONTINUE: | |
988 | break; | |
989 | case SKIP: | |
990 | case SKIP_ALL: | |
991 | default: | |
0e1d517e | 992 | return null; |
16f91bd5 BH |
993 | } |
994 | ||
995 | List<TraceFileSystemElement> subList = new ArrayList<>(); | |
996 | ||
16f91bd5 BH |
997 | FileSystemElement parentFolder = fileSystemElement.getParent(); |
998 | ||
0e1d517e MAL |
999 | IPath containerPath = fileSystemElement.getDestinationContainerPath(); |
1000 | IPath tracePath = containerPath.addTrailingSeparator().append(fileSystemElement.getLabel()); | |
16f91bd5 | 1001 | if (fileSystemElement.isDirectory() && (!fLink)) { |
0e1d517e | 1002 | containerPath = tracePath; |
16f91bd5 BH |
1003 | |
1004 | Object[] array = fileSystemElement.getFiles().getChildren(); | |
1005 | for (int i = 0; i < array.length; i++) { | |
1006 | subList.add((TraceFileSystemElement)array[i]); | |
1007 | } | |
1008 | parentFolder = fileSystemElement; | |
1009 | ||
1010 | } else { | |
1011 | subList.add(fileSystemElement); | |
1012 | } | |
1013 | ||
1014 | ||
1015 | ImportProvider fileSystemStructureProvider = new ImportProvider(); | |
252c602c | 1016 | |
252c602c BH |
1017 | IOverwriteQuery myQueryImpl = new IOverwriteQuery() { |
1018 | @Override | |
1019 | public String queryOverwrite(String file) { | |
16f91bd5 | 1020 | return IOverwriteQuery.NO_ALL; |
252c602c BH |
1021 | } |
1022 | }; | |
1023 | ||
16f91bd5 BH |
1024 | monitor.setTaskName(Messages.ImportTraceWizard_ImportOperationTaskName + " " + ((File)fileSystemElement.getFileSystemObject()).getAbsolutePath()); //$NON-NLS-1$ |
1025 | ImportOperation operation = new ImportOperation(containerPath, parentFolder, fileSystemStructureProvider, myQueryImpl, subList); | |
002f9f07 | 1026 | operation.setContext(getShell()); |
002f9f07 | 1027 | |
252c602c | 1028 | operation.setCreateContainerStructure(false); |
16f91bd5 | 1029 | operation.setOverwriteResources(false); |
252c602c BH |
1030 | operation.setCreateLinks(fLink); |
1031 | operation.setVirtualFolders(false); | |
002f9f07 | 1032 | |
252c602c | 1033 | operation.run(new SubProgressMonitor(monitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK)); |
89730b51 PT |
1034 | |
1035 | File file = (File) fileSystemElement.getFileSystemObject(); | |
1036 | String sourceLocation = null; | |
1037 | IResource sourceResource; | |
1038 | if (file.isDirectory()) { | |
1039 | sourceResource = ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation(Path.fromOSString(file.getAbsolutePath())); | |
1040 | } else { | |
1041 | sourceResource = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(Path.fromOSString(file.getAbsolutePath())); | |
1042 | } | |
1043 | if (sourceResource != null && sourceResource.exists()) { | |
1044 | sourceLocation = sourceResource.getPersistentProperty(TmfCommonConstants.SOURCE_LOCATION); | |
1045 | } | |
1046 | if (sourceLocation == null) { | |
1047 | sourceLocation = URIUtil.toUnencodedString(file.toURI()); | |
1048 | } | |
0e1d517e MAL |
1049 | |
1050 | IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(tracePath); | |
89730b51 PT |
1051 | resource.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation); |
1052 | ||
0e1d517e | 1053 | return resource; |
16f91bd5 | 1054 | } |
002f9f07 | 1055 | |
16f91bd5 BH |
1056 | private boolean isDirectoryTrace(FileSystemElement fileSystemElement) { |
1057 | File file = (File) fileSystemElement.getFileSystemObject(); | |
252c602c BH |
1058 | String path = file.getAbsolutePath(); |
1059 | if (TmfTraceType.getInstance().isDirectoryTrace(path)) { | |
1060 | return true; | |
1061 | } | |
002f9f07 BH |
1062 | return false; |
1063 | } | |
1064 | ||
16f91bd5 | 1065 | private ImportConfirmation checkForNameClashes(TraceFileSystemElement fileSystemElement) throws InterruptedException { |
0e1d517e | 1066 | IPath tracePath = getInitialDestinationPath(fileSystemElement); |
16f91bd5 BH |
1067 | |
1068 | // handle rename | |
0e1d517e | 1069 | if (getExistingTrace(tracePath) != null) { |
16f91bd5 BH |
1070 | if ((fConfirmationMode == ImportConfirmation.RENAME_ALL) || |
1071 | (fConfirmationMode == ImportConfirmation.OVERWRITE_ALL) || | |
1072 | (fConfirmationMode == ImportConfirmation.SKIP_ALL)) { | |
1073 | return fConfirmationMode; | |
1074 | } | |
1075 | ||
0e1d517e | 1076 | int returnCode = promptForOverwrite(tracePath); |
16f91bd5 BH |
1077 | if (returnCode < 0) { |
1078 | // Cancel | |
1079 | throw new InterruptedException(); | |
1080 | } | |
1081 | fConfirmationMode = ImportConfirmation.values()[returnCode]; | |
1082 | return fConfirmationMode; | |
1083 | } | |
1084 | return ImportConfirmation.CONTINUE; | |
1085 | } | |
1086 | ||
0e1d517e | 1087 | private int promptForOverwrite(IPath tracePath) { |
16f91bd5 | 1088 | final MessageDialog dialog = new MessageDialog(getContainer() |
0e1d517e | 1089 | .getShell(), null, null, NLS.bind(Messages.ImportTraceWizard_TraceAlreadyExists, tracePath.makeRelativeTo(fTraceFolderElement.getProject().getPath())), |
16f91bd5 BH |
1090 | MessageDialog.QUESTION, new String[] { |
1091 | ImportConfirmation.RENAME.getInName(), | |
1092 | ImportConfirmation.RENAME_ALL.getInName(), | |
1093 | ImportConfirmation.OVERWRITE.getInName(), | |
1094 | ImportConfirmation.OVERWRITE_ALL.getInName(), | |
1095 | ImportConfirmation.SKIP.getInName(), | |
1096 | ImportConfirmation.SKIP_ALL.getInName(), | |
1097 | }, 4) { | |
1098 | @Override | |
1099 | protected int getShellStyle() { | |
1100 | return super.getShellStyle() | SWT.SHEET; | |
1101 | } | |
1102 | }; | |
1103 | ||
1104 | final int[] returnValue = new int[1]; | |
1105 | getShell().getDisplay().syncExec(new Runnable() { | |
1106 | ||
1107 | @Override | |
1108 | public void run() { | |
1109 | returnValue[0] = dialog.open(); | |
1110 | } | |
1111 | }); | |
1112 | return returnValue[0]; | |
1113 | } | |
1114 | ||
0e1d517e MAL |
1115 | /** |
1116 | * @return the initial destination path, before rename, if any | |
1117 | */ | |
1118 | private IPath getInitialDestinationPath(TraceFileSystemElement fileSystemElement) { | |
1119 | IPath traceFolderPath = fileSystemElement.getDestinationContainerPath(); | |
1120 | return traceFolderPath.append(((File)fileSystemElement.getFileSystemObject()).getName()); | |
1121 | } | |
1122 | ||
16f91bd5 | 1123 | private void rename(TraceFileSystemElement fileSystemElement) { |
0e1d517e MAL |
1124 | IPath tracePath = getInitialDestinationPath(fileSystemElement); |
1125 | TmfTraceElement trace = getExistingTrace(tracePath); | |
16f91bd5 BH |
1126 | if (trace == null) { |
1127 | return; | |
1128 | } | |
1129 | ||
0e1d517e MAL |
1130 | // Not using IFolder on purpose to leave the door open to import directly into an IProject |
1131 | IContainer folder = (IContainer) trace.getParent().getResource(); | |
16f91bd5 BH |
1132 | int i = 2; |
1133 | while (true) { | |
1134 | String name = trace.getName() + '(' + Integer.toString(i++) + ')'; | |
1135 | IResource resource = folder.findMember(name); | |
1136 | if (resource == null) { | |
1137 | fileSystemElement.setLabel(name); | |
1138 | return; | |
1139 | } | |
1140 | } | |
1141 | } | |
1142 | ||
1143 | private void delete(TraceFileSystemElement fileSystemElement, IProgressMonitor monitor) throws CoreException { | |
0e1d517e MAL |
1144 | IPath tracePath = getInitialDestinationPath(fileSystemElement); |
1145 | TmfTraceElement trace = getExistingTrace(tracePath); | |
16f91bd5 BH |
1146 | if (trace == null) { |
1147 | return; | |
1148 | } | |
1149 | ||
1150 | trace.delete(monitor); | |
1151 | } | |
1152 | ||
0e1d517e | 1153 | private TmfTraceElement getExistingTrace(IPath tracePath) { |
16f91bd5 BH |
1154 | List<TmfTraceElement> traces = fTraceFolderElement.getTraces(); |
1155 | for (TmfTraceElement t : traces) { | |
0e1d517e | 1156 | if (t.getPath().equals(tracePath)) { |
16f91bd5 BH |
1157 | return t; |
1158 | } | |
1159 | } | |
1160 | return null; | |
1161 | } | |
1162 | ||
252c602c BH |
1163 | /** |
1164 | * Set the status for this operation | |
1165 | * | |
1166 | * @param status | |
1167 | * the status | |
1168 | */ | |
1169 | protected void setStatus(IStatus status) { | |
1170 | fStatus = status; | |
002f9f07 BH |
1171 | } |
1172 | ||
252c602c BH |
1173 | public IStatus getStatus() { |
1174 | return fStatus; | |
1175 | } | |
002f9f07 BH |
1176 | } |
1177 | ||
252c602c BH |
1178 | /** |
1179 | * The <code>TraceFileSystemElement</code> is a <code>FileSystemElement</code> that knows | |
1180 | * if it has been populated or not. | |
1181 | */ | |
16f91bd5 | 1182 | private static class TraceFileSystemElement extends FileSystemElement { |
252c602c | 1183 | |
16f91bd5 BH |
1184 | private boolean fIsPopulated = false; |
1185 | private String fLabel = null; | |
0e1d517e | 1186 | private IPath fDestinationContainerPath; |
252c602c BH |
1187 | |
1188 | public TraceFileSystemElement(String name, FileSystemElement parent, boolean isDirectory) { | |
1189 | super(name, parent, isDirectory); | |
1190 | } | |
1191 | ||
0e1d517e MAL |
1192 | public void setDestinationContainerPath(IPath destinationContainerPath) { |
1193 | fDestinationContainerPath = destinationContainerPath; | |
1194 | } | |
1195 | ||
252c602c | 1196 | public void setPopulated() { |
16f91bd5 | 1197 | fIsPopulated = true; |
252c602c BH |
1198 | } |
1199 | ||
1200 | public boolean isPopulated() { | |
16f91bd5 | 1201 | return fIsPopulated; |
252c602c BH |
1202 | } |
1203 | ||
1204 | @Override | |
1205 | public AdaptableList getFiles() { | |
16f91bd5 | 1206 | if(!fIsPopulated) { |
252c602c BH |
1207 | populateElementChildren(); |
1208 | } | |
1209 | return super.getFiles(); | |
1210 | } | |
1211 | ||
1212 | @Override | |
1213 | public AdaptableList getFolders() { | |
16f91bd5 | 1214 | if(!fIsPopulated) { |
252c602c BH |
1215 | populateElementChildren(); |
1216 | } | |
1217 | return super.getFolders(); | |
1218 | } | |
002f9f07 | 1219 | |
16f91bd5 BH |
1220 | /** |
1221 | * Sets the label for the trace to be used when importing at trace. | |
1222 | * @param name | |
1223 | * the label for the trace | |
1224 | */ | |
1225 | public void setLabel(String name) { | |
1226 | fLabel = name; | |
1227 | } | |
1228 | ||
1229 | /** | |
1230 | * Returns the label for the trace to be used when importing at trace. | |
1231 | * | |
1232 | * @return the label of trace resource | |
1233 | */ | |
1234 | public String getLabel() { | |
1235 | if (fLabel == null) { | |
1236 | //Get the name - if it is empty then return the path as it is a file root | |
1237 | File file = (File) getFileSystemObject(); | |
1238 | String name = file.getName(); | |
1239 | if (name.length() == 0) { | |
1240 | return file.getPath(); | |
1241 | } | |
1242 | return name; | |
1243 | } | |
1244 | return fLabel; | |
1245 | } | |
1246 | ||
0e1d517e MAL |
1247 | /** |
1248 | * The full path to the container that will contain the trace | |
1249 | * | |
1250 | * @return the destination container path | |
1251 | */ | |
1252 | public IPath getDestinationContainerPath() { | |
1253 | return fDestinationContainerPath; | |
1254 | } | |
1255 | ||
252c602c BH |
1256 | /** |
1257 | * Populates the children of the specified parent <code>FileSystemElement</code> | |
1258 | */ | |
1259 | private void populateElementChildren() { | |
1260 | FileSystemStructureProvider provider = FileSystemStructureProvider.INSTANCE; | |
1261 | List<File> allchildren = provider.getChildren(this.getFileSystemObject()); | |
1262 | File child = null; | |
1263 | TraceFileSystemElement newelement = null; | |
1264 | Iterator<File> iter = allchildren.iterator(); | |
1265 | while(iter.hasNext()) { | |
1266 | child = iter.next(); | |
1267 | newelement = new TraceFileSystemElement(provider.getLabel(child), this, provider.isFolder(child)); | |
1268 | newelement.setFileSystemObject(child); | |
1269 | } | |
1270 | setPopulated(); | |
1271 | } | |
1272 | } | |
16f91bd5 BH |
1273 | |
1274 | private class ImportProvider implements IImportStructureProvider { | |
1275 | ||
1276 | private FileSystemStructureProvider provider = FileSystemStructureProvider.INSTANCE; | |
1277 | ||
1278 | ImportProvider() { | |
1279 | } | |
1280 | ||
1281 | @Override | |
1282 | public String getLabel(Object element) { | |
1283 | TraceFileSystemElement resource = (TraceFileSystemElement)element; | |
1284 | return resource.getLabel(); | |
1285 | } | |
1286 | ||
1287 | @Override | |
1288 | public List getChildren(Object element) { | |
1289 | TraceFileSystemElement resource = (TraceFileSystemElement)element; | |
1290 | Object[] array = resource.getFiles().getChildren(); | |
1291 | List<Object> list = new ArrayList<>(); | |
1292 | for (int i = 0; i < array.length; i++) { | |
1293 | list.add(array[i]); | |
1294 | } | |
1295 | return list; | |
1296 | } | |
1297 | ||
1298 | @Override | |
1299 | public InputStream getContents(Object element) { | |
1300 | TraceFileSystemElement resource = (TraceFileSystemElement)element; | |
1301 | return provider.getContents(resource.getFileSystemObject()); | |
1302 | } | |
1303 | ||
1304 | @Override | |
1305 | public String getFullPath(Object element) { | |
1306 | TraceFileSystemElement resource = (TraceFileSystemElement)element; | |
1307 | return provider.getFullPath(resource.getFileSystemObject()); | |
1308 | } | |
1309 | ||
1310 | @Override | |
1311 | public boolean isFolder(Object element) { | |
1312 | TraceFileSystemElement resource = (TraceFileSystemElement)element; | |
1313 | return resource.isDirectory(); | |
1314 | } | |
1315 | } | |
1316 | ||
1317 | private enum ImportConfirmation { | |
1318 | // ------------------------------------------------------------------------ | |
1319 | // Enum definition | |
1320 | // ------------------------------------------------------------------------ | |
1321 | RENAME(Messages.ImportTraceWizard_ImportConfigurationRename), | |
1322 | RENAME_ALL(Messages.ImportTraceWizard_ImportConfigurationRenameAll), | |
1323 | OVERWRITE(Messages.ImportTraceWizard_ImportConfigurationOverwrite), | |
1324 | OVERWRITE_ALL(Messages.ImportTraceWizard_ImportConfigurationOverwriteAll), | |
1325 | SKIP(Messages.ImportTraceWizard_ImportConfigurationSkip), | |
1326 | SKIP_ALL(Messages.ImportTraceWizard_ImportConfigurationSkipAll), | |
1327 | CONTINUE("CONTINUE"); //$NON-NLS-1$ | |
1328 | ||
1329 | // ------------------------------------------------------------------------ | |
1330 | // Attributes | |
1331 | // ------------------------------------------------------------------------ | |
1332 | /** | |
1333 | * Name of enum | |
1334 | */ | |
1335 | private final String fInName; | |
1336 | ||
1337 | // ------------------------------------------------------------------------ | |
1338 | // Constuctors | |
1339 | // ------------------------------------------------------------------------ | |
1340 | ||
1341 | /** | |
1342 | * Private constructor | |
1343 | * @param name the name of state | |
1344 | */ | |
1345 | private ImportConfirmation(String name) { | |
1346 | fInName = name; | |
1347 | } | |
1348 | ||
1349 | // ------------------------------------------------------------------------ | |
1350 | // Accessors | |
1351 | // ------------------------------------------------------------------------ | |
1352 | /** | |
1353 | * @return state name | |
1354 | */ | |
1355 | public String getInName() { | |
1356 | return fInName; | |
1357 | } | |
1358 | } | |
002f9f07 | 1359 | } |