1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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 * Patrick Tasse - Initial API and implementation
11 * Matthew Khouzam - Added import functionalities
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
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
;
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
.signal
.TmfSignalManager
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
44 import org
.eclipse
.swt
.SWT
;
45 import org
.eclipse
.swt
.events
.SelectionEvent
;
46 import org
.eclipse
.swt
.events
.SelectionListener
;
47 import org
.eclipse
.swt
.layout
.RowLayout
;
48 import org
.eclipse
.swt
.widgets
.Button
;
49 import org
.eclipse
.swt
.widgets
.Display
;
50 import org
.eclipse
.swt
.widgets
.Shell
;
51 import org
.eclipse
.ui
.dialogs
.FileSystemElement
;
54 * Utility class for accessing TMF trace type extensions from the platform's
55 * extensions registry.
58 * @author Patrick Tasse
59 * @author Matthew Khouzam
61 public final class TmfTraceType
{
63 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$
65 private static final char SEPARATOR
= ':';
70 public static final String TMF_TRACE_TYPE_ID
= "org.eclipse.linuxtools.tmf.ui.tracetype"; //$NON-NLS-1$
73 * Extension point element 'Category'
75 public static final String CATEGORY_ELEM
= "category"; //$NON-NLS-1$
77 * Extension point element 'Type'
79 public static final String TYPE_ELEM
= "type"; //$NON-NLS-1$
81 * Extension point element 'Default editor'
83 public static final String DEFAULT_EDITOR_ELEM
= "defaultEditor"; //$NON-NLS-1$
85 * Extension point element 'Events table type'
87 public static final String EVENTS_TABLE_TYPE_ELEM
= "eventsTableType"; //$NON-NLS-1$
89 * Extension point element 'Statistics viewer type'
93 public static final String STATISTICS_VIEWER_ELEM
= "statisticsViewerType"; //$NON-NLS-1$
96 * Extension point attribute 'ID'
98 public static final String ID_ATTR
= "id"; //$NON-NLS-1$
100 * Extension point attribute 'name'
102 public static final String NAME_ATTR
= "name"; //$NON-NLS-1$
104 * Extension point attribute 'category'
106 public static final String CATEGORY_ATTR
= "category"; //$NON-NLS-1$
108 * Extension point attribute 'trace_type'
110 public static final String TRACE_TYPE_ATTR
= "trace_type"; //$NON-NLS-1$
112 * Extension point attribute 'event_type'
114 public static final String EVENT_TYPE_ATTR
= "event_type"; //$NON-NLS-1$
116 * Extension point attribute 'icon'
118 public static final String ICON_ATTR
= "icon"; //$NON-NLS-1$
120 * Extension point attribute 'class'
122 public static final String CLASS_ATTR
= "class"; //$NON-NLS-1$
125 * Custom text label used internally and therefore should not be
130 public static final String CUSTOM_TXT_CATEGORY
= "Custom Text"; //$NON-NLS-1$
132 * Custom XML label used internally and therefore should not be externalized
136 public static final String CUSTOM_XML_CATEGORY
= "Custom XML"; //$NON-NLS-1$
138 // The mapping of available trace type IDs to their corresponding
139 // configuration element
140 private final Map
<String
, IConfigurationElement
> fTraceTypeAttributes
= new HashMap
<>();
141 private final Map
<String
, IConfigurationElement
> fTraceCategories
= new HashMap
<>();
142 private final Map
<String
, TraceTypeHelper
> fTraceTypes
= new LinkedHashMap
<>();
144 private static TmfTraceType fInstance
= null;
147 * Retrieves the category name from the platform extension registry based on
152 * @return the category name or empty string if not found
154 public static String
getCategoryName(String categoryId
) {
155 IConfigurationElement
[] elements
= Platform
.getExtensionRegistry()
156 .getConfigurationElementsFor(TMF_TRACE_TYPE_ID
);
157 for (IConfigurationElement element
: elements
) {
158 if (element
.getName().equals(CATEGORY_ELEM
) && categoryId
.equals(element
.getAttribute(ID_ATTR
))) {
159 return element
.getAttribute(NAME_ATTR
);
162 return ""; //$NON-NLS-1$
166 * Retrieves and instantiates an element's object based on his plug-in
167 * definition for a specific trace type.
169 * The element's object is instantiated using its 0-argument constructor.
172 * The resource where to find the information about the trace
175 * The name of the element to find under the trace type
177 * @return a new Object based on his definition in plugin.xml, or null if no
178 * definition was found
181 public static Object
getTraceTypeElement(IResource resource
, String element
) {
183 if (resource
!= null) {
184 String traceType
= resource
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
186 * Search in the configuration if there is any viewer specified
187 * for this kind of trace type.
189 for (IConfigurationElement ce
: TmfTraceType
.getTypeElements()) {
190 if (ce
.getAttribute(TmfTraceType
.ID_ATTR
).equals(traceType
)) {
191 IConfigurationElement
[] viewerCE
= ce
.getChildren(element
);
192 if (viewerCE
.length
!= 1) {
195 return viewerCE
[0].createExecutableExtension(TmfTraceType
.CLASS_ATTR
);
199 } catch (CoreException e
) {
200 Activator
.getDefault().logError("Error creating the element from the resource", e
); //$NON-NLS-1$
206 * Retrieves all configuration elements from the platform extension registry
207 * for the trace type extension.
209 * @return an array of trace type configuration elements
211 public static IConfigurationElement
[] getTypeElements() {
212 IConfigurationElement
[] elements
= Platform
.getExtensionRegistry()
213 .getConfigurationElementsFor(TMF_TRACE_TYPE_ID
);
214 List
<IConfigurationElement
> typeElements
= new LinkedList
<>();
215 for (IConfigurationElement element
: elements
) {
216 if (element
.getName().equals(TYPE_ELEM
)) {
217 typeElements
.add(element
);
220 return typeElements
.toArray(new IConfigurationElement
[typeElements
.size()]);
223 private TmfTraceType() {
228 * The import utils instance
230 * @return the import utils instance
233 public static TmfTraceType
getInstance() {
234 if (fInstance
== null) {
235 fInstance
= new TmfTraceType();
240 // ------------------------------------------------------------------
242 // ------------------------------------------------------------------
245 * Returns a list of "category:tracetype , ..."
247 * @return returns a list of "category:tracetype , ..."
250 public String
[] getAvailableTraceTypes() {
252 // Generate the list of Category:TraceType to populate the ComboBox
253 List
<String
> traceTypes
= new ArrayList
<>();
255 for (String key
: this.fTraceTypes
.keySet()) {
256 TraceTypeHelper tt
= this.fTraceTypes
.get(key
);
257 traceTypes
.add(tt
.getCategoryName() + SEPARATOR
+ tt
.getName());
261 return traceTypes
.toArray(new String
[traceTypes
.size()]);
265 * Gets the custom trace types (custom text and friends)
268 * the type to get (Text, xml or other...)
269 * @return the list of custom trace types
272 public static List
<String
> getCustomTraceTypes(String type
) {
273 List
<String
> traceTypes
= new ArrayList
<>();
274 if (type
.equals(CUSTOM_TXT_CATEGORY
)) {
275 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
276 String traceTypeName
= def
.definitionName
;
277 traceTypes
.add(traceTypeName
);
280 if (type
.equals(CUSTOM_XML_CATEGORY
)) {
281 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
282 String traceTypeName
= def
.definitionName
;
283 traceTypes
.add(traceTypeName
);
290 * Gets all the custom trace types
292 * @return the list of custom trace types
295 public static List
<String
> getCustomTraceTypes() {
297 List
<String
> traceTypes
= new ArrayList
<>();
298 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
299 String traceTypeName
= def
.definitionName
;
300 traceTypes
.add(traceTypeName
);
302 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
303 String traceTypeName
= def
.definitionName
;
304 traceTypes
.add(traceTypeName
);
309 private void populateCustomTraceTypes() {
310 // add the custom trace types
311 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
312 String traceTypeId
= CustomTxtTrace
.class.getCanonicalName() + SEPARATOR
+ def
.definitionName
;
313 ITmfTrace trace
= new CustomTxtTrace(def
);
314 TraceTypeHelper tt
= new TraceTypeHelper(traceTypeId
, CUSTOM_TXT_CATEGORY
, def
.definitionName
, trace
);
315 fTraceTypes
.put(traceTypeId
, tt
);
316 // Deregister trace as signal handler because it is only used for validation
317 TmfSignalManager
.deregister(trace
);
319 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
320 String traceTypeId
= CustomXmlTrace
.class.getCanonicalName() + SEPARATOR
+ def
.definitionName
;
321 ITmfTrace trace
= new CustomXmlTrace(def
);
322 TraceTypeHelper tt
= new TraceTypeHelper(traceTypeId
, CUSTOM_XML_CATEGORY
, def
.definitionName
, trace
);
323 fTraceTypes
.put(traceTypeId
, tt
);
324 // Deregister trace as signal handler because it is only used for validation
325 TmfSignalManager
.deregister(trace
);
330 * Add or replace a custom trace type
333 * The custom parser category
334 * @param definitionName
335 * The custom parser definition name to add or replace
338 public void addCustomTraceType(String category
, String definitionName
) {
339 String traceTypeId
= null;
340 ITmfTrace trace
= null;
342 if (category
.equals(CUSTOM_TXT_CATEGORY
)) {
343 traceTypeId
= CustomTxtTrace
.class.getCanonicalName() + SEPARATOR
+ definitionName
;
344 CustomTxtTraceDefinition def
= CustomTxtTraceDefinition
.load(definitionName
);
346 trace
= new CustomTxtTrace(def
);
348 } else if (category
.equals(CUSTOM_XML_CATEGORY
)) {
349 traceTypeId
= CustomXmlTrace
.class.getCanonicalName() + SEPARATOR
+ definitionName
;
350 CustomXmlTraceDefinition def
= CustomXmlTraceDefinition
.load(definitionName
);
352 trace
= new CustomXmlTrace(def
);
356 if (traceTypeId
!= null && trace
!= null) {
357 TraceTypeHelper helper
= fTraceTypes
.get(traceTypeId
);
358 if (helper
!= null) {
359 helper
.getTrace().dispose();
361 TraceTypeHelper tt
= new TraceTypeHelper(traceTypeId
, category
, definitionName
, trace
);
362 fTraceTypes
.put(traceTypeId
, tt
);
363 // Deregister trace as signal handler because it is only used for validation
364 TmfSignalManager
.deregister(trace
);
369 * Remove a custom trace type
372 * The custom parser category
373 * @param definitionName
374 * The custom parser definition name to add or replace
377 public void removeCustomTraceType(String category
, String definitionName
) {
378 if (category
.equals(CUSTOM_TXT_CATEGORY
)) {
379 String traceTypeId
= CustomTxtTrace
.class.getCanonicalName() + SEPARATOR
+ definitionName
;
380 TraceTypeHelper helper
= fTraceTypes
.remove(traceTypeId
);
381 if (helper
!= null) {
382 helper
.getTrace().dispose();
384 } else if (category
.equals(CUSTOM_XML_CATEGORY
)) {
385 String traceTypeId
= CustomXmlTrace
.class.getCanonicalName() + SEPARATOR
+ definitionName
;
386 TraceTypeHelper helper
= fTraceTypes
.remove(traceTypeId
);
387 if (helper
!= null) {
388 helper
.getTrace().dispose();
394 * Gets a trace type for a given canonical id
397 * the ID of the trace
398 * @return the return type
401 public TraceTypeHelper
getTraceType(String id
) {
402 return fTraceTypes
.get(id
);
405 private void populateCategoriesAndTraceTypes() {
406 if (fTraceTypes
.isEmpty()) {
407 // Populate the Categories and Trace Types
408 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType
.TMF_TRACE_TYPE_ID
);
409 for (IConfigurationElement ce
: config
) {
410 String elementName
= ce
.getName();
411 if (elementName
.equals(TmfTraceType
.TYPE_ELEM
)) {
412 String traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
413 fTraceTypeAttributes
.put(traceTypeId
, ce
);
414 } else if (elementName
.equals(TmfTraceType
.CATEGORY_ELEM
)) {
415 String categoryId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
416 fTraceCategories
.put(categoryId
, ce
);
419 // create the trace types
420 for (String typeId
: fTraceTypeAttributes
.keySet()) {
421 IConfigurationElement ce
= fTraceTypeAttributes
.get(typeId
);
422 final String category
= getCategory(ce
);
423 final String attribute
= ce
.getAttribute(TmfTraceType
.NAME_ATTR
);
424 ITmfTrace trace
= null;
426 trace
= (ITmfTrace
) ce
.createExecutableExtension(TmfTraceType
.TRACE_TYPE_ATTR
);
427 // Deregister trace as signal handler because it is only used for validation
428 TmfSignalManager
.deregister(trace
);
429 } catch (CoreException e
) {
431 TraceTypeHelper tt
= new TraceTypeHelper(typeId
, category
, attribute
, trace
);
432 fTraceTypes
.put(typeId
, tt
);
437 private String
getCategory(IConfigurationElement ce
) {
438 final String categoryId
= ce
.getAttribute(TmfTraceType
.CATEGORY_ATTR
);
439 if (categoryId
!= null) {
440 IConfigurationElement category
= fTraceCategories
.get(categoryId
);
441 if (category
!= null && !category
.getName().equals("")) { //$NON-NLS-1$
442 return category
.getAttribute(TmfTraceType
.NAME_ATTR
);
445 return "[no category]"; //$NON-NLS-1$
449 * Returns the list of trace categories
451 * @return the list of trace categories
454 public List
<String
> getTraceCategories() {
455 List
<String
> categoryNames
= new ArrayList
<>();
456 for (String key
: fTraceTypes
.keySet()) {
457 final String categoryName
= fTraceTypes
.get(key
).getCategoryName();
458 if (!categoryNames
.contains(categoryName
)) {
459 categoryNames
.add(categoryName
);
462 return categoryNames
;
466 * Get the trace type helper classes from category name
468 * @param categoryName
469 * the categoryName to lookup
470 * @return a list of trace type helper classes {@link TraceTypeHelper}
474 public List
<TraceTypeHelper
> getTraceTypes(String categoryName
) {
475 List
<TraceTypeHelper
> traceNames
= new ArrayList
<>();
476 for (String key
: fTraceTypes
.keySet()) {
477 final String storedCategoryName
= fTraceTypes
.get(key
).getCategoryName();
478 if (storedCategoryName
.equals(categoryName
)) {
479 traceNames
.add(fTraceTypes
.get(key
));
485 private void init() {
486 populateCategoriesAndTraceTypes();
487 populateCustomTraceTypes();
491 private static List
<File
> isolateTraces(List
<FileSystemElement
> selectedResources
) {
493 List
<File
> traces
= new ArrayList
<>();
496 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
498 // Get the sorted list of unique entries
499 Map
<String
, File
> fileSystemObjects
= new HashMap
<>();
500 while (resources
.hasNext()) {
501 File resource
= (File
) resources
.next().getFileSystemObject();
502 String key
= resource
.getAbsolutePath();
503 fileSystemObjects
.put(key
, resource
);
505 List
<String
> files
= new ArrayList
<>(fileSystemObjects
.keySet());
506 Collections
.sort(files
);
508 // After sorting, traces correspond to the unique prefixes
509 String prefix
= null;
510 for (int i
= 0; i
< files
.size(); i
++) {
511 File file
= fileSystemObjects
.get(files
.get(i
));
512 String name
= file
.getAbsolutePath();
513 if (prefix
== null || !name
.startsWith(prefix
)) {
514 prefix
= name
; // new prefix
523 * Validate a trace type
525 * @param traceTypeName
526 * the trace category (canonical name)
528 * the file name (and path)
529 * @return true if the trace is of a valid type
532 public boolean validate(String traceTypeName
, String fileName
) {
533 if (traceTypeName
!= null && !traceTypeName
.isEmpty()) {
534 final TraceTypeHelper traceTypeHelper
= fTraceTypes
.get(traceTypeName
);
535 if (!traceTypeHelper
.validate(fileName
)) {
545 * @param traceToValidate
546 * the trace category (canonical name)
547 * @return true if the trace is of a valid type
550 public boolean validate(TraceValidationHelper traceToValidate
) {
551 return validate(traceToValidate
.getTraceType(), traceToValidate
.getTraceToScan());
555 * validate list of traces with a tracetype
557 * @param traceTypeName
558 * the trace category (canonical name)
559 * @param selectedResources
560 * List of traces to validate
561 * @return true if all the traces are valid
564 public boolean validateTrace(String traceTypeName
, List
<FileSystemElement
> selectedResources
) {
565 List
<File
> traces
= isolateTraces(selectedResources
);
566 return validateTraceFiles(traceTypeName
, traces
);
570 * Validate a list of files with a tracetype
572 * @param traceTypeName
573 * the trace category (canonical name)
575 * the list of files to check if they are trace
576 * @return true if all the traces are valid
579 public boolean validateTraceFiles(String traceTypeName
, List
<File
> traces
) {
580 if (traceTypeName
!= null && !"".equals(traceTypeName
) && //$NON-NLS-1$
581 !traceTypeName
.startsWith(TmfTraceType
.CUSTOM_TXT_CATEGORY
) && !traceTypeName
.startsWith(TmfTraceType
.CUSTOM_XML_CATEGORY
)) {
582 for (File trace
: traces
) {
583 if (!validate(traceTypeName
, trace
.getAbsolutePath())) {
592 * Get a configuration element for a given name
596 * @return the configuration element, can be null
599 public IConfigurationElement
getTraceAttributes(String traceType
) {
600 return fTraceTypeAttributes
.get(traceType
);
604 * Find the id of a trace type by its parameters
607 * like "ctf" or "custom text"
610 * @return an id like "org.eclipse.linuxtools.blabla...
613 public String
getTraceTypeId(String category
, String traceType
) {
614 for (String key
: fTraceTypes
.keySet()) {
615 if (fTraceTypes
.get(key
).getCategoryName().equals(category
.trim()) && fTraceTypes
.get(key
).getName().equals(traceType
.trim())) {
623 * Is the trace a custom (user-defined) trace type. These are the traces
624 * like : text and xml defined by the custom trace wizard.
627 * the trace type in human form (category:name)
628 * @return true if the trace is a custom type
631 public static boolean isCustomTrace(String traceType
) {
632 final boolean startsWithTxt
= traceType
.startsWith(TmfTraceType
.CUSTOM_TXT_CATEGORY
);
633 final boolean startsWithXML
= traceType
.startsWith(TmfTraceType
.CUSTOM_XML_CATEGORY
);
634 return (startsWithTxt
|| startsWithXML
);
638 * Is the trace type id a custom (user-defined) trace type. These are the
639 * traces like : text and xml defined by the custom trace wizard.
643 * @return true if the trace is a custom type
645 private static boolean isCustomTraceId(String traceTypeId
) {
646 TraceTypeHelper traceType
= getInstance().getTraceType(traceTypeId
);
647 if (traceType
!= null) {
648 return isCustomTrace(traceType
.getCategoryName() + SEPARATOR
+ traceType
.getName());
655 * Gets the custom trace type ID from the custom trace name
658 * The trace type in human form (category:name)
659 * @return the trace type ID or null if the trace is not a custom one
662 public static String
getCustomTraceTypeId(String traceType
) {
663 String traceTypeId
= null;
665 // do custom trace stuff here
666 String traceTypeToken
[] = traceType
.split(":", 2); //$NON-NLS-1$
667 if (traceTypeToken
.length
== 2) {
668 final boolean startsWithTxt
= traceType
.startsWith(TmfTraceType
.CUSTOM_TXT_CATEGORY
);
669 final boolean startsWithXML
= traceType
.startsWith(TmfTraceType
.CUSTOM_XML_CATEGORY
);
671 traceTypeId
= CustomTxtTrace
.class.getCanonicalName() + SEPARATOR
+ traceTypeToken
[1];
672 } else if (startsWithXML
) {
673 traceTypeId
= CustomXmlTrace
.class.getCanonicalName() + SEPARATOR
+ traceTypeToken
[1];
679 TraceTypeHelper
selectTraceType(String path
, Shell shell
) throws TmfTraceImportException
{
680 return selectTraceType(path
, shell
, null);
684 * This member figures out the trace type of a given file. It will prompt
685 * the user if it needs more information to properly pick the trace type.
688 * The path of file to import
690 * a shell to display the message to. If it is null, it is
691 * assumed to be cancelled.
692 * @param traceTypeHint the ID of a trace (like "o.e.l.specifictrace" )
693 * @return null if the request is cancelled or a TraceTypeHelper if it passes.
694 * @throws TmfTraceImportException
695 * if the traces don't match or there are errors in the trace
699 public TraceTypeHelper
selectTraceType(String path
, Shell shell
, String traceTypeHint
) throws TmfTraceImportException
{
700 List
<TraceTypeHelper
> validCandidates
= new ArrayList
<>();
701 final Set
<String
> traceTypes
= fTraceTypes
.keySet();
702 for (String traceType
: traceTypes
) {
703 if (validate(traceType
, path
)) {
704 validCandidates
.add(fTraceTypes
.get(traceType
));
708 TraceTypeHelper traceTypeToSet
= null;
709 if (validCandidates
.isEmpty()) {
710 final String errorMsg
= Messages
.TmfOpenTraceHelper_NoTraceTypeMatch
+ path
;
711 throw new TmfTraceImportException(errorMsg
);
712 } else if (validCandidates
.size() != 1) {
713 List
<TraceTypeHelper
> reducedCandidates
= reduce(validCandidates
);
714 for (TraceTypeHelper tth
: reducedCandidates
) {
715 if (tth
.getCanonicalName().equals(traceTypeHint
)) {
716 traceTypeToSet
= tth
;
719 if (traceTypeToSet
== null) {
720 if (reducedCandidates
.size() == 0) {
721 throw new TmfTraceImportException(Messages
.TmfOpenTraceHelper_ReduceError
);
722 } else if (reducedCandidates
.size() == 1) {
723 traceTypeToSet
= reducedCandidates
.get(0);
728 traceTypeToSet
= getTraceTypeToSet(reducedCandidates
, shell
);
732 traceTypeToSet
= validCandidates
.get(0);
734 return traceTypeToSet
;
737 private static List
<TraceTypeHelper
> reduce(List
<TraceTypeHelper
> candidates
) {
738 List
<TraceTypeHelper
> retVal
= new ArrayList
<>();
740 // get all the tracetypes that are unique in that stage
741 for (TraceTypeHelper trace
: candidates
) {
742 if (isUnique(trace
, candidates
)) {
750 * Only return the leaves of the trace types. Ignore custom trace types.
752 private static boolean isUnique(TraceTypeHelper trace
, List
<TraceTypeHelper
> set
) {
753 if (TmfTraceType
.isCustomTraceId(trace
.getCanonicalName())) {
756 // check if the trace type is the leaf. we make an instance of the trace
757 // type and if it is only an instance of itself, it is a leaf
758 final ITmfTrace tmfTrace
= trace
.getTrace();
760 for (TraceTypeHelper child
: set
) {
761 final ITmfTrace traceCandidate
= child
.getTrace();
762 if (tmfTrace
.getClass().isInstance(traceCandidate
)) {
769 private TraceTypeHelper
getTraceTypeToSet(List
<TraceTypeHelper
> candidates
, Shell shell
) {
770 final Map
<String
, String
> names
= new HashMap
<>();
771 Shell shellToShow
= new Shell(shell
);
772 shellToShow
.setText(Messages
.TmfTraceType_SelectTraceType
);
773 final String candidatesToSet
[] = new String
[1];
774 for (TraceTypeHelper candidate
: candidates
) {
775 Button b
= new Button(shellToShow
, SWT
.RADIO
);
776 final String displayName
= candidate
.getCategoryName() + ':' + candidate
.getName();
777 b
.setText(displayName
);
778 names
.put(displayName
, candidate
.getCanonicalName());
780 b
.addSelectionListener(new SelectionListener() {
783 public void widgetSelected(SelectionEvent e
) {
784 final Button source
= (Button
) e
.getSource();
785 candidatesToSet
[0] = (names
.get(source
.getText()));
786 source
.getParent().dispose();
790 public void widgetDefaultSelected(SelectionEvent e
) {
795 shellToShow
.setLayout(new RowLayout(SWT
.VERTICAL
));
799 Display display
= shellToShow
.getDisplay();
800 while (!shellToShow
.isDisposed()) {
801 if (!display
.readAndDispatch()) {
805 return fTraceTypes
.get(candidatesToSet
[0]);
809 * Set the trace type of a {@Link TraceTypeHelper}. Should only be
810 * used internally by this project.
813 * the {@link IPath} path of the resource to set
815 * the {@link TraceTypeHelper} to set the trace type to.
816 * @return Status.OK_Status if successful, error is otherwise.
817 * @throws CoreException
818 * An exception caused by accessing eclipse project items.
821 public static IStatus
setTraceType(IPath path
, TraceTypeHelper traceType
) throws CoreException
{
822 IResource resource
= ResourcesPlugin
.getWorkspace().getRoot().findMember(path
);
823 String traceBundle
= null, traceTypeId
= traceType
.getCanonicalName(), traceIcon
= null;
824 if (TmfTraceType
.isCustomTraceId(traceTypeId
)) {
825 traceBundle
= Activator
.getDefault().getBundle().getSymbolicName();
826 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
828 IConfigurationElement ce
= TmfTraceType
.getInstance().getTraceAttributes(traceTypeId
);
829 traceBundle
= ce
.getContributor().getName();
830 traceIcon
= ce
.getAttribute(TmfTraceType
.ICON_ATTR
);
833 resource
.setPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
, traceBundle
);
834 resource
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceTypeId
);
835 resource
.setPersistentProperty(TmfCommonConstants
.TRACEICON
, traceIcon
);
837 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(resource
.getProject(), true);
838 final TmfTraceFolder tracesFolder
= tmfProject
.getTracesFolder();
839 List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
840 for (TmfTraceElement traceElement
: traces
) {
841 if (traceElement
.getName().equals(resource
.getName())) {
842 traceElement
.refreshTraceType();
846 tmfProject
.refresh();
847 return Status
.OK_STATUS
;