analysis: Move plugins to their own sub-directory
[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 */
36 class ImportTraceContentProvider implements ITreeContentProvider {
37
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;
42
43 public ImportTraceContentProvider(List<String> traceTypesToScan, Set<String> parentFilesToScan) {
44 fTraceTypesToScan = traceTypesToScan;
45 fParentFilesToScan = parentFilesToScan;
46 }
47
48 /**
49 * Add a trace candidate to display
50 *
51 * @param traceTypeId
52 * the trace type id of the trace
53 * @param traceToOpen
54 * the trace file.
55 */
56 public synchronized void addCandidate(String traceTypeId, File traceToOpen) {
57 TraceTypeHelper traceTypeHelper = TmfTraceType.getTraceType(traceTypeId);
58 if (traceTypeHelper == null) {
59 return;
60 }
61 fTraceTypes.put(traceTypeHelper.getName(), traceTypeId);
62 if (!fTraceFiles.containsKey(traceTypeId)) {
63 fTraceFiles.put(traceTypeId, new TreeSet<FileAndName>());
64 }
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);
69 }
70
71 /**
72 * Reset all the candidates
73 */
74 public synchronized void clearCandidates() {
75 fTraceTypes.clear();
76 fTraceFiles.clear();
77 }
78
79 @Override
80 public void dispose() {
81 fTraceFiles.clear();
82 fTraceTypes.clear();
83
84 }
85
86 @Override
87 public synchronized void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
88 if (oldInput != newInput && newInput != null) {
89 ImportTraceContentProvider input = (ImportTraceContentProvider) newInput;
90 clearCandidates();
91 fTraceTypes.putAll(input.fTraceTypes);
92 fTraceFiles.putAll(fTraceFiles);
93 }
94 }
95
96 @Override
97 public synchronized Object[] getElements(Object inputElement) {
98 List<String> candidates = new ArrayList<>();
99
100 for (String candidate : fTraceTypesToScan) {
101 for (Entry<String, String> entry : fTraceTypes.entrySet()) {
102 if (entry.getValue().equals(candidate)) {
103 candidates.add(entry.getKey());
104 break;
105 }
106 }
107
108 }
109 return candidates.toArray(new String[candidates.size()]);
110 }
111
112 @Override
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
122 // elements.
123 if (child.getFile().getAbsolutePath().startsWith(parent)) {
124 candidates.add(child);
125 }
126 }
127 }
128 return candidates.toArray(new FileAndName[0]);
129 }
130 }
131 return null;
132 }
133
134 /**
135 * Gets the brothers and systems of a file element
136 *
137 * @param element
138 * the child leaf
139 * @return the siblings of an element, including itself. Should never be
140 * null
141 */
142 public synchronized FileAndName[] getSiblings(FileAndName element) {
143 String key = (String) getParent(element);
144 return (FileAndName[]) getChildren(key);
145
146 }
147
148 @Override
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)) {
154 return key;
155 }
156 }
157 }
158 return null;
159 }
160
161 @Override
162 public synchronized boolean hasChildren(Object element) {
163 if (element instanceof String) {
164 String key = (String) element;
165 return fTraceFiles.containsKey(fTraceTypes.get(key));
166 }
167 return false;
168 }
169
170 /**
171 * Gets the number of traces to import
172 *
173 * @return the number of traces to import
174 */
175 public synchronized int getSize() {
176 int tot = 0;
177 for (String s : fTraceFiles.keySet()) {
178 tot += fTraceFiles.get(s).size();
179 }
180 return tot;
181 }
182 }
This page took 0.035053 seconds and 5 git commands to generate.