package swt; import org.eclipse.swt.*; import org.eclipse.swt.events.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; import java.util.*; /* * Main.java * * All rights reserved, Copyright(C) 2004 Oki Electric Industry Co.,Ltd. */ public class Main { private static final int TABLE_ROW_COUNT = 10000; private static final int TABLE_COLUMN_COUNT = 3; private Table table; private Button viewBtn; private Button exitBtn; public Main(Shell shell) { // レイアウト、コンポーネントの生成 initComponent(shell); // イベントハンドラの設定 setEventHandler(shell); } private void initComponent(Shell shell) { // レイアウト。 FormLayout layout = new FormLayout(); shell.setLayout(layout); // テーブル生成 FormData tableData = new FormData(); tableData.top = new FormAttachment(0, 0); tableData.left = new FormAttachment(0, 0); tableData.right = new FormAttachment(100, -5); tableData.bottom = new FormAttachment(93, -5); createTable(shell); table.setLayoutData(tableData); // table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // ↑対応するメソッドが無い? // ボタン生成 viewBtn = new Button(shell, SWT.PUSH); viewBtn.setText("view"); FormData viewData = new FormData(); viewData.top = new FormAttachment(93, 0); viewData.left = new FormAttachment(80, 0); viewData.right = new FormAttachment(90, -5); viewData.bottom = new FormAttachment(100, -5); viewBtn.setLayoutData(viewData); exitBtn = new Button(shell, SWT.PUSH); exitBtn.setText("exit"); FormData exitData = new FormData(); exitData.top = new FormAttachment(93, 0); exitData.left = new FormAttachment(90, 0); exitData.right = new FormAttachment(100, -5); exitData.bottom = new FormAttachment(100, -5); exitBtn.setLayoutData(exitData); } private void setEventHandler(final Shell shell) { // viewボタン viewBtn.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { // テーブルから選択行の情報の取得 String[] row = getSelectedRow(); if (row == null) { MessageBox box = new MessageBox(shell, SWT.NULL | SWT.ICON_ERROR); box.setMessage("行が選択されていません"); box.open(); return; } else { // ダイアログ表示用Shell Shell child = new Shell(shell, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL); MyDialog dialog = new MyDialog(child, row); dialog.open(); } } }); // exitボタン exitBtn.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { windowClose(); } }); } private String[] getSelectedRow() { java.util.List row = new ArrayList(); int selectedCount = table.getSelectionCount(); if (selectedCount <= 0) { // 行が選択されていない return null; } else { // 行が選択されている TableItem[] items = table.getSelection(); for (int i = 0; i < TABLE_COLUMN_COUNT; i++) { row.add(items[0].getText(i)); } } return (String[]) row.toArray(new String[0]); } private void windowClose() { System.exit(0); } private void createTable(Composite composite) { table = new Table(composite, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER); // 罫線を表示する table.setLinesVisible(true); // ヘッダを可視にする table.setHeaderVisible(true); // ヘッダを設定 String[] cols = { "A", "B", "C" }; for (int i = 0; i < cols.length; i++) { TableColumn col = new TableColumn(table, SWT.LEFT); col.setText(cols[i]); col.setWidth(100); } for (int i = 0; i < TABLE_ROW_COUNT; i++) { TableItem item = new TableItem(table, SWT.NULL); java.util.List row = new ArrayList(); for (int j = 0; j < TABLE_COLUMN_COUNT; j++) { row.add("cell" + (i + 1) + ":" + (j + 1)); } item.setText((String[]) row.toArray(new String[0])); } } public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); Main main = new Main(shell); shell.setText("SWT Table Example"); shell.setSize(600, 400); //shell.pack(); shell.open(); // 窓が閉じるまでメインスレッドを終了しない while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } /** * テーブル選択行の内容を表示するダイアログ * @author Hiroshi Ogawa */ class MyDialog { private Shell shell; private String result; public MyDialog(Shell shell, String[] row) { this.shell = shell; // レイアウト、コンポーネントの生成 initComponent(shell, row); } private void initComponent(final Shell shell, String[] row) { int rowCount = row.length; GridLayout layout = new GridLayout(); layout.numColumns = 1; shell.setLayout(layout); for (int i = 0; i < rowCount; i++) { Label label = new Label(shell, SWT.NULL); label.setText(row[i]); } Button closeBtn = new Button(shell, SWT.PUSH); closeBtn.setText("close"); closeBtn.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { shell.close(); // これは不要? shell.dispose(); } }); } public String open() { shell.pack(); shell.open(); // 窓が消滅するまで待機 while (!shell.isDisposed()) { if (!shell.getDisplay().readAndDispatch()) { shell.getDisplay().sleep(); } } return result; } } }