Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / handlers / SelectTraceTypeContributionItem.java
CommitLineData
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
13package org.eclipse.linuxtools.tmf.ui.project.handlers;
14
15import java.util.HashMap;
16import java.util.HashSet;
17import java.util.LinkedList;
18import java.util.Map;
19
20import org.eclipse.core.runtime.IConfigurationElement;
21import org.eclipse.core.runtime.Platform;
22import org.eclipse.jface.action.IContributionItem;
23import org.eclipse.jface.action.MenuManager;
24import org.eclipse.jface.resource.ImageDescriptor;
25import org.eclipse.jface.viewers.ISelection;
26import org.eclipse.jface.viewers.StructuredSelection;
6c13869b 27import org.eclipse.linuxtools.tmf.core.util.TmfTraceType;
12c155f5 28import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
4bf17f4a 29import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtTrace;
30import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
31import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTrace;
32import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
12c155f5
FC
33import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
34import org.eclipse.ui.IWorkbenchPage;
35import org.eclipse.ui.IWorkbenchWindow;
36import org.eclipse.ui.PlatformUI;
37import org.eclipse.ui.actions.CompoundContributionItem;
38import org.eclipse.ui.menus.CommandContributionItem;
39import org.eclipse.ui.menus.CommandContributionItemParameter;
40
41public class SelectTraceTypeContributionItem extends CompoundContributionItem {
42
43 //private static final ImageDescriptor SELECTED_ICON = ImageDescriptor.createFromImage(TmfUiPlugin.getDefault().getImageFromPath("icons/elcl16/bullet.gif")); //$NON-NLS-1$
44 private static final ImageDescriptor SELECTED_ICON = TmfUiPlugin.getDefault().getImageDescripterFromPath(
45 "icons/elcl16/bullet.gif"); //$NON-NLS-1$
4bf17f4a 46 private static final String BUNDLE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.bundle"; //$NON-NLS-1$
47 private static final String TYPE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.type"; //$NON-NLS-1$
48 private static final String ICON_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.icon"; //$NON-NLS-1$
49 private static final String SELECT_TRACE_TYPE_COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.command.project.trace.select_trace_type"; //$NON-NLS-1$
50 private static final String CUSTOM_TXT_CATEGORY = "Custom Text"; //$NON-NLS-1$
51 private static final String CUSTOM_XML_CATEGORY = "Custom XML"; //$NON-NLS-1$
52 private static final String DEFAULT_TRACE_ICON_PATH = "icons/elcl16/trace.gif"; //$NON-NLS-1$
12c155f5
FC
53
54 @Override
55 protected IContributionItem[] getContributionItems() {
12c155f5
FC
56 LinkedList<IContributionItem> list = new LinkedList<IContributionItem>();
57
58 HashMap<String, MenuManager> categoriesMap = new HashMap<String, MenuManager>();
59 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(
4bf17f4a 60 TmfTraceType.TMF_TRACE_TYPE_ID);
12c155f5 61 for (IConfigurationElement ce : config) {
4bf17f4a 62 if (ce.getName().equals(TmfTraceType.CATEGORY_ELEM)) {
63 MenuManager subMenu = new MenuManager(ce.getAttribute(TmfTraceType.NAME_ATTR));
64 categoriesMap.put(ce.getAttribute(TmfTraceType.ID_ATTR), subMenu);
12c155f5
FC
65 list.add(subMenu);
66 }
67 }
4bf17f4a 68 if (CustomTxtTraceDefinition.loadAll().length > 0) {
69 MenuManager subMenu = new MenuManager(CUSTOM_TXT_CATEGORY);
70 categoriesMap.put(CUSTOM_TXT_CATEGORY, subMenu);
71 list.add(subMenu);
72 }
73 if (CustomXmlTraceDefinition.loadAll().length > 0) {
74 MenuManager subMenu = new MenuManager(CUSTOM_XML_CATEGORY);
75 categoriesMap.put(CUSTOM_XML_CATEGORY, subMenu);
76 list.add(subMenu);
77 }
12c155f5
FC
78
79 HashSet<String> selectedTraceTypes = new HashSet<String>();
80 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
81 IWorkbenchPage page = window.getActivePage();
82 ISelection selection = page.getSelection();
83 if (selection instanceof StructuredSelection) {
84 for (Object element : ((StructuredSelection) selection).toList()) {
85 if (element instanceof TmfTraceElement) {
86 TmfTraceElement trace = (TmfTraceElement) element;
87 selectedTraceTypes.add(trace.getTraceType());
88 }
89 }
90 }
91
92 for (IConfigurationElement ce : config) {
4bf17f4a 93 if (ce.getName().equals(TmfTraceType.TYPE_ELEM)) {
12c155f5 94 String traceBundle = ce.getContributor().getName();
4bf17f4a 95 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
96 String traceIcon = ce.getAttribute(TmfTraceType.ICON_ATTR);
97 String label = ce.getAttribute(TmfTraceType.NAME_ATTR).replaceAll("&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$
98 boolean selected = selectedTraceTypes.contains(traceTypeId);
99 MenuManager subMenu = categoriesMap.get(ce.getAttribute(TmfTraceType.CATEGORY_ATTR));
12c155f5 100
4bf17f4a 101 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
12c155f5
FC
102 }
103 }
104
4bf17f4a 105 // add the custom trace types
106 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
107 String traceBundle = TmfUiPlugin.getDefault().getBundle().getSymbolicName();
108 String traceTypeId = CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$
109 String traceIcon = DEFAULT_TRACE_ICON_PATH;
110 String label = def.definitionName;
111 boolean selected = selectedTraceTypes.contains(traceTypeId);
112 MenuManager subMenu = categoriesMap.get(CUSTOM_TXT_CATEGORY);
113
114 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
115 }
116 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
117 String traceBundle = TmfUiPlugin.getDefault().getBundle().getSymbolicName();
118 String traceTypeId = CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$
119 String traceIcon = DEFAULT_TRACE_ICON_PATH;
120 String label = def.definitionName;
121 boolean selected = selectedTraceTypes.contains(traceTypeId);
122 MenuManager subMenu = categoriesMap.get(CUSTOM_XML_CATEGORY);
123
124 addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
125 }
126
12c155f5
FC
127 return list.toArray(new IContributionItem[list.size()]);
128 }
129
4bf17f4a 130 private void addContributionItem(LinkedList<IContributionItem> list,
131 String traceBundle, String traceTypeId, String traceIcon, String label, boolean selected,
132 MenuManager subMenu) {
133 Map<String, String> params;
134
135 params = new HashMap<String, String>();
136 params.put(BUNDLE_PARAMETER, traceBundle);
137 params.put(TYPE_PARAMETER, traceTypeId);
138 params.put(ICON_PARAMETER, traceIcon);
139
140 ImageDescriptor icon = null;
141 if (selected) {
142 icon = SELECTED_ICON;
143 }
144
145 CommandContributionItemParameter param = new CommandContributionItemParameter(PlatformUI.getWorkbench()
146 .getActiveWorkbenchWindow(), "my.parameterid", // id //$NON-NLS-1$
147 SELECT_TRACE_TYPE_COMMAND_ID, // commandId
148 params, // parameters
149 icon, // icon
150 icon, // disabled icon
151 icon, // hover icon
152 label, // label
153 null, // mnemonic
154 null, // tooltip
155 CommandContributionItem.STYLE_PUSH, // style
156 null, // help context id
157 true // visibleEnable
158 );
159
160 if (subMenu != null) {
161 subMenu.add(new CommandContributionItem(param));
162 } else {
163 list.add(new CommandContributionItem(param));
164 }
165 }
12c155f5 166}
This page took 0.050034 seconds and 5 git commands to generate.