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 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
;
16 import java
.util
.ArrayList
;
17 import java
.util
.HashMap
;
18 import java
.util
.List
;
20 import java
.util
.Map
.Entry
;
22 import java
.util
.TreeSet
;
24 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
25 import org
.eclipse
.jface
.viewers
.Viewer
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TraceTypeHelper
;
30 * A helper class to show the trace types and files and names. it contains the
31 * model which can be defined as follows : {tracetype -> { file1, file2, ... }
34 * @author Matthew Khouzam
36 class ImportTraceContentProvider
implements ITreeContentProvider
{
38 private final Map
<String
, String
> fTraceTypes
= new HashMap
<>();
39 private final Map
<String
, Set
<FileAndName
>> fTraceFiles
= new HashMap
<>();
40 private final List
<String
> fTraceTypesToScan
;
41 private final Set
<String
> fParentFilesToScan
;
43 public ImportTraceContentProvider(List
<String
> traceTypesToScan
, Set
<String
> parentFilesToScan
) {
44 fTraceTypesToScan
= traceTypesToScan
;
45 fParentFilesToScan
= parentFilesToScan
;
49 * Add a trace candidate to display
52 * the trace type id of the trace
56 public synchronized void addCandidate(String traceTypeId
, File traceToOpen
) {
57 TraceTypeHelper traceTypeHelper
= TmfTraceType
.getTraceType(traceTypeId
);
58 if (traceTypeHelper
== null) {
61 fTraceTypes
.put(traceTypeHelper
.getName(), traceTypeId
);
62 if (!fTraceFiles
.containsKey(traceTypeId
)) {
63 fTraceFiles
.put(traceTypeId
, new TreeSet
<FileAndName
>());
65 final FileAndName traceFile
= new FileAndName(traceToOpen
, traceToOpen
.getName());
66 traceFile
.setTraceTypeId(traceTypeId
);
67 final Set
<FileAndName
> categorySet
= fTraceFiles
.get(traceTypeId
);
68 categorySet
.add(traceFile
);
72 * Reset all the candidates
74 public synchronized void clearCandidates() {
80 public void dispose() {
87 public synchronized void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
88 if (oldInput
!= newInput
&& newInput
!= null) {
89 ImportTraceContentProvider input
= (ImportTraceContentProvider
) newInput
;
91 fTraceTypes
.putAll(input
.fTraceTypes
);
92 fTraceFiles
.putAll(fTraceFiles
);
97 public synchronized Object
[] getElements(Object inputElement
) {
98 List
<String
> candidates
= new ArrayList
<>();
100 for (String candidate
: fTraceTypesToScan
) {
101 for (Entry
<String
, String
> entry
: fTraceTypes
.entrySet()) {
102 if (entry
.getValue().equals(candidate
)) {
103 candidates
.add(entry
.getKey());
109 return candidates
.toArray(new String
[candidates
.size()]);
113 public synchronized Object
[] getChildren(Object parentElement
) {
114 if (parentElement
instanceof String
) {
115 final Set
<FileAndName
> children
= fTraceFiles
.get(fTraceTypes
.get(parentElement
));
116 if (children
!= null) {
117 Set
<FileAndName
> candidates
= new TreeSet
<>();
118 for (FileAndName child
: children
) {
119 for (String parent
: fParentFilesToScan
) {
120 // this is going to be slow, but less slow than UI
121 // display and should not be done for more than 10k
123 if (child
.getFile().getAbsolutePath().startsWith(parent
)) {
124 candidates
.add(child
);
128 return candidates
.toArray(new FileAndName
[0]);
135 * Gets the brothers and systems of a file element
139 * @return the siblings of an element, including itself. Should never be
142 public synchronized FileAndName
[] getSiblings(FileAndName element
) {
143 String key
= (String
) getParent(element
);
144 return (FileAndName
[]) getChildren(key
);
149 public synchronized Object
getParent(Object element
) {
150 if (element
instanceof FileAndName
) {
151 for (String key
: fTraceFiles
.keySet()) {
152 Set
<FileAndName
> fanSet
= fTraceFiles
.get(key
);
153 if (fanSet
.contains(element
)) {
162 public synchronized boolean hasChildren(Object element
) {
163 if (element
instanceof String
) {
164 String key
= (String
) element
;
165 return fTraceFiles
.containsKey(fTraceTypes
.get(key
));
171 * Gets the number of traces to import
173 * @return the number of traces to import
175 public synchronized int getSize() {
177 for (String s
: fTraceFiles
.keySet()) {
178 tot
+= fTraceFiles
.get(s
).size();