tmf: Switch tmf.ui to Java 7 + fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / importtrace / ImportTraceContentProvider.java
CommitLineData
d04ec5a7
MK
1/*******************************************************************************
2 * Copyright (c) 2013 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
13package org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace;
14
15import java.io.File;
ba486486 16import java.util.ArrayList;
d04ec5a7 17import java.util.HashMap;
ba486486 18import java.util.List;
d04ec5a7 19import java.util.Map;
ba486486 20import java.util.Map.Entry;
d04ec5a7
MK
21import java.util.Set;
22import java.util.TreeSet;
23
24import org.eclipse.jface.viewers.ITreeContentProvider;
25import org.eclipse.jface.viewers.Viewer;
26import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceType;
27
28/**
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, ... }
31 * }+
32 *
33 * @author Matthew Khouzam
34 * @since 2.0
35 */
36class ImportTraceContentProvider implements ITreeContentProvider {
37
507b1336
AM
38 private final Map<String, String> fTraceTypes = new HashMap<>();
39 private final Map<String, Set<FileAndName>> fTraceFiles = new HashMap<>();
ba486486
MK
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
d04ec5a7
MK
48 /**
49 * Add a trace candidate to display
50 *
51 * @param category
52 * the category of the trace
53 * @param traceToOpen
54 * the trace file.
55 */
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>());
60 }
61 final FileAndName traceFile = new FileAndName(traceToOpen, traceToOpen.getName());
ba486486 62 traceFile.setTraceTypeId(category);
d04ec5a7
MK
63 final Set<FileAndName> categorySet = fTraceFiles.get(category);
64 categorySet.add(traceFile);
65 }
66
67 /**
68 * Reset all the candidates
69 */
70 public synchronized void clearCandidates() {
71 fTraceTypes.clear();
72 fTraceFiles.clear();
73 }
74
75 @Override
76 public void dispose() {
77 fTraceFiles.clear();
78 fTraceTypes.clear();
79
80 }
81
82 @Override
83 public synchronized void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
84 if (oldInput != newInput && newInput != null) {
85 ImportTraceContentProvider input = (ImportTraceContentProvider) newInput;
86 clearCandidates();
87 fTraceTypes.putAll(input.fTraceTypes);
88 fTraceFiles.putAll(fTraceFiles);
89 }
90 }
91
92 @Override
93 public synchronized Object[] getElements(Object inputElement) {
507b1336 94 List<String> candidates = new ArrayList<>();
ba486486
MK
95
96 for (String candidate : fTraceTypesToScan) {
97 for (Entry<String, String> entry : fTraceTypes.entrySet()) {
98 if (entry.getValue().equals(candidate)) {
99 candidates.add(entry.getKey());
100 break;
101 }
102 }
103
104 }
105 return candidates.toArray(new String[candidates.size()]);
d04ec5a7
MK
106 }
107
108 @Override
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) {
507b1336 113 Set<FileAndName> candidates = new TreeSet<>();
ba486486
MK
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
118 // elements.
119 if (child.getFile().getAbsolutePath().startsWith(parent)) {
120 candidates.add(child);
121 }
122 }
123 }
124 return candidates.toArray(new FileAndName[0]);
d04ec5a7
MK
125 }
126 }
127 return null;
128 }
129
130 /**
131 * Gets the brothers and systems of a file element
132 *
133 * @param element
134 * the child leaf
135 * @return the siblings of an element, including itself. Should never be
136 * null
137 */
138 public synchronized FileAndName[] getSiblings(FileAndName element) {
139 String key = (String) getParent(element);
140 return (FileAndName[]) getChildren(key);
141
142 }
143
144 @Override
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)) {
150 return key;
151 }
152 }
153 }
154 return null;
155 }
156
157 @Override
158 public synchronized boolean hasChildren(Object element) {
159 if (element instanceof String) {
160 String key = (String) element;
161 return fTraceFiles.containsKey(fTraceTypes.get(key));
162 }
163 return false;
164 }
165
166 /**
167 * Gets the number of traces to import
ba486486 168 *
d04ec5a7
MK
169 * @return the number of traces to import
170 */
171 public synchronized int getSize() {
ba486486
MK
172 int tot = 0;
173 for (String s : fTraceFiles.keySet()) {
d04ec5a7
MK
174 tot += fTraceFiles.get(s).size();
175 }
176 return tot;
177 }
178}
This page took 0.046768 seconds and 5 git commands to generate.