Bug 78465 - [Perspectives] Provide public API to do perspective switching
Summary: [Perspectives] Provide public API to do perspective switching
Status: RESOLVED WORKSFORME
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.0.1   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Nick Edgar CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-11-11 19:51 EST by Alex Le CLA
Modified: 2004-11-12 13:39 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Le CLA 2004-11-11 19:51:36 EST
Ok, I found the solution that involves using two internal classes, 
WorkbenchPlugin and PerpectiveDescriptor.

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveRegistry;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.registry.PerspectiveDescriptor;

// Check the active window first
final IWorkbench workBench = PluginX.getInstance().getWorkbench();
Display display = workBench.getDisplay();

IWorkbenchPage[] pages = null;
IWorkbenchWindow window = workBench.getActiveWorkbenchWindow();
String rtPerspectiveId = "com.xyz.PerspectiveX";
    			   
// Get the perspective 'X's descriptor.
IPerspectiveRegistry reg = WorkbenchPlugin.getDefault().getPerspectiveRegistry
();
PerspectiveDescriptor rtPerspectiveDesc =(PerspectiveDescriptor) 
reg.findPerspectiveWithId(rtPerspectiveId);

// Now set it as the active perspective.
// Set the perspective
if (window != null)
{
   IWorkbenchPage page = window.getActivePage();
   page.setPerspective(rtPerspectiveDesc);
    			   	  
   // Do again
   window = workBench.getActiveWorkbenchWindow();
}

With the above and the below code, I should be able to find the 'ViewX' or any 
view in the perspective 'X'!

AL wrote:

> Thanks! The code gives me understanding the API calls to use...But it is 
> still not working for me...Here is my scenario...
> 
> I have two perspectives, 'X' and 'Y', respectively.  Perspective 'X' is 
> implemented in PluginX and likewise PluginY for perspective 'Y'.  I want 
>  to switch to perspective 'X' to get the view 'ViewX' in perspective 'X'.
> 
> Currently I am in a PluginY.  For some reason, I could not get the 
> IWorkbenchWindow of perspective X; that is, 'isFound' could not get set 
> to true.  I can switch back and forth between the perspectives manually, 
> though.
> 
> Here is the snippet of code.  All the calls in the below keep returning 
> info on perspective 'Y', because currently perspective 'Y' is active. 
> So, I need to make 'X' active.
> 
> // TESTING:        
> final IWorkbench workBench = PluginX.getInstance().getWorkbench();
> Display display = workBench.getDisplay();
> 
> // Make sure to run the UI thread.           
> display.asyncExec( new Runnable()
> {
>     public void run()
>     {
>        // We want to switch to the 'X' perpspective.
>        // Look for the plugin's window using the perspective
>        // id and switch to the perspective.
>        
>        // Check the active window first
>        IWorkbenchPage page = null;
>        IWorkbenchWindow window = workBench.getActiveWorkbenchWindow();
>        String perspectiveId = "com.xyz.PerspectiveX";
>            
>        boolean isFound = false;
>                
>        if (window != null)
>        {
>           page = window.getActivePage();
>           if (page != null)
>           {
>              IPerspectiveDescriptor perspectiveDescriptor =
> page.getPerspective();
>              if (perspectiveDescriptor != null && 
> perspectiveDescriptor.getId().equals(perspectiveId))
>              {
>             isFound = true;
>              }
>           }
>         }
>                
>         if (isFound == false)
>         {
>            // Let's look at *ALL* the windows!
>            // Then check all other windows
>            IWorkbenchWindow[] windows = 
> PlatformUI.getWorkbench().getWorkbenchWindows();
>            for (int i = 0; i < windows.length; i++)
>            {
>               window = windows[i];
>                        
>               // Duplicate code here--for testing purposes
>               if (window != null)
>               {
>                  page = window.getActivePage();
>                  if (page != null)
>                  {
>                     IPerspectiveDescriptor perspectiveDescriptor = 
> page.getPerspective();
>                     if (perspectiveDescriptor != null && 
> perspectiveDescriptor.getId().equals(perspectiveId))
>                     {
>                     isFound = true;
>                     }
>                  }
>                }
>             }
>          }
>                
>          if (isFound == true)
>          {
>             // Switch to the 'X' perspective
>             try
>         {
>                          
> window.getWorkbench().showPerspective(perspectiveId, window);
>                        
>             }
>             catch (Exception e)
>         {
>             System.err.println(e);
>             }       
>        
>             // Get the runtime view from the runtime perspective
>             RuntimeView view = 
> (RuntimeView)workBench.getWorkbenchWindows()[0].getActivePage().findView
("com.xyz.XView"); 
> 
>             if (view != null)
>                     view.refreshView();
>          }
>     }
> });
> 
> Darin Swanson wrote:
> 
>> The code for perspective switching that is performed for debug launching
>> lives in
>> org.eclipse.debug.internal.ui.launchConfigurations.PerspectiveManager.
>>
>> You could use that as your reference for implementation
>>
>> HTH
>> Darins
>>
>> "AL" <unbonnevie@yahoo.com> wrote in message
>> news:cn0j8l$me5$1@eclipse.org...
>>
>>> Hi,
>>>
>>> How does one switch perspective programmatically?  I have gotten this
>>> far...Please see below...
>>>
>>> IPerspectiveRegistry reg =
>>> WorkbenchPlugin.getDefault().getPerspectiveRegistry();
>>>
>>> PerspectiveDescriptor persDesc =(PerspectiveDescriptor)
>>> reg.findPerspectiveWithId("com.xyz.myPerspective");
>>>
>>> My goal is to get the perspective, then from there get a particular view
>>>  in that perspective.
>>>
>>> Thanks
>>>
>>>
>>
>>
>>
>
Comment 1 Nick Edgar CLA 2004-11-12 12:05:16 EST
You can obtain the perspective registry through API using
IWorkbench.getPerspectiveRegistry();
For example:
IWorkbench workbench = PlatformUI.getWorkbench();
IPerspectiveRegistry registry = workbench.getPerspectiveRegistry();

However, there is no API for querying the open perspective in a given workbench
page, and there should be.  This is the subject of bug 44874.
I'm assuming that's what you want here, so I'm closing this as a dup of that one.


*** This bug has been marked as a duplicate of 44874 ***
Comment 2 Nick Edgar CLA 2004-11-12 12:06:45 EST
Sorry, wrong bug.  I meant bug 74012.
Comment 3 Nick Edgar CLA 2004-11-12 12:06:54 EST

*** This bug has been marked as a duplicate of 74012 ***
Comment 4 Alex Le CLA 2004-11-12 13:24:46 EST
Per Comment #1

Sorry...It's not what wanted...What I am looking for is public API to *switch* 
from one perspective to next without importing the below internal classes, 
unless, of course, there is another way to do it without using the below.  
(Please see the complete code snippet from my original comment.)

import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.registry.PerspectiveDescriptor;

Of course, it would be nice *also* to have public API that queries the 
currently open perspectives.

Thanks.
Comment 5 Nick Edgar CLA 2004-11-12 13:39:19 EST
Your code below can be reworked using only API as follows:

IWorkbench workbench = PluginX.getInstance().getWorkbench();
String rtPerspectiveId = "com.xyz.PerspectiveX";
IPerspectiveRegistry reg = workbench.getPerspectiveRegistry();
IPerspectiveDescriptor rtPerspectiveDesc =
reg.findPerspectiveWithId(rtPerspectiveId);
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
if (window != null) {
   IWorkbenchPage page = window.getActivePage();
   page.setPerspective(rtPerspectiveDesc);
}