SWT里的表格编程(翻译)

来源:互联网 发布:末日人工智能怎么算赢 编辑:程序博客网 时间:2024/04/30 21:01
//原文摘自《The Definitive Guide to SWT and JFace》
Tables
SWT使用Table、TableColumn和TableItem这3个独立的类来构建table.
这3个类都不需要被继承.

*****Creating Tables

Table类只提供了一个构造方法:
public Table(Composite parent, int style)

下表显示了style的作用,在构造方法中允许用逻辑与操作符表示style.
Style                Description
SWT.SINGLE            一次只能选择一行,默认的参数.
SWT.MULTI            一次能选择多行,一般是使用Ctrl+鼠标实现选择多行.
SWT.CHECK            将放置一个checkbox在每行的最开头处.注意checkbox
                    的选择状态与表格行是否处于被选择的状态无关.                    
SWT.FULL_SELECTION    当表格行被选择时,高亮显示,而不是默认的只显示第一列.
SWT.HIDE_SELECTIN    当表格所在的窗口不是当前的窗口时,除去被选择行的高亮
                    显示.默认的是无论表格所在窗口是否为当前窗口都使被选
                    择行处于高亮显示.
                    
*****Adding Columns

TableColumn类表示表格里的一列,你创建一列使用一个parent table, a style
,和一个可选的index.如果你不设置index,那么这列就将被默认把index设置为0.

TableColumn的构造方法:
public TableColumn(Table parent, int style)
public TableColumn(Table parent, int style, int index)

所有设置表格列的内容的对齐参数有:
SWT.LEFT    左对齐
SWT.CENTER    居中
SWT.RIGHT    右对齐

只能设置其中一个参数,如果设置了多于一个参数,后果将不确定.可以在构造方法后改
变对齐:通过setAlignment()方法.默认对齐是左对齐并且影响在这列中所有的行.
表格列能够显示标题.每个标题只能显示一行,它将按照ASC2码显示字符.
parent table可以通过setHeadersVisible()方法控制标题是否显示.

*****Adding Rows

构造方法:
TableItem(Table parent, int style)
TableItem(Table parent, int style, int index)

使用第2个参数在表格中插入行,将已经存在的行往后移,如果传入的参数超过范围就抛
出IllegalArgumentException.比如,如果没有行存在与table中,这样写的话:
new TableItem(table, SWT.NONE, 1);
结果如下:
java.lang.IllegalArgumentException: Index out of bounds
没有style提供给TableItem,所以你必须经常传递SWT.NONE参数以忽略其它值.
你可以改变TableItem的前景颜色和背景颜色,以整行宽或者一个单元格宽都可以.单
元格能够显示文字,图片,或者两者都显示.如果你两者都设置了,图片将显示在文字的左
方.

*****示例程序:
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * Displays ASCII Codes
 */
public class AsciiTable {
  // The number of characters to show.
  private static final int MAX_CHARS = 128;
  // Names for each of the columns
  private static final String[] COLUMN_NAMES = { "Char", "Dec", "Hex", "Oct",
      "Bin", "Name"};

  // The names of the first 32 characters
  private static final String[] CHAR_NAMES = { "NUL", "SOH", "STX", "ETX", "EOT",
      "ENQ", "ACK", "BEL", "BS", "TAB", "LF", "VT", "FF", "CR", "SO", "SI",
      "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB",
      "ESC", "FS", "GS", "RS", "US", "Space"};

  // The font to use for displaying characters
  private Font font;

  // The background colors to use for the rows
  private Color[] colors = new Color[MAX_CHARS];

  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("ASCII Codes");
    createContents(shell);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    // Call dispose to dispose any resources
    // we have created
    dispose();
    display.dispose();
  }

  /**
   * Disposes the resources created
   */
  private void dispose() {
    // We created this font; we must dispose it
    if (font != null) {
      font.dispose();
    }

    // We created the colors; we must dispose them
    for (int i = 0, n = colors.length; i < n; i++) {
      if (colors[i] != null) {
        colors[i].dispose();
      }
    }
  }

  /**
   * Creates the font
   */
  private void createFont() {
    // Create a font that will display the range
    // of characters. "Terminal" works well in
    // Windows
    font = new Font(Display.getCurrent(), "Terminal", 10, SWT.NORMAL);
  }

  /**
   * Creates the columns for the table
   *
   * @param table the table
   * @return TableColumn[]
   */
  private TableColumn[] createColumns(Table table) {
    TableColumn[] columns = new TableColumn[COLUMN_NAMES.length];
    for (int i = 0, n = columns.length; i < n; i++) {
      // Create the TableColumn with right alignment
      columns[i] = new TableColumn(table, SWT.RIGHT);

      // This text will appear in the column header
      columns[i].setText(COLUMN_NAMES[i]);
    }
    return columns;
  }

  /**
   * Creates the window's contents (the table)
   *
   * @param composite the parent composite
   */
  private void createContents(Composite composite) {
    composite.setLayout(new FillLayout());

    // The system font will not display the lower 32
    // characters, so create one that will
    createFont();

    // Create a table with visible headers
    // and lines, and set the font that we
    // created
    Table table = new Table(composite, SWT.SINGLE | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    table.setRedraw(false);
    table.setFont(font);

    // Create the columns
    TableColumn[] columns = createColumns(table);

    for (int i = 0; i < MAX_CHARS; i++) {
      // Create a background color for this row
      colors[i] = new Color(table.getDisplay(), 255 - i, 127 + i, i);

      // Create the row in the table by creating
      // a TableItem and setting text for each
      // column
      int c = 0;
      TableItem item = new TableItem(table, SWT.NONE);
      item.setText(c++, String.valueOf((char) i));
      item.setText(c++, String.valueOf(i));
      item.setText(c++, Integer.toHexString(i).toUpperCase());
      item.setText(c++, Integer.toOctalString(i));
      item.setText(c++, Integer.toBinaryString(i));
      item.setText(c++, i < CHAR_NAMES.length ? CHAR_NAMES[i] : "");
      item.setBackground(colors[i]);
    }

    // Now that we've set the text into the columns,
    // we call pack() on each one to size it to the
    // contents
    for (int i = 0, n = columns.length; i < n; i++) {
      columns[i].pack();
    }

    // Set redraw back to true so that the table
    // will paint appropriately
    table.setRedraw(true);
  }

  /**
   * The application entry point
   *
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new AsciiTable().run();
  }
}
*****Sorting Tables
用户希望能通过点击表格标题使每行按照列排序,以升序或者降序交替.SWT表格不会自动
做这些事,但是可以通过编程实现,要实现排序,你可以按照下面的步骤:
1.为列标题栏增加一个listener,这样可以侦察到标题栏被鼠标点击.
2.取得现在要排序的信息.(按哪一列排序,按哪个方向排序--升序或降序)
3.对数据进行排序并且重新显示.

*****小结
如果你是一个Swing develloper,你一定会取笑SWT表格这种排序方法:取得数据,排序,
然后重新填入.用SWT编程意味着在widget level上,使用data(model),view,并且意
味着view和controller不可分割.象Swing的表格那样将data和view分离,并且用MVC
模式使用controller,使表格排序更加直接.
但是记住虽然SWT是在widget的level上,但是JFace提供了一个在SWT之上的MVC层次编
程,从而使在JFace中编程变得象Swing一样简单.