To display the time zones in table form, a new view will be created called Time Zone Table View.
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:com.packtpub.e4.clock.ui.views.TimeZoneTableView
Time Zone Table View
com.packtpub.e4.clock.ui.views.TimeZoneTableView
com.packtpub.e4.clock.ui
icons/sample.gif
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>
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(); } }
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);
TimeZoneTreeView
example. Make a selection in the table, and see the change in the Properties view: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; } }
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; }
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"; } }
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);
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";
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);
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.