Jump to: navigation, search

Difference between revisions of "FAQ How do I find the active workbench page?"

m (Typo fixed: sue -> use)
(One intermediate revision by one other user not shown)
Line 5: Line 5:
 
   IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
 
   IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
 
   IWorkbenchPage page = win.getActiveWorkbenchPage();
 
   IWorkbenchPage page = win.getActiveWorkbenchPage();
 +
 +
on new versions it may need to be changed to
 +
  IWorkbenchPage page = win.getActivePage();
 
</pre>
 
</pre>
  
Line 30: Line 33:
  
 
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
 
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
 +
 +
[[category:FAQs]]

Revision as of 04:47, 30 June 2011

Many workbench APIs are accessible only from IWorkbenchWindow or IWorkbenchPage. This generally raises the question, How do I get a reference to a window or a page? As it turns out, the answer isn’t always straightforward There appears to be an obvious API on IWorkbench for getting this:

   IWorkbench wb = PlatformUI.getWorkbench();
   IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
   IWorkbenchPage page = win.getActiveWorkbenchPage();

on new versions it may need to be changed to 
   IWorkbenchPage page = win.getActivePage();

However, if you read the fine print on these methods, you’ll see that they will return null if the active shell is not a window. This means that when a dialog or other shell has focus, you might not be able to use these APIs to access the active window or page.

To avoid getting null windows and pages, you can get your hands on a window or a page in another way. From within the implementation of any view or editor, you can do the following:

   IWorkbenchPage page = getSite().getPage();

From an action defined in a workbench action set, you can access the window from the init method:

   class MyAction implements IWorkbenchWindowActionDelegate {
      private IWorkbenchWindow window;
      ...
      public void init(IWorkbenchWindow win) {
         this.window = win;
      }
   }

Similarly, actions contributed to the popupMenus extension point always have an initialization method that sets the current part before the action’s run method is called. All wizard extension points also have an IWorkbenchWizard init method that supplies the wizard with the current workbench window before the wizard is launched. In short, if you look carefully, you can almost always get at the current window or page, no matter where you are in the Eclipse UI.


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.