Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Detecting active editor change in my plugin(Attempting to create plugin which performs action on editor gaining focus)
Detecting active editor change in my plugin [message #542092] Wed, 23 June 2010 16:24 Go to next message
Kathryn Huxtable is currently offline Kathryn Huxtable
Messages: 7
Registered: June 2010
Junior Member
I'm trying to develop a plugin which performs a very simple platform-specific action whenever the active editor changes. The user should not have to take any action to make this happen other than having the plugin installed and active.

I can see how to become active at startup. What I don't see is how to listen for changes in the active editor.

Is this done through iPartService?

If so, how do I retrieve the service in the IStartup.earlyStartup method?
Re: Detecting active editor change in my plugin [message #542121 is a reply to message #542092] Wed, 23 June 2010 19:16 Go to previous messageGo to next message
Eugene Ostroukhov is currently offline Eugene Ostroukhov
Messages: 11
Registered: July 2009
Junior Member
You can add a listener to workbench page (that you can retrieve page(s) from window(s) and you can get window(s) from the workbench).
You can obtain workbench by calling PlatformUI.getWorkbench()

In many cases you may want to review your requirements to make sure you need to use earlyStartup (I'm not even sure it isn't deprecated already).
Re: Detecting active editor change in my plugin [message #542133 is a reply to message #542121] Wed, 23 June 2010 21:46 Go to previous messageGo to next message
Kathryn Huxtable is currently offline Kathryn Huxtable
Messages: 7
Registered: June 2010
Junior Member
I had not understood the earlyStartup method to be deprecated, but just discouraged, which is a different thing. Sometimes one must do something that is discouraged because there's no other way to do it.

Is there an extension point to get notifications of pages becoming active? I didn't see one, but I haven't done much of this. (I normally do middleware plumbing and Swing UIs.)
Re: Detecting active editor change in my plugin [message #542276 is a reply to message #542133] Thu, 24 June 2010 08:23 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6850
Registered: July 2009
Location: Ottawa
Senior Member

If you need to use the early startup plugin (because you're not an RCP
app, but just eclipse) please make sure that it contains no UI elements
(actionSets, almost not commands/handlers, views, editors, etc). Also
don't forget that if you are touching UI services (in windows, pages,
views, etc) you need to create a UIJob in your IStartup class (please
don't use your plugin activator as your IStartup class)

You can register your IPartListener2 on the page in the workbench
windows returned from PlatformUI.getWorkbench(). Don't forget to add
an IWindowListener sh you can add new IPartListener2 to a new window if
it shows up.

The other alternative to using the part listeners/window listeners is to
use the IEvaluationService and register a property change listener on
org.eclipse.ui.ISources.ACTIVE_EDITOR_NAME. That tracks the active
editor for the whole eclipse application.

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/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Detecting active editor change in my plugin [message #542368 is a reply to message #542276] Thu, 24 June 2010 12:01 Go to previous messageGo to next message
Kathryn Huxtable is currently offline Kathryn Huxtable
Messages: 7
Registered: June 2010
Junior Member
Thanks! This is quite useful.

The plugin I'm working on is only useful on Mac OS X because no other OS needs this feature. I want to set the "icon" in the title bar of Eclipse (not the editor decoration) using the "setRepresentedFilename:" method in Cocoa. This allows external accessibility apps to get the directory associated with the currently active file.

So I'm getting the shell and from that the NSView and from that the NSWindow object. Then I'm setting its represented filename.

Does that count as containing a UI element? I'm not creating anything, just setting something in the top-level window for Eclipse.

Of *course* I'm not using my activator as my startup class. Wink

Thanks for the info on IEvaluationService. I'll check it out.
Re: Detecting active editor change in my plugin [message #542385 is a reply to message #542276] Thu, 24 June 2010 12:34 Go to previous messageGo to next message
Kathryn Huxtable is currently offline Kathryn Huxtable
Messages: 7
Registered: June 2010
Junior Member
How do I construct an expression to pass to addEvaluationListener, or am I misunderstanding something?
Re: Detecting active editor change in my plugin [message #542437 is a reply to message #542385] Thu, 24 June 2010 15:11 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6850
Registered: July 2009
Location: Ottawa
Senior Member

Kathryn Huxtable wrote:
> How do I construct an expression to pass to addEvaluationListener, or am
> I misunderstanding something?

Expression exp = new Expression() {
boolean rc = false;
@Override
public void collectExpressionInfo(ExpressionInfo info) {
info.addVariableNameAccess(ISources.ACTIVE_EDITOR_NAME);
}
@Override
public EvaluationResult evaluate(IEvaluationContext context) throws
CoreException {
rc = !rc;
return EvaluationResult.valueOf(rc);
}
};

Your IPropertyChangeListener can request the current state from the
IEvaluationService.

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/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Detecting active editor change in my plugin [message #542440 is a reply to message #542437] Thu, 24 June 2010 16:43 Go to previous messageGo to next message
Kathryn Huxtable is currently offline Kathryn Huxtable
Messages: 7
Registered: June 2010
Junior Member
Thanks!

Again, very helpful!

-K
Re: Detecting active editor change in my plugin [message #542448 is a reply to message #542440] Thu, 24 June 2010 17:34 Go to previous messageGo to next message
Kathryn Huxtable is currently offline Kathryn Huxtable
Messages: 7
Registered: June 2010
Junior Member
Okay, I've got this working. Is there any way to *not* use earlyStartup?

Is there some extension point where I can say, "run when active editor name changes"? Is there some way I can implement such a thing?

Is it worth it?

-K
Re: Detecting active editor change in my plugin [message #542594 is a reply to message #542448] Fri, 25 June 2010 07:52 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6850
Registered: July 2009
Location: Ottawa
Senior Member

Not really. In eclipse most contributions are UI contributions
(commands, actions, menu and tool items, views, wizards) so they will be
activated on a user action, and there's no need to activate them prior
to that.

For something that's trying to enhance eclipse on an editor open,
there's no hook unless it's attached to a view (so it doesn't need to
run until the view appears).

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/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Detecting active editor change in my plugin [message #542695 is a reply to message #542594] Fri, 25 June 2010 12:25 Go to previous message
Kathryn Huxtable is currently offline Kathryn Huxtable
Messages: 7
Registered: June 2010
Junior Member
Thanks again!

I think I'm done. A bit of polishing and finding one memory leak with NSString and then Mavenizing, creating a site and that's it.

Not that anyone will care, or someone would already have done this.
Previous Topic:Simple Feature Patch Question
Next Topic:Working with JNI using Eclipse in Linux (Ubuntu)
Goto Forum:
  


Current Time: Sun Apr 20 12:27:31 EDT 2014

Powered by FUDForum. Page generated in 0.02134 seconds