Commit | Line | Data |
---|---|---|
002f9f07 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2013 Ericsson and others. | |
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 | |
16 | *******************************************************************************/ | |
17 | ||
18 | package org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace; | |
19 | ||
20 | import java.io.File; | |
21 | import java.io.IOException; | |
22 | import java.lang.reflect.InvocationTargetException; | |
23 | import java.util.ArrayList; | |
24 | import java.util.Collections; | |
25 | import java.util.Comparator; | |
26 | import java.util.HashMap; | |
27 | import java.util.Iterator; | |
28 | import java.util.LinkedList; | |
29 | import java.util.List; | |
30 | import java.util.Map; | |
31 | import java.util.Map.Entry; | |
32 | ||
33 | import org.eclipse.core.resources.IContainer; | |
34 | import org.eclipse.core.resources.IFolder; | |
35 | import org.eclipse.core.resources.IProject; | |
36 | import org.eclipse.core.resources.IResource; | |
37 | import org.eclipse.core.resources.ResourcesPlugin; | |
38 | import org.eclipse.core.runtime.CoreException; | |
39 | import org.eclipse.core.runtime.IPath; | |
40 | import org.eclipse.core.runtime.IStatus; | |
41 | import org.eclipse.core.runtime.Path; | |
42 | import org.eclipse.core.runtime.Platform; | |
43 | import org.eclipse.jface.dialogs.ErrorDialog; | |
44 | import org.eclipse.jface.dialogs.MessageDialog; | |
45 | import org.eclipse.jface.viewers.CheckStateChangedEvent; | |
46 | import org.eclipse.jface.viewers.CheckboxTreeViewer; | |
47 | import org.eclipse.jface.viewers.ICheckStateListener; | |
48 | import org.eclipse.jface.viewers.IStructuredSelection; | |
49 | import org.eclipse.jface.viewers.ITreeContentProvider; | |
50 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
51 | import org.eclipse.linuxtools.tmf.core.TmfProjectNature; | |
52 | import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType; | |
53 | import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper; | |
54 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement; | |
55 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry; | |
56 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
57 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
58 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceTypeUIUtils; | |
59 | import org.eclipse.linuxtools.tmf.ui.project.wizards.Messages; | |
60 | import org.eclipse.swt.SWT; | |
61 | import org.eclipse.swt.custom.BusyIndicator; | |
62 | import org.eclipse.swt.events.FocusEvent; | |
63 | import org.eclipse.swt.events.FocusListener; | |
64 | import org.eclipse.swt.events.KeyEvent; | |
65 | import org.eclipse.swt.events.KeyListener; | |
66 | import org.eclipse.swt.events.SelectionAdapter; | |
67 | import org.eclipse.swt.events.SelectionEvent; | |
68 | import org.eclipse.swt.events.SelectionListener; | |
69 | import org.eclipse.swt.layout.GridData; | |
70 | import org.eclipse.swt.layout.GridLayout; | |
71 | import org.eclipse.swt.widgets.Button; | |
72 | import org.eclipse.swt.widgets.Combo; | |
73 | import org.eclipse.swt.widgets.Composite; | |
74 | import org.eclipse.swt.widgets.DirectoryDialog; | |
75 | import org.eclipse.swt.widgets.Event; | |
76 | import org.eclipse.swt.widgets.Group; | |
77 | import org.eclipse.swt.widgets.Label; | |
78 | import org.eclipse.ui.IWorkbench; | |
79 | import org.eclipse.ui.dialogs.FileSystemElement; | |
80 | import org.eclipse.ui.dialogs.WizardResourceImportPage; | |
81 | import org.eclipse.ui.model.WorkbenchContentProvider; | |
82 | import org.eclipse.ui.model.WorkbenchLabelProvider; | |
83 | import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider; | |
84 | import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider; | |
85 | import org.eclipse.ui.wizards.datatransfer.ImportOperation; | |
86 | ||
87 | /** | |
88 | * A variant of the standard resource import wizard with the following changes: | |
89 | * <ul> | |
90 | * <li>A folder/file combined checkbox tree viewer to select traces | |
91 | * <li>Cherry-picking of traces in the file structure without re-creating the | |
92 | * file hierarchy | |
93 | * <li>A trace types dropbox for optional characterization | |
94 | * </ul> | |
95 | * For our purpose, a trace can either be a single file or a whole directory | |
96 | * sub-tree, whichever is reached first from the root directory. | |
97 | * <p> | |
98 | * | |
99 | * @version 1.0 | |
100 | * @author Francois Chouinard | |
101 | * @since 2.0 | |
102 | */ | |
103 | public class ImportTraceWizardPage extends WizardResourceImportPage { | |
104 | ||
105 | // ------------------------------------------------------------------------ | |
106 | // Classes | |
107 | // ------------------------------------------------------------------------ | |
108 | ||
109 | private static class FileSystemElementComparator implements Comparator<Object> { | |
110 | ||
111 | FileSystemStructureProvider provider = FileSystemStructureProvider.INSTANCE; | |
112 | ||
113 | @Override | |
114 | public int compare(Object o1, Object o2) { | |
115 | String label1 = provider.getLabel(o1); | |
116 | String label2 = provider.getLabel(o2); | |
117 | return label1.compareTo(label2); | |
118 | } | |
119 | ||
120 | } | |
121 | ||
122 | // ------------------------------------------------------------------------ | |
123 | // Constants | |
124 | // ------------------------------------------------------------------------ | |
125 | ||
126 | private static final String IMPORT_WIZARD_PAGE = "ImportTraceWizardPage"; //$NON-NLS-1$ | |
127 | private static final FileSystemElementComparator FILE_SYSTEM_ELEMENT_COMPARATOR = new FileSystemElementComparator(); | |
128 | ||
129 | // ------------------------------------------------------------------------ | |
130 | // Attributes | |
131 | // ------------------------------------------------------------------------ | |
132 | ||
133 | // Folder navigation start point (saved between invocations) | |
134 | private static String fRootDirectory = null; | |
135 | ||
136 | // Navigation folder content viewer and selector | |
137 | private CheckboxTreeViewer fFolderViewer; | |
138 | ||
139 | // Target import directory ('Traces' folder) | |
140 | private IFolder fTargetFolder; | |
141 | private static final String SEPARATOR = ":"; //$NON-NLS-1$ | |
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) { | |
177 | TmfTraceFolder tmfTraceFolder = (TmfTraceFolder) element; | |
178 | tmfTraceFolder.getProject().getResource(); | |
179 | traceFolder = tmfTraceFolder.getResource(); | |
180 | } else if (element instanceof IProject) { | |
181 | IProject project = (IProject) element; | |
182 | try { | |
183 | if (project.hasNature(TmfProjectNature.ID)) { | |
184 | traceFolder = (IFolder) project.findMember(TmfTraceFolder.TRACE_FOLDER_NAME); | |
185 | } | |
186 | } catch (CoreException e) { | |
187 | } | |
188 | } | |
189 | ||
190 | // Set the target trace folder | |
191 | if (traceFolder != null) { | |
192 | fTargetFolder = traceFolder; | |
193 | String path = traceFolder.getFullPath().toOSString(); | |
194 | setContainerFieldValue(path); | |
195 | } | |
196 | } | |
197 | ||
198 | // ------------------------------------------------------------------------ | |
199 | // WizardResourceImportPage | |
200 | // ------------------------------------------------------------------------ | |
201 | ||
202 | @Override | |
203 | public void createControl(Composite parent) { | |
204 | super.createControl(parent); | |
205 | // Restore last directory if applicable | |
206 | if (fRootDirectory != null) { | |
207 | directoryNameField.setText(fRootDirectory); | |
208 | updateFromSourceField(); | |
209 | } | |
210 | } | |
211 | ||
212 | @Override | |
213 | protected void createSourceGroup(Composite parent) { | |
214 | createDirectorySelectionGroup(parent); | |
215 | createFileSelectionGroup(parent); | |
216 | createTraceTypeGroup(parent); | |
217 | validateSourceGroup(); | |
218 | } | |
219 | ||
220 | @Override | |
221 | protected void createFileSelectionGroup(Composite parent) { | |
222 | ||
223 | // This Composite is only used for widget alignment purposes | |
224 | Composite composite = new Composite(parent, SWT.NONE); | |
225 | composite.setFont(parent.getFont()); | |
226 | GridLayout layout = new GridLayout(); | |
227 | composite.setLayout(layout); | |
228 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
229 | ||
230 | final int PREFERRED_LIST_HEIGHT = 150; | |
231 | ||
232 | fFolderViewer = new CheckboxTreeViewer(composite, SWT.BORDER); | |
233 | GridData data = new GridData(GridData.FILL_BOTH); | |
234 | data.heightHint = PREFERRED_LIST_HEIGHT; | |
235 | fFolderViewer.getTree().setLayoutData(data); | |
236 | fFolderViewer.getTree().setFont(parent.getFont()); | |
237 | ||
238 | fFolderViewer.setContentProvider(getFileProvider()); | |
239 | fFolderViewer.setLabelProvider(new WorkbenchLabelProvider()); | |
240 | fFolderViewer.addCheckStateListener(new ICheckStateListener() { | |
241 | @Override | |
242 | public void checkStateChanged(CheckStateChangedEvent event) { | |
243 | Object elem = event.getElement(); | |
244 | if (elem instanceof FileSystemElement) { | |
245 | FileSystemElement element = (FileSystemElement) elem; | |
246 | if (fFolderViewer.getGrayed(element)) { | |
247 | fFolderViewer.setSubtreeChecked(element, false); | |
248 | fFolderViewer.setGrayed(element, false); | |
249 | } else if (event.getChecked()) { | |
250 | fFolderViewer.setSubtreeChecked(event.getElement(), true); | |
251 | } else { | |
252 | fFolderViewer.setParentsGrayed(element, true); | |
253 | if (!element.isDirectory()) { | |
254 | fFolderViewer.setGrayed(element, false); | |
255 | } | |
256 | } | |
257 | updateWidgetEnablements(); | |
258 | } | |
259 | } | |
260 | }); | |
261 | } | |
262 | ||
263 | @Override | |
264 | protected ITreeContentProvider getFolderProvider() { | |
265 | return null; | |
266 | } | |
267 | ||
268 | @Override | |
269 | protected ITreeContentProvider getFileProvider() { | |
270 | return new WorkbenchContentProvider() { | |
271 | @Override | |
272 | public Object[] getChildren(Object o) { | |
273 | if (o instanceof FileSystemElement) { | |
274 | FileSystemElement element = (FileSystemElement) o; | |
275 | populateChildren(element); | |
276 | // For our purpose, we need folders + files | |
277 | Object[] folders = element.getFolders().getChildren(); | |
278 | Object[] files = element.getFiles().getChildren(); | |
279 | ||
280 | List<Object> result = new LinkedList<>(); | |
281 | for (Object folder : folders) { | |
282 | result.add(folder); | |
283 | } | |
284 | for (Object file : files) { | |
285 | result.add(file); | |
286 | } | |
287 | ||
288 | return result.toArray(); | |
289 | } | |
290 | return new Object[0]; | |
291 | } | |
292 | }; | |
293 | } | |
294 | ||
295 | private static void populateChildren(FileSystemElement parent) { | |
296 | // Do not re-populate if the job was done already... | |
297 | FileSystemStructureProvider provider = FileSystemStructureProvider.INSTANCE; | |
298 | if (parent.getFolders().size() == 0 && parent.getFiles().size() == 0) { | |
299 | Object fileSystemObject = parent.getFileSystemObject(); | |
300 | List<?> children = provider.getChildren(fileSystemObject); | |
301 | if (children != null) { | |
302 | Collections.sort(children, FILE_SYSTEM_ELEMENT_COMPARATOR); | |
303 | Iterator<?> iterator = children.iterator(); | |
304 | while (iterator.hasNext()) { | |
305 | Object child = iterator.next(); | |
306 | String label = provider.getLabel(child); | |
307 | FileSystemElement element = new FileSystemElement(label, parent, provider.isFolder(child)); | |
308 | element.setFileSystemObject(child); | |
309 | } | |
310 | } | |
311 | } | |
312 | } | |
313 | ||
314 | @Override | |
315 | protected List<FileSystemElement> getSelectedResources() { | |
316 | List<FileSystemElement> resources = new ArrayList<>(); | |
317 | Object[] checkedItems = fFolderViewer.getCheckedElements(); | |
318 | for (Object item : checkedItems) { | |
319 | if (item instanceof FileSystemElement && !fFolderViewer.getGrayed(item)) { | |
320 | resources.add((FileSystemElement) item); | |
321 | } | |
322 | } | |
323 | return resources; | |
324 | } | |
325 | ||
326 | // ------------------------------------------------------------------------ | |
327 | // Directory Selection Group (forked WizardFileSystemResourceImportPage1) | |
328 | // ------------------------------------------------------------------------ | |
329 | ||
330 | /** | |
331 | * The directory name field | |
332 | */ | |
333 | protected Combo directoryNameField; | |
334 | /** | |
335 | * The directory browse button. | |
336 | */ | |
337 | protected Button directoryBrowseButton; | |
338 | ||
339 | private boolean entryChanged = false; | |
340 | ||
341 | /** | |
342 | * creates the directory selection group. | |
343 | * | |
344 | * @param parent | |
345 | * the parent composite | |
346 | */ | |
347 | protected void createDirectorySelectionGroup(Composite parent) { | |
348 | ||
349 | Composite directoryContainerGroup = new Composite(parent, SWT.NONE); | |
350 | GridLayout layout = new GridLayout(); | |
351 | layout.numColumns = 3; | |
352 | directoryContainerGroup.setLayout(layout); | |
353 | directoryContainerGroup.setFont(parent.getFont()); | |
354 | directoryContainerGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); | |
355 | ||
356 | // Label ("Trace directory:") | |
357 | Label groupLabel = new Label(directoryContainerGroup, SWT.NONE); | |
358 | groupLabel.setText(Messages.ImportTraceWizard_DirectoryLocation); | |
359 | groupLabel.setFont(parent.getFont()); | |
360 | ||
361 | // Directory name entry field | |
362 | directoryNameField = new Combo(directoryContainerGroup, SWT.BORDER); | |
363 | GridData data = new GridData(SWT.FILL, SWT.FILL, true, false); | |
364 | data.widthHint = SIZING_TEXT_FIELD_WIDTH; | |
365 | directoryNameField.setLayoutData(data); | |
366 | directoryNameField.setFont(parent.getFont()); | |
367 | ||
368 | directoryNameField.addSelectionListener(new SelectionAdapter() { | |
369 | @Override | |
370 | public void widgetSelected(SelectionEvent e) { | |
371 | updateFromSourceField(); | |
372 | } | |
373 | }); | |
374 | ||
375 | directoryNameField.addKeyListener(new KeyListener() { | |
376 | @Override | |
377 | public void keyPressed(KeyEvent e) { | |
378 | // If there has been a key pressed then mark as dirty | |
379 | entryChanged = true; | |
380 | if (e.character == SWT.CR) { // Windows... | |
381 | entryChanged = false; | |
382 | updateFromSourceField(); | |
383 | } | |
384 | } | |
385 | ||
386 | @Override | |
387 | public void keyReleased(KeyEvent e) { | |
388 | } | |
389 | }); | |
390 | ||
391 | directoryNameField.addFocusListener(new FocusListener() { | |
392 | @Override | |
393 | public void focusGained(FocusEvent e) { | |
394 | // Do nothing when getting focus | |
395 | } | |
396 | ||
397 | @Override | |
398 | public void focusLost(FocusEvent e) { | |
399 | // Clear the flag to prevent constant update | |
400 | if (entryChanged) { | |
401 | entryChanged = false; | |
402 | updateFromSourceField(); | |
403 | } | |
404 | } | |
405 | }); | |
406 | ||
407 | // Browse button | |
408 | directoryBrowseButton = new Button(directoryContainerGroup, SWT.PUSH); | |
409 | directoryBrowseButton.setText(Messages.ImportTraceWizard_BrowseButton); | |
410 | directoryBrowseButton.addListener(SWT.Selection, this); | |
411 | directoryBrowseButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); | |
412 | directoryBrowseButton.setFont(parent.getFont()); | |
413 | setButtonLayoutData(directoryBrowseButton); | |
414 | } | |
415 | ||
416 | // ------------------------------------------------------------------------ | |
417 | // Browse for the source directory | |
418 | // ------------------------------------------------------------------------ | |
419 | ||
420 | @Override | |
421 | public void handleEvent(Event event) { | |
422 | if (event.widget == directoryBrowseButton) { | |
423 | handleSourceDirectoryBrowseButtonPressed(); | |
424 | } | |
425 | super.handleEvent(event); | |
426 | } | |
427 | ||
428 | /** | |
429 | * Handle the button pressed event | |
430 | */ | |
431 | protected void handleSourceDirectoryBrowseButtonPressed() { | |
432 | String currentSource = directoryNameField.getText(); | |
433 | DirectoryDialog dialog = new DirectoryDialog(directoryNameField.getShell(), SWT.SAVE | SWT.SHEET); | |
434 | dialog.setText(Messages.ImportTraceWizard_SelectTraceDirectoryTitle); | |
435 | dialog.setMessage(Messages.ImportTraceWizard_SelectTraceDirectoryMessage); | |
436 | dialog.setFilterPath(getSourceDirectoryName(currentSource)); | |
437 | ||
438 | String selectedDirectory = dialog.open(); | |
439 | if (selectedDirectory != null) { | |
440 | // Just quit if the directory is not valid | |
441 | if ((getSourceDirectory(selectedDirectory) == null) || selectedDirectory.equals(currentSource)) { | |
442 | return; | |
443 | } | |
444 | // If it is valid then proceed to populate | |
445 | setErrorMessage(null); | |
446 | setSourceName(selectedDirectory); | |
447 | } | |
448 | } | |
449 | ||
450 | private File getSourceDirectory() { | |
451 | return getSourceDirectory(directoryNameField.getText()); | |
452 | } | |
453 | ||
454 | private static File getSourceDirectory(String path) { | |
455 | File sourceDirectory = new File(getSourceDirectoryName(path)); | |
456 | if (!sourceDirectory.exists() || !sourceDirectory.isDirectory()) { | |
457 | return null; | |
458 | } | |
459 | ||
460 | return sourceDirectory; | |
461 | } | |
462 | ||
463 | private static String getSourceDirectoryName(String sourceName) { | |
464 | IPath result = new Path(sourceName.trim()); | |
465 | if (result.getDevice() != null && result.segmentCount() == 0) { | |
466 | result = result.addTrailingSeparator(); | |
467 | } else { | |
468 | result = result.removeTrailingSeparator(); | |
469 | } | |
470 | return result.toOSString(); | |
471 | } | |
472 | ||
473 | private String getSourceDirectoryName() { | |
474 | return getSourceDirectoryName(directoryNameField.getText()); | |
475 | } | |
476 | ||
477 | private void updateFromSourceField() { | |
478 | setSourceName(directoryNameField.getText()); | |
479 | updateWidgetEnablements(); | |
480 | } | |
481 | ||
482 | private void setSourceName(String path) { | |
483 | if (path.length() > 0) { | |
484 | String[] currentItems = directoryNameField.getItems(); | |
485 | int selectionIndex = -1; | |
486 | for (int i = 0; i < currentItems.length; i++) { | |
487 | if (currentItems[i].equals(path)) { | |
488 | selectionIndex = i; | |
489 | } | |
490 | } | |
491 | if (selectionIndex < 0) { | |
492 | int oldLength = currentItems.length; | |
493 | String[] newItems = new String[oldLength + 1]; | |
494 | System.arraycopy(currentItems, 0, newItems, 0, oldLength); | |
495 | newItems[oldLength] = path; | |
496 | directoryNameField.setItems(newItems); | |
497 | selectionIndex = oldLength; | |
498 | } | |
499 | directoryNameField.select(selectionIndex); | |
500 | } | |
501 | resetSelection(); | |
502 | } | |
503 | ||
504 | // ------------------------------------------------------------------------ | |
505 | // File Selection Group (forked WizardFileSystemResourceImportPage1) | |
506 | // ------------------------------------------------------------------------ | |
507 | ||
508 | private void resetSelection() { | |
509 | FileSystemElement root = getFileSystemTree(); | |
510 | populateListViewer(root); | |
511 | } | |
512 | ||
513 | private void populateListViewer(final Object treeElement) { | |
514 | fFolderViewer.setInput(treeElement); | |
515 | } | |
516 | ||
517 | private FileSystemElement getFileSystemTree() { | |
518 | File sourceDirectory = getSourceDirectory(); | |
519 | if (sourceDirectory == null) { | |
520 | return null; | |
521 | } | |
522 | return selectFiles(sourceDirectory, FileSystemStructureProvider.INSTANCE); | |
523 | } | |
524 | ||
525 | private FileSystemElement selectFiles(final Object rootFileSystemObject, final IImportStructureProvider structureProvider) { | |
526 | final FileSystemElement[] results = new FileSystemElement[1]; | |
527 | BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() { | |
528 | @Override | |
529 | public void run() { | |
530 | // Create the root element from the supplied file system object | |
531 | results[0] = createRootElement(rootFileSystemObject, structureProvider); | |
532 | } | |
533 | }); | |
534 | return results[0]; | |
535 | } | |
536 | ||
537 | private static FileSystemElement createRootElement(Object fileSystemObject, | |
538 | IImportStructureProvider provider) { | |
539 | ||
540 | boolean isContainer = provider.isFolder(fileSystemObject); | |
541 | String elementLabel = provider.getLabel(fileSystemObject); | |
542 | ||
543 | FileSystemElement dummyParent = new FileSystemElement("", null, true); //$NON-NLS-1$ | |
544 | FileSystemElement element = new FileSystemElement(elementLabel, dummyParent, isContainer); | |
545 | element.setFileSystemObject(fileSystemObject); | |
546 | ||
547 | // Get the first level | |
548 | populateChildren(element); | |
549 | ||
550 | return dummyParent; | |
551 | } | |
552 | ||
553 | // ------------------------------------------------------------------------ | |
554 | // Trace Type Group | |
555 | // ------------------------------------------------------------------------ | |
556 | ||
557 | private Combo fTraceTypes; | |
558 | ||
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 | |
575 | fTraceTypes = new Combo(composite, SWT.BORDER); | |
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(); | |
581 | fTraceTypes.setItems(availableTraceTypes); | |
582 | ||
583 | fTraceTypes.addSelectionListener(new SelectionListener() { | |
584 | @Override | |
585 | public void widgetSelected(SelectionEvent e) { | |
586 | updateWidgetEnablements(); | |
587 | } | |
588 | ||
589 | @Override | |
590 | public void widgetDefaultSelected(SelectionEvent e) { | |
591 | } | |
592 | }); | |
593 | } | |
594 | ||
595 | // ------------------------------------------------------------------------ | |
596 | // Options | |
597 | // ------------------------------------------------------------------------ | |
598 | ||
599 | private Button overwriteExistingResourcesCheckbox; | |
600 | private Button createLinksInWorkspaceButton; | |
601 | ||
602 | @Override | |
603 | protected void createOptionsGroupButtons(Group optionsGroup) { | |
604 | ||
605 | // Overwrite checkbox | |
606 | overwriteExistingResourcesCheckbox = new Button(optionsGroup, SWT.CHECK); | |
607 | overwriteExistingResourcesCheckbox.setFont(optionsGroup.getFont()); | |
608 | overwriteExistingResourcesCheckbox.setText(Messages.ImportTraceWizard_OverwriteExistingTrace); | |
609 | overwriteExistingResourcesCheckbox.setSelection(false); | |
610 | ||
611 | // Create links checkbox | |
612 | createLinksInWorkspaceButton = new Button(optionsGroup, SWT.CHECK); | |
613 | createLinksInWorkspaceButton.setFont(optionsGroup.getFont()); | |
614 | createLinksInWorkspaceButton.setText(Messages.ImportTraceWizard_CreateLinksInWorkspace); | |
615 | createLinksInWorkspaceButton.setSelection(true); | |
616 | ||
617 | createLinksInWorkspaceButton.addSelectionListener(new SelectionAdapter() { | |
618 | @Override | |
619 | public void widgetSelected(SelectionEvent e) { | |
620 | updateWidgetEnablements(); | |
621 | } | |
622 | }); | |
623 | ||
624 | updateWidgetEnablements(); | |
625 | } | |
626 | ||
627 | // ------------------------------------------------------------------------ | |
628 | // Determine if the finish button can be enabled | |
629 | // ------------------------------------------------------------------------ | |
630 | ||
631 | @Override | |
632 | public boolean validateSourceGroup() { | |
633 | ||
634 | File sourceDirectory = getSourceDirectory(); | |
635 | if (sourceDirectory == null) { | |
636 | setMessage(Messages.ImportTraceWizard_SelectTraceSourceEmpty); | |
637 | return false; | |
638 | } | |
639 | ||
640 | if (sourceConflictsWithDestination(new Path(sourceDirectory.getPath()))) { | |
641 | setMessage(null); | |
642 | setErrorMessage(getSourceConflictMessage()); | |
643 | return false; | |
644 | } | |
645 | ||
646 | List<FileSystemElement> resourcesToImport = getSelectedResources(); | |
647 | if (resourcesToImport.size() == 0) { | |
648 | setMessage(null); | |
649 | setErrorMessage(Messages.ImportTraceWizard_SelectTraceNoneSelected); | |
650 | return false; | |
651 | } | |
652 | ||
653 | IContainer container = getSpecifiedContainer(); | |
654 | if (container != null && container.isVirtual()) { | |
655 | if (Platform.getPreferencesService().getBoolean(Activator.PLUGIN_ID, ResourcesPlugin.PREF_DISABLE_LINKING, false, null)) { | |
656 | setMessage(null); | |
657 | setErrorMessage(Messages.ImportTraceWizard_CannotImportFilesUnderAVirtualFolder); | |
658 | return false; | |
659 | } | |
660 | if (createLinksInWorkspaceButton == null || !createLinksInWorkspaceButton.getSelection()) { | |
661 | setMessage(null); | |
662 | setErrorMessage(Messages.ImportTraceWizard_HaveToCreateLinksUnderAVirtualFolder); | |
663 | return false; | |
664 | } | |
665 | } | |
666 | ||
667 | // Perform trace validation | |
668 | String traceTypeName = fTraceTypes.getText(); | |
669 | String tokens[] = traceTypeName.split(SEPARATOR, 2); | |
670 | if (tokens.length >= 2) { | |
671 | String id = TmfTraceType.getInstance().getTraceTypeId(tokens[0], tokens[1]); | |
672 | if (!TmfTraceTypeUIUtils.validateTrace(TmfTraceType.getInstance(), id, getSelectedResources())) { | |
673 | setMessage(null); | |
674 | setErrorMessage(Messages.ImportTraceWizard_TraceValidationFailed); | |
675 | return false; | |
676 | } | |
677 | } | |
678 | setErrorMessage(null); | |
679 | return true; | |
680 | } | |
681 | ||
682 | // ------------------------------------------------------------------------ | |
683 | // Import the trace(s) | |
684 | // ------------------------------------------------------------------------ | |
685 | ||
686 | /** | |
687 | * Finish the import. | |
688 | * | |
689 | * @return <code>true</code> if successful else false | |
690 | */ | |
691 | public boolean finish() { | |
692 | // Ensure source is valid | |
693 | File sourceDir = new File(getSourceDirectoryName()); | |
694 | if (!sourceDir.isDirectory()) { | |
695 | setErrorMessage(Messages.ImportTraceWizard_InvalidTraceDirectory); | |
696 | return false; | |
697 | } | |
698 | ||
699 | try { | |
700 | sourceDir.getCanonicalPath(); | |
701 | } catch (IOException e) { | |
702 | MessageDialog.openInformation(getContainer().getShell(), Messages.ImportTraceWizard_Information, | |
703 | Messages.ImportTraceWizard_InvalidTraceDirectory); | |
704 | return false; | |
705 | } | |
706 | ||
707 | // Save directory for next import operation | |
708 | fRootDirectory = getSourceDirectoryName(); | |
709 | ||
710 | List<FileSystemElement> selectedResources = getSelectedResources(); | |
711 | Iterator<FileSystemElement> resources = selectedResources.iterator(); | |
712 | ||
713 | // Use a map to end up with unique resources (getSelectedResources() can | |
714 | // return duplicates) | |
715 | Map<String, File> fileSystemObjects = new HashMap<>(); | |
716 | while (resources.hasNext()) { | |
717 | File file = (File) resources.next().getFileSystemObject(); | |
718 | String key = file.getAbsolutePath(); | |
719 | fileSystemObjects.put(key, file); | |
720 | } | |
721 | ||
722 | if (fileSystemObjects.size() > 0) { | |
723 | boolean ok = importResources(fileSystemObjects); | |
724 | String traceTypeName = fTraceTypes.getText(); | |
725 | boolean traceTypeOK = false; | |
726 | TraceTypeHelper traceTypeHelper = null; | |
727 | ||
728 | if (!traceTypeName.isEmpty()) { | |
729 | // Trace type was selected | |
730 | String temp[] = traceTypeName.split(SEPARATOR, 2); | |
731 | if (temp.length < 2) { | |
732 | Activator.getDefault().logError("Error with trace type " + traceTypeName); //$NON-NLS-1$ | |
733 | return false; | |
734 | } | |
735 | final String traceId = TmfTraceType.getInstance().getTraceTypeId(temp[0], temp[1]); | |
736 | if (traceId != null) { | |
737 | traceTypeHelper = TmfTraceType.getInstance().getTraceType(traceId); | |
738 | if (traceTypeHelper == null || !TmfTraceTypeUIUtils.validateTrace(TmfTraceType.getInstance(), traceId, getSelectedResources())) { | |
739 | setMessage(null); | |
740 | setErrorMessage(Messages.ImportTraceWizard_TraceValidationFailed); | |
741 | return false; | |
742 | } | |
743 | ||
744 | traceTypeOK = true; | |
745 | } else { | |
746 | setMessage(null); | |
747 | setErrorMessage(Messages.ImportTraceWizard_TraceValidationFailed); | |
748 | return false; | |
749 | } | |
750 | } | |
751 | ||
752 | if (ok && traceTypeOK) { | |
753 | // Tag the selected traces with their type | |
754 | List<String> files = new ArrayList<>(fileSystemObjects.keySet()); | |
755 | Collections.sort(files, new Comparator<String>() { | |
756 | @Override | |
757 | public int compare(String o1, String o2) { | |
758 | String v1 = o1 + File.separatorChar; | |
759 | String v2 = o2 + File.separatorChar; | |
760 | return v1.compareTo(v2); | |
761 | } | |
762 | }); | |
763 | // After sorting, traces correspond to the unique prefixes | |
764 | String prefix = null; | |
765 | for (int i = 0; i < files.size(); i++) { | |
766 | File file = fileSystemObjects.get(files.get(i)); | |
767 | String name = file.getAbsolutePath() + File.separatorChar; | |
768 | if (fTargetFolder != null && (prefix == null || !name.startsWith(prefix))) { | |
769 | prefix = name; // new prefix | |
770 | IResource resource = fTargetFolder.findMember(file.getName()); | |
771 | if (resource != null) { | |
772 | try { | |
773 | TmfTraceTypeUIUtils.setTraceType(resource.getFullPath(), traceTypeHelper); | |
774 | ||
775 | TmfProjectElement tmfProject = TmfProjectRegistry.getProject(resource.getProject()); | |
776 | if (tmfProject != null) { | |
777 | for (TmfTraceElement traceElement : tmfProject.getTracesFolder().getTraces()) { | |
778 | if (traceElement.getName().equals(resource.getName())) { | |
779 | traceElement.refreshTraceType(); | |
780 | break; | |
781 | } | |
782 | } | |
783 | } | |
784 | } catch (CoreException e) { | |
785 | Activator.getDefault().logError("Error importing trace resource " + resource.getName(), e); //$NON-NLS-1$ | |
786 | } | |
787 | } | |
788 | } | |
789 | } | |
790 | } | |
791 | return ok; | |
792 | } | |
793 | ||
794 | MessageDialog.openInformation(getContainer().getShell(), Messages.ImportTraceWizard_Information, | |
795 | Messages.ImportTraceWizard_SelectTraceNoneSelected); | |
796 | return false; | |
797 | } | |
798 | ||
799 | private boolean importResources(Map<String, File> fileSystemObjects) { | |
800 | ||
801 | // Determine the sorted canonical list of items to import | |
802 | List<File> fileList = new ArrayList<>(); | |
803 | for (Entry<String, File> entry : fileSystemObjects.entrySet()) { | |
804 | fileList.add(entry.getValue()); | |
805 | } | |
806 | Collections.sort(fileList, new Comparator<File>() { | |
807 | @Override | |
808 | public int compare(File o1, File o2) { | |
809 | String v1 = o1.getAbsolutePath() + File.separatorChar; | |
810 | String v2 = o2.getAbsolutePath() + File.separatorChar; | |
811 | return v1.compareTo(v2); | |
812 | } | |
813 | }); | |
814 | ||
815 | // Perform a distinct import operation for everything that has the same | |
816 | // prefix | |
817 | // (distinct prefixes correspond to traces - we don't want to re-create | |
818 | // parent structures) | |
819 | boolean ok = true; | |
820 | boolean isLinked = createLinksInWorkspaceButton.getSelection(); | |
821 | for (int i = 0; i < fileList.size(); i++) { | |
822 | File resource = fileList.get(i); | |
823 | File parentFolder = new File(resource.getParent()); | |
824 | ||
825 | List<File> subList = new ArrayList<>(); | |
826 | subList.add(resource); | |
827 | if (resource.isDirectory()) { | |
828 | String prefix = resource.getAbsolutePath() + File.separatorChar; | |
829 | boolean hasSamePrefix = true; | |
830 | for (int j = i + 1; j < fileList.size() && hasSamePrefix; j++) { | |
831 | File res = fileList.get(j); | |
832 | hasSamePrefix = res.getAbsolutePath().startsWith(prefix); | |
833 | if (hasSamePrefix) { | |
834 | // Import children individually if not linked | |
835 | if (!isLinked) { | |
836 | subList.add(res); | |
837 | } | |
838 | i = j; | |
839 | } | |
840 | } | |
841 | } | |
842 | ||
843 | // Perform the import operation for this subset | |
844 | FileSystemStructureProvider fileSystemStructureProvider = FileSystemStructureProvider.INSTANCE; | |
845 | ImportOperation operation = new ImportOperation(getContainerFullPath(), parentFolder, fileSystemStructureProvider, this, | |
846 | subList); | |
847 | operation.setContext(getShell()); | |
848 | ok = executeImportOperation(operation); | |
849 | } | |
850 | ||
851 | return ok; | |
852 | } | |
853 | ||
854 | private boolean executeImportOperation(ImportOperation op) { | |
855 | initializeOperation(op); | |
856 | ||
857 | try { | |
858 | getContainer().run(true, true, op); | |
859 | } catch (InterruptedException e) { | |
860 | return false; | |
861 | } catch (InvocationTargetException e) { | |
862 | displayErrorDialog(e.getTargetException()); | |
863 | return false; | |
864 | } | |
865 | ||
866 | IStatus status = op.getStatus(); | |
867 | if (!status.isOK()) { | |
868 | ErrorDialog.openError(getContainer().getShell(), Messages.ImportTraceWizard_ImportProblem, null, status); | |
869 | return false; | |
870 | } | |
871 | ||
872 | return true; | |
873 | } | |
874 | ||
875 | private void initializeOperation(ImportOperation op) { | |
876 | op.setCreateContainerStructure(false); | |
877 | op.setOverwriteResources(overwriteExistingResourcesCheckbox.getSelection()); | |
878 | op.setCreateLinks(createLinksInWorkspaceButton.getSelection()); | |
879 | op.setVirtualFolders(false); | |
880 | } | |
881 | ||
882 | } |