原文链接:http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html
Custom Drawing Table and Tree Items
Summary
Populating a table or tree widget involves creating items and setting their attributes (eg.- texts, images, etc.), after which the table or tree takes responsibility for displaying the items. This approach makes item creation straightforward and visually consistent. As of Eclipse 3.2, clients of Table and Tree can now custom draw their items, enabling a wide range of potential visual appearances. This article explores the custom draw mechanism for Table and Tree.
SWT.HOT
style on June 8, 2007)
Background
Tables and trees are powerful tools for presenting data in a structured manner. Data is displayed as a collection of items which have attributes like text(s), image(s), and sometimes interactive controls such as checkboxes. Typically, clients create the items and set their attributes, after which the table or tree takes responsibility for displaying them. This approach makes item creation straightforward and visually consistent, but is inflexible. For example, an item in a table or tree can only contain one image, and it must appear before its text. Given the range of visual appearances that a client may want an item to have, the best way to ensure that clients can create items with custom appearances is to allow items to be drawn.
The ability to partially or fully custom draw TableItems and TreeItems has been added as of Eclipse 3.2, and will be described in this article. It should be noted that all references made throughout the article to tables can be equally applied to trees, and vice versa.
Custom Draw Events
Custom drawing is done on a per-cell basis, where a cell is the portion of an item that resides within some row and column of the parent table, or the full item if the table has no columns. For example, if a table has two columns and three items then it has six cells.
The following Table events have been defined to provide hooks into the drawing process:
-
SWT.MeasureItem
: allows a client to specify the dimensions of a cell's content -
SWT.EraseItem
: allows a client to custom draw a cell's background and/or selection, and to influence whether the cell's foreground should be drawn -
SWT.PaintItem
: allows a client to custom draw or augment a cell's foreground and/or focus rectangle
To minimize the amount of work required to draw cells, these events are configured to reflect how the cell would be drawn by default. This makes it easy to augment a cell's default appearance without having to draw the whole cell. If a client does not hook one of these listeners then the default cell drawing process will occur. The following sections will examine each of these events in detail along with some example code snippets.
SWT.MeasureItem
SWT.MeasureItem
is the first custom draw event that is sent. This event gives a client the opportunity to specify the width and/or height of a cell's content. It is important to note that content size is not necessarily equivalent to cell size, since the latter may include additional decorations such as checkboxes or tree indentation. Contexts where this event is typically sent include drawing a cell and packing a table column. This event comes pre-configured with the following fields:
-
item
: the item -
index
: the column index ofitem
to be measured -
width
: the default content width that the table would use if it were to draw the cell itself, based on its text, image, and checkbox -
height
: the default content height that the table would use, based on the height of all of its items -
gc
: a GC that is configured with the cell's font, which can be useful for performing string measurements
An application that wishes to specify a different content width and/or height for the cell can do so by changing the event's width
andheight
fields. A listener may choose to not change one or both of these values.
Example 1: Specifying cell widths and heights
Listing 1 (snippet) demonstrates the use of SWT.MeasureItem
to specify custom content dimensions in a table with no columns.
1 | Display display = new Display(); |
2 | Shell shell = new Shell(display); |
3 | shell.setBounds(10,10,200,250); |
4 | final Table table = new Table(shell, SWT.NONE); |
5 | table.setBounds(10,10,150,200); |
6 | table.setLinesVisible(true); |
7 | for (int i = 0; i < 5; i++) {
|
8 | new TableItem(table, SWT.NONE).setText("item " + i); |
9 | } |
10 | table.addListener(SWT.MeasureItem, new Listener() {
|
11 | public void handleEvent(Event event) {
|
12 | int clientWidth = table.getClientArea().width; |
13 | event.height = event.gc.getFontMetrics().getHeight() * 2; |
14 | event.width = clientWidth * 2; |
15 | } |
16 | }); |
17 | shell.open(); |
18 | while (!shell.isDisposed()) {
|
19 | if (!display.readAndDispatch()) display.sleep(); |
20 | } |
21 | display.dispose(); |
Listing 1. Specifying custom widths and heights for items in a table with no columns
Lines 1-3:
Creates a display and shell, and sets the shell's bounds.
Lines 4-6:
Creates the table with no columns, sets its bounds, and sets its lines to be visible.
Lines 7-9:
Creates the table's items.
Lines 10-11
Adds an SWT.MeasureItem
listener to the table, which will be invoked whenever the size of a cell's content is needed.
Line 12:
Gets the table's client width, which will be used when specifying the cell's content width.
Line 13:
Sets the event's height
field to double the height of the font. This effectively doubles the height of the item in the event's item
field relative to its default height.
Line 14:
Sets the event's width
field to double the width of the table. This specifies that the width of the cell should be this value regardless of its content. Note that since this table has no columns, the width of the cell is equivalent to that of the full item.
Lines 17 to 21:
Opens the shell, runs the event loop until the shell has been disposed, and disposes the display just before exiting.
Figure 1 shows a screenshot of the snippet in Listing 1 running. Note the increased item heights (caused by line 13), and increased item widths (caused by line 14) which can be inferred from the table's horizontal scrollbar. Figure 2 shows a screenshot of the same snippet running with lines 10 through 16 commented out, allowing the table to draw in its default manner.
Figure 1. Screenshot of Listing 1 running, which specifies custom widths and heights for the items in a table
Figure 2. Screenshot of Listing 1 running without the SWT.MeasureItem
listener
Note that attempts to change a cell's width or height in SWT.MeasureItem
events are subject to the following constraints:
- As of Eclipse 3.2 cell heights are not allowed to shrink, only to grow.
- All items in a table have the same height, so increasing the height of a cell will result in the height of all items in the table growing accordingly.
- If the cell is within a table column then its width is determined by the column's width. However, the
SWT.MeasureItem
event'swidth
field should still always be set to the cell's desired content width because this value will be used if the table has no columns or if its column is being packed.
Example 2: Packing columns
Listing 2 (snippet) demonstrates the use of SWT.MeasureItem
to specify cell widths when a table column is packed.
1 | Display display = new Display(); |
2 | Shell shell = new Shell(display); |
3 | shell.setBounds(10,10,400,200); |
4 | Table table = new Table(shell, SWT.NONE); |
5 | table.setBounds(10,10,350,150); |
6 | table.setHeaderVisible(true); |
7 | table.setLinesVisible(true); |
8 | final TableColumn column0 = new TableColumn(table, SWT.NONE); |
9 | column0.setWidth(100); |
10 | final TableColumn column1 = new TableColumn(table, SWT.NONE); |
11 | column1.setWidth(100); |
12 | column0.addListener(SWT.Selection, new Listener() {
|
13 | public void handleEvent(Event event) {
|
14 | column0.pack(); |
15 | } |
16 | }); |
17 | column1.addListener(SWT.Selection, new Listener() {
|
18 | public void handleEvent(Event event) {
|
19 | column1.pack(); |
20 | } |
21 | }); |
22 | for (int i = 0; i < 5; i++) {
|
23 | TableItem item = new TableItem(table, SWT.NONE); |
24 | item.setText(0, "item " + i + " col 0"); |
25 | item.setText(1, "item " + i + " col 1"); |
26 | } |
27 | table.addListener(SWT.MeasureItem, new Listener() {
|
28 | public void handleEvent(Event event) {
|
29 | event.width *= 2; |