Bug 296477 - [EditorMgmt] IWorkbenchPage#openEditors(IEditorInput[], ..) does not set keyboard focus
Summary: [EditorMgmt] IWorkbenchPage#openEditors(IEditorInput[], ..) does not set keyb...
Status: VERIFIED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.6   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 4.2.2   Edit
Assignee: Platform UI Triaged CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 178231 178229 389478
  Show dependency tree
 
Reported: 2009-11-30 09:32 EST by Dani Megert CLA
Modified: 2012-12-19 05:13 EST (History)
5 users (show)

See Also:


Attachments
Patch (1.30 KB, patch)
2009-12-01 15:51 EST, Oleg Besedin CLA
no flags Details | Diff
Patch doing full activation (3.99 KB, patch)
2009-12-03 15:00 EST, Oleg Besedin CLA
no flags Details | Diff
Here's a screen cap taken immediately after the open using the dialog (44.23 KB, image/png)
2012-09-11 11:03 EDT, Eric Moffatt CLA
no flags Details
Patch to move the part activation firing until after the focus is set (1.17 KB, patch)
2012-10-12 15:32 EDT, Eric Moffatt CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dani Megert CLA 2009-11-30 09:32:59 EST
org.eclipse.ui.ide from HEAD.

[EditorMgmt] IWorkbenchPage#openEditors(IEditorInput[], ..) does not set keyboard focus

Steps to reproduce:
1. in dev workspace modify 
   org.eclipse.ui.internal.ide.handlers.OpenResourceHandler to use the new API
2. start target workspace
3. in target workspace create project 'P' and file 'f', close editor
4. in target workspace use Ctrl+Shift+R to open 'f'
==> f is opened, editor seems active but keyboard focus is still in the Package Explorer
Comment 1 Oleg Besedin CLA 2009-11-30 17:02:42 EST
Hmm.. OK, now I agree, that API is cursed :-).

This seems to be a side effect of using deferUpdates() in the WorkbenchPage#openEditors(). Too bad that removing those calls causes extra editor activations.
Comment 2 Oleg Besedin CLA 2009-12-01 15:51:12 EST
Created attachment 153533 [details]
Patch

The source of the problem: in this code path we do editor activation while deferring updates. (Otherwise all editors will be activated.) In text editors, StyledText.parent.isVisible() == false when they gets focus set request. As a result, the StyledText widget ignores set focus request raised during the activation. 

The patch adds code to perform second request to set focus once deferred updates are processed. This is not the most elegant solution, but all other things I tried had some interesting side effects.
Comment 3 Oleg Besedin CLA 2009-12-01 15:55:19 EST
Patch applied to CVS Head.
Comment 4 Dani Megert CLA 2009-12-02 09:35:53 EST
Sorry Oleg to be a pain but the fix violates the IWorkbenchPage contract that says:

 *  <li>When a part is activated or gets focus:
 *    <ul>
 *		<li>call <code>part.setFocus()</code></li>
 *		<li>fire <code>partActivated</code> event to all listeners</li>
 *	  </ul>
 *   </li>

