Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011 Ericsson | |
abbdd66a | 3 | * |
12c155f5 FC |
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 | |
abbdd66a | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
d34665f9 | 13 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
12c155f5 FC |
14 | |
15 | import java.util.HashMap; | |
16 | import java.util.HashSet; | |
17 | import java.util.LinkedList; | |
9fa32496 | 18 | import java.util.List; |
12c155f5 | 19 | import java.util.Map; |
9fa32496 | 20 | import java.util.Set; |
12c155f5 FC |
21 | |
22 | import org.eclipse.core.runtime.IConfigurationElement; | |
23 | import org.eclipse.core.runtime.Platform; | |
24 | import org.eclipse.jface.action.IContributionItem; | |
25 | import org.eclipse.jface.action.MenuManager; | |
26 | import org.eclipse.jface.resource.ImageDescriptor; | |
27 | import org.eclipse.jface.viewers.ISelection; | |
28 | import org.eclipse.jface.viewers.StructuredSelection; | |
8fd82db5 | 29 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
d34665f9 FC |
30 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace; |
31 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition; | |
32 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace; | |
33 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition; | |
12c155f5 | 34 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
bfc779a0 | 35 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceType; |
12c155f5 FC |
36 | import org.eclipse.ui.IWorkbenchPage; |
37 | import org.eclipse.ui.IWorkbenchWindow; | |
38 | import org.eclipse.ui.PlatformUI; | |
39 | import org.eclipse.ui.actions.CompoundContributionItem; | |
40 | import org.eclipse.ui.menus.CommandContributionItem; | |
41 | import org.eclipse.ui.menus.CommandContributionItemParameter; | |
42 | ||
43 | public class SelectTraceTypeContributionItem extends CompoundContributionItem { | |
44 | ||
45 | //private static final ImageDescriptor SELECTED_ICON = ImageDescriptor.createFromImage(TmfUiPlugin.getDefault().getImageFromPath("icons/elcl16/bullet.gif")); //$NON-NLS-1$ | |
8fd82db5 | 46 | private static final ImageDescriptor SELECTED_ICON = Activator.getDefault().getImageDescripterFromPath( |
12c155f5 | 47 | "icons/elcl16/bullet.gif"); //$NON-NLS-1$ |
37d150dd FC |
48 | private static final String BUNDLE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.bundle"; //$NON-NLS-1$ |
49 | private static final String TYPE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.type"; //$NON-NLS-1$ | |
50 | private static final String ICON_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.icon"; //$NON-NLS-1$ | |
51 | private static final String SELECT_TRACE_TYPE_COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.command.select_trace_type"; //$NON-NLS-1$ | |
4bf17f4a | 52 | private static final String CUSTOM_TXT_CATEGORY = "Custom Text"; //$NON-NLS-1$ |
53 | private static final String CUSTOM_XML_CATEGORY = "Custom XML"; //$NON-NLS-1$ | |
54 | private static final String DEFAULT_TRACE_ICON_PATH = "icons/elcl16/trace.gif"; //$NON-NLS-1$ | |
12c155f5 FC |
55 | |
56 | @Override | |
57 | protected IContributionItem[] getContributionItems() { | |
587660fc PT |
58 | |
59 | Set<String> selectedTraceTypes = new HashSet<String>(); | |
60 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
61 | IWorkbenchPage page = window.getActivePage(); | |
62 | ISelection selection = page.getSelection(); | |
63 | if (selection instanceof StructuredSelection) { | |
64 | for (Object element : ((StructuredSelection) selection).toList()) { | |
65 | if (element instanceof TmfTraceElement) { | |
66 | TmfTraceElement trace = (TmfTraceElement) element; | |
67 | selectedTraceTypes.add(trace.getTraceType()); | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
9fa32496 | 72 | List<IContributionItem> list = new LinkedList<IContributionItem>(); |
12c155f5 | 73 | |
9fa32496 | 74 | Map<String, MenuManager> categoriesMap = new HashMap<String, MenuManager>(); |
12c155f5 | 75 | IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor( |
4bf17f4a | 76 | TmfTraceType.TMF_TRACE_TYPE_ID); |
12c155f5 | 77 | for (IConfigurationElement ce : config) { |
4bf17f4a | 78 | if (ce.getName().equals(TmfTraceType.CATEGORY_ELEM)) { |
587660fc PT |
79 | String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR); |
80 | ImageDescriptor icon = isSelectedCategory(categoryId, config, selectedTraceTypes) ? SELECTED_ICON : null; | |
81 | MenuManager subMenu = new MenuManager(ce.getAttribute(TmfTraceType.NAME_ATTR), icon, null); | |
82 | categoriesMap.put(categoryId, subMenu); | |
12c155f5 FC |
83 | list.add(subMenu); |
84 | } | |
85 | } | |
587660fc PT |
86 | CustomTxtTraceDefinition[] customTxtTraceDefinitions = CustomTxtTraceDefinition.loadAll(); |
87 | if (customTxtTraceDefinitions.length > 0) { | |
88 | ImageDescriptor icon = isSelectedCategory(customTxtTraceDefinitions, selectedTraceTypes) ? SELECTED_ICON : null; | |
89 | MenuManager subMenu = new MenuManager(CUSTOM_TXT_CATEGORY, icon, null); | |
4bf17f4a | 90 | categoriesMap.put(CUSTOM_TXT_CATEGORY, subMenu); |
91 | list.add(subMenu); | |
92 | } | |
587660fc PT |
93 | CustomXmlTraceDefinition[] customXmlTraceDefinitions = CustomXmlTraceDefinition.loadAll(); |
94 | if (customXmlTraceDefinitions.length > 0) { | |
95 | ImageDescriptor icon = isSelectedCategory(customXmlTraceDefinitions, selectedTraceTypes) ? SELECTED_ICON : null; | |
96 | MenuManager subMenu = new MenuManager(CUSTOM_XML_CATEGORY, icon, null); | |
4bf17f4a | 97 | categoriesMap.put(CUSTOM_XML_CATEGORY, subMenu); |
98 | list.add(subMenu); | |
99 | } | |
12c155f5 | 100 | |
12c155f5 | 101 | for (IConfigurationElement ce : config) { |
4bf17f4a | 102 | if (ce.getName().equals(TmfTraceType.TYPE_ELEM)) { |
12c155f5 | 103 | String traceBundle = ce.getContributor().getName(); |
4bf17f4a | 104 | String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR); |
105 | String traceIcon = ce.getAttribute(TmfTraceType.ICON_ATTR); | |
106 | String label = ce.getAttribute(TmfTraceType.NAME_ATTR).replaceAll("&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$ | |
107 | boolean selected = selectedTraceTypes.contains(traceTypeId); | |
108 | MenuManager subMenu = categoriesMap.get(ce.getAttribute(TmfTraceType.CATEGORY_ATTR)); | |
12c155f5 | 109 | |
4bf17f4a | 110 | addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu); |
12c155f5 FC |
111 | } |
112 | } | |
113 | ||
4bf17f4a | 114 | // add the custom trace types |
587660fc | 115 | for (CustomTxtTraceDefinition def : customTxtTraceDefinitions) { |
8fd82db5 | 116 | String traceBundle = Activator.getDefault().getBundle().getSymbolicName(); |
4bf17f4a | 117 | String traceTypeId = CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$ |
118 | String traceIcon = DEFAULT_TRACE_ICON_PATH; | |
119 | String label = def.definitionName; | |
120 | boolean selected = selectedTraceTypes.contains(traceTypeId); | |
121 | MenuManager subMenu = categoriesMap.get(CUSTOM_TXT_CATEGORY); | |
122 | ||
123 | addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu); | |
124 | } | |
587660fc | 125 | for (CustomXmlTraceDefinition def : customXmlTraceDefinitions) { |
8fd82db5 | 126 | String traceBundle = Activator.getDefault().getBundle().getSymbolicName(); |
4bf17f4a | 127 | String traceTypeId = CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$ |
128 | String traceIcon = DEFAULT_TRACE_ICON_PATH; | |
129 | String label = def.definitionName; | |
130 | boolean selected = selectedTraceTypes.contains(traceTypeId); | |
131 | MenuManager subMenu = categoriesMap.get(CUSTOM_XML_CATEGORY); | |
132 | ||
133 | addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu); | |
134 | } | |
135 | ||
12c155f5 FC |
136 | return list.toArray(new IContributionItem[list.size()]); |
137 | } | |
138 | ||
abbdd66a AM |
139 | private static void addContributionItem(List<IContributionItem> list, |
140 | String traceBundle, String traceTypeId, String traceIcon, | |
141 | String label, boolean selected, | |
4bf17f4a | 142 | MenuManager subMenu) { |
143 | Map<String, String> params; | |
abbdd66a | 144 | |
4bf17f4a | 145 | params = new HashMap<String, String>(); |
146 | params.put(BUNDLE_PARAMETER, traceBundle); | |
147 | params.put(TYPE_PARAMETER, traceTypeId); | |
148 | params.put(ICON_PARAMETER, traceIcon); | |
149 | ||
150 | ImageDescriptor icon = null; | |
151 | if (selected) { | |
152 | icon = SELECTED_ICON; | |
153 | } | |
154 | ||
828e5592 PT |
155 | CommandContributionItemParameter param = new CommandContributionItemParameter( |
156 | PlatformUI.getWorkbench().getActiveWorkbenchWindow(), // serviceLocator | |
157 | "my.parameterid", // id //$NON-NLS-1$ | |
4bf17f4a | 158 | SELECT_TRACE_TYPE_COMMAND_ID, // commandId |
828e5592 | 159 | CommandContributionItem.STYLE_PUSH // style |
4bf17f4a | 160 | ); |
828e5592 PT |
161 | param.parameters = params; |
162 | param.icon = icon; | |
163 | param.disabledIcon = icon; | |
164 | param.hoverIcon = icon; | |
165 | param.label = label; | |
166 | param.visibleEnabled = true; | |
4bf17f4a | 167 | |
168 | if (subMenu != null) { | |
169 | subMenu.add(new CommandContributionItem(param)); | |
170 | } else { | |
171 | list.add(new CommandContributionItem(param)); | |
172 | } | |
173 | } | |
587660fc PT |
174 | |
175 | private static boolean isSelectedCategory(String categoryId, IConfigurationElement[] config, Set<String> selectedTraceTypes) { | |
176 | for (IConfigurationElement ce : config) { | |
177 | if (ce.getName().equals(TmfTraceType.TYPE_ELEM)) { | |
178 | String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR); | |
179 | if (selectedTraceTypes.contains(traceTypeId)) { | |
180 | if (categoryId.equals(ce.getAttribute(TmfTraceType.CATEGORY_ATTR))) { | |
181 | return true; | |
182 | } | |
183 | } | |
184 | } | |
185 | } | |
186 | return false; | |
187 | } | |
188 | ||
189 | private static boolean isSelectedCategory(CustomTxtTraceDefinition[] customTxtTraceDefinitions, Set<String> selectedTraceTypes) { | |
190 | for (CustomTxtTraceDefinition def : customTxtTraceDefinitions) { | |
191 | String traceTypeId = CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$ | |
192 | if (selectedTraceTypes.contains(traceTypeId)) { | |
193 | return true; | |
194 | } | |
195 | } | |
196 | return false; | |
197 | } | |
198 | ||
199 | private static boolean isSelectedCategory(CustomXmlTraceDefinition[] customXmlTraceDefinitions, Set<String> selectedTraceTypes) { | |
200 | for (CustomXmlTraceDefinition def : customXmlTraceDefinitions) { | |
201 | String traceTypeId = CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$ | |
202 | if (selectedTraceTypes.contains(traceTypeId)) { | |
203 | return true; | |
204 | } | |
205 | } | |
206 | return false; | |
207 | } | |
12c155f5 | 208 | } |