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() { | |
9fa32496 | 58 | List<IContributionItem> list = new LinkedList<IContributionItem>(); |
12c155f5 | 59 | |
9fa32496 | 60 | Map<String, MenuManager> categoriesMap = new HashMap<String, MenuManager>(); |
12c155f5 | 61 | IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor( |
4bf17f4a | 62 | TmfTraceType.TMF_TRACE_TYPE_ID); |
12c155f5 | 63 | for (IConfigurationElement ce : config) { |
4bf17f4a | 64 | if (ce.getName().equals(TmfTraceType.CATEGORY_ELEM)) { |
65 | MenuManager subMenu = new MenuManager(ce.getAttribute(TmfTraceType.NAME_ATTR)); | |
66 | categoriesMap.put(ce.getAttribute(TmfTraceType.ID_ATTR), subMenu); | |
12c155f5 FC |
67 | list.add(subMenu); |
68 | } | |
69 | } | |
4bf17f4a | 70 | if (CustomTxtTraceDefinition.loadAll().length > 0) { |
71 | MenuManager subMenu = new MenuManager(CUSTOM_TXT_CATEGORY); | |
72 | categoriesMap.put(CUSTOM_TXT_CATEGORY, subMenu); | |
73 | list.add(subMenu); | |
74 | } | |
75 | if (CustomXmlTraceDefinition.loadAll().length > 0) { | |
76 | MenuManager subMenu = new MenuManager(CUSTOM_XML_CATEGORY); | |
77 | categoriesMap.put(CUSTOM_XML_CATEGORY, subMenu); | |
78 | list.add(subMenu); | |
79 | } | |
12c155f5 | 80 | |
9fa32496 | 81 | Set<String> selectedTraceTypes = new HashSet<String>(); |
12c155f5 FC |
82 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
83 | IWorkbenchPage page = window.getActivePage(); | |
84 | ISelection selection = page.getSelection(); | |
85 | if (selection instanceof StructuredSelection) { | |
86 | for (Object element : ((StructuredSelection) selection).toList()) { | |
87 | if (element instanceof TmfTraceElement) { | |
88 | TmfTraceElement trace = (TmfTraceElement) element; | |
89 | selectedTraceTypes.add(trace.getTraceType()); | |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | for (IConfigurationElement ce : config) { | |
4bf17f4a | 95 | if (ce.getName().equals(TmfTraceType.TYPE_ELEM)) { |
12c155f5 | 96 | String traceBundle = ce.getContributor().getName(); |
4bf17f4a | 97 | String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR); |
98 | String traceIcon = ce.getAttribute(TmfTraceType.ICON_ATTR); | |
99 | String label = ce.getAttribute(TmfTraceType.NAME_ATTR).replaceAll("&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$ | |
100 | boolean selected = selectedTraceTypes.contains(traceTypeId); | |
101 | MenuManager subMenu = categoriesMap.get(ce.getAttribute(TmfTraceType.CATEGORY_ATTR)); | |
12c155f5 | 102 | |
4bf17f4a | 103 | addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu); |
12c155f5 FC |
104 | } |
105 | } | |
106 | ||
4bf17f4a | 107 | // add the custom trace types |
108 | for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) { | |
8fd82db5 | 109 | String traceBundle = Activator.getDefault().getBundle().getSymbolicName(); |
4bf17f4a | 110 | String traceTypeId = CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$ |
111 | String traceIcon = DEFAULT_TRACE_ICON_PATH; | |
112 | String label = def.definitionName; | |
113 | boolean selected = selectedTraceTypes.contains(traceTypeId); | |
114 | MenuManager subMenu = categoriesMap.get(CUSTOM_TXT_CATEGORY); | |
115 | ||
116 | addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu); | |
117 | } | |
118 | for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) { | |
8fd82db5 | 119 | String traceBundle = Activator.getDefault().getBundle().getSymbolicName(); |
4bf17f4a | 120 | String traceTypeId = CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$ |
121 | String traceIcon = DEFAULT_TRACE_ICON_PATH; | |
122 | String label = def.definitionName; | |
123 | boolean selected = selectedTraceTypes.contains(traceTypeId); | |
124 | MenuManager subMenu = categoriesMap.get(CUSTOM_XML_CATEGORY); | |
125 | ||
126 | addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu); | |
127 | } | |
128 | ||
12c155f5 FC |
129 | return list.toArray(new IContributionItem[list.size()]); |
130 | } | |
131 | ||
abbdd66a AM |
132 | private static void addContributionItem(List<IContributionItem> list, |
133 | String traceBundle, String traceTypeId, String traceIcon, | |
134 | String label, boolean selected, | |
4bf17f4a | 135 | MenuManager subMenu) { |
136 | Map<String, String> params; | |
abbdd66a | 137 | |
4bf17f4a | 138 | params = new HashMap<String, String>(); |
139 | params.put(BUNDLE_PARAMETER, traceBundle); | |
140 | params.put(TYPE_PARAMETER, traceTypeId); | |
141 | params.put(ICON_PARAMETER, traceIcon); | |
142 | ||
143 | ImageDescriptor icon = null; | |
144 | if (selected) { | |
145 | icon = SELECTED_ICON; | |
146 | } | |
147 | ||
828e5592 PT |
148 | CommandContributionItemParameter param = new CommandContributionItemParameter( |
149 | PlatformUI.getWorkbench().getActiveWorkbenchWindow(), // serviceLocator | |
150 | "my.parameterid", // id //$NON-NLS-1$ | |
4bf17f4a | 151 | SELECT_TRACE_TYPE_COMMAND_ID, // commandId |
828e5592 | 152 | CommandContributionItem.STYLE_PUSH // style |
4bf17f4a | 153 | ); |
828e5592 PT |
154 | param.parameters = params; |
155 | param.icon = icon; | |
156 | param.disabledIcon = icon; | |
157 | param.hoverIcon = icon; | |
158 | param.label = label; | |
159 | param.visibleEnabled = true; | |
4bf17f4a | 160 | |
161 | if (subMenu != null) { | |
162 | subMenu.add(new CommandContributionItem(param)); | |
163 | } else { | |
164 | list.add(new CommandContributionItem(param)); | |
165 | } | |
166 | } | |
12c155f5 | 167 | } |