Views

A view is a workbench part that can navigate a hierarchy of information or display properties for an object. Only one instance of any given view is open in a workbench page. When the user makes selections or other changes in a view, those changes are immediately reflected in the workbench.

The extension point org.jlense.uiworks.views allows plug-ins to add views to the workbench. Plug-ins that contribute a view must register the view in their plugin.xml file and provide configuration information about the view, such as its implementation class, the category (or group) of views to which it belongs, and the name and icon that should be used to describe the view in menus and labels.

The interface for views is defined in IViewPart, but plug-ins can choose to extend the OutlookPart class or the ViewPart class rather than implement an IViewPart from scratch.

We implemented a minimal view extension in the hello world example. Now we'll look at a more sophisticated view that informs the workbench of selection changes. First, let's take a look at the declaration of the extension in the plugin.xml.

<extension point="org.jlense.uiworks.views">
    <category name="&General" id="org.jledger.ui.general" />
    <view
       name="Accounts"
       class="org.jledger.ui.accounts.AccountsView"
       id="org.jledger.ui.accounts.AccountsView"
        tooltip="Chart of Accounts"
       category="org.jledger.ui.general"
       showTitleBar="false" />
</extension>

This should look pretty familiar. We see that a new view, Accounts, is contributed to the workbench. The view id, name are specified as we've seen before. A category is also defined. Categories are manifested in the workbench UI as "button sets" in the Shortcuts pane. An icon is also provided for the view, using a path relative to the plug-in's installation directory.

Let's look at the Accounts view. See this for instructions and how to start the JLedger server and client applications. You can show the Accounts view in the workbench by choosing Accounts shortcut in the workbench shortcuts view.

When the Accounts view is displayed, a view with a list in it pops up. The Accounts view displays all the Account objects defined in the default Chart Of Accounts.

When an item is selected in the Accounts view the File/Open and Edit/Delete menu items are enabled. How does the workbench know that some object is selected? And how does it respond meaningfully to changes in the selection? If we can track down the answers to these questions, we are well on our way to understanding how to build integrated workbench plug-ins.

We'll start with the familiar createPartControl method. As we saw in the Hello World example, this is where the widgets that represent a view are created. We'll ignore some of the code to get started.

public JComponent createPartControl()
{
    AccountSplitPane control = null;
    ZoneConnection connection = null;
    AccountListJPanel jAccountsPanel = null;

    try
    {
        ...some initialization here

        /*
        load all items the first time this view is displayed
        */
        ObjectListModel model = jAccountsPanel.getModel();
        Collection objects = JLedgerUIUtils.getAllAccounts(shell);
        model.clear();
        model.addAll(objects);

        /*
        Set the selection provider for this part's site to a selection
        provider that provides access to accounts.
        */
        getSite().setSelectionProvider(
            jAccountsPanel.getSelectionProvider());

This view creates a widget to display Account items, jAccountPanel. After getting the objects to be displayed the objects are added to this widget's model. The view then get's the widget's selection provider and set its associated site's selection provider to the widget's selction provider. This is key to how workbench elements respond to the selection within an application. Views register thier selection providers with thier associated site and then any other workbench element can register with the views site as a selection listener.
The workbench handles the details of tracking and delivering selection notifications. Other UI elements, like actions, will also receive selection notifications and thus enables the ability for the toolbar, menubar, and status line to respond to selection events. This topic will be covered more in the sections on actionSets.