Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011 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 | ||
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; | |
d34665f9 FC |
29 | import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin; |
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$ | |
46 | private static final ImageDescriptor SELECTED_ICON = TmfUiPlugin.getDefault().getImageDescripterFromPath( | |
47 | "icons/elcl16/bullet.gif"); //$NON-NLS-1$ | |
4bf17f4a | 48 | private static final String BUNDLE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.bundle"; //$NON-NLS-1$ |
49 | private static final String TYPE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.type"; //$NON-NLS-1$ | |
50 | private static final String ICON_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.icon"; //$NON-NLS-1$ | |
51 | private static final String SELECT_TRACE_TYPE_COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.command.project.trace.select_trace_type"; //$NON-NLS-1$ | |
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()) { | |
109 | String traceBundle = TmfUiPlugin.getDefault().getBundle().getSymbolicName(); | |
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()) { | |
119 | String traceBundle = TmfUiPlugin.getDefault().getBundle().getSymbolicName(); | |
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 | ||
9fa32496 | 132 | private void addContributionItem(List<IContributionItem> list, |
4bf17f4a | 133 | String traceBundle, String traceTypeId, String traceIcon, String label, boolean selected, |
134 | MenuManager subMenu) { | |
135 | Map<String, String> params; | |
136 | ||
137 | params = new HashMap<String, String>(); | |
138 | params.put(BUNDLE_PARAMETER, traceBundle); | |
139 | params.put(TYPE_PARAMETER, traceTypeId); | |
140 | params.put(ICON_PARAMETER, traceIcon); | |
141 | ||
142 | ImageDescriptor icon = null; | |
143 | if (selected) { | |
144 | icon = SELECTED_ICON; | |
145 | } | |
146 | ||
828e5592 PT |
147 | CommandContributionItemParameter param = new CommandContributionItemParameter( |
148 | PlatformUI.getWorkbench().getActiveWorkbenchWindow(), // serviceLocator | |
149 | "my.parameterid", // id //$NON-NLS-1$ | |
4bf17f4a | 150 | SELECT_TRACE_TYPE_COMMAND_ID, // commandId |
828e5592 | 151 | CommandContributionItem.STYLE_PUSH // style |
4bf17f4a | 152 | ); |
828e5592 PT |
153 | param.parameters = params; |
154 | param.icon = icon; | |
155 | param.disabledIcon = icon; | |
156 | param.hoverIcon = icon; | |
157 | param.label = label; | |
158 | param.visibleEnabled = true; | |
4bf17f4a | 159 | |
160 | if (subMenu != null) { | |
161 | subMenu.add(new CommandContributionItem(param)); | |
162 | } else { | |
163 | list.add(new CommandContributionItem(param)); | |
164 | } | |
165 | } | |
12c155f5 | 166 | } |