tmf: add title to trace type selection dialog
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceType.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 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 * Patrick Tasse - Initial API and implementation
11 * Matthew Khouzam - Added import functionalities
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.project.model;
15
16 import java.io.File;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.LinkedHashMap;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.resources.ResourcesPlugin;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IConfigurationElement;
31 import org.eclipse.core.runtime.IPath;
32 import org.eclipse.core.runtime.IStatus;
33 import org.eclipse.core.runtime.Platform;
34 import org.eclipse.core.runtime.Status;
35 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
36 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
37 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
38 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
39 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
40 import org.eclipse.linuxtools.internal.tmf.ui.project.model.TmfTraceImportException;
41 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
42 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.events.SelectionEvent;
45 import org.eclipse.swt.events.SelectionListener;
46 import org.eclipse.swt.layout.RowLayout;
47 import org.eclipse.swt.widgets.Button;
48 import org.eclipse.swt.widgets.Display;
49 import org.eclipse.swt.widgets.Shell;
50 import org.eclipse.ui.dialogs.FileSystemElement;
51
52 /**
53 * Utility class for accessing TMF trace type extensions from the platform's
54 * extensions registry.
55 *
56 * @version 1.0
57 * @author Patrick Tasse
58 * @author Matthew Khouzam
59 */
60 public final class TmfTraceType {
61
62 private static final String DEFAULT_TRACE_ICON_PATH = "icons" + File.separator + "elcl16" + File.separator + "trace.gif"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
63
64 private static final char SEPARATOR = ':';
65
66 /**
67 * Extension point ID
68 */
69 public static final String TMF_TRACE_TYPE_ID = "org.eclipse.linuxtools.tmf.ui.tracetype"; //$NON-NLS-1$
70
71 /**
72 * Extension point element 'Category'
73 */
74 public static final String CATEGORY_ELEM = "category"; //$NON-NLS-1$
75 /**
76 * Extension point element 'Type'
77 */
78 public static final String TYPE_ELEM = "type"; //$NON-NLS-1$
79 /**
80 * Extension point element 'Default editor'
81 */
82 public static final String DEFAULT_EDITOR_ELEM = "defaultEditor"; //$NON-NLS-1$
83 /**
84 * Extension point element 'Events table type'
85 */
86 public static final String EVENTS_TABLE_TYPE_ELEM = "eventsTableType"; //$NON-NLS-1$
87 /**
88 * Extension point element 'Statistics viewer type'
89 *
90 * @since 2.0
91 */
92 public static final String STATISTICS_VIEWER_ELEM = "statisticsViewerType"; //$NON-NLS-1$
93
94 /**
95 * Extension point attribute 'ID'
96 */
97 public static final String ID_ATTR = "id"; //$NON-NLS-1$
98 /**
99 * Extension point attribute 'name'
100 */
101 public static final String NAME_ATTR = "name"; //$NON-NLS-1$
102 /**
103 * Extension point attribute 'category'
104 */
105 public static final String CATEGORY_ATTR = "category"; //$NON-NLS-1$
106 /**
107 * Extension point attribute 'trace_type'
108 */
109 public static final String TRACE_TYPE_ATTR = "trace_type"; //$NON-NLS-1$
110 /**
111 * Extension point attribute 'event_type'
112 */
113 public static final String EVENT_TYPE_ATTR = "event_type"; //$NON-NLS-1$
114 /**
115 * Extension point attribute 'icon'
116 */
117 public static final String ICON_ATTR = "icon"; //$NON-NLS-1$
118 /**
119 * Extension point attribute 'class'
120 */
121 public static final String CLASS_ATTR = "class"; //$NON-NLS-1$
122
123 /**
124 * Custom text label used internally and therefore should not be
125 * externalized
126 *
127 * @since 2.0
128 */
129 public static final String CUSTOM_TXT_CATEGORY = "Custom Text"; //$NON-NLS-1$
130 /**
131 * Custom XML label used internally and therefore should not be externalized
132 *
133 * @since 2.0
134 */
135 public static final String CUSTOM_XML_CATEGORY = "Custom XML"; //$NON-NLS-1$
136
137 // The mapping of available trace type IDs to their corresponding
138 // configuration element
139 private final Map<String, IConfigurationElement> fTraceTypeAttributes = new HashMap<String, IConfigurationElement>();
140 private final Map<String, IConfigurationElement> fTraceCategories = new HashMap<String, IConfigurationElement>();
141 private final Map<String, TraceTypeHelper> fTraceTypes = new LinkedHashMap<String, TraceTypeHelper>();
142
143 private static TmfTraceType fInstance = null;
144
145 /**
146 * Retrieves the category name from the platform extension registry based on
147 * the category ID
148 *
149 * @param categoryId
150 * The category ID
151 * @return the category name or empty string if not found
152 */
153 public static String getCategoryName(String categoryId) {
154 IConfigurationElement[] elements = Platform.getExtensionRegistry()
155 .getConfigurationElementsFor(TMF_TRACE_TYPE_ID);
156 for (IConfigurationElement element : elements) {
157 if (element.getName().equals(CATEGORY_ELEM) && categoryId.equals(element.getAttribute(ID_ATTR))) {
158 return element.getAttribute(NAME_ATTR);
159 }
160 }
161 return ""; //$NON-NLS-1$
162 }
163
164 /**
165 * Retrieves and instantiates an element's object based on his plug-in
166 * definition for a specific trace type.
167 *
168 * The element's object is instantiated using its 0-argument constructor.
169 *
170 * @param resource
171 * The resource where to find the information about the trace
172 * properties
173 * @param element
174 * The name of the element to find under the trace type
175 * definition
176 * @return a new Object based on his definition in plugin.xml, or null if no
177 * definition was found
178 * @since 2.0
179 */
180 public static Object getTraceTypeElement(IResource resource, String element) {
181 try {
182 if (resource != null) {
183 String traceType = resource.getPersistentProperty(TmfCommonConstants.TRACETYPE);
184 /*
185 * Search in the configuration if there is any viewer specified
186 * for this kind of trace type.
187 */
188 for (IConfigurationElement ce : TmfTraceType.getTypeElements()) {
189 if (ce.getAttribute(TmfTraceType.ID_ATTR).equals(traceType)) {
190 IConfigurationElement[] viewerCE = ce.getChildren(element);
191 if (viewerCE.length != 1) {
192 break;
193 }
194 return viewerCE[0].createExecutableExtension(TmfTraceType.CLASS_ATTR);
195 }
196 }
197 }
198 } catch (CoreException e) {
199 Activator.getDefault().logError("Error creating the element from the resource", e); //$NON-NLS-1$
200 }
201 return null;
202 }
203
204 /**
205 * Retrieves all configuration elements from the platform extension registry
206 * for the trace type extension.
207 *
208 * @return an array of trace type configuration elements
209 */
210 public static IConfigurationElement[] getTypeElements() {
211 IConfigurationElement[] elements = Platform.getExtensionRegistry()
212 .getConfigurationElementsFor(TMF_TRACE_TYPE_ID);
213 List<IConfigurationElement> typeElements = new LinkedList<IConfigurationElement>();
214 for (IConfigurationElement element : elements) {
215 if (element.getName().equals(TYPE_ELEM)) {
216 typeElements.add(element);
217 }
218 }
219 return typeElements.toArray(new IConfigurationElement[typeElements.size()]);
220 }
221
222 private TmfTraceType() {
223 init();
224 }
225
226 /**
227 * The import utils instance
228 *
229 * @return the import utils instance
230 * @since 2.0
231 */
232 public static TmfTraceType getInstance() {
233 if (fInstance == null) {
234 fInstance = new TmfTraceType();
235 }
236 return fInstance;
237 }
238
239 // ------------------------------------------------------------------
240 // Get trace types
241 // ------------------------------------------------------------------
242
243 /**
244 * Returns a list of "category:tracetype , ..."
245 *
246 * @return returns a list of "category:tracetype , ..."
247 * @since 2.0
248 */
249 public String[] getAvailableTraceTypes() {
250
251 // Generate the list of Category:TraceType to populate the ComboBox
252 List<String> traceTypes = new ArrayList<String>();
253
254 // re-populate custom trace types
255 getCustomTraceTypes();
256 for (String key : this.fTraceTypes.keySet()) {
257 TraceTypeHelper tt = this.fTraceTypes.get(key);
258 traceTypes.add(tt.getCategoryName() + SEPARATOR + tt.getName());
259 }
260
261 // Format result
262 return traceTypes.toArray(new String[traceTypes.size()]);
263 }
264
265 /**
266 * Gets the custom trace types (custom text and friends)
267 *
268 * @param type
269 * the type to get (Text, xml or other...)
270 * @return the list of custom trace types
271 * @since 2.0
272 */
273 public static List<String> getCustomTraceTypes(String type) {
274 List<String> traceTypes = new ArrayList<String>();
275 if (type.equals(CUSTOM_TXT_CATEGORY)) {
276 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
277 String traceTypeName = def.definitionName;
278 traceTypes.add(traceTypeName);
279 }
280 }
281 if (type.equals(CUSTOM_XML_CATEGORY)) {
282 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
283 String traceTypeName = def.definitionName;
284 traceTypes.add(traceTypeName);
285 }
286 }
287 return traceTypes;
288 }
289
290 /**
291 * Gets all the custom trace types
292 *
293 * @return the list of custom trace types
294 * @since 2.0
295 */
296 public List<String> getCustomTraceTypes() {
297 List<String> traceTypes = new ArrayList<String>();
298 // remove the customTraceTypes
299 final String[] keySet = fTraceTypes.keySet().toArray(new String[0]);
300 for (String key : keySet) {
301 TraceTypeHelper helper = fTraceTypes.get(key);
302 if (helper.getCategoryName().equals(CUSTOM_TXT_CATEGORY) || helper.getCategoryName().equals(CUSTOM_XML_CATEGORY)) {
303 helper.getTrace().dispose();
304 fTraceTypes.remove(key);
305 }
306 }
307
308 // add the custom trace types
309 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
310 String traceTypeId = CustomTxtTrace.class.getCanonicalName() + SEPARATOR + def.definitionName;
311 TraceTypeHelper tt = new TraceTypeHelper(traceTypeId, CUSTOM_TXT_CATEGORY, def.definitionName, new CustomTxtTrace(def));
312 fTraceTypes.put(traceTypeId, tt);
313 traceTypes.add(traceTypeId);
314 }
315 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
316 String traceTypeId = CustomXmlTrace.class.getCanonicalName() + SEPARATOR + def.definitionName;
317 TraceTypeHelper tt = new TraceTypeHelper(traceTypeId, CUSTOM_XML_CATEGORY, def.definitionName, new CustomXmlTrace(def));
318 fTraceTypes.put(traceTypeId, tt);
319 traceTypes.add(traceTypeId);
320 }
321 return traceTypes;
322 }
323
324 /**
325 * Gets a trace type for a given canonical id
326 *
327 * @param id
328 * the ID of the trace
329 * @return the return type
330 * @since 2.0
331 */
332 public TraceTypeHelper getTraceType(String id) {
333 init();
334 return fTraceTypes.get(id);
335 }
336
337 private void populateCategoriesAndTraceTypes() {
338 if (fTraceTypes.isEmpty()) {
339 // Populate the Categories and Trace Types
340 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
341 for (IConfigurationElement ce : config) {
342 String elementName = ce.getName();
343 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
344 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
345 fTraceTypeAttributes.put(traceTypeId, ce);
346 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
347 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
348 fTraceCategories.put(categoryId, ce);
349 }
350 }
351 // create the trace types
352 for (String typeId : fTraceTypeAttributes.keySet()) {
353 IConfigurationElement ce = fTraceTypeAttributes.get(typeId);
354 final String category = getCategory(ce);
355 final String attribute = ce.getAttribute(TmfTraceType.NAME_ATTR);
356 ITmfTrace trace = null;
357 try {
358 trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
359 } catch (CoreException e) {
360 }
361 TraceTypeHelper tt = new TraceTypeHelper(typeId, category, attribute, trace);
362 fTraceTypes.put(typeId, tt);
363 }
364 }
365 }
366
367 private String getCategory(IConfigurationElement ce) {
368 final String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
369 if (categoryId != null) {
370 IConfigurationElement category = fTraceCategories.get(categoryId);
371 if (category != null && !category.getName().equals("")) { //$NON-NLS-1$
372 return category.getAttribute(TmfTraceType.NAME_ATTR);
373 }
374 }
375 return "[no category]"; //$NON-NLS-1$
376 }
377
378 /**
379 * Returns the list of trace categories
380 *
381 * @return the list of trace categories
382 * @since 2.0
383 */
384 public List<String> getTraceCategories() {
385 List<String> categoryNames = new ArrayList<String>();
386 for (String key : fTraceTypes.keySet()) {
387 final String categoryName = fTraceTypes.get(key).getCategoryName();
388 if (!categoryNames.contains(categoryName)) {
389 categoryNames.add(categoryName);
390 }
391 }
392 return categoryNames;
393 }
394
395 /**
396 * Get the trace type helper classes from category name
397 *
398 * @param categoryName
399 * the categoryName to lookup
400 * @return a list of trace type helper classes {@link TraceTypeHelper}
401 * @since 2.0
402 */
403
404 public List<TraceTypeHelper> getTraceTypes(String categoryName) {
405 init();
406 List<TraceTypeHelper> traceNames = new ArrayList<TraceTypeHelper>();
407 for (String key : fTraceTypes.keySet()) {
408 final String storedCategoryName = fTraceTypes.get(key).getCategoryName();
409 if (storedCategoryName.equals(categoryName)) {
410 traceNames.add(fTraceTypes.get(key));
411 }
412 }
413 return traceNames;
414 }
415
416 private void init() {
417 populateCategoriesAndTraceTypes();
418 getCustomTraceTypes();
419
420 }
421
422 private static List<File> isolateTraces(List<FileSystemElement> selectedResources) {
423
424 List<File> traces = new ArrayList<File>();
425
426 // Get the selection
427 Iterator<FileSystemElement> resources = selectedResources.iterator();
428
429 // Get the sorted list of unique entries
430 Map<String, File> fileSystemObjects = new HashMap<String, File>();
431 while (resources.hasNext()) {
432 File resource = (File) resources.next().getFileSystemObject();
433 String key = resource.getAbsolutePath();
434 fileSystemObjects.put(key, resource);
435 }
436 List<String> files = new ArrayList<String>(fileSystemObjects.keySet());
437 Collections.sort(files);
438
439 // After sorting, traces correspond to the unique prefixes
440 String prefix = null;
441 for (int i = 0; i < files.size(); i++) {
442 File file = fileSystemObjects.get(files.get(i));
443 String name = file.getAbsolutePath();
444 if (prefix == null || !name.startsWith(prefix)) {
445 prefix = name; // new prefix
446 traces.add(file);
447 }
448 }
449
450 return traces;
451 }
452
453 /**
454 * Validate a trace type
455 *
456 * @param traceTypeName
457 * the trace category (canonical name)
458 * @param fileName
459 * the file name (and path)
460 * @return true if the trace is of a valid type
461 * @since 2.0
462 */
463 public boolean validate(String traceTypeName, String fileName) {
464 if (traceTypeName != null && !traceTypeName.isEmpty()) {
465 if (!fTraceTypes.get(traceTypeName).validate(fileName)) {
466 return false;
467 }
468 }
469 return true;
470 }
471
472 /**
473 * Validate a trace
474 *
475 * @param traceToValidate
476 * the trace category (canonical name)
477 * @return true if the trace is of a valid type
478 * @since 2.0
479 */
480 public boolean validate(TraceValidationHelper traceToValidate) {
481 return validate(traceToValidate.getTraceType(), traceToValidate.getTraceToScan());
482 }
483
484 /**
485 * validate list of traces with a tracetype
486 *
487 * @param traceTypeName
488 * the trace category (canonical name)
489 * @param selectedResources
490 * List of traces to validate
491 * @return true if all the traces are valid
492 * @since 2.0
493 */
494 public boolean validateTrace(String traceTypeName, List<FileSystemElement> selectedResources) {
495 List<File> traces = isolateTraces(selectedResources);
496 return validateTraceFiles(traceTypeName, traces);
497 }
498
499 /**
500 * Validate a list of files with a tracetype
501 *
502 * @param traceTypeName
503 * the trace category (canonical name)
504 * @param traces
505 * the list of files to check if they are trace
506 * @return true if all the traces are valid
507 * @since 2.0
508 */
509 public boolean validateTraceFiles(String traceTypeName, List<File> traces) {
510 if (traceTypeName != null && !"".equals(traceTypeName) && //$NON-NLS-1$
511 !traceTypeName.startsWith(TmfTraceType.CUSTOM_TXT_CATEGORY) && !traceTypeName.startsWith(TmfTraceType.CUSTOM_XML_CATEGORY)) {
512 for (File trace : traces) {
513 if (!validate(traceTypeName, trace.getAbsolutePath())) {
514 return false;
515 }
516 }
517 }
518 return true;
519 }
520
521 /**
522 * Get a configuration element for a given name
523 *
524 * @param traceType
525 * the name canonical
526 * @return the configuration element, can be null
527 * @since 2.0
528 */
529 public IConfigurationElement getTraceAttributes(String traceType) {
530 return fTraceTypeAttributes.get(traceType);
531 }
532
533 /**
534 * Find the id of a trace type by its parameters
535 *
536 * @param category
537 * like "ctf" or "custom text"
538 * @param traceType
539 * like "kernel"
540 * @return an id like "org.eclipse.linuxtools.blabla...
541 * @since 2.0
542 */
543 public String getTraceTypeId(String category, String traceType) {
544 for (String key : fTraceTypes.keySet()) {
545 if (fTraceTypes.get(key).getCategoryName().equals(category.trim()) && fTraceTypes.get(key).getName().equals(traceType.trim())) {
546 return key;
547 }
548 }
549 return null;
550 }
551
552 /**
553 * Is the trace a custom (user-defined) trace type. These are the traces
554 * like : text and xml defined by the custom trace wizard.
555 *
556 * @param traceType
557 * the trace type in human form (category:name)
558 * @return true if the trace is a custom type
559 * @since 2.1
560 */
561 public static boolean isCustomTrace(String traceType) {
562 final boolean startsWithTxt = traceType.startsWith(TmfTraceType.CUSTOM_TXT_CATEGORY);
563 final boolean startsWithXML = traceType.startsWith(TmfTraceType.CUSTOM_XML_CATEGORY);
564 return (startsWithTxt || startsWithXML);
565 }
566
567 /**
568 * Is the trace type id a custom (user-defined) trace type. These are the
569 * traces like : text and xml defined by the custom trace wizard.
570 *
571 * @param traceTypeId
572 * the trace type id
573 * @return true if the trace is a custom type
574 */
575 private static boolean isCustomTraceId(String traceTypeId) {
576 TraceTypeHelper traceType = getInstance().getTraceType(traceTypeId);
577 if (traceType != null) {
578 return isCustomTrace(traceType.getCategoryName() + SEPARATOR + traceType.getName());
579 }
580
581 return false;
582 }
583
584 /**
585 * Gets the custom trace type ID from the custom trace name
586 *
587 * @param traceType
588 * The trace type in human form (category:name)
589 * @return the trace type ID or null if the trace is not a custom one
590 * @since 2.1
591 */
592 public static String getCustomTraceTypeId(String traceType) {
593 String traceTypeId = null;
594
595 // do custom trace stuff here
596 String traceTypeToken[] = traceType.split(":", 2); //$NON-NLS-1$
597 if (traceTypeToken.length == 2) {
598 final boolean startsWithTxt = traceType.startsWith(TmfTraceType.CUSTOM_TXT_CATEGORY);
599 final boolean startsWithXML = traceType.startsWith(TmfTraceType.CUSTOM_XML_CATEGORY);
600 if (startsWithTxt) {
601 traceTypeId = CustomTxtTrace.class.getCanonicalName() + SEPARATOR + traceTypeToken[1];
602 } else if (startsWithXML) {
603 traceTypeId = CustomXmlTrace.class.getCanonicalName() + SEPARATOR + traceTypeToken[1];
604 }
605 }
606 return traceTypeId;
607 }
608
609 TraceTypeHelper selectTraceType(String path, Shell shell) throws TmfTraceImportException {
610 return selectTraceType(path, shell, null);
611 }
612
613 /**
614 * This member figures out the trace type of a given file. It will prompt
615 * the user if it needs more information to properly pick the trace type.
616 *
617 * @param path
618 * The path of file to import
619 * @param shell
620 * a shell to display the message to. If it is null, it is
621 * assumed to be cancelled.
622 * @param traceTypeHint the ID of a trace (like "o.e.l.specifictrace" )
623 * @return null if the request is cancelled or a TraceTypeHelper if it passes.
624 * @throws TmfTraceImportException
625 * if the traces don't match or there are errors in the trace
626 * file
627 */
628 TraceTypeHelper selectTraceType(String path, Shell shell, String traceTypeHint) throws TmfTraceImportException {
629 List<TraceTypeHelper> validCandidates = new ArrayList<TraceTypeHelper>();
630 getCustomTraceTypes();
631 final Set<String> traceTypes = fTraceTypes.keySet();
632 for (String traceType : traceTypes) {
633 if (validate(traceType, path)) {
634 validCandidates.add(fTraceTypes.get(traceType));
635 }
636 }
637
638 TraceTypeHelper traceTypeToSet = null;
639 if (validCandidates.isEmpty()) {
640 final String errorMsg = Messages.TmfOpenTraceHelper_NoTraceTypeMatch + path;
641 throw new TmfTraceImportException(errorMsg);
642 } else if (validCandidates.size() != 1) {
643 List<TraceTypeHelper> reducedCandidates = reduce(validCandidates);
644 for (TraceTypeHelper tth : reducedCandidates) {
645 if (tth.getCanonicalName().equals(traceTypeHint)) {
646 traceTypeToSet = tth;
647 }
648 }
649 if (traceTypeToSet == null) {
650 if (reducedCandidates.size() == 0) {
651 throw new TmfTraceImportException(Messages.TmfOpenTraceHelper_ReduceError);
652 } else if (reducedCandidates.size() == 1) {
653 traceTypeToSet = reducedCandidates.get(0);
654 } else {
655 if (shell == null) {
656 return null;
657 }
658 traceTypeToSet = getTraceTypeToSet(reducedCandidates, shell);
659 }
660 }
661 } else {
662 traceTypeToSet = validCandidates.get(0);
663 }
664 return traceTypeToSet;
665 }
666
667 private static List<TraceTypeHelper> reduce(List<TraceTypeHelper> candidates) {
668 List<TraceTypeHelper> retVal = new ArrayList<TraceTypeHelper>();
669
670 // get all the tracetypes that are unique in that stage
671 for (TraceTypeHelper trace : candidates) {
672 if (isUnique(trace, candidates)) {
673 retVal.add(trace);
674 }
675 }
676 return retVal;
677 }
678
679 /*
680 * Only return the leaves of the trace types. Ignore custom trace types.
681 */
682 private static boolean isUnique(TraceTypeHelper trace, List<TraceTypeHelper> set) {
683 if (TmfTraceType.isCustomTraceId(trace.getCanonicalName())) {
684 return true;
685 }
686 // check if the trace type is the leaf. we make an instance of the trace
687 // type and if it is only an instance of itself, it is a leaf
688 final ITmfTrace tmfTrace = trace.getTrace();
689 int count = -1;
690 for (TraceTypeHelper child : set) {
691 final ITmfTrace traceCandidate = child.getTrace();
692 if (tmfTrace.getClass().isInstance(traceCandidate)) {
693 count++;
694 }
695 }
696 return count == 0;
697 }
698
699 private TraceTypeHelper getTraceTypeToSet(List<TraceTypeHelper> candidates, Shell shell) {
700 final Map<String, String> names = new HashMap<String, String>();
701 Shell shellToShow = new Shell(shell);
702 shellToShow.setText(Messages.TmfTraceType_SelectTraceType);
703 final String candidatesToSet[] = new String[1];
704 for (TraceTypeHelper candidate : candidates) {
705 Button b = new Button(shellToShow, SWT.RADIO);
706 final String displayName = candidate.getCategoryName() + ':' + candidate.getName();
707 b.setText(displayName);
708 names.put(displayName, candidate.getCanonicalName());
709
710 b.addSelectionListener(new SelectionListener() {
711
712 @Override
713 public void widgetSelected(SelectionEvent e) {
714 final Button source = (Button) e.getSource();
715 candidatesToSet[0] = (names.get(source.getText()));
716 source.getParent().dispose();
717 }
718
719 @Override
720 public void widgetDefaultSelected(SelectionEvent e) {
721
722 }
723 });
724 }
725 shellToShow.setLayout(new RowLayout(SWT.VERTICAL));
726 shellToShow.pack();
727 shellToShow.open();
728
729 Display display = shellToShow.getDisplay();
730 while (!shellToShow.isDisposed()) {
731 if (!display.readAndDispatch()) {
732 display.sleep();
733 }
734 }
735 return fTraceTypes.get(candidatesToSet[0]);
736 }
737
738 /**
739 * Set the trace type of a {@Link TraceTypeHelper}. Should only be
740 * used internally by this project.
741 *
742 * @param path
743 * the {@link IPath} path of the resource to set
744 * @param traceType
745 * the {@link TraceTypeHelper} to set the trace type to.
746 * @return Status.OK_Status if successful, error is otherwise.
747 * @throws CoreException
748 * An exception caused by accessing eclipse project items.
749 * @since 2.1
750 */
751 public static IStatus setTraceType(IPath path, TraceTypeHelper traceType) throws CoreException {
752 IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
753 String TRACE_NAME = path.lastSegment();
754 String traceBundle = null, traceTypeId = traceType.getCanonicalName(), traceIcon = null;
755 if (TmfTraceType.isCustomTraceId(traceTypeId)) {
756 traceBundle = Activator.getDefault().getBundle().getSymbolicName();
757 traceIcon = DEFAULT_TRACE_ICON_PATH;
758 } else {
759 IConfigurationElement ce = TmfTraceType.getInstance().getTraceAttributes(traceTypeId);
760 traceBundle = ce.getContributor().getName();
761 traceIcon = ce.getAttribute(TmfTraceType.ICON_ATTR);
762 }
763
764 resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle);
765 resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId);
766 resource.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon);
767
768 TmfProjectElement tmfProject = TmfProjectRegistry.getProject(resource.getProject());
769 if (tmfProject != null) {
770 final TmfTraceFolder tracesFolder = tmfProject.getTracesFolder();
771 tracesFolder.refresh();
772
773 List<TmfTraceElement> traces = tracesFolder.getTraces();
774 boolean found = false;
775 for (TmfTraceElement traceElement : traces) {
776 if (traceElement.getName().equals(resource.getName())) {
777 traceElement.refreshTraceType();
778 found = true;
779 break;
780 }
781 }
782 if (!found) {
783 TmfTraceElement te = new TmfTraceElement(TRACE_NAME, resource, tracesFolder);
784 te.refreshTraceType();
785 traces = tracesFolder.getTraces();
786 for (TmfTraceElement traceElement : traces) {
787 traceElement.refreshTraceType();
788 }
789 }
790 }
791 return Status.OK_STATUS;
792 }
793
794 }
This page took 0.0482 seconds and 5 git commands to generate.