Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipse-dev] Using JDT's @Nullable/@NonNull within the Eclipse Platform

On 15-Jul-2015, at 8:03 AM, Susinda <susinda@xxxxxxxxx> wrote:
On Tue, Jul 14, 2015 at 9:35 PM, Brian de Alwis <briandealwis@xxxxxxxxx> wrote:
I’ve seen far too much code that uses the following broken idiom:

        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()…

and it needs to be stopped!
What is the alternative for this (i.e to get the activePage) 

There are circumstances under which there is no active workbench window.  You need to handle such situations.

On several (all?) platforms, users can click on toolbar items on inactive windows, and the corresponding actions/handlers will run before that window is made the active window.  So the appropriate workbench window (i.e., the window hosting that tool bar item) is different from the active workbench window.  PlatformUI.getWorkbench().getActiveWorkbenchWindow() will return the *wrong window*.

Most callers to PlatformUI.getWorkbench().getActiveWorkbenchWindow() actually have their workbench window available somewhere in their situational context:

  • Command handlers can use HandlerUtils#getActiveWorkbenchWindow() with their ExecutionEvent
  • Workbench Parts can use getSite().getWorkbenchWindow()
  • WorkbenchWindowControlContribution’s have a getWorkbenchWindow()

The appropriate workbench window is also available through the IEclipseContext (with Eclipse 4.x, all RCP and IDE apps are Eclipse 4 apps too), so you can use Dependency Injection to communicate through to other objects.

Brian.

Back to the top