tmf: Simplify SelectElementTypeContributionItem
authorPatrick Tasse <patrick.tasse@gmail.com>
Mon, 20 Mar 2017 17:51:22 +0000 (13:51 -0400)
committerPatrick Tasse <patrick.tasse@gmail.com>
Tue, 21 Mar 2017 15:26:01 +0000 (11:26 -0400)
The class TmfTraceType already manages the list of available trace
types, including experiment types and custom trace types. Its list of
trace type helpers can be used to build the menu items.

The unused command parameters BUNDLE and ICON are removed.

Note that menu items will now be grouped by category name instead of
category id. If two different category id used the same name, they will
now be merged instead of showing two sub-menus with the same name.

The items in a category sub-menu will be sorted alphabetically.

Change-Id: I411e88ad34db66d4ec3d32d8e4e6390ca66b6c6a
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/93446
Reviewed-by: Hudson CI
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
tmf/org.eclipse.tracecompass.tmf.ui/plugin.properties
tmf/org.eclipse.tracecompass.tmf.ui/plugin.xml
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/project/handlers/SelectElementTypeContributionItem.java

index 29d7be360e79f76a7b730eb993b72dc761def9a2..2b6302147d45ccb912835f5a9671ee8481a60db5 100644 (file)
@@ -111,9 +111,7 @@ command.delete_suppl_files.description = Delete Supplementary Files
 command.select_trace_type = Select Trace Type...
 command.select_trace_type.mnemonic = T
 command.select_trace_type.description = Select a trace type
-command.select_trace_type.bundle = Bundle
 command.select_trace_type.type = Trace Type
-command.select_trace_type.icon = Icon
 
 command.copy_to_clipboard = Copy to Clipboard
 command.copy_to_clipboard.description = Copy to Clipboard
index c123862c0419b3a8fe6a30fa0363156f0e3cd52a..91f5cda40923b373da2f2f44101247f50551c2f3 100644 (file)
             description="%command.select_trace_type.description"
             id="org.eclipse.linuxtools.tmf.ui.command.select_trace_type"
             name="%command.select_trace_type">
-         <commandParameter
-               id="org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.bundle"
-               name="%command.select_trace_type.bundle"
-               optional="false">
-         </commandParameter>
          <commandParameter
                id="org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.type"
                name="%command.select_trace_type.type"
                optional="false">
          </commandParameter>
-         <commandParameter
-               id="org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.icon"
-               name="%command.select_trace_type.icon"
-               optional="false">
-         </commandParameter>
       </command>
       <command
             categoryId="org.eclipse.linuxtools.tmf.ui.commands.category"
index 2c09e8d3d560563c916f51ae0b2e41facce97685..b99b14b7c6baadcfce26daaa4ebd2613987d9656 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal
+ * Copyright (c) 2011, 2017 Ericsson, École Polytechnique de Montréal
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -13,6 +13,7 @@
 
 package org.eclipse.tracecompass.internal.tmf.ui.project.handlers;
 
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -20,26 +21,18 @@ import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Set;
 
-import org.eclipse.core.runtime.IConfigurationElement;
-import org.eclipse.core.runtime.Platform;
 import org.eclipse.jface.action.IContributionItem;
 import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
-import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace;
-import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition;
-import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace;
-import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition;
 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
