Time for action – viewing time zones in tables

To display the time zones in table form, a new view will be created called Time Zone Table View.

  1. Right-click on the com.packtpub.e4.clock.ui project and select Plug-in Tools | Open Manifest. Open the Extensions tab and right-click on the org.eclipse.ui.views, followed by selecting New | View and filling in the following fields:
    • ID: com.packtpub.e4.clock.ui.views.TimeZoneTableView
    • Name: Time Zone Table View
    • Class: com.packtpub.e4.clock.ui.views.TimeZoneTableView
    • Category: com.packtpub.e4.clock.ui
    • Icon: icons/sample.gif
  2. The plugin.xml file should now contain the following code snippet:
    <view
      category="com.packtpub.e4.clock.ui"
      class="com.packtpub.e4.clock.ui.views.TimeZoneTableView"
      icon="icons/sample.gif"
      id="com.packtpub.e4.clock.ui.views.TimeZoneTableView"
      name="Time Zone Table View"
      restorable="true">
    </view>
  3. Create the class using the editor's shortcuts to create a new class, TimeZoneTableView, which extends ViewPart, or with the new class wizard. Once the view is created, add an empty TableViewer, and use an ArrayContentProvider with the set of available TimeZone IDs:
    public class TimeZoneTableView extends ViewPart {
      private TableViewer tableViewer;
      public void createPartControl(Composite parent) {
        tableViewer=new TableViewer(parent,SWT.H_SCROLL|SWT.V_SCROLL);
        tableViewer.getTable().setHeaderVisible(true);
        tableViewer.setContentProvider(
         ArrayContentProvider.getInstance());
        tableViewer.setInput(TimeZone.getAvailableIDs());
      }
      public void setFocus() {
        tableViewer.getControl().setFocus();
      }
    }

    Note

    Time for action – viewing time zones in tables E4: If creating a part for an E4 application, remember to use the @Inject annotation for the constructor and @Focus for the setFocus() method.

  4. Run the Eclipse instance, and a one-dimensional list of time zones will be shown in the Time Zone Table View:
    Time for action – viewing time zones in tables
  5. Convert the array of Strings to an array of TimeZones and set that as the input:
    //The following  commented code needs to be removed
    /*tableViewer.setInput(TimeZone.getAvailableIDs());*/
    String[] ids = TimeZone.getAvailableIDs();
    TimeZone[] timeZones = new TimeZone[ids.length];
    for(int i=0;i<ids.length;i++) {
      timeZones[i] = TimeZone.getTimeZone(ids[i]);
    }
    tableViewer.setInput(timeZones);
    getSite().setSelectionProvider(tableViewer);
  6. The selection provider is wired up like in the TimeZoneTreeView example. Make a selection in the table, and see the change in the Properties view:
    Time for action – viewing time zones in tables
  7. The table shows a list of the ZoneInfo objects. That's because there is no LabelProvider, so they're just being rendered with their toString() representation. Because a table has multiple columns, TableViewers have a number of TableViewerColumn instances. Each one represents a column in the Table, and each has its own size, title, and label provider. Creating a new column often involves setting up standard features (such as the width) as well as hooking in the required fields to display.

    To make it easy to re-use, create an abstract subclass of ColumnLabelProvider called TimeZoneColumn (in the com.packtpub.e4.clock.ui.internal package) with abstract getText() and getTitle() methods, and a concrete getWidth() method.

    public abstract class TimeZoneColumn extends ColumnLabelProvider {
      public abstract String getText(Object element);
      public abstract String getTitle();
      public int getWidth() {
        return 250;
      }
    }
  8. Add a helper method to the TimeZoneColumn class, which makes it easier to add it to a viewer:
    public TableViewerColumn addColumnTo(TableViewer viewer) {
      TableViewerColumn tableViewerColumn =
        new TableViewerColumn(viewer,SWT.NONE);
      TableColumn column = tableViewerColumn.getColumn();
      column.setMoveable(true);
      column.setResizable(true);
      column.setText(getTitle());
      column.setWidth(getWidth());
      tableViewerColumn.setLabelProvider(this);
      return tableViewerColumn;
    }
  9. Now create a custom subclass TimeZoneIDColumn in the same package that returns the ID column for a TimeZone:
    public class TimeZoneIDColumn extends TimeZoneColumn {
      public String getText(Object element) {
        if (element instanceof TimeZone) {
          return ((TimeZone) element).getID();
        } else {
          return "";
        }
      }public String getTitle() {
       return "ID";
      }
    }
  10. Modify the TimeZoneTableView class, and at the end of the createPartControl() method, instantiate the column and call the addColumnTo() method, above the call to setInput():
    new TimeZoneIDColumn().addColumnTo(tableViewer);
    tableViewer.setInput(timeZones);

    Note

    Note that the columns need to be created prior to the setInput() call, otherwise they won't display properly.

  11. Run the Eclipse instance, and show the Time Zone Table View. The ID column should be displayed on its own.
  12. To add additional columns, copy the TimeZoneIDColumn class, modifying the title returned and the returned property of the associated TimeZone. For example, create a copy of the TimeZoneIDColumn called TimeZoneDisplayNameColumn, and modify the get method and title.
    //The following  commented lines needs to be removed
    /*return ((TimeZone) element).getID();*/
    return ((TimeZone) element).getDisplayName();
    /*return "ID";*/
    return "Display Name"; 
  13. Optionally, do the same with the other properties of TimeZone, such as the offset (with getOffset()), and whether it's in summer time or not (with useDaylightTime()). The columns can then be added to the table.
    new TimeZoneOffsetColumn().addColumnTo(tableViewer);
    new TimeZoneDisplayNameColumn().addColumnTo(tableViewer);
    new TimeZoneSummerTimeColumn().addColumnTo(tableViewer);
  14. Run the Eclipse instance, go to the Time Zone Table View, and the additional column(s) should be seen:
    Time for action – viewing time zones in tables

What just happened?

A TableViewer was created and multiple ColumnLabelProviders were added to it for displaying individual fields of an object. Subclassing ColumnLabelProvider avoids the need to use anonymous inner classes and it gives a helper function. This can be used to create and wire in the column (with specified title and width), while delegating those properties to the concrete subclasses of TimeZoneIDColumn and so on. This avoids the need for tracking columns by ID.

For specific customizations of the columns, the underlying SWT Column is used to set functionality required by the application, including allowing the column to be movable with setMovable(true), and to be resizable with setResizable(true). Similarly, table-wide operations (such as showing the header) are performed by manipulating the underlying SWT Table and invoking setHeaderVisible(true).

It's important to note that the columns of the tree viewer are calculated when the setInput() method is called, so columns that are added after this line may not show properly. Generally, the setInput() should be left until the end of the table's construction.

All of the other functionality from the other view is portable, for example, by wiring up the selection appropriately, the Properties view can show properties of the selected object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset