Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

 
m (Category, Syntax Highlighting)
(5 intermediate revisions by 5 users not shown)
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>
+
<source lang="java">
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>
+
 
   IWorkbench wb = PlatformUI.getWorkbench();
 
   IWorkbench wb = PlatformUI.getWorkbench();
   IWorkbenchWindow = win = wb.getActiveWorkbenchWindow();
+
   IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
 
   IWorkbenchPage page = win.getActiveWorkbenchPage();
 
   IWorkbenchPage page = win.getActiveWorkbenchPage();
</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.
 
  
 +
  // on new versions it may need to be changed to:
 +
  IWorkbenchPage page = win.getActivePage();
 +
</source>
  
To avoid getting <tt>null</tt> windows and pages, you can get your hands on a window or a page in another way. From
+
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.
within the implementation of any view or editor, you can do the following:
+
 
<pre>
+
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:
 +
 
 +
<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 32: Line 28:
 
       }
 
       }
 
   }
 
   }
</pre>
+
</source>
  
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>
 +
 +
[[Category:FAQ]]

Revision as of 04: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&#146;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&#146;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&#146;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.

Back to the top