While calling setFocus() twice might not be the big deal (you never know), it's a problem that
- setFocus() is called again after partActivated() got called where it e.g. might set the focus on something different than in setFocus() and hence get broken
- the parent isn't visible yet and the focus not in the part when partActivated is called. This again can lead to unexepcted result.
Comment 5 Boris Bokowski CLA 2009-12-02 12:05:09 EST
sigh... it's never easy, is it?
Comment 6 Oleg Besedin CLA 2009-12-03 14:59:47 EST
(In reply to comment #4)
> Sorry Oleg to be a pain but the fix violates the IWorkbenchPage contract ...

(That's IWorkbenchPart.) That is debatable as workbench code does do everything outlined there on part activation. The Javadoc does not say "nothing else happens" :-).

To me, setting the focus again is the simplest change with the lowest risk of breaking anybody. It is similar to, say, what happend when a user switches from Eclipse to another app and then back to Eclipse. 

I guess I can do the whole proper activation request after deferred updates are processed - as opposing to simply setting the SWT focus.
Comment 7 Oleg Besedin CLA 2009-12-03 15:00:44 EST
Created attachment 153772 [details]
Patch doing full activation
Comment 8 Oleg Besedin CLA 2009-12-03 15:02:05 EST
Patch applied to CVS Head.
Comment 9 Oleg Besedin CLA 2009-12-08 10:38:18 EST
Verified in I20091207-1800.
Comment 10 Dani Megert CLA 2009-12-10 05:06:12 EST
>(That's IWorkbenchPart.) 
Yep of course.

I cannot see a difference with your latest patch. Calling setFocus() twice is not nice but I could live with it but that the widgets don't have the focus yet when partActivated(IWorkbenchPart) is called is not good.
Comment 11 Oleg Besedin CLA 2009-12-10 09:32:22 EST
(In reply to comment #10)
> I cannot see a difference with your latest patch.
The second patch is doing full activation as you have experessed a concern in the comment 4 that simply setting focus won't be good enough.

Could you provide a code snippet or an example in Eclipse where the functionality added in the "Patch doing full activation" is going to create a problem?
Comment 12 Dani Megert CLA 2009-12-10 09:44:05 EST
>Could you provide a code snippet or an example in Eclipse where the
>functionality added in the "Patch doing full activation" is going to create a
>problem?

Just imagine a listener with this code in partActivated(...):

if (myControl.isFocusControl())
  doSomethingImportant()

The important code will not be called with your fix. I don't have time to look through the SDK and poll clients whether such code exists. And as I've tried to explain before: the API contract in IWorkbenchPart says:

 *  <li>When a part is activated or gets focus:
 *    <ul>
 *        <li>call <code>part.setFocus()</code></li>
 *        <li>fire <code>partActivated</code> event to all listeners</li>
 *      </ul>
 *   </li>

Which clearly indicates that I can expect the part's main control to have focus because setFocus() specifies:

     * Asks this part to take focus within the workbench. Parts must
     * assign focus to one of the controls contained in the part's
     * parent composite.
Comment 13 Oleg Besedin CLA 2009-12-10 10:01:15 EST
(In reply to comment #12)
> Just imagine a listener with this code in partActivated(...):
> if (myControl.isFocusControl())
>   doSomethingImportant()

That code *will* be called because we do full activation after ensuring that tabs are properly positioned in the tab folder.
Comment 14 Dani Megert CLA 2009-12-10 10:21:54 EST
>That code *will* be called because we do full activation after ensuring that
>tabs are properly positioned in the tab folder.
On what tests is your strong "*will*" based? I guess the difference here is that I actually tested it while you assume it works ;-)

Here's how you can test it:

1. get 'org.eclipse.ui.workbench.texteditor' from CVS
2. add the follwing code to 
  AbstractTextEditor.ActivationListener.partActivated(IWorkbenchPart):
	if(fSourceViewer.getTextWidget().isFocusControl())
		System.out.println("xxx");
3. add a breakpoint to the line with System.out
4. repeat steps from comment 0 and observe: breakpoint is hit when opening the
   file normally but not when using Open Resource that uses the new API


NOTE:
>1. in dev workspace modify 
>   org.eclipse.ui.internal.ide.handlers.OpenResourceHandler to use the new API
This can be done by applying the patch I attached to bug 178229.
Comment 15 Dani Megert CLA 2012-09-08 03:48:36 EDT
The missing focus (see test case in comment 14) is still not fixed in 3.8 and 4.2.
Comment 16 Eric Moffatt CLA 2012-09-11 10:59:33 EDT
Dani, when I open a file using the resource dialog it appears to be setting the KB focus correctly in both 3.8 and 4.2...

1) Hack the OpenResourceHandler to use the multi-open API.
2) Create "P" and "f" as indicated
3) Close any open editors
4) Open 'f' using the OpenResource dialog

start typing...the editor gets the text

What am I missing ?
Comment 17 Eric Moffatt CLA 2012-09-11 11:03:57 EDT
Created attachment 220936 [details]
Here's a screen cap taken immediately after the open using the dialog


Do you have any preference mods that might be affecting this ?
Comment 18 Dani Megert CLA 2012-09-11 11:09:20 EDT
(In reply to comment #16)
> Dani, when I open a file using the resource dialog it appears to be setting
> the KB focus correctly in both 3.8 and 4.2...
> 
> 1) Hack the OpenResourceHandler to use the multi-open API.
> 2) Create "P" and "f" as indicated
> 3) Close any open editors
> 4) Open 'f' using the OpenResource dialog
> 
> start typing...the editor gets the text
> 
> What am I missing ?

