Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Pitfalls when command executes another command?
Pitfalls when command executes another command? [message #1725761] Mon, 07 March 2016 13:28 Go to next message
Andreas Sewe is currently offline Andreas SeweFriend
Messages: 111
Registered: June 2013
Senior Member
Hi,

I have a Handled Toolitem toolbar contribution. Previously, this toolitem was handled by org.eclipse.ui.browser.openBrowser, but now I need to not only open a browser, but also perform some other bookkeeping action. To avoid code duplication, I decided to implement my own command such that it executes org.eclipse.ui.browser.openBrowser:

@Execute
public void execute(ECommandService commandService, EHandlerService handlerService) {
  // do bookkeeping
  Map parameters = new HashMap();
  parameters.put("url", "http://www.example.org/");
  Command command = commandService.createCommand("org.eclipse.ui.browser.openBrowser", parameters);
  handlerService.executeHandler(command);
}

Unfortunately, executing the command like this changes behavior: Where before the browser was opened in an editor, now it opens a new window (of Firefox, in my case).

I suspect this is because the IEclipseContext, in which the org.eclipse.ui.browser.openBrowser command handler is looked up changes. But injecting the IEclipseContext into my custom command and then using

handlerService.executeHandler(command, eclipseContext);

doesn't do the trick either.

So, how do I execute a command from within another command in the same context (or at least in a context so similar that the behavior of openBrowser doesn't change)?
Re: Pitfalls when command executes another command? [message #1725778 is a reply to message #1725761] Mon, 07 March 2016 15:10 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 853
Registered: July 2009
Senior Member
I would suggest to debug into:

org.eclipse.ui.internal.browser.OpenBrowserHandler.execute(ExecutionEvent)


The handler may grab the wrong browser support (internal vs. external) implementation.

Andreas Sewe wrote on Mon, 07 March 2016 14:28
Hi,

I have a Handled Toolitem toolbar contribution. Previously, this toolitem was handled by org.eclipse.ui.browser.openBrowser, but now I need to not only open a browser, but also perform some other bookkeeping action. To avoid code duplication, I decided to implement my own command such that it executes org.eclipse.ui.browser.openBrowser:

@Execute
public void execute(ECommandService commandService, EHandlerService handlerService) {
  // do bookkeeping
  Map parameters = new HashMap();
  parameters.put("url", "http://www.example.org/");
  Command command = commandService.createCommand("org.eclipse.ui.browser.openBrowser", parameters);
  handlerService.executeHandler(command);
}

Unfortunately, executing the command like this changes behavior: Where before the browser was opened in an editor, now it opens a new window (of Firefox, in my case).

I suspect this is because the IEclipseContext, in which the org.eclipse.ui.browser.openBrowser command handler is looked up changes. But injecting the IEclipseContext into my custom command and then using

handlerService.executeHandler(command, eclipseContext);

doesn't do the trick either.

So, how do I execute a command from within another command in the same context (or at least in a context so similar that the behavior of openBrowser doesn't change)?
Re: Pitfalls when command executes another command? [message #1725779 is a reply to message #1725778] Mon, 07 March 2016 15:12 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 853
Registered: July 2009
Senior Member
There is also a preference page that may be of relevance: General | Web Browser
Re: Pitfalls when command executes another command? [message #1725800 is a reply to message #1725779] Mon, 07 March 2016 17:38 Go to previous messageGo to next message
Andreas Sewe is currently offline Andreas SeweFriend
Messages: 111
Registered: June 2013
Senior Member
Thanks for the pointer.

I know about OpenBrowserHandler and the General > Web Browser preference page, but reviewing that I noticed something odd: as soon as my command executes (and opens an external browser), the Use internal web browser radio button is disabled. Apparently, my command breaks something.

Digging a bit further I found this message on the console:

!ENTRY org.eclipse.ui.browser 2 0 2016-03-07 18:31:38.853
!MESSAGE Internal browser is not available: Invalid thread access

It looks like that a command executed from within another command does not run on the UI thread. The "outer" command runs on the main thread, the "inner" on (in my case) "Worker-9".

Is this to be expected? And is there any way to avoid this?
Re: Pitfalls when command executes another command? [message #1725809 is a reply to message #1725800] Mon, 07 March 2016 20:06 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 853
Registered: July 2009
Senior Member
Are you sure the trigger of your outer handler is called within the UI thread? Does not look like.

Andreas Sewe wrote on Mon, 07 March 2016 18:38
Thanks for the pointer.

I know about OpenBrowserHandler and the General > Web Browser preference page, but reviewing that I noticed something odd: as soon as my command executes (and opens an external browser), the Use internal web browser radio button is disabled. Apparently, my command breaks something.

Digging a bit further I found this message on the console:

!ENTRY org.eclipse.ui.browser 2 0 2016-03-07 18:31:38.853
!MESSAGE Internal browser is not available: Invalid thread access

It looks like that a command executed from within another command does not run on the UI thread. The "outer" command runs on the main thread, the "inner" on (in my case) "Worker-9".

Is this to be expected? And is there any way to avoid this?

Re: Pitfalls when command executes another command? [message #1725878 is a reply to message #1725809] Tue, 08 March 2016 09:00 Go to previous messageGo to next message
Andreas Sewe is currently offline Andreas SeweFriend
Messages: 111
Registered: June 2013
Senior Member
Quote:
Are you sure the trigger of your outer handler is called within the UI thread? Does not look like.

Yes, the outer handler is triggered, using EHandlerService.executeHandler, on the UI thread. It's @Execute method also runs on the UI thread. And here's the thing: The outer handler spawns a job, running on some worker thread, which then uses EHandlerService.executeHandler to trigger the inner handler (OpenBrowserHandler). As that inner handler also runs on the worker thread, it fails to open the internal browser.

This gives rise to two questions:

  1. Do I have to ensure that EHandlerService.executeHandler is executed on the UI thread myself?
  2. What assumptions can an @Execute method make about the thread it's running on? In particular, shouldn't OpenBrowserHandler be able to execute on arbitrary threads?

Re: Pitfalls when command executes another command? [message #1728925 is a reply to message #1725878] Fri, 08 April 2016 13:33 Go to previous messageGo to next message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 877
Registered: January 2012
Senior Member

Coming to this late. The handler service does not shunt requests onto the UI thread. Although most commands are related to UI features, the command/handler framework can be used for headless or background purposes.

Your @Execute shouldn't make any assumptions of threadedness.

Brian.
Re: Pitfalls when command executes another command? [message #1728931 is a reply to message #1728925] Fri, 08 April 2016 13:48 Go to previous messageGo to next message
Andreas Sewe is currently offline Andreas SeweFriend
Messages: 111
Registered: June 2013
Senior Member
Quote:
Coming to this late. The handler service does not shunt requests onto the UI thread. Although most commands are related to UI features, the command/handler framework can be used for headless or background purposes.

Your @Execute shouldn't make any assumptions of threadedness.

Makes sense. If my handler wants to do something on the UI thread, there's always UISynchronize.

In light of this, shouldn't the platform's handler for org.eclipse.ui.browser.openBrowser also be able to handle being executed on a non-UI thread? (Currently it is not; I have to call HandlerService.executeHandler on the UI thread already.) If so, should I open a bug?
Re: Pitfalls when command executes another command? [message #1728970 is a reply to message #1728931] Fri, 08 April 2016 17:31 Go to previous messageGo to next message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 877
Registered: January 2012
Senior Member

High quality patches are always welcome Smile
Re: Pitfalls when command executes another command? [message #1729135 is a reply to message #1728970] Mon, 11 April 2016 12:27 Go to previous messageGo to next message
Andreas Sewe is currently offline Andreas SeweFriend
Messages: 111
Registered: June 2013
Senior Member
Brian de Alwis wrote on Fri, 08 April 2016 13:31
High quality patches are always welcome Smile

Opened Bug 491425. First draft of patch will be pushed to Gerrit soon. Let's discuss its quality there, then.
Re: Pitfalls when command executes another command? [message #1821728 is a reply to message #1725761] Wed, 19 February 2020 08:48 Go to previous message
Horros Dito is currently offline Horros DitoFriend
Messages: 1
Registered: February 2020
Junior Member
Andreas Sewe wrote on Mon, 07 March 2016 13:28
Hi,

I have a Handled Toolitem toolbar contribution. Previously, this toolitem was handled by org.eclipse.ui.browser.openBrowser, but now I need to not only open a browser, but also perform some other action. To avoid code duplication, I decided to implement my own command such that it executes org.eclipse.ui.browser.openBrowser:

@Execute
public void execute(ECommandService commandService, EHandlerService handlerService) {
  // do bookkeeping
  Map parameters = new HashMap();
  parameters.put("url",  
  Command command = commandService.createCommand("org.eclipse.ui.browser.openBrowser", parameters);
  handlerService.executeHandler(command);
}

Unfortunately, executing the command like this changes behavior: Where before the browser was opened in an editor, now it opens a new window (of Firefox, in my case).

I suspect this is because the IEclipseContext, in which the org.eclipse.ui.browser.openBrowser command handler is looked up changes. But injecting the IEclipseContext into my custom command and then using

handlerService.executeHandler(command, eclipseContext);

doesn't do the trick either.

So, how do I execute a command from within another command in the same context (or at least in a context so similar that the behavior of openBrowser doesn't change)?


how did you solve the issue?
Previous Topic:Is there a way to detect when Eclipse finished the workspace upgrade?
Next Topic:Open file from command line (Pure E4 RCP)
Goto Forum:
  


Current Time: Fri Apr 16 12:34:24 GMT 2021

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

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

Back to the top