Jump to: navigation, search

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

 
m
Line 1: Line 1:
''
+
Many workbench APIs are accessible only from <tt>IWorkbenchWindow</tt> or <tt>IWorkbenchPage</tt>. This generally raises the question, How do I get a reference to a window or a page? As it turns out, the answer isn&#146;t always straightforward  There appears to be an obvious API on  <tt>IWorkbench</tt> for getting this:
  
Many workbench APIs are accessible only from <tt>IWorkbenchWindow</tt>
 
or <tt>IWorkbenchPage</tt>. This generally raises the question, How
 
do I get a reference to a window or a page? As it turns out, the answer
 
isn&#146;t always straightforward  There appears to be an obvious API on
 
<tt>IWorkbench</tt> for getting this:
 
 
<pre>
 
<pre>
 
   IWorkbench wb = PlatformUI.getWorkbench();
 
   IWorkbench wb = PlatformUI.getWorkbench();
Line 11: Line 6:
 
   IWorkbenchPage page = win.getActiveWorkbenchPage();
 
   IWorkbenchPage page = win.getActiveWorkbenchPage();
 
</pre>
 
</pre>
However, if you read the fine print on these methods, you&#146;ll see
 
that they will return <tt>null</tt> 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 sue these APIs to access the active window or page.
 
  
 +
However, if you read the fine print on these methods, you&#146;ll see that they will return <tt>null</tt> 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 sue these APIs to access the active window or page.
 +
 +
To avoid getting <tt>null</tt> 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:
  
To avoid getting <tt>null</tt> 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:
 
 
<pre>
 
<pre>
 
   IWorkbenchPage page = getSite().getPage();
 
   IWorkbenchPage page = getSite().getPage();
 
</pre>
 
</pre>
From an action defined in a workbench action set, you can access
+
 
the window from the <tt>init</tt> method:
+
From an action defined in a workbench action set, you can access the window from the <tt>init</tt> method:
 +
 
 
<pre>
 
<pre>
 
   class MyAction implements IWorkbenchWindowActionDelegate {
 
   class MyAction implements IWorkbenchWindowActionDelegate {
Line 34: Line 27:
 
</pre>
 
</pre>
  
Similarly, actions contributed to the <tt>popupMenus</tt> extension
+
Similarly, actions contributed to the <tt>popupMenus</tt> extension point always have an initialization method that sets the current part before the action&#146;s <tt>run</tt> method is called.  All wizard extension points also have an <tt>IWorkbenchWizard init</tt> 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.
point always have an initialization method that sets the current part
+
before the action&#146;s <tt>run</tt> method is called.  All wizard extension
+
points also have an <tt>IWorkbenchWizard init</tt> 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.
+
  
 
<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>

Revision as of 21:17, 29 May 2006

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();

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 sue 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.