http://www.richclient2.eu/2006_08_29/creating-your-own-perspective-switcher-a-first-try/

Do you know the Perspective-Bar? - And have you ever tried to integrate the perspecitve-bar in your RCP? If yes, did you suceed?

From my experience, perspectives are really useful for representing blocks of funtionality, something like a workflow or a set of requirements that are bundled. The perspective-bar is a very nice feature, necessary in the JDT but has a great disadvantage: The lack of integrity.

In my opinion perspectives have different goals: On the one hand they have to provide different user-roles different presentations of an application and on the other hand they have to bundle different views that are responsible for a special set of functionality.

Unfortunately the Eclipse-Framework doesn’t provide the approach to use this nice bar for different user-roles. Neither it’s possible to contribute to this bar nor to filter or hide perspectives. This is confusing, because everywhere you contribute your items, action, views, etc. to the workbench, but not in this special case. You have to reimplement the perspective-bar if you want to provide special behavior.

The art or reimplementing

The easiest way is to declare some actions, registering a perspective-listener and contributing the actions, depending from your business logic to a coolbar of the workbench-window. But that’s ugly, isn’t it?

I want to have a big and colored perspective-switcher (like in Lotus Notes) without implementing a new Presentation using the org.eclipse.ui.themes Extension-Point. But that’s not so easy, because Eclipse doesn’t have a special “Area”, something like a Composite where I can provide something, that’s always present.

A first try

I just had an idea. Using a standalone-View that is in all perspectives on the same place could simulate a fully customizable perspective-bar. Although it’s not a satisfying solution it’s the first step of a better fully controllable perspective-switcher.

(The Example RCP with two Perspectives)

Download

Download the Perspective-Switcher Example as RCP (Source included - 9,3 Mbyte)
CVS-Checkout (more info)

