自定义Editor的Command Handler无响应问题及plugin.xml配置咨询
Great question! The short answer is: Yes, you do need to configure menuContribution with the right locationURI and tie it to your command/editor, but there are a few other key pieces to get right too—let's walk through exactly what to do.
First, let's clarify the root issue: When you launch your custom editor via "Open With", Eclipse needs to know that your command handler should be active for this editor, and your command needs to be linked to the appropriate UI triggers (like right-click menus or toolbar buttons) for that editor context.
Step 1: Verify Your Command & Handler Configuration
First, make sure you've properly defined your command and its handler, with an activation condition that targets your ChartEditor specifically. If your handler isn't restricted to activate only when your editor is active, it won't trigger.
Here's how to set that up in your plugin.xml:
<!-- Define your command --> <extension point="org.eclipse.ui.commands"> <command id="your.plugin.id.chartEditorCommand" name="Do Something in Chart Editor"> </command> </extension> <!-- Bind the handler to the command, with activation condition --> <extension point="org.eclipse.ui.handlers"> <handler commandId="your.plugin.id.chartEditorCommand" class="launcher.ChartEditorCommandHandler"> <!-- Only activate this handler when ChartEditor is the active editor --> <activeWhen> <with variable="activeEditorId"> <equals value="launcher.ChartEditor"/> </with> </activeWhen> </handler> </extension>
Step 2: Add Menu Contribution with Correct LocationURI
Next, you need to bind your command to a UI location (like the editor's right-click menu) so it's accessible when using your ChartEditor. The locationURI tells Eclipse where to place your command in the menu structure.
For the editor's right-click context menu, use this configuration:
<extension point="org.eclipse.ui.menus"> <menuContribution locationURI="popup:org.eclipse.ui.popup.any?after=additions"> <command commandId="your.plugin.id.chartEditorCommand" label="Execute Chart Editor Command" style="push"> <!-- Only show this command when ChartEditor is active --> <visibleWhen> <with variable="activeEditorId"> <equals value="launcher.ChartEditor"/> </with> </visibleWhen> </command> </menuContribution> </extension>
- The
popup:org.eclipse.ui.popup.anylocation targets general editor context menus. If you've defined a custom context menu for your editor, replace this with your menu's ID (e.g.,popup:launcher.chartEditorContextMenu). - The
?after=additionsensures your command is placed in the standard "additions" section of the menu, which is the recommended spot for plugin contributions.
Step 3: Double-Check Your Editor Registration
You already have the org.eclipse.ui.editors extension, but make sure it's properly linked to the file types you want to open—this ensures "Open With" recognizes your editor correctly:
<extension point="org.eclipse.ui.editors"> <editor class="launcher.ChartEditor" default="false" id="launcher.ChartEditor" name="Chart Editor" fileExtensions="chart,dat"> <!-- Add your target file extensions here --> </editor> </extension>
Common Pitfalls to Avoid
- Misspelled IDs: Double-check that your editor ID, command ID, and handler class paths are all correctly spelled—even a tiny typo will break everything.
- Missing Activation/Visibility Conditions: If you skip the
<activeWhen>or<visibleWhen>blocks, your handler might never activate, or your command might show up in the wrong contexts. - Incorrect Editor Initialization: Ensure your
ChartEditorclass properly extendsEditorPartand callssuper.init(site, input)in itsinitmethod—this sets up the editor's context correctly so commands can bind to it.
Final Check
After updating your plugin.xml, clean and rebuild your plugin, then launch a new Eclipse runtime instance. Right-click your target file, select "Open With" → "Chart Editor", and your command should now trigger when you use its UI entry (like the right-click menu option).
内容的提问来源于stack exchange,提问作者luft

