Blog, Trixi

RCP: Custom Perspective Switcher

If you are not satisfied with a standard Eclipse perspective switcher, you have a few options to customize perspective switching:

  • you can set PERSPECTIVE_BAR_EXTRAS and SHOW_OPEN_ON_PERSPECTIVE_BAR
  • you can write your own perspective switcher using toolbar
  • you can write custom perspective switcher and place it into a banner in top of rcp application window

This post is about the third option. The following text will tell you how to place your content into a banner in top of rcp application window.

Custom Perspective Switcher

First of all, hide the standard perspective switcher in ApplicationWorkbenchWindowAdvisor class:

public void preWindowOpen() {
    IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
    configurer.setShowPerspectiveBar(false);
}

Then override createWindowContents method in ApplicationWorkbenchWindowAdvisor class. Just call the method from parent WorkbenchWindowAdvisor and then do the magic trick: locate CBanner from top of the window and place your own content in it.

    @Override
    public void createWindowContents( Shell shell ) {
    	// call standard eclipse method creating window contents
        super.createWindowContents( shell );
        // find cbanner
        CBanner banner = findTopBanner( shell );

        // create custom perspective switcher
        MyPerspectiveSwitcher perspectiveSwitcher = new MyPerspectiveSwitcher( getWindowConfigurer().getWindow(), banner );
        perspectiveSwitcher.createControl( );
    }

    /**
     * Find CBanner among children of the given shell.
     */
    private CBanner findTopBanner( Shell shell ) {
        Control[] children = shell.getChildren();
        CBanner result = null;
        for ( Control child : children ) {
            if ( child instanceof CBanner ) {
                if ( result != null ) {
                    throw new IllegalStateException( "More than one CBanner." );
                }
                result = ( CBanner ) child;
            }
        }
        if ( result == null ) {
            throw new IllegalStateException( "No CBanner." );
        }
        return result;
    }

Example source code

Posted in programming


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>