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