Free tutorials for Java, Eclipse and Web programming



13. Adding a status line

13.1. Setup Status line

Create a new RCP project "Statusline". Use the "Hello RCP" as the template.

Go into the ApplicationWorkbenchWindowAdvisor and change method preWindowOpen(). The relevant line in the coding is: “configurer.setShowStatusLine(true); “


				
package statusline;

import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

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

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

	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(400, 300));
		configurer.setShowStatusLine(true);
		configurer.setShowCoolBar(false);
		configurer.setShowMenuBar(false);
		configurer.setTitle("Status Line Example");
	}
}

			

If you run the application you should already see a status line. At this point the status line does not contain text.

13.2. Shared Status Line

The shared message area can be used by all parts of the application to write messages to this area.

The following will explain how you can add a status line to your application. It will also indicate how the status line can be filled from a view or editor.

Tip

As the whole RCP application has access to the information in the shared status line the information in the shared status line might be overwritten.

Lets now fill the status line. We can do this for example via the postWindowOpen() method in ApplicationWorkbenchWindowAdvisor. You have two options to add this method:

  • You can either just paste this method into the class.

  • Or use the menu "Source" -> Implement / Overwrite Methods and then select "postWindowOpen()" to create the method and then adjust it to your needs.


				
package statusline;

import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

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

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

	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(400, 300));
		configurer.setShowCoolBar(false);
		configurer.setShowStatusLine(true);
		configurer.setTitle("Hello RCP");
	}
	// This is the new method
	@Override
	public void postWindowOpen() {

		IStatusLineManager statusline = getWindowConfigurer()
				.getActionBarConfigurer().getStatusLineManager();
		statusline.setMessage(null, "Status line is ready");
	}
}

			

Run your application. You should now see the following.

Add a view with the ID "StatusLine.View1" to your application. You can access the status line, via the following.

				
IActionBars bars = getViewSite().getActionBars();
bars.getStatusLineManager().setMessage("Hello");

			

In our example we create a button which set the status line.

				
package statusline;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.part.ViewPart;

public class ViewPart1 extends ViewPart {

	boolean pressed = false;

	public ViewPart1() {
	}

	@Override
	public void createPartControl(Composite parent) {
		Button setStatusLine = new Button(parent, SWT.PUSH);
		setStatusLine.setText("Set Statusline ");
		setStatusLine.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				IActionBars bars = getViewSite().getActionBars();
				if (pressed) {
					bars.getStatusLineManager().setMessage(
							"I would like to say hello to you.");
				} else {
					bars.getStatusLineManager().setMessage(
							"Thank you for using me");
				}
				pressed = !pressed;

			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
	}

	@Override
	public void setFocus() {
	}

}

			

If you run it the result should look like the following.

Tip

From an editor you can access the status line via the following:

					
IEditorPart.getEditorSite().getActionBars();