2 Comments »

  1. Well this could be achieved if you create your own ApplicationWorkbenchWindowAdvisor like I did it to create logo for my RCP application, but you can use it for this task too. You can find what I mean if you look at http://birosoft.zexxo.net/demo/UpdateDemo.htm
    . ApplicationLogo class in my case is a simple composite.

    Source example:

    package com.birosoft.workday.platform.intro;

    import org.eclipse.core.runtime.Preferences;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.layout.FormAttachment;
    import org.eclipse.swt.layout.FormData;
    import org.eclipse.swt.layout.FormLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Control;
    import org.eclipse.swt.widgets.CoolBar;
    import org.eclipse.swt.widgets.Menu;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.ui.PlatformUI;
    import org.eclipse.ui.application.ActionBarAdvisor;
    import org.eclipse.ui.application.IActionBarConfigurer;
    import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
    import org.eclipse.ui.application.WorkbenchWindowAdvisor;
    import org.eclipse.ui.internal.WorkbenchWindow;
    import org.eclipse.ui.internal.progress.ProgressRegion;

    import com.birosoft.workday.platform.Activator;

    public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
    private Control logo;

    private Control toolbar;

    private Control page;

    private Control statusline;

    private ProgressRegion progressRegion;

    public ApplicationWorkbenchWindowAdvisor(
    IWorkbenchWindowConfigurer configurer) {
    super(configurer);
    }

    public ActionBarAdvisor createActionBarAdvisor(
    IActionBarConfigurer configurer) {
    return new ApplicationActionBarAdvisor(configurer);
    }

    public void preWindowOpen() {
    IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
    Preferences prefs = Activator.getDefault().getPluginPreferences();
    if (prefs.getInt(Application.APP_WIN_WIDTH) == 0
    || prefs.getInt(Application.APP_WIN_HEIGHT) == 0) {
    configurer.setInitialSize(new Point(800, 600));
    } else {
    int width = prefs.getInt(Application.APP_WIN_WIDTH);
    int height = prefs.getInt(Application.APP_WIN_HEIGHT);
    configurer.setInitialSize(new Point(width, height));
    }
    configurer.setShowCoolBar(true);
    configurer.setShowStatusLine(true);
    configurer.setShowProgressIndicator(true);
    configurer.setTitle(”Birosoft Workday Client - ”
    + Application.getCurrentClient().toString() + ” (”
    + Application.getUsername() + “)”);
    }

    public void postWindowOpen() {
    IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
    Preferences prefs = Activator.getDefault().getPluginPreferences();
    if (prefs.getInt(Application.APP_WIN_POS_X) == 0
    || prefs.getInt(Application.APP_WIN_POS_Y) == 0) {
    configurer.getWindow().getShell().setLocation(0, 0);
    } else {
    int x = prefs.getInt(Application.APP_WIN_POS_X);
    int y = prefs.getInt(Application.APP_WIN_POS_Y);
    configurer.getWindow().getShell().setLocation(x, y);
    }
    }

    @Override
    public void postWindowClose() {
    Preferences prefs = Activator.getDefault().getPluginPreferences();
    Point size = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
    .getShell().getSize();
    prefs.setValue(Application.APP_WIN_WIDTH, size.x);
    prefs.setValue(Application.APP_WIN_HEIGHT, size.y);
    Point position = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
    .getShell().getLocation();
    prefs.setValue(Application.APP_WIN_POS_X, position.x);
    prefs.setValue(Application.APP_WIN_POS_Y, position.y);
    super.postWindowClose();
    }

    public void createWindowContents(Shell shell) {
    IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
    Menu menu = configurer.createMenuBar();
    shell.setMenuBar(menu);
    FormLayout layout = new FormLayout();
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    shell.setLayout(layout);
    logo = new ApplicationLogo(
    getWindowConfigurer().getWindow().getShell(), SWT.NONE);
    Application.setApplicationLogo(((ApplicationLogo) logo));
    toolbar = configurer.createCoolBarControl(shell);
    ((CoolBar) toolbar).setLocked(true);
    page = configurer.createPageComposite(shell);
    statusline = configurer.createStatusLineControl(shell);
    createProgressIndicator(shell);

    // The layout method does the work of connecting the
    // controls together.
    layoutNormal();
    }

    private void layoutNormal() {
    // APPLOGO
    FormData data = new FormData();
    data.top = new FormAttachment(0, 0);
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    logo.setLayoutData(data);
    // TOOLBAR
    data = new FormData();
    data.top = new FormAttachment(logo, 5, SWT.BOTTOM);
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(page, 0, SWT.LEFT);
    toolbar.setLayoutData(data);
    // STATUS LINE
    data = new FormData();
    data.bottom = new FormAttachment(100, 0);
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    statusline.setLayoutData(data);
    // PAGE CONTENTS
    data = new FormData();
    data.top = new FormAttachment(logo, 5, SWT.BOTTOM);
    // data.left = new FormAttachment(toolbar, 0, SWT.RIGHT);
    data.left = new FormAttachment(0, 5);
    data.right = new FormAttachment(100, -5);
    data.bottom = new FormAttachment(statusline);
    page.setLayoutData(data);
    layout();
    }

    private void layout() {
    getWindowConfigurer().getWindow().getShell().layout(true);
    if (page != null) {
    ((Composite) page).layout(true);
    }
    }

    /**
    * Create the progress indicator for the receiver.
    * @param shell the parent shell
    */
    private void createProgressIndicator(Shell shell) {
    if (getWindowConfigurer().getShowProgressIndicator()) {
    WorkbenchWindow window = (WorkbenchWindow) getWindowConfigurer()
    .getWindow();
    progressRegion = new ProgressRegion();
    progressRegion.createContents(shell, window);
    }
    }

    }

    Comment by Mickey — 13. March 2007 @ 19:41

  2. Yes, you’re right.
    Thanks for your addition

    Comment by Tom Seidel — 13. March 2007 @ 19:58