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
.linuxtools
.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
.linuxtools
.tmf
.core
.project
.model
.TmfTraceType
;
29 * A helper class to show the trace types and files and names. it contains the
30 * model which can be defined as follows : {tracetype -> { file1, file2, ... }
33 * @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 category of the trace
56 public synchronized void addCandidate(String category
, File traceToOpen
) {
57 fTraceTypes
.put(TmfTraceType
.getInstance().getTraceType(category
).getName(), category
);
58 if (!fTraceFiles
.containsKey(category
)) {
59 fTraceFiles
.put(category
, new TreeSet
<FileAndName
>());
61 final FileAndName traceFile
= new FileAndName(traceToOpen
, traceToOpen
.getName());
62 traceFile
.setTraceTypeId(category
);
63 final Set
<FileAndName
> categorySet
= fTraceFiles
.get(category
);
64 categorySet
.add(traceFile
);
68 * Reset all the candidates
70 public synchronized void clearCandidates() {
76 public void dispose() {
83 public synchronized void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
84 if (oldInput
!= newInput
&& newInput
!= null) {
85 ImportTraceContentProvider input
= (ImportTraceContentProvider
) newInput
;
87 fTraceTypes
.putAll(input
.fTraceTypes
);
88 fTraceFiles
.putAll(fTraceFiles
);
93 public synchronized Object
[] getElements(Object inputElement
) {
94 List
<String
> candidates
= new ArrayList
<>();
96 for (String candidate
: fTraceTypesToScan
) {
97 for (Entry
<String
, String
> entry
: fTraceTypes
.entrySet()) {
98 if (entry
.getValue().equals(candidate
)) {
99 candidates
.add(entry
.getKey());
105 return candidates
.toArray(new String
[candidates
.size()]);
109 public synchronized Object
[] getChildren(Object parentElement
) {
110 if (parentElement
instanceof String
) {
111 final Set
<FileAndName
> children
= fTraceFiles
.get(fTraceTypes
.get(parentElement
));
112 if (children
!= null) {
113 Set
<FileAndName
> candidates
= new TreeSet
<>();
114 for (FileAndName child
: children
) {
115 for (String parent
: fParentFilesToScan
) {
116 // this is going to be slow, but less slow than UI
117 // display and should not be done for more than 10k
119 if (child
.getFile().getAbsolutePath().startsWith(parent
)) {
120 candidates
.add(child
);
124 return candidates
.toArray(new FileAndName
[0]);
131 * Gets the brothers and systems of a file element
135 * @return the siblings of an element, including itself. Should never be
138 public synchronized FileAndName
[] getSiblings(FileAndName element
) {
139 String key
= (String
) getParent(element
);
140 return (FileAndName
[]) getChildren(key
);
145 public synchronized Object
getParent(Object element
) {
146 if (element
instanceof FileAndName
) {
147 for (String key
: fTraceFiles
.keySet()) {
148 Set
<FileAndName
> fanSet
= fTraceFiles
.get(key
);
149 if (fanSet
.contains(element
)) {
158 public synchronized boolean hasChildren(Object element
) {
159 if (element
instanceof String
) {
160 String key
= (String
) element
;
161 return fTraceFiles
.containsKey(fTraceTypes
.get(key
));
167 * Gets the number of traces to import
169 * @return the number of traces to import
171 public synchronized int getSize() {
173 for (String s
: fTraceFiles
.keySet()) {
174 tot
+= fTraceFiles
.get(s
).size();