Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Save handler when editing files not in workspace using eclipse default editor
Save handler when editing files not in workspace using eclipse default editor [message #325737] Tue, 26 February 2008 09:37 Go to next message
Allan Ramirez is currently offline Allan RamirezFriend
Messages: 14
Registered: July 2009
Junior Member
Hi,

Here's the code that would open an external file (meaning file that does
not exist in my workspace) and then edit it using eclipse default editor.


File fileToOpen = new File("external_file.xml");

if (fileToOpen.exists() && fileToOpen.isFile()) {
IFileStore fileStore =
EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
IWorkbenchPage page =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage();

try {
IDE.openEditorOnFileStore( page, fileStore );
} catch ( PartInitException e ) {
//Put your exception handler here if you wish to
}
} else {
//Do something if the file does not exist
}


I wanted to execute a code when the user saves the document by ctrl+s or
file->save or by pressing the save icon.

How should I do this?

I tried

editorPart.getEditorSite().getActionBars().setGlobalActionHa ndler(
ActionFactory.SAVE.getId(), myaction );

where myaction is an instance of a class that extends Action class and
override s the run method.

But then the run method will never be executed once the document is saved.

I also believe the resource change listener can't be of help either
because it's would only track down workspace resources.

What should I do? Please enlighten me gurus.
Re: Save handler when editing files not in workspace using eclipse default editor [message #325767 is a reply to message #325737] Tue, 26 February 2008 15:29 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

You could add an IExecutionListener to the ICommandService ... when the
save command is executed it fires events. The command id is
org.eclipse.ui.file.save

Check out some of the links in my sig for information on the command
framework.

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: Save handler when editing files not in workspace using eclipse default edito [message #325788 is a reply to message #325767] Wed, 27 February 2008 05:24 Go to previous messageGo to next message
Allan Ramirez is currently offline Allan RamirezFriend
Messages: 14
Registered: July 2009
Junior Member
Thanks Paul.

Adding IExecutionListener to the ICommandService worked. However, clicking
the save icon in the toolbar or clicking file->save menu,
IExecutionListener was not executed.

I read your articles but I still can't find how to fire the codes when i
click save icon in the toolbar or if i click file->save menu.



Paul Webster wrote:

> You could add an IExecutionListener to the ICommandService ... when the
> save command is executed it fires events. The command id is
> org.eclipse.ui.file.save

> Check out some of the links in my sig for information on the command
> framework.

> PW
Re: Save handler when editing files not in workspace using eclipse default edito [message #325789 is a reply to message #325788] Wed, 27 February 2008 06:06 Go to previous messageGo to next message
Allan Ramirez is currently offline Allan RamirezFriend
Messages: 14
Registered: July 2009
Junior Member
I also used IHandlerService.activateHandler but still same effect. It
works only when i pressed ctrl+s but won't work if i press the save in
toolbar on on file->save menu.
Re: Save handler when editing files not in workspace using eclipse default edito [message #325790 is a reply to message #325789] Wed, 27 February 2008 08:10 Go to previous messageGo to next message
Allan Ramirez is currently offline Allan RamirezFriend
Messages: 14
Registered: July 2009
Junior Member
I tried the codes below

<extension point="org.eclipse.ui.handlers">
<handler commandId="org.eclipse.ui.file.save"
class="org.ui.handler.SaveHandler">
<enabledWhen>
<with variable="activePartId">
<equals
value=" org.eclipse.wst.xml.ui.internal.tabletree.XMLMultiPageEditor Part " />
</with>
</enabledWhen>
<activeWhen>
<with variable="activePartId">
<equals
value=" org.eclipse.wst.xml.ui.internal.tabletree.XMLMultiPageEditor Part " />
</with>
</activeWhen>
</handler>
</extension>



But still SaveHandler will only be execute in Ctrl+S. Clicking save icon
or file->save won't execute my code.
Re: Save handler when editing files not in workspace using eclipse default editor [message #325793 is a reply to message #325767] Wed, 27 February 2008 10:53 Go to previous messageGo to next message
Francis Upton IV is currently offline Francis Upton IVFriend
Messages: 472
Registered: July 2009
Location: Oakland, CA
Senior Member
I didn't see in the ActionFactory that SAVE was hooked up to a command
like the other actions (in 3.4); might this explain Allan's
difficulties? And if that's the case, seems like a bug. I can help if
you want.

Paul Webster wrote:
> You could add an IExecutionListener to the ICommandService ... when the
> save command is executed it fires events. The command id is
> org.eclipse.ui.file.save
>
> Check out some of the links in my sig for information on the command
> framework.
>
> PW
>


Re: Save handler when editing files not in workspace using eclipse default edito [message #325800 is a reply to message #325788] Wed, 27 February 2008 13:59 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Allan Ramirez wrote:
> Thanks Paul.
> Adding IExecutionListener to the ICommandService worked. However,
> clicking the save icon in the toolbar or clicking file->save menu,
> IExecutionListener was not executed.
> I read your articles but I still can't find how to fire the codes when i
> click save icon in the toolbar or if i click file->save menu.

In 3.3 legacy actions (like SAVE) don't fire command execution events
.... only using the keybinding will.

In 3.4 we added extra legacy support so clicking the button should also
fire the same kind of events.

In 3.3 the other way around (in an RCP app, anyway) is to turn the
SaveAction into a handler and make Save in the menu and toolbar a
command contribution.

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: Save handler when editing files not in workspace using eclipse default edito [message #325819 is a reply to message #325800] Thu, 28 February 2008 06:24 Go to previous message
Allan Ramirez is currently offline Allan RamirezFriend
Messages: 14
Registered: July 2009
Junior Member
Thanks a lot Paul.


Paul Webster wrote:

> Allan Ramirez wrote:
>> Thanks Paul.
>> Adding IExecutionListener to the ICommandService worked. However,
>> clicking the save icon in the toolbar or clicking file->save menu,
>> IExecutionListener was not executed.
>> I read your articles but I still can't find how to fire the codes when i
>> click save icon in the toolbar or if i click file->save menu.

> In 3.3 legacy actions (like SAVE) don't fire command execution events
> .... only using the keybinding will.

> In 3.4 we added extra legacy support so clicking the button should also
> fire the same kind of events.

> In 3.3 the other way around (in an RCP app, anyway) is to turn the
> SaveAction into a handler and make Save in the menu and toolbar a
> command contribution.

> PW
Previous Topic:Java editor import/method collapsing
Next Topic:Adding actions to context menu
Goto Forum:
  


Current Time: Thu Dec 26 12:46:44 GMT 2024

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

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

Back to the top