1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.handlers
;
15 import java
.util
.HashMap
;
16 import java
.util
.HashSet
;
17 import java
.util
.LinkedList
;
18 import java
.util
.List
;
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
;
29 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
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
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceType
;
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
;
43 public class SelectTraceTypeContributionItem
extends CompoundContributionItem
{
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
= Activator
.getDefault().getImageDescripterFromPath(
47 "icons/elcl16/bullet.gif"); //$NON-NLS-1$
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$
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$
57 protected IContributionItem
[] getContributionItems() {
58 List
<IContributionItem
> list
= new LinkedList
<IContributionItem
>();
60 Map
<String
, MenuManager
> categoriesMap
= new HashMap
<String
, MenuManager
>();
61 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(
62 TmfTraceType
.TMF_TRACE_TYPE_ID
);
63 for (IConfigurationElement ce
: config
) {
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
);
70 if (CustomTxtTraceDefinition
.loadAll().length
> 0) {
71 MenuManager subMenu
= new MenuManager(CUSTOM_TXT_CATEGORY
);
72 categoriesMap
.put(CUSTOM_TXT_CATEGORY
, subMenu
);
75 if (CustomXmlTraceDefinition
.loadAll().length
> 0) {
76 MenuManager subMenu
= new MenuManager(CUSTOM_XML_CATEGORY
);
77 categoriesMap
.put(CUSTOM_XML_CATEGORY
, subMenu
);
81 Set
<String
> selectedTraceTypes
= new HashSet
<String
>();
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());
94 for (IConfigurationElement ce
: config
) {
95 if (ce
.getName().equals(TmfTraceType
.TYPE_ELEM
)) {
96 String traceBundle
= ce
.getContributor().getName();
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
));
103 addContributionItem(list
, traceBundle
, traceTypeId
, traceIcon
, label
, selected
, subMenu
);
107 // add the custom trace types
108 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
109 String traceBundle
= Activator
.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
);
116 addContributionItem(list
, traceBundle
, traceTypeId
, traceIcon
, label
, selected
, subMenu
);
118 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
119 String traceBundle
= Activator
.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
);
126 addContributionItem(list
, traceBundle
, traceTypeId
, traceIcon
, label
, selected
, subMenu
);
129 return list
.toArray(new IContributionItem
[list
.size()]);
132 private static void addContributionItem(List
<IContributionItem
> list
,
133 String traceBundle
, String traceTypeId
, String traceIcon
,
134 String label
, boolean selected
,
135 MenuManager subMenu
) {
136 Map
<String
, String
> params
;
138 params
= new HashMap
<String
, String
>();
139 params
.put(BUNDLE_PARAMETER
, traceBundle
);
140 params
.put(TYPE_PARAMETER
, traceTypeId
);
141 params
.put(ICON_PARAMETER
, traceIcon
);
143 ImageDescriptor icon
= null;
145 icon
= SELECTED_ICON
;
148 CommandContributionItemParameter param
= new CommandContributionItemParameter(
149 PlatformUI
.getWorkbench().getActiveWorkbenchWindow(), // serviceLocator
150 "my.parameterid", // id //$NON-NLS-1$
151 SELECT_TRACE_TYPE_COMMAND_ID
, // commandId
152 CommandContributionItem
.STYLE_PUSH
// style
154 param
.parameters
= params
;
156 param
.disabledIcon
= icon
;
157 param
.hoverIcon
= icon
;
159 param
.visibleEnabled
= true;
161 if (subMenu
!= null) {
162 subMenu
.add(new CommandContributionItem(param
));
164 list
.add(new CommandContributionItem(param
));