RCP中TableViewer自定义选择一行时样式

代码
Display display = new Display();
    Shell shell 
= new Shell(display);
    
final Color red = display.getSystemColor(SWT.COLOR_RED);
    
final Color yellow = display.getSystemColor(SWT.COLOR_YELLOW);
    
final Table table = new Table(shell, SWT.FULL_SELECTION);
    table.setHeaderVisible(
true);
    
new TableColumn(table, SWT.NONE).setWidth(100);
    
new TableColumn(table, SWT.NONE).setWidth(100);
    
new TableColumn(table, SWT.NONE).setWidth(100);
    
for (int i = 0; i < 5; i++) {
       TableItem item 
= new TableItem(table, SWT.NONE);
       item.setText(
0"item " + i + " col 0");
       item.setText(
1"item " + i + " col 1");
       item.setText(
2"item " + i + " col 2");
    }
    table.pack();
    table.addListener(SWT.EraseItem, 
new Listener() {
       
public void handleEvent(Event event) {
          event.detail 
&= ~SWT.HOT;
          
if ((event.detail & SWT.SELECTED) == 0return/* item not selected */
          
int clientWidth = table.getClientArea().width;
          GC gc 
= event.gc;
          Color oldForeground 
= gc.getForeground();
          Color oldBackground 
= gc.getBackground();
          gc.setForeground(red);
          gc.setBackground(yellow);
          gc.fillGradientRectangle(
0, event.y, clientWidth, event.height, false);
          gc.setForeground(oldForeground);
          gc.setBackground(oldBackground);
          event.detail 
&= ~SWT.SELECTED;
       }
    });
    shell.pack();
    shell.open();
    
while (!shell.isDisposed()) {
       
if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();

Lines 1-2:
Creates a display and shell.

Lines 3-4:
Obtains the system's red and yellow colors, which will be used for drawing the custom selection.

Lines 5-6:
Creates the table and sets its header to be visible. The table's style is specified to be SWT.FULL_SELECTION since the custom-drawn selection will span the full table width.

Lines 7-9:
Creates three table columns and sets the initial width of each to 100 pixels.

Lines 10-15:
Creates the table's items.

Line 16:
Packs the table to its preferred size.

Lines 17-18:
Adds an SWT.EraseItem listener to the table, which will be invoked whenever the background of a cell is about to be drawn.

Line 19:
Clears the SWT.HOT bit from the event's detail field to ensure that hover backgrounds are not natively drawn.

Line 20:
Checks the detail field for the SWT.SELECTED bit, and returns out of the listener if it is not there since there is no selection to draw.

Line 21:
Gets the table's client width, to be used for drawing the cell's selection.

Lines 22-24:
Obtains the GC to draw on from the event and stores its foreground and background colors for the purpose of restoring them later.

Lines 25-27:
Draws the custom selection rectangle using a gradient that spans from red to yellow. Line 26 specifies the full width of the item as the gradient bounds so that the color range will span the table width properly. Since the GC's clipping is pre-configured to the bounds of the cell, only this portion of this gradient drawing will appear.

Lines 28-29:
Restores the GC's foreground and background colors to their previous values.

Line 30:
Clears the SWT.SELECTED bit from the event's detail field to indicate that the default selection should not be drawn for this cell. Note that this item is still considered to be logically selected in the table.

Lines 33-38:
Packs and opens the shell, runs the event loop until the shell has been disposed, and disposes the display just before exiting.

 

http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html 

 

posted @ 2011-01-04 17:18  轻剑  阅读(2071)  评论(0编辑  收藏  举报