Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Web Tools Project (WTP) » How to capture perspective changes?
How to capture perspective changes? [message #662332] Wed, 30 March 2011 09:32 Go to next message
Dwight Johnson is currently offline Dwight Johnson
Messages: 19
Registered: March 2011
Junior Member
Hello,

I am working on a new perspective called MyPerspective, next to existing perspectives like Java, Debugger, CVS, etc.

Question: how can I intercept if the user clicks on the MyPerspective button (or selects the MyPerspective item in the Perspective dialog list), in other words: how can I intercept that MyPerspective gets activated?

Likewise: how can I intercept that MyPerspective gets deactivated?

I need to know this in order to be able to carry out file loading during both events.

Thanks in advance.
Re: How to capture perspective changes? [message #662350 is a reply to message #662332] Wed, 30 March 2011 10:37 Go to previous messageGo to next message
Dwight Johnson is currently offline Dwight Johnson
Messages: 19
Registered: March 2011
Junior Member
Solved problem myself.

Open plugin wizard, tab Extensions
Add org.eclipse.ui.startup and add class org.yourfirm.EarlyStartup

In the package org.yourfirm add class EarlyStartup;


package org.yourfirm;

import java.util.ArrayList;

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PerspectiveAdapter;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.Perspective;
import org.eclipse.ui.internal.WorkbenchPage;
import org.eclipse.ui.internal.registry.IActionSetDescriptor;

/**
 * This class is loaded when the workbench start up
 * 
 * @author empolis GmbH
 * @version %version: %
 * @since e:CLS 3.1
 * 
 *        Copyright (c) 2006 empolis GmbH
 */
@SuppressWarnings("restriction")
public class EarlyStartup implements IStartup {
    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IStartup#earlyStartup()
     */
    public void earlyStartup() {
        /*
         * The registration of the listener should have been done in the UI
         * thread since PlatformUI.getWorkbench().getActiveWorkbenchWindow()
         * returns null if it is called outside of the UI thread.
         */
        Display.getDefault().asyncExec(new Runnable() {
            /*
             * (non-Javadoc)
             * 
             * @see java.lang.Runnable#run()
             */
            public void run() {
                final IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                if (workbenchWindow != null) {
                    workbenchWindow.addPerspectiveListener(new PerspectiveAdapter() {
                        /*
                         * (non-Javadoc)
                         * 
                         * @seeorg.eclipse.ui.PerspectiveAdapter#
                         * perspectiveActivated (org.eclipse.ui.IWorkbenchPage,
                         * org.eclipse.ui.IPerspectiveDescriptor)
                         */
                        @Override
                        public void perspectiveActivated(IWorkbenchPage page,
                                IPerspectiveDescriptor perspectiveDescriptor) {
                            super.perspectiveActivated(page, perspectiveDescriptor);
                            System.err.println("xx--> " + perspectiveDescriptor.getId());
                        }
                    });
                }
            }
        });
    }
}
Re: How to capture perspective changes? [message #662352 is a reply to message #662350] Wed, 30 March 2011 10:50 Go to previous message
Dwight Johnson is currently offline Dwight Johnson
Messages: 19
Registered: March 2011
Junior Member
BTW: discovered that you can ommit the Earlystartup class altogether if you generated an Activator class during setup of the plugin using the plugin wizard.

To achieve the same result (capturing perspective change) implement the start method like this:

	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		System.out.println("start plugin");
		
                final IWorkbenchWindow workbenchWindow =   
                    PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                if (workbenchWindow != null) {
                    workbenchWindow.addPerspectiveListener(new     
                                                    PerspectiveAdapter() {
                /*
                 * (non-Javadoc)
                 * 
                 * @seeorg.eclipse.ui.PerspectiveAdapter#
                 * perspectiveActivated (org.eclipse.ui.IWorkbenchPage,
                 * org.eclipse.ui.IPerspectiveDescriptor)
                 */
                @Override
                public void perspectiveActivated(IWorkbenchPage page,
                        IPerspectiveDescriptor perspectiveDescriptor) {
                    super.perspectiveActivated(page, perspectiveDescriptor);
                    System.err.println("activator --> " + perspectiveDescriptor.getId());
                }
            });
        }

	}
Previous Topic:JS editor adaptions
Next Topic:xpath expression could not find function matches
Goto Forum:
  


Current Time: Thu Oct 23 13:59:05 GMT 2014

Powered by FUDForum. Page generated in 0.06072 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software