-import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType.TraceElementType;
+import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
-import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceTypeUIUtils;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
@@ -54,12 +47,30 @@ import org.eclipse.ui.menus.CommandContributionItemParameter;
  */
 public class SelectElementTypeContributionItem extends CompoundContributionItem {
 
+    private final static class ItemComparator implements Comparator<IContributionItem> {
+        @Override
+        public int compare(IContributionItem o1, IContributionItem o2) {
+            if (o1 instanceof MenuManager) {
+                if (o2 instanceof MenuManager) {
+                    MenuManager m1 = (MenuManager) o1;
+                    MenuManager m2 = (MenuManager) o2;
+                    return m1.getMenuText().compareTo(m2.getMenuText());
+                }
+                return -1;
+            }
+            if (o2 instanceof MenuManager) {
+                return 1;
+            }
+            CommandContributionItem c1 = (CommandContributionItem) o1;
+            CommandContributionItem c2 = (CommandContributionItem) o2;
+            return c1.getData().label.compareTo(c2.getData().label);
+        }
+    }
+
+    private static final Comparator<IContributionItem> ITEM_COMPARATOR = new ItemComparator();
     private static final ImageDescriptor SELECTED_ICON = Activator.getDefault().getImageDescripterFromPath("icons/elcl16/bullet.gif"); //$NON-NLS-1$
-    private static final String BUNDLE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.bundle"; //$NON-NLS-1$
     private static final String TYPE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.type"; //$NON-NLS-1$
-    private static final String ICON_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.icon"; //$NON-NLS-1$
     private static final String SELECT_TRACE_TYPE_COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.command.select_trace_type"; //$NON-NLS-1$
-    private static final String DEFAULT_TRACE_ICON_PATH = "icons/elcl16/trace.gif"; //$NON-NLS-1$
 
     @Override
     protected IContributionItem[] getContributionItems() {
@@ -108,125 +119,43 @@ public class SelectElementTypeContributionItem extends CompoundContributionItem
      */
     protected IContributionItem[] getContributionItems(Set<String> selectedTraceTypes, boolean forExperiments) {
 
-        String ceType = forExperiments ? TmfTraceType.EXPERIMENT_ELEM : TmfTraceType.TYPE_ELEM;
-        TraceElementType elementType = forExperiments ? TraceElementType.EXPERIMENT : TraceElementType.TRACE;
-
         List<IContributionItem> list = new LinkedList<>();
 
         Map<String, MenuManager> categoriesMap = new HashMap<>();
-        IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(
-                TmfTraceType.TMF_TRACE_TYPE_ID);
-        for (IConfigurationElement ce : config) {
-            if (ce.getName().equals(TmfTraceType.CATEGORY_ELEM)) {
-                String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
-                ImageDescriptor icon = isSelectedCategory(categoryId, config, selectedTraceTypes) ? SELECTED_ICON : null;
-                MenuManager subMenu = new MenuManager(ce.getAttribute(TmfTraceType.NAME_ATTR), icon, null);
-                categoriesMap.put(categoryId, subMenu);
-                list.add(subMenu);
-            }
-        }
-
-        for (IConfigurationElement ce : config) {
-            if (ce.getName().equals(ceType)) {
-                String traceBundle = ce.getContributor().getName();
-                String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
-                String label = ce.getAttribute(TmfTraceType.NAME_ATTR).replaceAll("&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$
-                boolean selected = selectedTraceTypes.contains(traceTypeId);
-                MenuManager subMenu = categoriesMap.get(ce.getAttribute(TmfTraceType.CATEGORY_ATTR));
-
-                /* Get the icon from the tmftracetypeui extension, if it exists */
-                String traceIcon = null;
-                IConfigurationElement uiCE = TmfTraceTypeUIUtils.getTraceUIAttributes(traceTypeId, elementType);
-                if (uiCE != null) {
-                    traceIcon = uiCE.getAttribute(TmfTraceTypeUIUtils.ICON_ATTR);
-                }
-
-                addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
+        for (TraceTypeHelper traceTypeHelper : TmfTraceType.getTraceTypeHelpers()) {
+            if (forExperiments != traceTypeHelper.isExperimentType()) {
+                continue;
             }
-        }
 
-        Comparator<IContributionItem> comparator = new Comparator<IContributionItem>() {
-            @Override
-            public int compare(IContributionItem o1, IContributionItem o2) {
-                if (o1 instanceof MenuManager) {
-                    if (o2 instanceof MenuManager) {
-                        MenuManager m1 = (MenuManager) o1;
-                        MenuManager m2 = (MenuManager) o2;
-                        return m1.getMenuText().compareTo(m2.getMenuText());
-                    }
-                    return -1;
-                }
-                if (o2 instanceof MenuManager) {
-                    return 1;
+            String categoryName = traceTypeHelper.getCategoryName().replaceAll("&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$
+            MenuManager subMenu = null;
+            if (!categoryName.isEmpty()) {
+                subMenu = categoriesMap.get(categoryName);
+                if (subMenu == null) {
+                    subMenu = new MenuManager(categoryName);
+                    categoriesMap.put(categoryName, subMenu);
+                    list.add(subMenu);
                 }
-                CommandContributionItem c1 = (CommandContributionItem) o1;
-                CommandContributionItem c2 = (CommandContributionItem) o2;
-                return c1.getData().label.compareTo(c2.getData().label);
             }
-        };
-
-        if (forExperiments) {
-            Collections.sort(list, comparator);
-            return list.toArray(new IContributionItem[list.size()]);
-        }
 
-        /*
-         * Add the custom txt and xml trace type to the contribution items for
-         * traces
-         */
-        for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
-            String traceBundle = Activator.getDefault().getBundle().getSymbolicName();
-            String traceTypeId = CustomTxtTrace.buildTraceTypeId(def.categoryName, def.definitionName);
-            String traceIcon = DEFAULT_TRACE_ICON_PATH;
-            String label = def.definitionName;
+            String traceTypeId = traceTypeHelper.getTraceTypeId();
+            String label = traceTypeHelper.getName().replaceAll("&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$
             boolean selected = selectedTraceTypes.contains(traceTypeId);
-            MenuManager subMenu = getCategorySubMenu(list, categoriesMap, def.categoryName, selected);
-
-            addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
-        }
-        for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
-            String traceBundle = Activator.getDefault().getBundle().getSymbolicName();
-            String traceTypeId = CustomXmlTrace.buildTraceTypeId(def.categoryName, def.definitionName);
-            String traceIcon = DEFAULT_TRACE_ICON_PATH;
-            String label = def.definitionName;
-            boolean selected = selectedTraceTypes.contains(traceTypeId);
-            MenuManager subMenu = getCategorySubMenu(list, categoriesMap, def.categoryName, selected);
+            if (selected && subMenu != null) {
+                subMenu.setImageDescriptor(SELECTED_ICON);
+            }
 
-            addContributionItem(list, traceBundle, traceTypeId, traceIcon, label, selected, subMenu);
+            addContributionItem(list, traceTypeId, label, selected, subMenu);
         }
 
-        Collections.sort(list, comparator);
+        Collections.sort(list, ITEM_COMPARATOR);
         return list.toArray(new IContributionItem[list.size()]);
     }
 
-    private static MenuManager getCategorySubMenu(List<IContributionItem> list,
-            Map<String, MenuManager> categoriesMap, String categoryName, boolean selected) {
-        for (Entry<String, MenuManager> entry : categoriesMap.entrySet()) {
-            MenuManager subMenu = entry.getValue();
-            if (subMenu.getMenuText().equals(categoryName)) {
-                if (selected) {
-                    subMenu.setImageDescriptor(SELECTED_ICON);
-                }
-                return subMenu;
-            }
-        }
-        ImageDescriptor icon = selected ? SELECTED_ICON : null;
-        MenuManager subMenu = new MenuManager(categoryName, icon, null);
-        categoriesMap.put(categoryName, subMenu);
-        list.add(subMenu);
-        return subMenu;
-    }
-
     private static void addContributionItem(List<IContributionItem> list,
-            String traceBundle, String traceTypeId, String traceIcon,
-            String label, boolean selected,
-            MenuManager subMenu) {
-        Map<String, String> params;
-
-        params = new HashMap<>();
-        params.put(BUNDLE_PARAMETER, traceBundle);
+            String traceTypeId, String label, boolean selected, MenuManager subMenu) {
+        Map<String, String> params = new HashMap<>();
         params.put(TYPE_PARAMETER, traceTypeId);
-        params.put(ICON_PARAMETER, traceIcon);
 
         ImageDescriptor icon = null;
         if (selected) {
@@ -247,23 +176,12 @@ public class SelectElementTypeContributionItem extends CompoundContributionItem
         param.visibleEnabled = true;
 
         if (subMenu != null) {
-            subMenu.add(new CommandContributionItem(param));
+            CommandContributionItem item = new CommandContributionItem(param);
+            int i = Collections.binarySearch(Arrays.asList(subMenu.getItems()), item, ITEM_COMPARATOR);
+            i = (i >= 0) ? i : -i - 1;
+            subMenu.insert(i, item);
         } else {
             list.add(new CommandContributionItem(param));
         }
     }
-
-    private static boolean isSelectedCategory(String categoryId, IConfigurationElement[] config, Set<String> selectedTraceTypes) {
-        for (IConfigurationElement ce : config) {
-            if (ce.getName().equals(TmfTraceType.TYPE_ELEM)) {
-                String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
-                if (selectedTraceTypes.contains(traceTypeId)) {
-                    if (categoryId.equals(ce.getAttribute(TmfTraceType.CATEGORY_ATTR))) {
-                        return true;
-                    }
-                }
-            }
-        }
-        return false;
-    }
 }
This page took 0.031882 seconds and 5 git commands to generate.