Commit | Line | Data |
---|---|---|
4bf17f4a | 1 | /******************************************************************************* |
b544077e | 2 | * Copyright (c) 2011, 2012 Ericsson |
cfd22ad0 | 3 | * |
4bf17f4a | 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 | |
cfd22ad0 | 8 | * |
4bf17f4a | 9 | * Contributors: |
10 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
bfc779a0 | 13 | package org.eclipse.linuxtools.tmf.ui.project.model; |
4bf17f4a | 14 | |
15 | import java.util.LinkedList; | |
16 | import java.util.List; | |
17 | ||
05627bda MD |
18 | import org.eclipse.core.resources.IResource; |
19 | import org.eclipse.core.runtime.CoreException; | |
4bf17f4a | 20 | import org.eclipse.core.runtime.IConfigurationElement; |
21 | import org.eclipse.core.runtime.Platform; | |
05627bda MD |
22 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
23 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; | |
4bf17f4a | 24 | |
b544077e BH |
25 | /** |
26 | * Utility class for accessing TMF trace type extensions from the platform's extensions registry. | |
cfd22ad0 | 27 | * |
b544077e BH |
28 | * @version 1.0 |
29 | * @author Patrick Tasse | |
30 | * | |
31 | */ | |
4bf17f4a | 32 | public class TmfTraceType { |
33 | ||
b544077e BH |
34 | /** |
35 | * Extension point ID | |
36 | */ | |
bfc779a0 | 37 | public static final String TMF_TRACE_TYPE_ID = "org.eclipse.linuxtools.tmf.ui.tracetype"; //$NON-NLS-1$ |
4bf17f4a | 38 | |
b544077e BH |
39 | /** |
40 | * Extension point element 'Category' | |
41 | */ | |
4bf17f4a | 42 | public static final String CATEGORY_ELEM = "category"; //$NON-NLS-1$ |
b544077e BH |
43 | /** |
44 | * Extension point element 'Type' | |
45 | */ | |
4bf17f4a | 46 | public static final String TYPE_ELEM = "type"; //$NON-NLS-1$ |
b544077e | 47 | /** |
cfd22ad0 | 48 | * Extension point element 'Default editor' |
b544077e | 49 | */ |
4bf17f4a | 50 | public static final String DEFAULT_EDITOR_ELEM = "defaultEditor"; //$NON-NLS-1$ |
b544077e BH |
51 | /** |
52 | * Extension point element 'Events table type' | |
53 | */ | |
4bf17f4a | 54 | public static final String EVENTS_TABLE_TYPE_ELEM = "eventsTableType"; //$NON-NLS-1$ |
cfd22ad0 MD |
55 | /** |
56 | * Extension point element 'Statistics viewer type' | |
57 | * | |
58 | * @since 2.0 | |
59 | */ | |
60 | public static final String STATISTICS_VIEWER_ELEM = "statisticsViewerType"; //$NON-NLS-1$ | |
4bf17f4a | 61 | |
b544077e BH |
62 | /** |
63 | * Extension point attribute 'ID' | |
64 | */ | |
4bf17f4a | 65 | public static final String ID_ATTR = "id"; //$NON-NLS-1$ |
b544077e BH |
66 | /** |
67 | * Extension point attribute 'name' | |
68 | */ | |
4bf17f4a | 69 | public static final String NAME_ATTR = "name"; //$NON-NLS-1$ |
b544077e BH |
70 | /** |
71 | * Extension point attribute 'category' | |
72 | */ | |
4bf17f4a | 73 | public static final String CATEGORY_ATTR = "category"; //$NON-NLS-1$ |
b544077e BH |
74 | /** |
75 | * Extension point attribute 'trace_type' | |
76 | */ | |
4bf17f4a | 77 | public static final String TRACE_TYPE_ATTR = "trace_type"; //$NON-NLS-1$ |
b544077e BH |
78 | /** |
79 | * Extension point attribute 'event_type' | |
80 | */ | |
4bf17f4a | 81 | public static final String EVENT_TYPE_ATTR = "event_type"; //$NON-NLS-1$ |
b544077e BH |
82 | /** |
83 | * Extension point attribute 'icon' | |
84 | */ | |
4bf17f4a | 85 | public static final String ICON_ATTR = "icon"; //$NON-NLS-1$ |
b544077e BH |
86 | /** |
87 | * Extension point attribute 'class' | |
88 | */ | |
4bf17f4a | 89 | public static final String CLASS_ATTR = "class"; //$NON-NLS-1$ |
90 | ||
b544077e BH |
91 | /** |
92 | * Retrieves the category name from the platform extension registry based on the category ID | |
93 | * @param categoryId The category ID | |
94 | * @return the category name or empty string if not found | |
95 | */ | |
4bf17f4a | 96 | public static String getCategoryName(String categoryId) { |
97 | IConfigurationElement[] elements = Platform.getExtensionRegistry() | |
98 | .getConfigurationElementsFor(TMF_TRACE_TYPE_ID); | |
99 | for (IConfigurationElement element : elements) { | |
100 | if (element.getName().equals(CATEGORY_ELEM) && categoryId.equals(element.getAttribute(ID_ATTR))) { | |
101 | return element.getAttribute(NAME_ATTR); | |
102 | } | |
103 | } | |
104 | return ""; //$NON-NLS-1$ | |
105 | } | |
106 | ||
05627bda MD |
107 | /** |
108 | * Retrieves and instantiates an element's object based on his plug-in | |
109 | * definition for a specific trace type. | |
110 | * | |
111 | * The element's object is instantiated using its 0-argument constructor. | |
112 | * | |
113 | * @param resource | |
114 | * The resource where to find the information about the trace | |
115 | * properties | |
116 | * @param element | |
117 | * The name of the element to find under the trace type | |
118 | * definition | |
119 | * @return a new Object based on his definition in plugin.xml, or null if no | |
120 | * definition was found | |
121 | * @since 2.0 | |
122 | */ | |
123 | public static Object getTraceTypeElement(IResource resource, String element) { | |
124 | try { | |
125 | if (resource != null) { | |
126 | String traceType = resource.getPersistentProperty(TmfCommonConstants.TRACETYPE); | |
127 | /* | |
128 | * Search in the configuration if there is any viewer specified | |
129 | * for this kind of trace type. | |
130 | */ | |
131 | for (IConfigurationElement ce : TmfTraceType.getTypeElements()) { | |
132 | if (ce.getAttribute(TmfTraceType.ID_ATTR).equals(traceType)) { | |
133 | IConfigurationElement[] viewerCE = ce.getChildren(element); | |
134 | if (viewerCE.length != 1) { | |
135 | break; | |
136 | } | |
137 | return viewerCE[0].createExecutableExtension(TmfTraceType.CLASS_ATTR); | |
138 | } | |
139 | } | |
140 | } | |
141 | } catch (CoreException e) { | |
142 | Activator.getDefault().logError("Error creating the element from the resource", e); //$NON-NLS-1$ | |
143 | } | |
144 | return null; | |
145 | } | |
146 | ||
b544077e | 147 | /** |
cfd22ad0 | 148 | * Retrieves all configuration elements from the platform extension registry |
b544077e | 149 | * for the trace type extension. |
cfd22ad0 MD |
150 | * |
151 | * @return an array of trace type configuration elements | |
b544077e | 152 | */ |
4bf17f4a | 153 | public static IConfigurationElement[] getTypeElements() { |
154 | IConfigurationElement[] elements = Platform.getExtensionRegistry() | |
155 | .getConfigurationElementsFor(TMF_TRACE_TYPE_ID); | |
156 | List<IConfigurationElement> typeElements = new LinkedList<IConfigurationElement>(); | |
157 | for (IConfigurationElement element : elements) { | |
158 | if (element.getName().equals(TYPE_ELEM)) { | |
159 | typeElements.add(element); | |
160 | } | |
161 | } | |
beae214a | 162 | return typeElements.toArray(new IConfigurationElement[typeElements.size()]); |
4bf17f4a | 163 | } |
164 | } |