Adding context to my view and binding key to this context - eclipse

My question is same as asked here. But the solution provided there not works for me.
I have activated the context as said in the answer by greg-449 in createPartControl of my view.
IContextService contextService = IContextService)getSite().getService(IContextService.class);
contextService.activateContext(myViewContextId);
When my view was activated I am getting below warning on eclipse console
!MESSAGE A conflict occurred for CTRL+F:
Binding(CTRL+F,
ParameterizedCommand(Command(myFindCmdId,Find,
,
Category(org.eclipse.core.commands.categories.autogenerated,Uncategorized,Commands that were either auto-generated or have no category,true),
org.eclipse.ui.internal.WorkbenchHandlerServiceHandler#f41266e,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
myViewContextId,,,system)
Binding(CTRL+F,
ParameterizedCommand(Command(org.eclipse.ui.edit.findReplace,Find and Replace,
Find and replace text,
Category(org.eclipse.ui.category.edit,Edit,null,true),
org.eclipse.ui.internal.WorkbenchHandlerServiceHandler#24ad92b0,
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
===Plugin.xml has ====
<extension
point="org.eclipse.ui.contexts">
<context
id=<myViewContextId>
name="abc">
</context>
</extension>
<extension
point="org.eclipse.ui.bindings">
<key
commandId=<myFindCmdId>
contextId=<myViewContextId>
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M1+F">
</key>
//some more key binding here
</extension>

To avoid the conflict message define you own key binding scheme using the org.eclipse.ui.bindings extension point and specify the standard org.eclipse.ui.defaultAcceleratorConfiguration as the parent id for the scheme. Put your key bindings in this new scheme.
Use:
org.eclipse.ui/KEY_CONFIGURATION_ID=schemeid
in your plugin_customization.ini to select your scheme as the default.
More here
Note: If your Ctrl+F is a Find command you should hook in to the existing Eclipse find/replace retargetable action rather than defining new commands and key bindings.
So for Find do not define any command, handler or key bindings. Instead in your ViewPart use
IActionBars bars = getViewSite().getActionBars();
bars.setGlobalActionHandler(ActionFactory.FIND.getId(), your find Action);

Try to add parentId="org.eclipse.ui.contexts.window" into your new context:
<extension
point="org.eclipse.ui.contexts">
<context
id=<myViewContextId>
name="abc">
parentId="org.eclipse.ui.contexts.window">
</context>
</extension>

Related

How to create a key binding for my own Eclipse plug-in

So here's the thing
I created a Eclipse plug-in(A plug-in with a view)
And now I just want to create a key binding to open the view
For example, some of the original views in Eclipse like "Problem" view can be opened and showed with the key combination of "Alt + Shift + Q"
So how should I create a key binding like "Ctrl + Space" to show my own view plug-in just like the original views?
I know this has something to do with the extension points, commands, actions and handlers but I'm still learning and can't figure this out.
It would be much appreciated if someone could give me some tips on how to do this
You use the org.eclipse.ui.bindings extension point to set up key bindings.
For opening a view you can use the existing open view command org.eclipse.ui.views.showView, so you just need something like:
<extension point="org.eclipse.ui.bindings">
<key
commandId="org.eclipse.ui.views.showView"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M2+M3+Q C">
<parameter
id="org.eclipse.ui.views.showView.viewId"
value="org.eclipse.ui.console.ConsoleView">
</parameter>
</key>
</extension>
This example is the standard binding for the Console view. You will need to use a different sequence and specify your view id in the value.

Select editor programmatically

I have written an Eclpse plugin that contains several editor types. For each editor a content type is associated. In some situations I have two possible editors for the same contenttype. I know that I can select the editor via "Open with". But I would like to decide automatically which editor should be opened.
Here is an example
In one plugins I have got the following extension point definitions:
<extension
point="org.eclipse.ui.editors">
<editor
class="de.dstg.adsplus.editors.program.MacroEditor"
contributorClass="de.dstg.adsplus.editors.core.ADSTextEditorActionContributor"
default="true"
icon="icons/macro.png"
id="de.dstg.adsplus.editors.editors.macroEditor"
name="ADS Macro Editor">
<contentTypeBinding
contentTypeId="de.dstg.adsplus.macro">
</contentTypeBinding>
</editor>
</extension
In another plugin I have this definiton:
<extension
point="org.eclipse.ui.editors">
<editor
class="de.dstg.adsplus.focusededitor.FocusedEditor"
contributorClass="de.dstg.adsplus.focusededitor.FocusedEditorContributor"
icon="icons/dveditor.png"
id="de.dstg.adsplus.focusededitor"
name="ADS Focused Editor">
<contentTypeBinding
contentTypeId="de.dstg.adsplus.macro">
</contentTypeBinding>
When I double click on a file with this content type I would like to decide programmatically which editor to open depending on the content of the file or the location where it is stored.
I have tried to find a method that I could implement but without success.
You could use two different content types both using the same file extensions and use a 'content type describer' to distinguish the files.
The content type describer is defined in the content type extension point:
<extension point="org.eclipse.core.contenttype.contentTypes">
<content-type id="ABC"
base-type="org.eclipse.core.runtime.xml"
file-extensions="a,b,c">
<describer class="org.eclipse.core.runtime.content.XMLRootElementContentDescriber2">
</describer>
</content-type>
The content describer class implements IContentDescriber or ITextContentDescriber. Eclipse calls the describe method to find out if the file is valid for the content type (and to get information about other attributes of the file).

mark file as active - eclipse plugin

i wrote a plugin for a eclipse that let user creating my own type of project , the template of the project have some configuration files and I want to give the user the option to chose which one will be active, it's mean which one i will use when i will compile the project, i add an action of this files that let the user set it as active, my question is if there is any way to mark it in the package explorer(maybe bold ar with some border) as active so when user look at this he can know which one is the active now
Thanks
You can use the decorator extension point org.eclipse.ui.decorators. Add this to your plugin.xml:
<extension
point="org.eclipse.ui.decorators">
<decorator
adaptable="true"
class="MyDecoratorClass"
id="some.id.for.decorator"
label="Active Build Configuration Decorator"
lightweight="true"
location="BOTTOM_RIGHT"
state="false">
<enablement>
<and>
<objectClass
name="org.eclipse.core.resources.IResource">
</objectClass>
<or>
<objectClass
name="org.eclipse.core.resources.IProject">
</objectClass>
<objectClass
name="org.eclipse.core.resources.IFile">
</objectClass>
</or>
</and>
</enablement>
</decorator>
</extension>
Provide implementation in your MyDecoratorClass (or whatever name you choose) to check your project's active config and either adding text, or images. Perhaps have something like this: []
Whenever a user makes a modification that requires you to update the decorators so that the latest changes can be decorated you can use this:
// the resource whose properties changed and needs to be re-decorated
IResource resource = ...;
IDecoratorManager manager = PlatformUI.getWorkbench().getDecoratorManager();
IBaseLabelProvider decorator = manager.getBaseLabelProvider("id.of.my.decorator");
if (decorator != null)
((ILabelProviderListener) manager).labelProviderChanged(new LabelProviderChangedEvent(decorator, resource));

Enable Action in navigator popup when nothing is selected

I have added an option in eclipse cnf as mentioned below:
<extension
point="org.eclipse.ui.popupMenus">
<objectContribution
adaptable="true"
objectClass="org.eclipse.core.resources.IProject"
nameFilter="*"
id="RemoteSync.contribution1">
<action
label="Enable RemoteSync"
class="remotesync.builder.ToggleNatureAction"
menubarPath="additions"
enablesFor="1"
id="RemoteSync.addRemoveNatureAction"
style="toggle">
</action>
</objectContribution>
Here enables for is set to 1. So it enables when 1 items is selected. But I want enable the action when nothing in the cnf is selected. What should be the value for enables for?
This can be obtained with: enablesFor="*"
You can know all possible values for 'enablesFor' and their meaning by hovering over the 'enablesFor' in the plugin editor in Eclipse.

Eclipse RCP menus & actions: Configure or code?

This is a general question but my current problem revolves around menu handling.
In a normal plugin with contributes menu actions you would configure ActionSets etc in the plugin.xml configuration. This is obviously sensible.
I am working on a RCP application (actually RAP) and I'm wondering if it's worth the effort to configure everything via plugin.xml. My plugin does not have to interact with an other unknown plugins so, theoretically, I have control. I can add menus and actions programmatically.
I have been trying to configure a menu which contains a submenu. I have tried defining ActionSets and linking one inside the other but without success. Some items need to be disabled depending on the user role.
I figure I could have coded the whole lot in a few minutes but I'm not sure if that fits with the eclipse 'ethos'.
What opinions are out there? The application will get fairly big so I'd like to get the approach right from the start. Perhaps someone can point me at an example for configuring a nested menu :-)
My opinion is that the plugin.xml implementation is the way to go.
My main two reasons for using this method:
It's really easy to reconfigure and reorganize the menus and buttons without writing java code.
Very clear hierarchical visualization of the menu trees.
Here is a code snippet that implements menus and submenus. In this example, they are added to the main menu.
You can paste this into your plugin.xml:
<extension
name="Main Menu Contributions"
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="menu:org.eclipse.ui.main.menu">
<menu
id="fileMenu"
label="File">
<command
commandId="org.eclipse.ui.file.exit"
label="Exit"
style="push">
</command>
</menu>
<menu
label="Edit">
<command
commandId="org.eclipse.ui.edit.selectAll"
label="Select All"
style="push">
</command>
<menu
label="Submenu">
<command
commandId="org.eclipse.ui.edit.selectAll"
label="Select All Submenu"
style="push">
</command>
<command
commandId="org.eclipse.ui.edit.delete"
label="Delete submenu"
style="push">
</command>
</menu>
</menu>
</menuContribution>
</extension>
For activating/deactivating a menu, you have to use Core Expressions to enable/disable command handlers. If a command doesn't have any active handlers attached, it will be disabled. So, the menu item that calls that command will also be disabled.
The following code snippets show how to create a button on the toolbar of a view and have it be enabled/disabled depending of a variable's value. Bare in mind that you will have to change some things in this code to make it work. Most of the changes are for reference names and class implementation.
Create the button in the toolbar (plugin.xml):
<extension
name="View Toolbar Contributions"
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="toolbar:myapp.views.MyView">
<command
commandId="myapp.commands.PauseSound"
icon=""
label="Pause Playback Sound"
style="push"
tooltip="Pause">
</command>
</menuContribution>
</extension>
Create the command (plugin.xml):
<extension
id="myapp.commands.PauseSound"
name="Pause sound command"
point="org.eclipse.ui.commands">
<command
id="myapp.commands.PauseSound"
name="Pause Sound">
</command>
</extension>
Create the command handler (plugin.xml):
<extension
point="org.eclipse.ui.handlers">
<handler
commandId="myapp.commands.PauseSound">
<activeWhen>
<with
variable="myapp.commands.sourceprovider.active">
<or>
<equals
value="PLAYING">
</equals>
<equals
value="PAUSED">
</equals>
</or>
</with>
</activeWhen>
<class
class="myapp.rcp.commands.toolbar.PausePlayback">
</class>
</handler>
</extension>
Create the state variable for the command (plugin.xml):
<extension
point="org.eclipse.ui.services">
<sourceProvider
provider="myapp.commands.sourceprovider.CommandState">
<variable
name="myapp.commands.sourceprovider.active"
priorityLevel="workbench">
</variable>
</sourceProvider>
</extension>
Implement the class that changes the variable's state:
public class CommandState extends AbstractSourceProvider {
public final static String STATE = "myapp.commands.sourceprovider.active";
public final static String STOPPED = "STOPPED";
public final static String PLAYING = "PLAYING";
public final static String PAUSED = "PAUSED";
public final static String NOT_LOADED = "NOT_LOADED";
enum State {
NOT_LOADED, PLAYING, PAUSED, STOPPED
};
private State curState = State.NOT_LOADED;
#Override
public void dispose() {
}
#Override
public String[] getProvidedSourceNames() {
return new String[] { STATE };
}
// You cannot return NULL
#SuppressWarnings("unchecked")
#Override
public Map getCurrentState() {
Map map = new HashMap(1);
if (curState == State.PLAYING)
map.put(STATE, PLAYING);
else if (curState == State.STOPPED)
map.put(STATE, STOPPED);
else if (curState == State.PAUSED)
map.put(STATE, PAUSED);
return map;
}
public void setPlaying() {
fireSourceChanged(ISources.WORKBENCH, STATE, PLAYING);
}
public void setPaused() {
fireSourceChanged(ISources.WORKBENCH, STATE, PAUSED);
}
public void setStopped() {
fireSourceChanged(ISources.WORKBENCH, STATE, STOPPED);
}
public void setNotLoaded() {
fireSourceChanged(ISources.WORKBENCH, STATE, NOT_LOADED);
}
}
More details on how to implement these features can be found at these locations:
Eclipse Commands Tutorial
Limiting Visibility of Commands
For Eclipse there are two different ways to contributing to the Workbench: Actions and Commands.
I definitely recommend the Commands as the newer and more advanced than Actions.
The drawbacks of Actions as specified here (1):
The UI and handling are always tied. There is no way you can separate each other
While Actions can be contributed to different parts of the workbench (popup menu/tool bar), all of them were different extension points and so you end up duplicating the XML in multiple places. The worst of it is that not all the extension points expect the same configuration.
Specifying Actions in multiple places is a maintenance nightmare. If you have to change the icon of an action, you need to change in all the places.
Another issue with duplicating Actions in plugin.xml is that multiple instance of the same Actions will be created in the memory
(1) Actions vs Commands
If you are writing the RCP application, the good practice is to create the place holders in your ActionBarAdvisor. While that would define the basic structure of your menus and toolbars, you can extend menuContributions and using commands to contribute the actual menu/tool items.
For adding actions in RCP you can also use ApplicationActinBarAcvisor.It is more easy than the above mentioned solution
In this u just have to first declare the action as an object of IWorkbenchAction, then in method "protected void makeActions(IWorkbenchWindow window)" u can register it.
And the last step is to add it into menu.
Following code will help u out.
1.First declare the action :-
private IWorkbenchAction newAction
2.Registering action :-
protected void makeActions(IWorkbenchWindow window) {
newAction = ActionFactory.NEW_WIZARD_DROP_DOWN.create(window);
register(newAction);
newAction.setText("New");
3.Last step is to add the action in menu:-
MenuManager filemenu = new MenuManager("&File", "file");
filemenu.add(newAction);
You can also add the action in toolbar as follows:-
protected void fillCoolBar(ICoolBarManager coolBar) {
IToolBarManager toolbar = new ToolBarManager(coolBar.getStyle());
coolBar.add(toolbar);
toolbar.add(newAction);