RSS

Search Engine

Saturday, October 23, 2010

Parts, Toolbar and Menu

We will extend our minimal e4 application with parts, a menu and a toolbar. Remember that e4 does not make anymore a distinction between editors and views, both are parts.

9.1. Add a part to the application

To add a part to an Eclipse 4.0 application you add a model element to your application model "Application.e4xmi" and define the class which represents the part via the "URL" property of the model element.

Create the following class. Notice that the TodoOverview.java code does not have any dependency to the Eclipse / OSGi framework. Just a simple POJO. By using the @inject annotation you are telling the Eclipse framework to inject a composite into the view.

    
package de.vogella.e4.todo.parts;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.log.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class TodoOverview {

@Inject
private Logger logger;
@Inject
private IEclipseContext context;

// PostConstruct ensures that the dependency injection has been finished
// this this method is called

private final Composite parent;

// e4 will inject the composite "parent" into the view
@Inject
public TodoOverview(Composite parent) {
// the following is standard SWT Stuff
this.parent = parent;
}

@PostConstruct
public void buildUI() {
logger.info("Start building UI");
final GridLayout layout = new GridLayout(2, false);
parent.setLayout(layout);
createLable("e4 Release");
createText("Eclipse 4.0 SDK");
createLable("Purpose");
createText("Doing awesome stuff");

}

// Standard SWT Stuff
private void createLable(String text) {
Label label = new Label(parent, SWT.NONE);
label.setText(text);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalIndent = 20;
label.setLayoutData(gridData);
}

// Standard SWT Stuff
private void createText(String text) {
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
Text textField = new Text(parent, SWT.NONE);
textField.setText(text);
textField.setLayoutData(gridData);
}

}

To add this view to your application open the "Application.e4xmi" in the editor, switch to controls under Windows, select "Part" and press add and maintain the values displayed below.

The URI describes the location of your view. The first part of this URI is the plugin and the second one the package. Make sure you don't have a typo here.

Run again your application you it should look like the following.

9.2. Menu, Toolbar and command handler

To add a menu entry or a toolbar entry you have to follow the same approach as with the Part. You have to create the model elements and these model elements must point to classes if required. Create the class which will handle the command. The @Execute annotation tells the framework which method should be called if this class is used as a handler.

    
package de.vogella.e4.todo.handler;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.workbench.IWorkbench;

public class ExitHandler {
@Execute
public void execute(IWorkbench workbench) {
workbench.close();
}
}


Select "Application.e4xmi" and add a menu and a toolbar. To add a menu select the window and select the "Main Menu" attribute.

Add a menu with one "DirectMenuItem" and a toolbar and The result should look like the following screenshot.

Also add a trimbar.

    

















If you run the application it should look like.

9.3. Add a container

You can either use a sash or a stack to display other containers or parts. A sash will divide the space between its container elements will a stack will only show the select element on top. We will now add another part and use a sash to divide two stacks to show both parts.

Lets first add another part to our application. Add the following Java class.

    
package de.vogella.e4.todo.parts;
import javax.inject.Inject;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

public class TodoDetails {

private final Composite parent;

// e4 will inject the composite "parent" into the view
@Inject
public TodoDetails(Composite parent) {
// the following is standard SWT Stuff
this.parent = parent;

final GridLayout layout = new GridLayout(2, false);
parent.setLayout(layout);

createLable("Release");
Button button = new Button(parent, SWT.BORDER);
button.setText("Do nothing");
}

// Standard SWT Stuff
private void createLable(String text) {
Label label = new Label(parent, SWT.NONE);
label.setText(text);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalIndent = 20;
label.setLayoutData(gridData);
}


}

Use the e4 editor to add a "PartSashContainer". The model editor allow you to set the model properties. Set the "orientation" to horizontal. Add twice a "PartStack" to the sash container. Use drag and drop to assign the first part to the first stack and then add the second part to the second stack. The result should look like the following.

The resulting "Application.e4xmi" should look like:


Launch your application. It should look like:

0 comments:

Post a Comment