You list different steps than those I give. The point is, that the focus is not there yet when it should (part activated ==> must have focus).

Simply try the given steps ;-).
Comment 19 Eric Moffatt CLA 2012-09-11 11:14:57 EDT
Perhaps I'm missing the point here. I just added the listener code and now see the effect you describe...very strange because the text editor does end up with the focus.
Comment 20 Dani Megert CLA 2012-09-11 11:15:41 EDT
Just to avoid any confusion: in 4.2 my test case will probably even fail on the old/current Open Resource scenario due to bug 375576 (I did not verify that).
Comment 21 Dani Megert CLA 2012-09-11 11:16:47 EDT
(In reply to comment #19)
> Perhaps I'm missing the point here. I just added the listener code and now
> see the effect you describe...very strange because the text editor does end
> up with the focus.

Whether it "looks" good and works in the end does not justify wrong behavior/state when the listeners are informed.
Comment 22 Eric Moffatt CLA 2012-10-12 10:43:53 EDT
The real question is whether there are differences in behavior depending on how the file is opened...

Double-click, right-click -> Open, F3, Open Resource, Open Type

If not then if there's an issue it doesn't really have anything to do with *this* defect does it ?
Comment 23 Dani Megert CLA 2012-10-12 11:18:18 EDT
(In reply to comment #22)
> The real question is whether there are differences in behavior depending on
> how the file is opened...
> 
> Double-click, right-click -> Open, F3, Open Resource, Open Type
> 
> If not then if there's an issue it doesn't really have anything to do with
> *this* defect does it ?

Sorry, I don't get what you try to say or avoid. Yes, at least in 3.8 there were differences compared to the specification/APIs. And you can simply try the steps from comment 14.
Comment 24 Eric Moffatt CLA 2012-10-12 15:26:14 EDT
Ooops, I didn't realize that this defect *is* specifically about the focus...thought is was the 'mementos' bug

I already have a fix ready for this in 4.x which I'll go over with Paul Monday...it's just a case of moving the 'firePartActive' out of the e4 listener and add it directly after we set the focus...

This will be in early next week and will fix all the cases I've been able to check so that we see 'xxx' however the part is opened.

On 3.x I'll (where it currently doesn't work) I'll see what I can do when implementing the new API...
Comment 25 Eric Moffatt CLA 2012-10-12 15:32:46 EDT
Created attachment 222248 [details]
Patch to move the part activation firing until after the focus is set
Comment 26 Eric Moffatt CLA 2012-10-15 13:17:08 EDT
http://git.eclipse.org/c/platform/eclipse.platform.ui.git/commit/?h=R4_2_maintenance&id=358512f39dc0be2f7af3ed06229c9aa4c9db5074

Moves the 'firePartActivated' call to a point immediately after the focus is set.

The snippet provided now exhibits the same behavior on both 3.8 and 4.x.
Comment 27 Dani Megert CLA 2012-10-17 07:09:43 EDT
(In reply to comment #26)
> http://git.eclipse.org/c/platform/eclipse.platform.ui.git/commit/
> ?h=R4_2_maintenance&id=358512f39dc0be2f7af3ed06229c9aa4c9db5074
> 
> Moves the 'firePartActivated' call to a point immediately after the focus is
> set.
> 
> The snippet provided now exhibits the same behavior on both 3.8 and 4.x.

Sorry if I can't follow here: The bug was reported against 3.6 and no fix was provided for 3.8.x yet (if I read the Git repo content and comment 24 correctly). In that case, it would be wrong if you get the same results for 3.8 and 4.2.2 when using the steps from comment 14.

Also, I can't see the fix in 'master' and hence this bug can't be marked as FIXED yet.
Comment 28 Paul Webster CLA 2012-12-13 18:49:38 EST
Eric, this fix should be in master now, can you confirm it's in M4?

PW
Comment 29 Dani Megert CLA 2012-12-19 05:13:33 EST
(In reply to comment #28)
> Eric, this fix should be in master now, can you confirm it's in M4?
> 
> PW

I verified that it is fixed in 'master' and 'R4_2_maintenance'.