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
37 class ImportTraceContentProvider
implements ITreeContentProvider
{
39 private final Map
<String
, String
> fTraceTypes
= new HashMap
<>();
40 private final Map
<String
, Set
<FileAndName
>> fTraceFiles
= new HashMap
<>();
41 private final List
<String
> fTraceTypesToScan
;
42 private final Set
<String
> fParentFilesToScan
;
44 public ImportTraceContentProvider(List
<String
> traceTypesToScan
, Set
<String
> parentFilesToScan
) {
45 fTraceTypesToScan
= traceTypesToScan
;
46 fParentFilesToScan
= parentFilesToScan
;
50 * Add a trace candidate to display
53 * the trace type id of the trace
57 public synchronized void addCandidate(String traceTypeId
, File traceToOpen
) {
58 TraceTypeHelper traceTypeHelper
= TmfTraceType
.getTraceType(traceTypeId
);
59 if (traceTypeHelper
== null) {
62 fTraceTypes
.put(traceTypeHelper
.getName(), traceTypeId
);
63 if (!fTraceFiles
.containsKey(traceTypeId
)) {
64 fTraceFiles
.put(traceTypeId
, new TreeSet
<FileAndName
>());
66 final FileAndName traceFile
= new FileAndName(traceToOpen
, traceToOpen
.getName());
67 traceFile
.setTraceTypeId(traceTypeId
);
68 final Set
<FileAndName
> categorySet
= fTraceFiles
.get(traceTypeId
);
69 categorySet
.add(traceFile
);
73 * Reset all the candidates
75 public synchronized void clearCandidates() {
81 public void dispose() {
88 public synchronized void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
89 if (oldInput
!= newInput
&& newInput
!= null) {
90 ImportTraceContentProvider input
= (ImportTraceContentProvider
) newInput
;
92 fTraceTypes
.putAll(input
.fTraceTypes
);
93 fTraceFiles
.putAll(fTraceFiles
);
98 public synchronized Object
[] getElements(Object inputElement
) {
99 List
<String
> candidates
= new ArrayList
<>();
101 for (String candidate
: fTraceTypesToScan
) {
102 for (Entry
<String
, String
> entry
: fTraceTypes
.entrySet()) {
103 if (entry
.getValue().equals(candidate
)) {
104 candidates
.add(entry
.getKey());
110 return candidates
.toArray(new String
[candidates
.size()]);
114 public synchronized Object
[] getChildren(Object parentElement
) {
115 if (parentElement
instanceof String
) {
116 final Set
<FileAndName
> children
= fTraceFiles
.get(fTraceTypes
.get(parentElement
));
117 if (children
!= null) {
118 Set
<FileAndName
> candidates
= new TreeSet
<>();
119 for (FileAndName child
: children
) {
120 for (String parent
: fParentFilesToScan
) {
121 // this is going to be slow, but less slow than UI
122 // display and should not be done for more than 10k
124 if (child
.getFile().getAbsolutePath().startsWith(parent
)) {
125 candidates
.add(child
);
129 return candidates
.toArray(new FileAndName
[0]);
136 * Gets the brothers and systems of a file element
140 * @return the siblings of an element, including itself. Should never be
143 public synchronized FileAndName
[] getSiblings(FileAndName element
) {
144 String key
= (String
) getParent(element
);
145 return (FileAndName
[]) getChildren(key
);
150 public synchronized Object
getParent(Object element
) {
151 if (element
instanceof FileAndName
) {
152 for (String key
: fTraceFiles
.keySet()) {
153 Set
<FileAndName
> fanSet
= fTraceFiles
.get(key
);
154 if (fanSet
.contains(element
)) {
163 public synchronized boolean hasChildren(Object element
) {
164 if (element
instanceof String
) {
165 String key
= (String
) element
;
166 return fTraceFiles
.containsKey(fTraceTypes
.get(key
));
172 * Gets the number of traces to import
174 * @return the number of traces to import
176 public synchronized int getSize() {
178 for (String s
: fTraceFiles
.keySet()) {
179 tot
+= fTraceFiles
.get(s
).size();