Jump to: navigation, search

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

m (Category, Syntax Highlighting)
 
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>
+
<source lang="java">
 
   IWorkbench wb = PlatformUI.getWorkbench();
 
   IWorkbench wb = PlatformUI.getWorkbench();
 
   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  
+
  // on new versions it may need to be changed to:
 
   IWorkbenchPage page = win.getActivePage();
 
   IWorkbenchPage page = win.getActivePage();
</pre>
+
</source>
  
 
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 use 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 use these APIs to access the active window or page.
Line 14: Line 14:
 
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>
+
<source lang="java">
 
   IWorkbenchPage page = getSite().getPage();
 
   IWorkbenchPage page = getSite().getPage();
</pre>
+
</source>
  
 
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>
+
<source lang="java">
 
   class MyAction implements IWorkbenchWindowActionDelegate {
 
   class MyAction implements IWorkbenchWindowActionDelegate {
 
       private IWorkbenchWindow window;
 
       private IWorkbenchWindow window;
Line 28: Line 28:
 
       }
 
       }
 
   }
 
   }
</pre>
+
</source>
  
 
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.
 
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.
Line 34: Line 34:
 
<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]]
+
[[Category:FAQ]]

Latest revision as of 03:03, 30 April 2014

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.