Several ways to remove redundant UI components from eclipse

Source: Internet
Author: User
Tags extend

Eclipse's extension mechanism is an important feature, but as Eclipse features become more powerful, plug-ins are more and more, you will find GUI more and more icons, Menu,toolbar,context Menu is full, in fact, many of the item is not what we need, But we need to contribute the plugins of these items, so how do we get rid of the menus that they extend?

1. Custom-Made in Plugin.xml

This is the simplest way, many times we want to write code to remove some of the menu items, but the effect is not good. So we can customize it in the Plugin.xml, we try to write it in Plugin.xml. Here's an example of a right-click menu:

The extended right-click menu needs to extend the org.eclipse.ui.popupMenus extension point, and we typically have a new action under it, but this action extension will appear regardless of any interface, and what if we want to hide it under certain conditions? Take a closer look at the org.eclipse.ui.popupMenus extension point, but we can also create a new objectcontribution extension

<extension point="org.eclipse.ui.popupMenus">
    <objectContribution
             id="my.example.objectContribution"
             nameFilter="*example*"
             objectClass="java.io.File">
          <action
                class="my.example.MyAction"
                id="my.example.MyAction"
                label="Exe"
                menubarPath="additional">
          </action>
       </objectContribution>
     </extension>

Objectcontribution also contains an action, but the action appears conditional on PopupMenu: We define a namefilter for it, only if the path of selection () contains the " Example "will show, otherwise this action will not appear in the PopupMenu." Here the selection assumes that a file is selected, and if you are selecting a class that you write yourself, then Namefilter will be in your class tostring method inside find keyword.

2. Use Eclipse's activities extension

Plugin.xml does not solve all the problems, when we really have no way to limit the presence of certain extension in plugin.xml, we can consider using The official definition of Eclipse's activities.activities you can google the help of Eclipse. My personal understanding is that it can control the display of the UI like perspective, But perspective design is too easy to expand, and if plugin a UI on perspective, then plugin B must be able to see it every time it enters the perspective, and under the extended mechanism of Eclipse, Plugin B does not have the right to delete the contribution of Plugin A (Eclipse's Extensionregistry provides a removeextension method, but it runs with an error). In such cases, The value of activities is reflected, you just give it a extension ID, it can help you to the extension disable. For example:

<extension
          point="org.eclipse.ui.activities">
       <activity
             id="my.example.activity"
             name="WizardActivity">
       </activity>
       <activityPatternBinding
             activityId="my.example.activity"
             pattern="my\.example/mywizard">
       </activityPatternBinding>
  </extension>

What is more important is the pattern attribute in activitypatternbinding, which is composed of plugin ID + "/" + Local-id. For example, in the plug-in My.example extended Org.eclipse.ui.newwizards,id is Mywizard, then this activitypatternbinding will disable out my.example extension, you can't see this in GUI Wizard. Pattern is a support for regular expressions, so if there is a "." You need to use the escape character \. Note that the disable here does not mean that I have deleted mywizard this extension, but shielded it, Mywizard still in Extensionregistry.

3. Use code to dynamically control the UI

Method 2 is to hide some extensions, but some of the requirements are not simple to hide on it, I recently encountered a demand is: there is a flag, only when the flag==1 extension is visible, otherwise is not seen, You need to disable this extension. Then you have to add some code to implement it, or take the Mywizard in method 2 for example:

IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI.getWorkbench().getActivitySupport();
   IActivityManager activityManager = workbenchActivitySupport.getActivityManager();
   Set enabledActivityIds = new HashSet(activityManager.getEnabledActivityIds());
         if(flag==1)
   {
           if (enabledActivityIds.add("my.example.activity")) 
               workbenchActivitySupport.setEnabledActivityIds(enabledActivityIds);
   }
   else{
            if(enabledActivityIds.remove("my.example.activity"))
               workbenchActivitySupport.setEnabledActivityIds(enabledActivityIds);
   }

Activities can be either enable or disable, and when you define an activity in Plugin.xml, the default is disable, which means activitypatternbinding The matching extension will be disable, but you can also set the activities to enable (either in Plugin.xml or in code), and its matching extension can be used normally.

In the code sample above, we get all the enable activities through Activitymanager.getenabledactivityids (). If flag==1, The my.example.activity should also be added to the enable activities so that the Mywizard can be displayed on the interface, and vice versa, it is necessary to enable Activities Remove My.example.activity, it becomes

Disable, will hide Mywizard.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.