1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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
.WizardDialog
;
49 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
50 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.model
.TmfImportHelper
;
51 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TmfTraceType
;
52 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TraceTypeHelper
;
53 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TraceValidationHelper
;
54 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
55 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
56 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
57 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
58 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
59 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceTypeUIUtils
;
60 import org
.eclipse
.ui
.IWorkbench
;
61 import org
.eclipse
.ui
.dialogs
.IOverwriteQuery
;
62 import org
.eclipse
.ui
.wizards
.datatransfer
.FileSystemStructureProvider
;
63 import org
.eclipse
.ui
.wizards
.datatransfer
.ImportOperation
;
66 * Batch Import trace wizard.
68 * @author Matthew Khouzam
71 public class BatchImportTraceWizard
extends ImportTraceWizard
{
73 private static final int WIN_HEIGHT
= 400;
74 private static final int WIN_WIDTH
= 800;
75 private static final Status CANCEL_STATUS
= new Status(IStatus
.CANCEL
, Activator
.PLUGIN_ID
, ""); //$NON-NLS-1$
76 private static final int TOTALWORK
= 65536;
81 private static final int MAX_FILES
= TOTALWORK
- 1;
82 private static final String BATCH_IMPORT_WIZARD
= "BatchImportTraceWizard"; //$NON-NLS-1$
88 private IWizardPage fSelectDirectoriesPage
;
89 private ImportTraceWizardScanPage fScanPage
;
90 private IWizardPage fSelectTypePage
;
91 private IWizardPage fOptions
;
93 private final List
<String
> fTraceTypesToScan
= new ArrayList
<>();
94 private final Set
<String
> fParentFilesToScan
= new HashSet
<>();
96 private ImportTraceContentProvider fScannedTraces
= new ImportTraceContentProvider(fTraceTypesToScan
, fParentFilesToScan
);
98 private final Map
<TraceValidationHelper
, Boolean
> fResults
= new HashMap
<>();
99 private boolean fOverwrite
= true;
100 private boolean fLinked
= true;
102 private BlockingQueue
<TraceValidationHelper
> fTracesToScan
;
103 private final Set
<FileAndName
> fTraces
= new TreeSet
<>();
105 private Map
<String
, Set
<String
>> fParentFiles
= new HashMap
<>();
107 // Target import directory ('Traces' folder)
108 private IFolder fTargetFolder
;
111 * Returns the ScannedTraces model
113 * @return the ScannedTraces model
115 public ImportTraceContentProvider
getScannedTraces() {
116 return fScannedTraces
;
122 public BatchImportTraceWizard() {
123 IDialogSettings workbenchSettings
= Activator
.getDefault().getDialogSettings();
124 IDialogSettings section
= workbenchSettings
.getSection(BATCH_IMPORT_WIZARD
);
125 if (section
== null) {
126 section
= workbenchSettings
.addNewSection(BATCH_IMPORT_WIZARD
);
128 setDialogSettings(section
);
129 setNeedsProgressMonitor(true);
133 public void init(IWorkbench workbench
, IStructuredSelection selection
) {
135 fSelectDirectoriesPage
= new ImportTraceWizardSelectDirectoriesPage(workbench
, selection
);
136 fScanPage
= new ImportTraceWizardScanPage(workbench
, selection
);
137 fSelectTypePage
= new ImportTraceWizardSelectTraceTypePage(workbench
, selection
);
138 fOptions
= new ImportTraceWizardPageOptions(workbench
, selection
);
139 // keep in case it's called later
140 Iterator
<?
> iter
= selection
.iterator();
141 while (iter
.hasNext()) {
142 Object selected
= iter
.next();
143 if (selected
instanceof TmfTraceFolder
) {
144 fTargetFolder
= ((TmfTraceFolder
) selected
).getResource();
152 public void addPages() {
153 addPage(fSelectTypePage
);
154 addPage(fSelectDirectoriesPage
);
157 final WizardDialog container
= (WizardDialog
) getContainer();
158 if (container
!= null) {
159 container
.setPageSize(WIN_WIDTH
, WIN_HEIGHT
);
169 public void addFileToScan(final String fileName
) {
170 String absolutePath
= new File(fileName
).getAbsolutePath();
171 if (!fParentFiles
.containsKey(absolutePath
)) {
172 fParentFiles
.put(absolutePath
, new HashSet
<String
>());
173 startUpdateTask(Messages
.BatchImportTraceWizardAdd
+ ' ' + absolutePath
, absolutePath
);
180 * Remove files from selection
183 * the name of the file to remove
185 public void removeFile(final String fileName
) {
186 fParentFiles
.remove(fileName
);
187 fParentFilesToScan
.remove(fileName
);
188 startUpdateTask(Messages
.BatchImportTraceWizardRemove
+ ' ' + fileName
, null);
191 private void startUpdateTask(final String taskName
, final String fileAbsolutePath
) {
193 this.getContainer().run(true, true, new IRunnableWithProgress() {
196 public void run(IProgressMonitor monitor
) throws InvocationTargetException
, InterruptedException
{
197 synchronized (BatchImportTraceWizard
.this) { // this should
201 sm
= SubMonitor
.convert(monitor
);
202 sm
.setTaskName(taskName
);
203 sm
.setWorkRemaining(TOTALWORK
);
204 updateFiles(sm
, fileAbsolutePath
);
209 } catch (InvocationTargetException e
) {
210 Activator
.getDefault().logError(Messages
.ImportTraceWizardImportProblem
, e
);
211 } catch (InterruptedException e
) {
216 * The set of names of the selected files
218 * @return the set of names of the selected files
220 public Set
<String
> getFileNames() {
221 return fParentFilesToScan
;
225 * Reset the trace list to import
227 public void clearTraces() {
232 public boolean performFinish() {
233 if (fTraces
.isEmpty()) {
236 // if this turns out to be too slow, put in a progress monitor. Does not
237 // appear to be slow for the moment.
238 boolean success
= importTraces();
242 private boolean importTraces() {
243 boolean success
= false;
244 IOverwriteQuery overwriteQuery
= new IOverwriteQuery() {
246 public String
queryOverwrite(String file
) {
247 return fOverwrite ? IOverwriteQuery
.ALL
: IOverwriteQuery
.NO_ALL
;
250 FileSystemStructureProvider fileSystemStructureProvider
= FileSystemStructureProvider
.INSTANCE
;
252 for (FileAndName traceToImport
: fTraces
) {
255 if (TmfImportHelper
.createLink(fTargetFolder
, Path
.fromOSString(traceToImport
.getFile().getAbsolutePath()), traceToImport
.getName()) == null) {
259 success
= setTraceType(traceToImport
).isOK();
263 List
<File
> subList
= new ArrayList
<>();
264 IPath path
= fTargetFolder
.getFullPath();
265 File parentFile
= traceToImport
.getFile();
266 final boolean isFile
= parentFile
.isFile();
268 IFile resource
= ResourcesPlugin
.getWorkspace().getRoot().getFile(path
.append(traceToImport
.getName()));
269 if (fOverwrite
|| !resource
.exists()) {
270 subList
.add(parentFile
);
271 parentFile
= parentFile
.getParentFile();
272 try (final FileInputStream source
= new FileInputStream(traceToImport
.getFile());) {
273 if (resource
.exists()) {
274 resource
.delete(IResource
.FORCE
, new NullProgressMonitor());
276 resource
.create(source
, true, new NullProgressMonitor());
278 setTraceType(traceToImport
);
282 // Add trace directory
283 subList
.add(traceToImport
.getFile());
284 // Add all files in trace directory
285 File
[] fileList
= traceToImport
.getFile().listFiles();
286 for (File child
: fileList
) {
290 Collections
.sort(subList
, new Comparator
<File
>() {
292 public int compare(File o1
, File o2
) {
293 return o1
.getAbsolutePath().compareTo(o2
.getAbsolutePath());
296 ImportOperation operation
= new ImportOperation(
298 parentFile
.getParentFile(),
299 fileSystemStructureProvider
,
302 operation
.setContext(getShell());
303 operation
.setCreateContainerStructure(false);
304 if (executeImportOperation(operation
)) {
305 setTraceType(traceToImport
);
311 } catch (Exception e
) {
317 private IStatus
setTraceType(FileAndName traceToImport
) {
318 IStatus validate
= Status
.OK_STATUS
;
319 IResource resource
= fTargetFolder
.findMember(traceToImport
.getName());
320 if (resource
!= null) {
322 // Set the trace type for this resource
323 String traceTypeId
= traceToImport
.getTraceTypeId();
324 TraceTypeHelper traceType
= TmfTraceType
.getInstance().getTraceType(traceTypeId
);
325 if (traceType
!= null) {
326 TmfTraceTypeUIUtils
.setTraceType(resource
, traceType
);
329 TmfProjectElement tmfProject
=
330 TmfProjectRegistry
.getProject(resource
.getProject());
331 if (tmfProject
!= null) {
332 final TmfTraceFolder tracesFolder
= tmfProject
.getTracesFolder();
334 List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
335 boolean found
= false;
336 for (TmfTraceElement traceElement
: traces
) {
337 if (traceElement
.getName().equals(resource
.getName())) {
338 traceElement
.refreshTraceType();
344 TmfTraceElement te
= new TmfTraceElement(traceToImport
.getName(), resource
, tracesFolder
);
345 te
.refreshTraceType();
346 traces
= tracesFolder
.getTraces();
347 for (TmfTraceElement traceElement
: traces
) {
348 if (traceElement
.getName().equals(resource
.getName())) {
349 traceElement
.refreshTraceType();
350 ITmfTrace tmfTrace
= null;
352 tmfTrace
= traceElement
.instantiateTrace();
353 if (tmfTrace
!= null) {
354 validate
= tmfTrace
.validate(tmfProject
.getResource(), traceElement
.getLocation().getPath());
356 return new Status(IStatus
.ERROR
, traceElement
.getName(), "File does not exist : " + traceElement
.getLocation().getPath()); //$NON-NLS-1$
359 if (tmfTrace
!= null) {
370 } catch (CoreException e
) {
371 Activator
.getDefault().logError(Messages
.BatchImportTraceWizardErrorImportingTraceResource
372 + ' ' + resource
.getName(), e
);
379 public boolean canFinish() {
380 return super.canFinish() && hasTracesToImport() && !hasConflicts() && (fTargetFolder
!= null);
384 * Returns if a trace to import is selected
386 * @return if there are traces to import
388 public boolean hasTracesToImport() {
389 return fTraces
.size() > 0;
393 * Reset the files to scan
395 public void clearFilesToScan() {
396 fTracesToScan
.clear();
400 * Set the trace types to scan
402 * @param tracesToScan
403 * a list of trace types to scan for
405 public void setTraceTypesToScan(List
<String
> tracesToScan
) {
406 // intersection to know if there's a diff.
407 // if there's a diff, we need to re-enque everything
408 List
<String
> added
= new ArrayList
<>();
409 for (String traceLoc
: tracesToScan
) {
410 if (!fTraceTypesToScan
.contains(traceLoc
)) {
414 fTraceTypesToScan
.clear();
415 fTraceTypesToScan
.addAll(tracesToScan
);
416 updateTracesToScan(added
);
420 * Get the trace types to scan
422 * @return a list of traces to Scan for
424 public List
<String
> getTraceTypesToScan() {
425 return fTraceTypesToScan
;
429 * Add files to Import
432 * add the file and tracetype to import
434 public void addFileToImport(FileAndName element
) {
435 fTraces
.add(element
);
440 * Remove the file to scan
443 * the element to remove
445 public void removeFileToImport(FileAndName element
) {
446 fTraces
.remove(element
);
447 element
.setConflictingName(false);
452 * Updates the trace to see if there are conflicts.
454 public void updateConflicts() {
455 final FileAndName
[] fChildren
= fTraces
.toArray(new FileAndName
[0]);
456 for (int i
= 0; i
< fChildren
.length
; i
++) {
457 fChildren
[i
].setConflictingName(false);
459 for (int i
= 1; i
< fChildren
.length
; i
++) {
460 for (int j
= 0; j
< i
; j
++) {
461 if (fChildren
[i
].getName().equals(fChildren
[j
].getName())) {
462 fChildren
[i
].setConflictingName(true);
463 fChildren
[j
].setConflictingName(true);
467 getContainer().updateButtons();
471 * Is there a name conflict
473 boolean hasConflicts() {
474 boolean conflict
= false;
475 for (FileAndName child
: fTraces
) {
476 conflict
|= child
.isConflictingName();
481 private boolean executeImportOperation(ImportOperation op
) {
482 initializeOperation(op
);
485 getContainer().run(true, true, op
);
486 } catch (InterruptedException e
) {
488 } catch (InvocationTargetException e
) {
489 Activator
.getDefault().logError(Messages
.ImportTraceWizardImportProblem
, e
);
493 IStatus status
= op
.getStatus();
494 if (!status
.isOK()) {
495 ErrorDialog
.openError(getContainer().getShell(), Messages
.ImportTraceWizardImportProblem
, null, status
);
502 private static void initializeOperation(ImportOperation op
) {
503 op
.setCreateContainerStructure(false);
504 op
.setOverwriteResources(false);
505 op
.setVirtualFolders(false);
509 * Override existing resources
514 public void setOverwrite(boolean selection
) {
515 fOverwrite
= selection
;
519 * Is the trace linked?
524 public void setLinked(boolean isLink
) {
529 * @param tracesToScan
530 * sets the common traces to scan
532 public void setTracesToScan(BlockingQueue
<TraceValidationHelper
> tracesToScan
) {
533 fTracesToScan
= tracesToScan
;
539 * @return if the trace has been scanned yet or not
542 public boolean hasScanned(TraceValidationHelper traceToScan
) {
543 return fResults
.containsKey(traceToScan
);
547 * Add a result to a cache
550 * The trace that has been scanned
552 * if the trace is valid
555 public void addResult(TraceValidationHelper traceToScan
, boolean validate
) {
556 fResults
.put(traceToScan
, validate
);
560 * Gets if the trace has been scanned or not
564 * @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
;