Enable context menu item for Java files - java

My Eclipse plugin defines menu items which are not enabled for the Java file selections but are enabled for other file formats. (.xml, .txt)
<plugin>
<extension
point="org.eclipse.ui.popupMenus">
<objectContribution
objectClass="org.eclipse.core.resources.IResource"
nameFilter="*"
id="test1.contribution1">
<menu
label="MY Plugin"
path="additions"
id="test1.menu1">
<separator
name="group1">
</separator>
</menu>
<action
label="Plugin Launcher"
class="plugin.model.ExecutePlugin"
menubarPath="test1.menu1/group1"
enablesFor="*"
id="test1.newAction">
</action>
</objectContribution>
</extension>
</plugin>
I want to enable my menu items for the .java files.

You probably need to specify the adaptable option:
<objectContribution
objectClass="org.eclipse.core.resources.IResource"
adaptable="true"
... >
Note: The org.eclipse.ui.popupMenus extension point is now deprecated, you should move away from using it.
Edit:
Specifying true for adaptable means that the system will use the IAdapterManager interface to check if the object adapts to the objectClass instead of requiring that the object implements the objectClass directly. This allows the view to use a different class for the actual view objects. The view code uses an IAdapterFactory to tell the adapter manager how to get the required class from the view object class.

Related

Add QuickAccess TextField to an eclipse RCP application

I have an eclipse RCP application (RCP version 4.12.0.v20190605-1801) using SWT. I want to add the eclipse QuickAccess TextField as search bar to my project like in the Java eclipse IDE (which is also available by pressing ctrl + 3).
I have scoured the documentation and the only thing I have found is the following:
#Override
protected void fillCoolBar(ICoolBarManager coolBar) {
// ToolBar File & Additions
IToolBarManager fileToolBar = new ToolBarManager(coolBar.getStyle());
fileToolBar.add(ActionFactory.SHOW_QUICK_ACCESS.create(window)); // window == class attribute
// Add some other stuff
}
This produces a QuickAccess button in my case, but no TextField. It works the same way if you click on it, but I would prefer to have the TextField as it is more clear to the user.
I only found threads on how to remove the TextField, but not how to add it, e.g. SO post here. So I guess it must be a somewhat built in feature.
If anyone's interested it is for the JCrypTool project:
GitHub link
File on GitHub containing the above function call for building the toolbar
Hope I didn't miss anything important, thanks for your help in advance.
I now settled on a slightly different solution.
I included the QuickAccess entry as Extension in my plugin.xml where I define my toolbar. Instead of an input text, it is now a button (depicting a magnifier icon). I took this approach because it is quite easy to implement using the RCP extension points.
This is a (reduced) version of what my plugin.xml looks like now. The relevant part is the extension point org.eclipse.ui.menus which calls org.eclipse.ui.window.quickAccess
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension id="application" point="org.eclipse.core.runtime.applications">
<application>
<run class="org.jcryptool.core.Application"/>
</application>
</extension>
<extension point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="toolbar:org.jcryptool.core.searchToolbar">
<command
commandId="org.eclipse.ui.window.quickAccess"
icon="icons/searchIcon.png"
style="push">
</command>
</menuContribution>
</extension>
<extension id="product" point="org.eclipse.core.runtime.products">
<product application="org.jcryptool.core.application" name="JCrypTool">
<property name="appName" value="JCrypTool"/>
<property name="cssTheme" value="org.jcryptool.themes.default"/>
</product>
</extension>
</plugin>
This looks like this:
For a really fancy implementation I guess you have to write some code for it. But as quite nice RCP solution it's ok for me.

Eclipse plugin, custom Run command

So, for learning purposes, I am writing an eclipse plugin which should take an already existing launch configuration, and rerun it with just some new VM - attributes.
Through the org.eclipse.ui.commands extension point i was able to create the command.
<extension point="org.eclipse.ui.commands">
<command
defaultHandler="launchconfigurator.LaunchConfiguratorCommandHandler"
id="launchconfigurator.toolbar.command"
name="JCCRun">
</command>
</extension>
Next I added the button to the toolbar :
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="toolbar:org.eclipse.ui.main.toolbar">
</menuContribution>
<menuContribution
locationURI="toolbar:org.eclipse.debug.ui.launchActionSet">
<command
commandId="launchconfigurator.toolbar.command"
icon="favicon_1_-3.png"
style="pulldown">
<visibleWhen
checkEnabled="true">
</visibleWhen>
</command>
</menuContribution>
At this point i have a button on my toolbar which shows me my button and has an arrow for a drop down menu. But when i click on the menue arrow, nothing happens...
What i want to have is exactly the same menu like the eclipse run or debug buttons have...
Does anyone know how i could aproach this?
I guess there should be something what i need to do with my plugin.xml to make eclipse see my button as a run button, but i am not sure what exactly does eclipse need...
Maybe there is some eclipse source code i could look at?
I even implemented own delegates and tab groups, which i didn't need for my execution but thought it would help... But , sadly, it didn't...
Thx in advance for your answer,
May the force be with you
The 'Run' button is defined using the old style org.eclipse.ui.actionSets extension point:
<action
id="org.eclipse.debug.internal.ui.actions.RunDropDownAction"
toolbarPath="org.eclipse.debug.ui.launchActionSet/debug"
hoverIcon="$nl$/icons/full/etool16/run_exc.png"
class="org.eclipse.debug.internal.ui.actions.RunToolbarAction"
disabledIcon="$nl$/icons/full/dtool16/run_exc.png"
icon="$nl$/icons/full/etool16/run_exc.png"
helpContextId="run_action_context"
label="%RunDropDownAction.label"
style="pulldown">
</action>
So the code that creates the Run dropdown menu is org.eclipse.debug.internal.ui.actions.RunToolbarAction. This is just a tiny class:
public class RunToolbarAction extends AbstractLaunchToolbarAction {
public RunToolbarAction() {
super(IDebugUIConstants.ID_RUN_LAUNCH_GROUP);
}
}
So this is using the more general class AbstractLaunchToolbarAction and specifying the launch group to be shown. You may be able to do something similar.

