tmf: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / wizards / importtrace / ImportTraceContentProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace;
14
15 import java.io.File;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.Set;
22 import java.util.TreeSet;
23
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;
28
29 /**
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, ... }
32 * }+
33 *
34 * @author Matthew Khouzam
35 * @since 2.0
36 */
37 class ImportTraceContentProvider implements ITreeContentProvider {
38
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;
43
44 public ImportTraceContentProvider(List<String> traceTypesToScan, Set<String> parentFilesToScan) {
45 fTraceTypesToScan = traceTypesToScan;
46 fParentFilesToScan = parentFilesToScan;
47 }
48
49 /**
50 * Add a trace candidate to display
51 *
52 * @param traceTypeId
53 * the trace type id of the trace
54 * @param traceToOpen
55 * the trace file.
56 */
57 public synchronized void addCandidate(String traceTypeId, File traceToOpen) {
58 TraceTypeHelper traceTypeHelper = TmfTraceType.getTraceType(traceTypeId);
59 if (traceTypeHelper == null) {
60 return;
61 }
62 fTraceTypes.put(traceTypeHelper.getName(), traceTypeId);
63 if (!fTraceFiles.containsKey(traceTypeId)) {
64 fTraceFiles.put(traceTypeId, new TreeSet<FileAndName>());
65 }
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);
70 }
71
72 /**
73 * Reset all the candidates
74 */
75 public synchronized void clearCandidates() {
76 fTraceTypes.clear();
77 fTraceFiles.clear();
78 }
79
80 @Override
81 public void dispose() {
82 fTraceFiles.clear();
83 fTraceTypes.clear();
84
85 }
86
87 @Override
88 public synchronized void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
89 if (oldInput != newInput && newInput != null) {
90 ImportTraceContentProvider input = (ImportTraceContentProvider) newInput;
91 clearCandidates();
92 fTraceTypes.putAll(input.fTraceTypes);
93 fTraceFiles.putAll(fTraceFiles);
94 }
95 }
96
97 @Override
98 public synchronized Object[] getElements(Object inputElement) {
99 List<String> candidates = new ArrayList<>();
100
101 for (String candidate : fTraceTypesToScan) {
102 for (Entry<String, String> entry : fTraceTypes.entrySet()) {
103 if (entry.getValue().equals(candidate)) {
104 candidates.add(entry.getKey());
105 break;
106 }
107 }
108
109 }
110 return candidates.toArray(new String[candidates.size()]);
111 }
112
113 @Override
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
123 // elements.
124 if (child.getFile().getAbsolutePath().startsWith(parent)) {
125 candidates.add(child);
126 }
127 }
128 }
129 return candidates.toArray(new FileAndName[0]);
130 }
131 }
132 return null;
133 }
134
135 /**
136 * Gets the brothers and systems of a file element
137 *
138 * @param element
139 * the child leaf
140 * @return the siblings of an element, including itself. Should never be
141 * null
142 */
143 public synchronized FileAndName[] getSiblings(FileAndName element) {
144 String key = (String) getParent(element);
145 return (FileAndName[]) getChildren(key);
146
147 }
148
149 @Override
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)) {
155 return key;
156 }
157 }
158 }
159 return null;
160 }
161
162 @Override
163 public synchronized boolean hasChildren(Object element) {
164 if (element instanceof String) {
165 String key = (String) element;
166 return fTraceFiles.containsKey(fTraceTypes.get(key));
167 }
168 return false;
169 }
170
171 /**
172 * Gets the number of traces to import
173 *
174 * @return the number of traces to import
175 */
176 public synchronized int getSize() {
177 int tot = 0;
178 for (String s : fTraceFiles.keySet()) {
179 tot += fTraceFiles.get(s).size();
180 }
181 return tot;
182 }
183 }
This page took 0.036509 seconds and 5 git commands to generate.