Wednesday, September 3, 2008

Tutorial: Sample RCP application using Spring, Spring Dynamic Modules for OSGi Service Platforms and AgileRCP (3/3)

In the first two parts of the tutorial you have learned how to use Spring beans as OSGi services (see part 1 and part 2). You have used the ApplicationContextTracker of AgileRCP to access Spring's ApplicationContext. The third part of the tutorial will show you how to use Eclipse RCP Extensions in interaction with Spring beans. AgileRCP offers a convenient way to do this without the hassle of the direct use of the ApplicationContextTracker.
  1. Create a new interface for a user view:
    • New -> Interface
    • Name: IUserView
    • Extended Interfaces: org.agilercp.ui.IView, org.agilercp.ui.view.IWorkbenchPartView

  2. Add a method for setting a list of user names:
        void setUserNames(final List<String> names);
  3. The interface is not compile clean, because some packages are missing. Add the following packages to the list of imported packages (MANIFEST.MF, Dependencies tab):
    • org.agilercp.ui
    • org.agilercp.ui.dialog
    • org.agilercp.ui.view
    • org.agilercp.ui.workbench
    • org.agilercp.util.log

  4. Edit the view of the application (View.java):
    • The view extends DefaultViewParta (from AgileRCP) instead of ViewPart.
    • The view implements the interface IUserView.
    • The table viewer displays a list of user names.
    • This list will be set from outside.
    public class View extends DefaultViewPart implements IUserView {
    public static final String ID = "org.digitalcure.hellospring.app.view";

    private TableViewer tableViewer;

    @Override
    protected void doCreatePartControl(final Composite parent) {
    final Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout((new GridLayout(1, false)));
    composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    tableViewer = new TableViewer(composite, SWT.FULL_SELECTION | SWT.BORDER);
    tableViewer.getTable().setHeaderVisible(true);
    tableViewer.getTable().setLinesVisible(true);
    tableViewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final TableColumn firstNameColumn = new TableColumn(tableViewer.getTable(), SWT.NONE);
    firstNameColumn.setText("Name");
    firstNameColumn.setWidth(100);

    tableViewer.setContentProvider(new ArrayContentProvider());
    tableViewer.setLabelProvider(new UserTableLabelProvider());
    }

    @Override
    public void setFocus() {
    tableViewer.getTable().setFocus();
    }

    public void setUserNames(final List<String> names) {
    tableViewer.setInput(names);
    }

    private class UserTableLabelProvider extends LabelProvider implements ITableLabelProvider {
    public Image getColumnImage(final Object element, final int columnIndex) {
    return null;
    }

    public String getColumnText(final Object element, final int columnIndex) {
    if (!(element instanceof String)) {
    throw new IllegalArgumentException("The given object must be a string!");
    }

    return (String) element;
    }
    }
    }
  5. Edit the plugin.xml file:
    • Go to the Extensions tab, expand the node "org.eclipse.ui.views" and select "View (view)".
    • Change the class to: org.agilercp.ui.workbench.WorkbenchPartFactory

  6. The WorkbenchPartFactory implementation will create an instance of a Spring bean with the ID of the view (org.digitalcure.hellospring.app.view). The Spring bean has to implement the interface org.agilercp.ui.IPresenter. So now you have to create a new class for the presenter:
    • New -> Class
    • Name: UserPresenter
    • Superclass: org.agilercp.ui.DefaultPresenter

  7. You have to specify the type of the presented view:
    public class UserPresenter extends DefaultPresenter<IUserView> { ... }
  8. Add a member for the user manager to the user presenter class and a constructor that provides both the user manager and the user view:
        private final IUserManager userManager;

    public UserPresenter(final IUserView view, final IUserManager userManager) {
    super(view);
    this.userManager = userManager;
    }
  9. Override the handleViewCreated(...) method and initialize the view with the list of user names:
        @Override
    protected void handleViewCreated(final IUserView view) {
    super.handleViewCreated(view);
    getView().setUserNames(userManager.getUserNames());
    }
  10. Edit the Spring configuration file bundle-context.xml and add a Spring bean for the user presenter:
        <bean name="org.digitalcure.hellospring.app.view" class="org.digitalcure.hellospring.app.UserPresenter">
    <constructor-arg>
    <bean class="org.digitalcure.hellospring.app.View" />
    </constructor-arg>
    <constructor-arg ref="userManagerService" />
    </bean>
  11. The source code modifications are finished, so you have to modify the product configuration file "HelloSpringApp.product". Go to the Configuration tab and press "Add Required Plug-ins".

  12. Open the Run Configuration and press "Add Required Bundles".

  13. Press "Run" to run the application. The list inside the application window should display the user names now.
You may download the source code of the tutorial from here.

1 comment:

stefan222 said...

Added a new tutorial article about the export of this sample application here. Enjoy it!