How to disable global(File-->Rename) menu in RCP application

I am stuck with this issue since yesterday. I want to disable (or hide) the default eclipse rename menu item under file contextual menu.
I was able to hide the one under right click menu using activities like:
<extension point="org.eclipse.ui.activities">
<activity id="rename.disable" name="Hidden activities">
<enabledWhen>
<not> <reference definitionId="DataEnginePlugin.testProjectNatureExtension"/></not>
</enabledWhen>
</activity>
<activityPatternBinding activityId="rename.disable" pattern="org.eclipse.ui.edit.rename"/>
</extension>
But, i am not able to disable the global one under File menu (F2).
Any ideas please!!
Thanks
The format of the pattern for the activityPatternBinding is 'contributing plugin id / item id'. Also the default pattern is a regular expression, you want an exact match here. So you want:
<activityPatternBinding
isEqualityPattern="true"
activityId="rename.disable"
pattern="org.eclipse.ui/org.eclipse.ui.edit.rename"/>

Java RCP/SWT - "Android Toast like" dialog in Eclipse RCP

Does anybody know if there exist some implementation of some popup window, something like in Android: TOAST ?
The notifications are part of Mylyn commons.
To integrate them, add the Mylyn Commons Notifications feature from http://download.eclipse.org/mylyn/releases/latest to your target platform definition. The relevant bundles are
org.eclipse.mylyn.commons.notifications.ui
org.eclipse.mylyn.commons.notifications.core.
You can add a category and an event to the notifications extension point like this:
</extension>
<extension
point="org.eclipse.mylyn.commons.notifications.ui.notifications">
<category
icon="icons/obj16/repository.gif"
id="myNotificationCategory"
label="My Category">
</category>
<event
categoryId="myNotificationCategory"
icon="icons/obj16/some-image.gif"
id="myEvent"
label="Hello World">
<defaultHandler
sinkId="org.eclipse.mylyn.commons.notifications.sink.Popup">
</defaultHandler>
<description>
This is the description of the event.
</description>
</event>
</extension>
To trigger a notification, use the NotificationService like this:
AbstractUiNotification notification = ...
NotificationsUi.getService().notify( asList( notification ) );
The notification must be a subclass of AbstractUiNotification where the eventId passed to the constructor must match the one from the extension.
The notifications plug-in also adds a preference page under General > Notifications that lets the user choose which notifications should be shown.
No, but you can use the plugin org.eclipse.mylyn.commons.ui, which contains interesting classes to display notification(s) at the bottom right of the screen.

Eclipse-Plugin with submenu. How to handle JavaProjects and Projects?

I want to write an Eclipse-plugin that performs an Action with a selected Project. I used the plugin Template with submenu. My plugin.xml looks like this :
<extension
point="org.eclipse.ui.popupMenus">
<objectContribution
objectClass="org.eclipse.core.internal.resources.Project"
id="testplugin2.contribution1">
<menu
label="Propertie Manager"
path="additions"
id="testplugin2.menu1">
<separator
name="group1">
</separator>
</menu>
<action
label="list all *.properties"
class="testplugin2.popup.actions.ListPropertiesAction"
menubarPath="testplugin2.menu1/group1"
enablesFor="1"
id="testplugin2.projectAction">
</action>
</objectContribution>
</extension>
this works fine for everything but javaProjects. It turns out that javaProjects are not Projects. I want this Action to appear when a javaProjects or a normal Projects is selected and not if something else is selected.
How can I make the submenu appeare exactly if a javaProject or a Project is selected?
I didn't test it, but maybe this works:
<objectcontribution ...>
<visibility>
<objectClass
name="org.eclipse.jdt.core.IJavaProject" />
</visibility>
</objectContribution>
You can also try "enablement" instead of "visibility".
eclipse help pages on popup menus
Make sure the adaptable property of your object contribution is set to true (it defaults to false):
adaptable="true"
#iain suggestion to target the interface is also good practice.
Just tried your example and the menus were showing as expected on a Java project.
Always bear in mind adpatability to org.eclipse.core.resources.IResource in general to ensure your menus, actions to be consistently displayed and enabled (whatever the explorer or actual object class being rendered).
Finally, beware that the org.eclipse.ui.popupMenus extension point is deprecated.
Though in my experience using it is faster and easier than the recommended org.eclipse.ui.commands, you may end up having a hard time migrating all your menus when it is removed (that is, if it is removed at some point).
Cheers,
You should not reference the internal class in you object class. You should use the public interface instead
objectClass="org.eclipse.core.internal.resources.Project"
Try
objectClass="org.eclipse.core.IProject"
I haven't tried this but IJavaProject should adapt to the IProject, so this should work for both.

Categories

Resources