11

I am trying to override the Eclipse File > Save menu action to add some functionality. I have tried the following things

a) Create a new action and add it to the global action handler

actionBars.setGlobalActionHandler(ActionFactory.SAVE.getId(), mySaveAction); actionRegistry.registerAction(action);

b) Create a new handler and override the save command

<extension point="org.eclipse.ui.handlers">
<handler commandId="org.eclipse.ui.file.save"
  class="com.diagrams.ui.SaveFileHandler">
<enabledWhen> 
  <with variable="activePartId">
  <equals 
    value="com.diagrams.editors.MultiPageEditor" />
  </with>
</enabledWhen>
<activeWhen>
  <with variable="activePartId">
  <equals 
  value="com.diagrams.editors.MultiPageEditor" />
  </with>
</activeWhen>
</handler>
</extension> 

With both these approaches I have been able to override the Keyboard Ctrl+S functionality but the "File > Save" menu seem to work differently.

Would really appreciate any help, Thanks

Vineet
  • 1,028
  • 1
  • 9
  • 22
Abhishek Rakshit
  • 691
  • 7
  • 12

2 Answers2

8

In an RCP application, you can contribute the Save action in your ActionBarAdvisor. This also registers the action so it is available from the save command.

But as a plugin in the Eclipse IDE, the IDE provides the ActionBarAdvisor and hooks up the Save action in the File menu. Because that's not technically a command (Actions are a step above an SWT.Selection listener) that's why you can't override the File>Save action.

However, each part provides its own save implementation, so you can do whatever you want in your MultiPageEditor.

The other option is to use org.eclipse.ui.commands.ICommandService.addExecutionListener(IExecutionListener) and add an IExecutionListener (or IEL2). That can listen for the save command, the ID is declared in org.eclipse.ui.IWorkbenchCommandConstants.

Paul Webster
  • 10,614
  • 1
  • 25
  • 32
  • Thanks for the clarification Paul. My main motive for the query was that I needed to differentiate the save being called from the File > Save and when called due to a file being closed. I am for now able to accomplish my task why overriding the onSave and isSaveNeededOnClose methods in the editor. But I will keep in mind the listener approach if needed in the future. – Abhishek Rakshit May 06 '11 at 18:35
  • Is this still true? It doesn't seem to work for me at all (on Indigo) :( – snim2 Apr 20 '12 at 20:08
  • yes, it works on Indigo (although not currently in Juno, that's coming) – Paul Webster Apr 24 '12 at 13:16
0

It might have something to do with the activePartId being different when the main menu is selected versus a keystroke or using the right-click menu. Have you looked at other extension points?

Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
  • From what I have figured the activePartId should be the same. Moreover I have other actions(which I have created) which show up in the File menu for the same activePartId. I looked at the command and handler extension points and this looked as the correct way to go. Is there another extension point I should be using? – Abhishek Rakshit May 05 '11 at 17:18