Fix HistogramView recounts on range update
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / parsers / ParserProviderManager.java
1 /*******************************************************************************
2 * Copyright (c) 2010 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.parsers;
14
15 import java.util.ArrayList;
16 import java.util.LinkedHashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IExtensionRegistry;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.core.runtime.QualifiedName;
26 import org.eclipse.core.runtime.content.IContentType;
27 import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
28 import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
29 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
30 import org.eclipse.linuxtools.tmf.ui.viewers.events.TmfEventsTable;
31 import org.eclipse.swt.widgets.Composite;
32
33 public class ParserProviderManager {
34
35 public static final QualifiedName PARSER_PROPERTY = new QualifiedName(TmfUiPlugin.PLUGIN_ID, "PARSER"); //$NON-NLS-1$
36
37 private static List<IParserProvider> fParserProviders = new ArrayList<IParserProvider>();
38
39 public static void init() {
40 IExtensionRegistry reg = Platform.getExtensionRegistry();
41 IConfigurationElement[] extensions = reg.getConfigurationElementsFor("org.eclipse.linuxtools.tmf.ui.parserProviders"); //$NON-NLS-1$
42 for (int i = 0; i < extensions.length; i++) {
43 IConfigurationElement element = extensions[i];
44 try {
45 IParserProvider parserProvider = (IParserProvider) element.createExecutableExtension("class"); //$NON-NLS-1$
46 addParserProvider(parserProvider);
47 } catch (CoreException e) {
48 e.printStackTrace();
49 }
50 }
51 }
52
53 public static void addParserProvider(IParserProvider parserProvider) {
54 fParserProviders.add(parserProvider);
55 }
56
57 public static void removeParserProvider(IParserProvider parserProvider) {
58 fParserProviders.remove(parserProvider);
59 }
60
61 public static ITmfTrace getTrace(IResource resource) {
62 if (resource == null) {
63 return null;
64 }
65 try {
66 String parser = resource.getPersistentProperty(PARSER_PROPERTY);
67 if (parser != null) {
68 for (IParserProvider parserProvider : fParserProviders) {
69 if (parserProvider != null) {
70 ITmfTrace trace = parserProvider.getTraceForParser(parser, resource);
71 if (trace != null) {
72 return trace;
73 }
74 }
75 }
76 }
77 IContentType contentType = Platform.getContentTypeManager().findContentTypeFor(resource.getName());
78 if (contentType != null) {
79 for (IParserProvider parserProvider : fParserProviders) {
80 if (parserProvider != null) {
81 ITmfTrace trace = parserProvider.getTraceForContentType(contentType.getId(), resource);
82 if (trace != null) {
83 resource.setPersistentProperty(PARSER_PROPERTY, trace.getClass().getCanonicalName());
84 return trace;
85 }
86 }
87 }
88 }
89 } catch (CoreException e) {
90 e.printStackTrace();
91 }
92 return null;
93 }
94
95 public static String getEditorId(IResource resource) {
96 if (resource == null) {
97 return null;
98 }
99 try {
100 String parser = resource.getPersistentProperty(PARSER_PROPERTY);
101 if (parser != null) {
102 for (IParserProvider parserProvider : fParserProviders) {
103 if (parserProvider != null) {
104 String editorId = parserProvider.getEditorIdForParser(parser);
105 if (editorId != null) {
106 return editorId;
107 }
108 }
109 }
110 }
111 return TmfEventsEditor.ID;
112 } catch (CoreException e) {
113 e.printStackTrace();
114 }
115 return null;
116 }
117
118 public static Map<String, Map<String, String>> getParserMap() {
119 Map<String, Map<String, String>> parserMap = new LinkedHashMap<String, Map<String, String>>();
120 for (IParserProvider parserProvider : fParserProviders) {
121 parserMap.put(parserProvider.getCategory(), parserProvider.getParserMap());
122 }
123 return parserMap;
124 }
125
126 public static Map<String, String> getEventTypeMapForParser(String parser) {
127 for (IParserProvider parserProvider : fParserProviders) {
128 Map<String, String> map = parserProvider.getEventTypeMapForParser(parser);
129 if (map != null) {
130 return map;
131 }
132 }
133 return new LinkedHashMap<String, String>(0);
134 }
135
136 public static String[] getFieldLabelsForEventType(String eventType) {
137 for (IParserProvider parserProvider : fParserProviders) {
138 String[] fieldLabels = parserProvider.getFieldLabelsForEventType(eventType);
139 if (fieldLabels != null) {
140 return fieldLabels;
141 }
142 }
143 return new String[0];
144 }
145
146 public static TmfEventsTable getEventsTable(ITmfTrace trace, Composite parent, int cacheSize) {
147 for (IParserProvider parserProvider : fParserProviders) {
148 if (parserProvider != null) {
149 TmfEventsTable eventsTable = parserProvider.getEventsTable(trace, parent, cacheSize);
150 if (eventsTable != null) {
151 return eventsTable;
152 }
153 }
154 }
155 return null;
156 }
157
158 }
This page took 0.038023 seconds and 6 git commands to generate.