Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Re: What enables "Source > Toggle Comment" command/menu item?
Re: What enables "Source > Toggle Comment" command/menu item? [message #279613] Thu, 20 January 2005 16:42
Eclipse UserFriend
Originally posted by: mark.melvin.dspfactory.com

<cc'ing eclipse.platform for general interest>

Mark Melvin wrote:

> Daniel Megert wrote:
>
>> Mark Melvin wrote:
>>
>>> The reason I am asking is because I am trying to implement the same
>>> thing in my editor - using the JDT as a reference. I can't for the
>>> life of me get it to work. I have added all of the appropriate XML
>>> and "magic code" (man, this stuff is spread out over everywhere with
>>> no real information on how it works...), and I have successfully got
>>> my editor to show a context menu with a "Source > Toggle Comment"
>>> submenu - and that calls my ToggleCommentAction, and everything
>>> works. The hotkeys even work in the context of my editor.
>>>
>>> However, through all of this a new top-level menu was also added
>>> (called "Source") to Eclipse whenever my editor is active (I assume
>>> because of the <actionSet> in my XML). It also has a "Toggle
>>> Comment" menu item, but it is always disabled. I cannot see what
>>> enables the same menu item in the JDT. It is all a big black box
>>> because this action is "retargetable". I don't even know where to
>>> put a breakpoint to start looking, as everything I have found thus
>>> far seems to be for the editor context menu, not the top-level
>>> Eclipse menu item.
>>>
>>> So, please...<Dr. Evil voice> can someone throw me a frickin' bone
>>> here </Dr. Evil voice>...what the heck "enables" the "Source > Toggle
>>> Comment" menu item for the Java Editor?? I'm talking about the
>>> top-level "Source" menu in Eclipse when the Java editor is open, not
>>> the equivalent on the right-click menu.
>>
>>
>>
>> I'd look at JDT UI's ToggleCommentAction: look where/how it's created
>> and how it's used.
>>
>> Dani
>
>
> Thanks for the reply. Yes - that is exactly what I have been doing - I
> just can't see how it actually triggers the creation of the Toggle
> Comment entry in the top-level Source menu.
>
> I dug deeper last night and found myself in
> RetargetAction.partActivated(IWorkbenchPart part), which is called when
> the editor is opened. The last line of this method is:
>
> setActionHandler(bars.getGlobalActionHandler(getId()));
>
> For the JDT, bars.getGlobalActionHandler(getId()) returns the
> ToggleComment action. For my editor - it returns null, which is
> probably why the menu entry is disabled. ;o) This leads me to believe
> that I have an issue with how my action is registered. Which is weird
> because both the right-click context menu as well as the keyboard
> shortcuts work just fine.
>
> I'll keep looking.
> Thanks again,
> Mark.


OK, well I figured this out - it only took me 2.5 days... ;o)

As it turns out, the "retargetable" action adds a menu for itself in the
XML, but you need to hook it up to the actual action implementation
manually in the code. I thought I was doing this - but as it turns out
I was supplying the wrong id.

So, in short - I have a retargetable <action> defined in my plugin.xml
within an <actionSet>, and it has both a "definitionId" as well as an
"id". The "definitionId" is used later to hook it up to a <command> in
the XML, which then allows me to hook up keybindings to the <command>.

In my code, I need to create the actual implementation of the action
(ToggleCommentAction), and assign it an id, and a reference name. I did
this using the following code in my editor.createActions() method:

String actionName = "ToggleComment";
action= new ToggleCommentAction(
DefaultEditorMessages.getResourceBundle(),
"ToggleComment.", this); //$NON-NLS-1$
action.setActionDefinitionId("the.definitionId.from.plugin.xml ");
setAction(actionName , action); //$NON-NLS-1$
markAsStateDependentAction(actionName , true); //$NON-NLS-1$
configureToggleCommentAction();

Then, implementing "initializeKeyBindingScopes()" in my editor allows me
to use the shortcut keys and the action works. But I also want the
<action> to show up on the main Eclipse menu as declared in my XML.
So - also in the plugin.xml, I need to hook my editor to enable this
menu item when it is active. This is done by providing
contributorClass="my.EditorActionContributor" within the <editor> tag of
my editor extension. Then, in this action contributor, I was
registering a global action in setActiveEditorPart(), using the
following code:

public void setActiveEditor(IEditorPart part) {
super.setActiveEditor(part);
ITextEditor textEditor= null;
if (part instanceof ITextEditor)
textEditor= (ITextEditor) part;

IActionBars bars= getActionBars();
bars.setGlobalActionHandler("actionId.1",
getAction(textEditor, "actionId.2"));
}

The problem I had was, in the above code "actionId.1", should be the
"id" parameter from the <action> defined in the plugin.xml (I was
actually using the "definitionId", which was why it wasn't working), and
the value of "actionId.2" should be whatever name you registered the
actual handler with when you created the action in your editor (in the
createActions() method) - in my case "ToggleComment".

Anyway, it all works now. A little bit of black magic, but I'll know
for next time. I guess I may be better off not using retargetable
actions - it is very confusing, and there seems to be code and XML
sprinkled all over the place with no real docs on how to do this
properly. Hopefully this will help someone else.

Mark.
Previous Topic:Combine PreferencePage with org.eclipse.core.runtime.Preferences
Next Topic:Resource Navigator id
Goto Forum:
  


Current Time: Tue Jul 23 11:30:43 GMT 2024

Powered by FUDForum. Page generated in 0.03530 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top