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