Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize Eclipse menu items to remove redundant UI icons

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to customize Eclipse menu items to remove redundant UI icons. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The extension mechanism of Eclipse is an important feature, but as Eclipse becomes more and more powerful and there are more and more plug-ins, you will find that there are more and more icons on GUI, and Menu,toolbar,context menu is full. In fact, a lot of item is not what we need, but we need contribute plug-ins for item, so how do we get rid of the menu items that they expand?

Customize Eclipse menu items: customize in Plugin.xml

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

To expand the right-click menu, you need to extend the org.eclipse.ui.popupMenus extension point. We usually new an action under it, but this action extension will appear in any interface after it. What if we want to hide it under certain conditions? If you take a closer look at the org.eclipse.ui.popupMenus extension point, we can actually 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 there is a condition for this action to appear in popupmenu: we define a nameFilter for it, and it will be displayed only if the path of selection () contains "example", otherwise the action will not appear in popupmenu. The selection here assumes that a File is selected. If you select a class written by yourself, namefilter will look for keyword in the toString method of your class.

Customize Eclipse menu items. Using the Activities extension of Eclipse

Plugin.xml can't solve all the problems. When we really don't have a way to limit the emergence of some extension in plugin.xml, we can consider using Eclipse's official definition of Activities.Activities. You can google eclipse's help. My personal understanding is that it can control the display of UI like perspective, but Perspective is designed to be too easy to expand, if Plugin An extends a UI on perspective, then Plugin B must be able to see it every time it enters this perspective, and under Eclipse's extension mechanism, Plugin B has no right to delete Plugin A's contribution (Eclipse's ExtensionRegistry provides a removeExtension method, but an error will be reported at run time). In this case, the value of Activities is reflected, as long as you give it an Extension id, it can help you get rid of 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>

More important is the pattern attribute in activityPatternBinding, which is composed of plugin id + "/" + local-id. For example, if you extend org.eclipse.ui.newWizards,id to mywizard in the plug-in my.example, the above activityPatternBinding will disable the mywizard extension of my.example, and you won't see the wizard in GUI. Pattern supports regular expressions, so if there is a. You need to use the escape character. Note that the disable here does not mean that I deleted the mywizard extension, but blocked it. Mywizard is still in ExtensionRegistry.

Customize Eclipse menu items. Use code to dynamically control UI

Method 2 only hides some extensions, but there are some requirements that are not simply hidden. One of the requirements I encountered recently is: there is a flag, which is visible only when flag==1, otherwise it is invisible and requires the extension disable. At this point, you have to add some code to implement it, or take the mywizard in method 2 as an 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 enable or disable, when you define an Activity in plugin.xml, by default it is disable, that is to say, activityPatternBinding matching extension will be disable, but you can also set Activities to enable (in plugin.xml or code can be set), its matching extension can be used normally.

In the above code sample, we get all the Activities of enable through activityManager.getEnabledActivityIds (). If flag==1, then my.example.activity should also be added to enable Activities, so that mywizard can be displayed on the interface, otherwise, if you remove my.example.activity in enable Activities, it becomes disable and hides the mywizard.

Thank you for reading! On "how to customize Eclipse menu items to remove redundant UI icons" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report