1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 * Marc-Andre Laperle - Log some exceptions
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
.importtrace
;
17 import java
.io
.FileInputStream
;
18 import java
.lang
.reflect
.InvocationTargetException
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Collections
;
21 import java
.util
.Comparator
;
22 import java
.util
.HashMap
;
23 import java
.util
.HashSet
;
24 import java
.util
.Iterator
;
25 import java
.util
.List
;
28 import java
.util
.TreeSet
;
29 import java
.util
.concurrent
.BlockingQueue
;
31 import org
.eclipse
.core
.resources
.IFile
;
32 import org
.eclipse
.core
.resources
.IFolder
;
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
;
37 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
38 import org
.eclipse
.core
.runtime
.IStatus
;
39 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
40 import org
.eclipse
.core
.runtime
.Path
;
41 import org
.eclipse
.core
.runtime
.Status
;
42 import org
.eclipse
.core
.runtime
.SubMonitor
;
43 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
44 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
45 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
46 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
47 import org
.eclipse
.jface
.wizard
.IWizardPage
;
48 import org
.eclipse
.jface
.wizard
.Wizard
;
49 import org
.eclipse
.jface
.wizard
.WizardDialog
;
50 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
51 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.model
.TmfImportHelper
;
52 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
53 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
54 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
55 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
56 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
57 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceType
;
58 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TraceTypeHelper
;
59 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TraceValidationHelper
;
60 import org
.eclipse
.ui
.IImportWizard
;
61 import org
.eclipse
.ui
.IWorkbench
;
62 import org
.eclipse
.ui
.dialogs
.IOverwriteQuery
;
63 import org
.eclipse
.ui
.wizards
.datatransfer
.FileSystemStructureProvider
;
64 import org
.eclipse
.ui
.wizards
.datatransfer
.ImportOperation
;
67 * Batch Import trace wizard.
69 * @author Matthew Khouzam
72 public class BatchImportTraceWizard
extends Wizard
implements IImportWizard
{
74 private static final int WIN_HEIGHT
= 400;
75 private static final int WIN_WIDTH
= 800;
76 private static final Status CANCEL_STATUS
= new Status(IStatus
.CANCEL
, Activator
.PLUGIN_ID
, ""); //$NON-NLS-1$
77 private static final int TOTALWORK
= 65536;
82 private static final int MAX_FILES
= TOTALWORK
- 1;
83 private static final String BATCH_IMPORT_WIZARD
= "BatchImportTraceWizard"; //$NON-NLS-1$
89 private IWizardPage fSelectDirectoriesPage
;
90 private ImportTraceWizardScanPage fScanPage
;
91 private IWizardPage fSelectTypePage
;
92 private IWizardPage fOptions
;
94 private final List
<String
> fTraceTypesToScan
= new ArrayList
<>();
95 private final Set
<String
> fParentFilesToScan
= new HashSet
<>();
97 private ImportTraceContentProvider fScannedTraces
= new ImportTraceContentProvider(fTraceTypesToScan
, fParentFilesToScan
);
99 private final Map
<TraceValidationHelper
, Boolean
> fResults
= new HashMap
<>();
100 private boolean fOverwrite
= true;
101 private boolean fLinked
= true;
103 private BlockingQueue
<TraceValidationHelper
> fTracesToScan
;
104 private final Set
<FileAndName
> fTraces
= new TreeSet
<>();
106 private Map
<String
, Set
<String
>> fParentFiles
= new HashMap
<>();
108 // Target import directory ('Traces' folder)
109 private IFolder fTargetFolder
;
112 * Returns the ScannedTraces model
114 * @return the ScannedTraces model
116 public ImportTraceContentProvider
getScannedTraces() {
117 return fScannedTraces
;
123 public BatchImportTraceWizard() {
124 IDialogSettings workbenchSettings
= Activator
.getDefault().getDialogSettings();
125 IDialogSettings section
= workbenchSettings
.getSection(BATCH_IMPORT_WIZARD
);
126 if (section
== null) {
127 section
= workbenchSettings
.addNewSection(BATCH_IMPORT_WIZARD
);
129 setDialogSettings(section
);
130 setNeedsProgressMonitor(true);
134 public void init(IWorkbench workbench
, IStructuredSelection selection
) {
136 fSelectDirectoriesPage
= new ImportTraceWizardSelectDirectoriesPage(workbench
, selection
);
137 fScanPage
= new ImportTraceWizardScanPage(workbench
, selection
);
138 fSelectTypePage
= new ImportTraceWizardSelectTraceTypePage(workbench
, selection
);
139 fOptions
= new ImportTraceWizardPageOptions(workbench
, selection
);
140 // keep in case it's called later
141 Iterator
<?
> iter
= selection
.iterator();
142 while (iter
.hasNext()) {
143 Object selected
= iter
.next();
144 if (selected
instanceof TmfTraceFolder
) {
145 fTargetFolder
= ((TmfTraceFolder
) selected
).getResource();
153 public void addPages() {
154 addPage(fSelectTypePage
);
155 addPage(fSelectDirectoriesPage
);
158 final WizardDialog container
= (WizardDialog
) getContainer();
159 if (container
!= null) {
160 container
.setPageSize(WIN_WIDTH
, WIN_HEIGHT
);
170 public void addFileToScan(final String fileName
) {
171 String absolutePath
= new File(fileName
).getAbsolutePath();
172 if (!fParentFiles
.containsKey(absolutePath
)) {
173 fParentFiles
.put(absolutePath
, new HashSet
<String
>());
174 startUpdateTask(Messages
.BatchImportTraceWizardAdd
+ ' ' + absolutePath
, absolutePath
);
181 * Remove files from selection
184 * the name of the file to remove
186 public void removeFile(final String fileName
) {
187 fParentFiles
.remove(fileName
);
188 fParentFilesToScan
.remove(fileName
);
189 startUpdateTask(Messages
.BatchImportTraceWizardRemove
+ ' ' + fileName
, null);
192 private void startUpdateTask(final String taskName
, final String fileAbsolutePath
) {
194 this.getContainer().run(true, true, new IRunnableWithProgress() {
197 public void run(IProgressMonitor monitor
) throws InvocationTargetException
, InterruptedException
{
198 synchronized (BatchImportTraceWizard
.this) { // this should
202 sm
= SubMonitor
.convert(monitor
);
203 sm
.setTaskName(taskName
);
204 sm
.setWorkRemaining(TOTALWORK
);
205 updateFiles(sm
, fileAbsolutePath
);
210 } catch (InvocationTargetException e
) {
211 Activator
.getDefault().logError(Messages
.ImportTraceWizardImportProblem
, e
);
212 } catch (InterruptedException e
) {
217 * The set of names of the selected files
219 * @return the set of names of the selected files
221 public Set
<String
> getFileNames() {
222 return fParentFilesToScan
;
226 * Reset the trace list to import
228 public void clearTraces() {
233 public boolean performFinish() {
234 if (fTraces
.isEmpty()) {
237 // if this turns out to be too slow, put in a progress monitor. Does not
238 // appear to be slow for the moment.
239 boolean success
= importTraces();
243 private boolean importTraces() {
244 boolean success
= false;
245 IOverwriteQuery overwriteQuery
= new IOverwriteQuery() {
247 public String
queryOverwrite(String file
) {
248 return fOverwrite ? IOverwriteQuery
.ALL
: IOverwriteQuery
.NO_ALL
;
251 FileSystemStructureProvider fileSystemStructureProvider
= FileSystemStructureProvider
.INSTANCE
;
253 for (FileAndName traceToImport
: fTraces
) {
256 if (TmfImportHelper
.createLink(fTargetFolder
, Path
.fromOSString(traceToImport
.getFile().getAbsolutePath()), traceToImport
.getName()) == null) {
260 success
= setTraceType(traceToImport
).isOK();
264 List
<File
> subList
= new ArrayList
<>();
265 IPath path
= fTargetFolder
.getFullPath();
266 File parentFile
= traceToImport
.getFile();
267 final boolean isFile
= parentFile
.isFile();
269 IFile resource
= ResourcesPlugin
.getWorkspace().getRoot().getFile(path
.append(traceToImport
.getName()));
270 if (fOverwrite
|| !resource
.exists()) {
271 subList
.add(parentFile
);
272 parentFile
= parentFile
.getParentFile();
273 try (final FileInputStream source
= new FileInputStream(traceToImport
.getFile());) {
274 if (resource
.exists()) {
275 resource
.delete(IResource
.FORCE
, new NullProgressMonitor());
277 resource
.create(source
, true, new NullProgressMonitor());
279 setTraceType(traceToImport
);
283 // Add trace directory
284 subList
.add(traceToImport
.getFile());
285 // Add all files in trace directory
286 File
[] fileList
= traceToImport
.getFile().listFiles();
287 for (File child
: fileList
) {
291 Collections
.sort(subList
, new Comparator
<File
>() {
293 public int compare(File o1
, File o2
) {
294 return o1
.getAbsolutePath().compareTo(o2
.getAbsolutePath());
297 ImportOperation operation
= new ImportOperation(
299 parentFile
.getParentFile(),
300 fileSystemStructureProvider
,
303 operation
.setContext(getShell());
304 operation
.setCreateContainerStructure(false);
305 if (executeImportOperation(operation
)) {
306 setTraceType(traceToImport
);
312 } catch (Exception e
) {
318 private IStatus
setTraceType(FileAndName traceToImport
) {
319 IStatus validate
= Status
.OK_STATUS
;
320 IPath path
= fTargetFolder
.getFullPath().append(traceToImport
.getName());
321 IResource resource
= ResourcesPlugin
.getWorkspace().getRoot().findMember(path
);
322 if (resource
!= null) {
324 // Set the trace type for this resource
325 String traceTypeId
= traceToImport
.getTraceTypeId();
326 TraceTypeHelper traceType
= TmfTraceType
.getInstance().getTraceType(traceTypeId
);
327 if (traceType
!= null) {
328 TmfTraceType
.setTraceType(path
, traceType
);
331 TmfProjectElement tmfProject
=
332 TmfProjectRegistry
.getProject(resource
.getProject());
333 if (tmfProject
!= null) {
334 final TmfTraceFolder tracesFolder
= tmfProject
.getTracesFolder();
335 tracesFolder
.refresh();
337 List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
338 boolean found
= false;
339 for (TmfTraceElement traceElement
: traces
) {
340 if (traceElement
.getName().equals(resource
.getName())) {
341 traceElement
.refreshTraceType();
347 TmfTraceElement te
= new TmfTraceElement(traceToImport
.getName(), resource
, tracesFolder
);
348 te
.refreshTraceType();
349 traces
= tracesFolder
.getTraces();
350 for (TmfTraceElement traceElement
: traces
) {
351 if (traceElement
.getName().equals(resource
.getName())) {
352 traceElement
.refreshTraceType();
353 ITmfTrace tmfTrace
= null;
355 tmfTrace
= traceElement
.instantiateTrace();
356 if (tmfTrace
!= null) {
357 validate
= tmfTrace
.validate(tmfProject
.getResource(), traceElement
.getLocation().getPath());
359 return new Status(IStatus
.ERROR
, traceElement
.getName(), "File does not exist : " + traceElement
.getLocation().getPath()); //$NON-NLS-1$
362 if (tmfTrace
!= null) {
373 } catch (CoreException e
) {
374 Activator
.getDefault().logError(Messages
.BatchImportTraceWizardErrorImportingTraceResource
375 + ' ' + resource
.getName(), e
);
382 public boolean canFinish() {
383 return super.canFinish() && hasTracesToImport() && !hasConflicts() && (fTargetFolder
!= null);
387 * Returns if a trace to import is selected
389 * @return if there are traces to import
391 public boolean hasTracesToImport() {
392 return fTraces
.size() > 0;
396 * Reset the files to scan
398 public void clearFilesToScan() {
399 fTracesToScan
.clear();
403 * Set the trace types to scan
405 * @param tracesToScan
406 * a list of trace types to scan for
408 public void setTraceTypesToScan(List
<String
> tracesToScan
) {
409 // intersection to know if there's a diff.
410 // if there's a diff, we need to re-enque everything
411 List
<String
> added
= new ArrayList
<>();
412 for (String traceLoc
: tracesToScan
) {
413 if (!fTraceTypesToScan
.contains(traceLoc
)) {
417 fTraceTypesToScan
.clear();
418 fTraceTypesToScan
.addAll(tracesToScan
);
419 updateTracesToScan(added
);
423 * Get the trace types to scan
425 * @return a list of traces to Scan for
427 public List
<String
> getTraceTypesToScan() {
428 return fTraceTypesToScan
;
432 * Add files to Import
435 * add the file and tracetype to import
437 public void addFileToImport(FileAndName element
) {
438 fTraces
.add(element
);
443 * Remove the file to scan
446 * the element to remove
448 public void removeFileToImport(FileAndName element
) {
449 fTraces
.remove(element
);
450 element
.setConflictingName(false);
455 * Updates the trace to see if there are conflicts.
457 public void updateConflicts() {
458 final FileAndName
[] fChildren
= fTraces
.toArray(new FileAndName
[0]);
459 for (int i
= 0; i
< fChildren
.length
; i
++) {
460 fChildren
[i
].setConflictingName(false);
462 for (int i
= 1; i
< fChildren
.length
; i
++) {
463 for (int j
= 0; j
< i
; j
++) {
464 if (fChildren
[i
].getName().equals(fChildren
[j
].getName())) {
465 fChildren
[i
].setConflictingName(true);
466 fChildren
[j
].setConflictingName(true);
470 getContainer().updateButtons();
474 * Is there a name conflict
476 boolean hasConflicts() {
477 boolean conflict
= false;
478 for (FileAndName child
: fTraces
) {
479 conflict
|= child
.isConflictingName();
484 private boolean executeImportOperation(ImportOperation op
) {
485 initializeOperation(op
);
488 getContainer().run(true, true, op
);
489 } catch (InterruptedException e
) {
491 } catch (InvocationTargetException e
) {
492 Activator
.getDefault().logError(Messages
.ImportTraceWizardImportProblem
, e
);
496 IStatus status
= op
.getStatus();
497 if (!status
.isOK()) {
498 ErrorDialog
.openError(getContainer().getShell(), Messages
.ImportTraceWizardImportProblem
, null, status
);
505 private static void initializeOperation(ImportOperation op
) {
506 op
.setCreateContainerStructure(false);
507 op
.setOverwriteResources(false);
508 op
.setVirtualFolders(false);
512 * Override existing resources
517 public void setOverwrite(boolean selection
) {
518 fOverwrite
= selection
;
522 * Is the trace linked?
527 public void setLinked(boolean isLink
) {
532 * @param tracesToScan
533 * sets the common traces to scan
535 public void setTracesToScan(BlockingQueue
<TraceValidationHelper
> tracesToScan
) {
536 fTracesToScan
= tracesToScan
;
542 * @return if the trace has been scanned yet or not
544 public boolean hasScanned(TraceValidationHelper traceToScan
) {
545 return fResults
.containsKey(traceToScan
);
549 * Add a result to a cache
552 * The trace that has been scanned
554 * if the trace is valid
556 public void addResult(TraceValidationHelper traceToScan
, boolean validate
) {
557 fResults
.put(traceToScan
, validate
);
561 * Gets if the trace has been scanned or not
565 * @return whether it passes or not
567 public Boolean
getResult(TraceValidationHelper traceToScan
) {
568 return fResults
.get(traceToScan
);
572 * Returns the amount of files scanned
574 * @return the amount of files scanned
576 public int getNumberOfResults() {
577 return fResults
.size();
580 private void updateTracesToScan(final List
<String
> added
) {
581 // Treeset is used instead of a hashset since the traces should be read
582 // in the order they were added.
583 final Set
<String
> filesToScan
= new TreeSet
<>();
584 for (String name
: fParentFiles
.keySet()) {
585 filesToScan
.addAll(fParentFiles
.get(name
));
587 IProgressMonitor pm
= new NullProgressMonitor();
589 updateScanQueue(pm
, filesToScan
, added
);
590 } catch (InterruptedException e
) {
596 * I am a job. Make me work
598 private synchronized IStatus
updateFiles(IProgressMonitor monitor
, String traceToScanAbsPath
) {
599 final Set
<String
> filesToScan
= new TreeSet
<>();
602 for (String name
: fParentFiles
.keySet()) {
604 final File file
= new File(name
);
605 final File
[] listFiles
= file
.listFiles();
606 if (listFiles
!= null) {
607 workToDo
+= listFiles
.length
;
610 int step
= TOTALWORK
/ workToDo
;
612 for (String name
: fParentFiles
.keySet()) {
613 final File fileToAdd
= new File(name
);
614 final Set
<String
> parentFilesToScan
= fParentFiles
.get(fileToAdd
.getAbsolutePath());
615 recurse(parentFilesToScan
, fileToAdd
, monitor
, step
);
616 if (monitor
.isCanceled()) {
617 fParentFilesToScan
.remove(traceToScanAbsPath
);
618 fParentFiles
.remove(traceToScanAbsPath
);
619 return CANCEL_STATUS
;
623 for (String name
: fParentFiles
.keySet()) {
624 filesToScan
.addAll(fParentFiles
.get(name
));
625 fParentFilesToScan
.add(name
);
627 IStatus cancelled
= updateScanQueue(monitor
, filesToScan
, fTraceTypesToScan
);
628 if (cancelled
.matches(IStatus
.CANCEL
)) {
629 fParentFilesToScan
.remove(traceToScanAbsPath
);
630 fParentFiles
.remove(traceToScanAbsPath
);
632 } catch (InterruptedException e
) {
634 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, e
.getMessage(), e
);
638 return Status
.OK_STATUS
;
641 private IStatus
updateScanQueue(IProgressMonitor monitor
, final Set
<String
> filesToScan
, final List
<String
> traceTypes
) throws InterruptedException
{
642 for (String fileToScan
: filesToScan
) {
643 for (String traceCat
: traceTypes
) {
644 TraceValidationHelper tv
= new TraceValidationHelper(fileToScan
, traceCat
);
645 // for thread safety, keep checks in this order.
646 if (!fResults
.containsKey(tv
)) {
647 if (!fTracesToScan
.contains(tv
)) {
648 fTracesToScan
.put(tv
);
649 monitor
.subTask(tv
.getTraceToScan());
650 if (monitor
.isCanceled()) {
652 return CANCEL_STATUS
;
659 return Status
.OK_STATUS
;
662 private IStatus
recurse(Set
<String
> filesToScan
, File fileToAdd
, IProgressMonitor monitor
, int step
) {
663 final String absolutePath
= fileToAdd
.getAbsolutePath();
664 if (!filesToScan
.contains(absolutePath
) && (filesToScan
.size() < MAX_FILES
)) {
665 filesToScan
.add(absolutePath
);
666 final File
[] listFiles
= fileToAdd
.listFiles();
667 if (null != listFiles
) {
668 for (File child
: listFiles
) {
669 monitor
.subTask(child
.getName());
670 if (monitor
.isCanceled()) {
671 return CANCEL_STATUS
;
673 IStatus retVal
= recurse(filesToScan
, child
, monitor
);
674 if (retVal
.matches(IStatus
.CANCEL
)) {
677 monitor
.worked(step
);
681 return Status
.OK_STATUS
;
684 private IStatus
recurse(Set
<String
> filesToScan
, File fileToAdd
, IProgressMonitor monitor
) {
685 final String absolutePath
= fileToAdd
.getAbsolutePath();
686 if (!filesToScan
.contains(absolutePath
) && (filesToScan
.size() < MAX_FILES
)) {
687 filesToScan
.add(absolutePath
);
688 final File
[] listFiles
= fileToAdd
.listFiles();
689 if (null != listFiles
) {
690 for (File child
: listFiles
) {
691 if (monitor
.isCanceled()) {
692 return CANCEL_STATUS
;
694 IStatus retVal
= recurse(filesToScan
, child
, monitor
);
695 if (retVal
.matches(IStatus
.CANCEL
)) {
701 return Status
.OK_STATUS
;
705 * Gets the folder in the resource (project)
707 * @param targetFolder
708 * the folder to import to
710 public void setTraceFolder(IFolder targetFolder
) {
711 fTargetFolder
= targetFolder
;
712 if (this.getContainer() != null && this.getContainer().getCurrentPage() != null) {
713 this.getContainer().updateButtons();
718 * Gets the target folder
720 * @return the target folder
722 public IFolder
getTargetFolder() {
723 return fTargetFolder
;