Example usage for org.eclipse.jface.internal.databinding.provisional.swt TableUpdater TableUpdater

List of usage examples for org.eclipse.jface.internal.databinding.provisional.swt TableUpdater TableUpdater

Introduction

In this page you can find the example usage for org.eclipse.jface.internal.databinding.provisional.swt TableUpdater TableUpdater.

Prototype

public TableUpdater(Table table, IObservableList<E> list) 

Source Link

Document

Creates an updator for the given control.

Usage

From source file:net.efano.sandbox.jface.snippets.SpreadSheet.java

License:Open Source License

public static void main(String[] args) {

    final Display display = new Display();
    Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
        public void run() {
            Shell shell = new Shell(display);
            shell.setText("Data Binding Snippet 006");

            final Table table = new Table(shell, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | SWT.VIRTUAL);
            table.setLinesVisible(true);
            table.setHeaderVisible(true);

            for (int i = 0; i < NUM_COLUMNS; i++) {
                TableColumn tableColumn = new TableColumn(table, SWT.NONE);
                tableColumn.setText(Character.toString((char) ('A' + i)));
                tableColumn.setWidth(60);
            }/* w  w  w . j  a v a2  s .  co  m*/
            WritableList list = new WritableList();
            for (int i = 0; i < NUM_ROWS; i++) {
                list.add(new Object());
                for (int j = 0; j < NUM_COLUMNS; j++) {
                    cellFormulas[i][j] = new WritableValue();
                    cellValues[i][j] = new ComputedCellValue(cellFormulas[i][j]);
                    if (!FUNKY_FORMULAS || i == 0 || j == 0) {
                        cellFormulas[i][j].setValue("");
                    } else {
                        cellFormulas[i][j]
                                .setValue("=" + cellReference(i - 1, j) + "+" + cellReference(i, j - 1));
                    }
                }
            }

            new TableUpdater(table, list) {
                protected void updateItem(int rowIndex, TableItem item, Object element) {
                    if (DEBUG_LEVEL >= 1) {
                        System.out.println("updating row " + rowIndex);
                    }
                    for (int j = 0; j < NUM_COLUMNS; j++) {
                        item.setText(j, (String) cellValues[rowIndex][j].getValue());
                    }
                }
            };

            if (FUNKY_COUNTER) {
                // counter in A1
                display.asyncExec(new Runnable() {
                    public void run() {
                        cellFormulas[0][1].setValue("" + counter++);
                        display.timerExec(COUNTER_UPDATE_DELAY, this);
                    }
                });
            }

            // create a TableCursor to navigate around the table
            final TableCursor cursor = new TableCursor(table, SWT.NONE);
            // create an editor to edit the cell when the user hits "ENTER"
            // while over a cell in the table
            final ControlEditor editor = new ControlEditor(cursor);
            editor.grabHorizontal = true;
            editor.grabVertical = true;

            cursor.addSelectionListener(new SelectionAdapter() {
                // when the TableEditor is over a cell, select the
                // corresponding row
                // in
                // the table
                public void widgetSelected(SelectionEvent e) {
                    table.setSelection(new TableItem[] { cursor.getRow() });
                }

                // when the user hits "ENTER" in the TableCursor, pop up a
                // text
                // editor so that
                // they can change the text of the cell
                public void widgetDefaultSelected(SelectionEvent e) {
                    final Text text = new Text(cursor, SWT.NONE);
                    TableItem row = cursor.getRow();
                    int rowIndex = table.indexOf(row);
                    int columnIndex = cursor.getColumn();
                    text.setText((String) cellFormulas[rowIndex][columnIndex].getValue());
                    text.addKeyListener(new KeyAdapter() {
                        public void keyPressed(KeyEvent e) {
                            // close the text editor and copy the data over
                            // when the user hits "ENTER"
                            if (e.character == SWT.CR) {
                                TableItem row = cursor.getRow();
                                int rowIndex = table.indexOf(row);
                                int columnIndex = cursor.getColumn();
                                cellFormulas[rowIndex][columnIndex].setValue(text.getText());
                                text.dispose();
                            }
                            // close the text editor when the user hits
                            // "ESC"
                            if (e.character == SWT.ESC) {
                                text.dispose();
                            }
                        }
                    });
                    editor.setEditor(text);
                    text.setFocus();
                }
            });
            // Hide the TableCursor when the user hits the "MOD1" or "MOD2"
            // key.
            // This alows the user to select multiple items in the table.
            cursor.addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent e) {
                    if (e.keyCode == SWT.MOD1 || e.keyCode == SWT.MOD2 || (e.stateMask & SWT.MOD1) != 0
                            || (e.stateMask & SWT.MOD2) != 0) {
                        cursor.setVisible(false);
                    }
                }
            });
            // Show the TableCursor when the user releases the "MOD2" or
            // "MOD1" key.
            // This signals the end of the multiple selection task.
            table.addKeyListener(new KeyAdapter() {
                public void keyReleased(KeyEvent e) {
                    if (e.keyCode == SWT.MOD1 && (e.stateMask & SWT.MOD2) != 0)
                        return;
                    if (e.keyCode == SWT.MOD2 && (e.stateMask & SWT.MOD1) != 0)
                        return;
                    if (e.keyCode != SWT.MOD1 && (e.stateMask & SWT.MOD1) != 0)
                        return;
                    if (e.keyCode != SWT.MOD2 && (e.stateMask & SWT.MOD2) != 0)
                        return;

                    TableItem[] selection = table.getSelection();
                    TableItem row = (selection.length == 0) ? table.getItem(table.getTopIndex()) : selection[0];
                    table.showItem(row);
                    cursor.setSelection(row, 0);
                    cursor.setVisible(true);
                    cursor.setFocus();
                }
            });

            GridLayoutFactory.fillDefaults().generateLayout(shell);
            shell.setSize(400, 300);
            shell.open();

            // The SWT event loop
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        }
    });
    display.dispose();
}

From source file:org.eclipse.jface.examples.databinding.snippets.Snippet006Spreadsheet.java

License:Open Source License

public static void main(String[] args) {

    final Display display = new Display();
    Realm.runWithDefault(DisplayRealm.getRealm(display), () -> {
        Shell shell = new Shell(display);
        shell.setText("Data Binding Snippet 006");

        final Table table = new Table(shell, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | SWT.VIRTUAL);
        table.setLinesVisible(true);//  w w  w  . j  a  v a 2s.  c o  m
        table.setHeaderVisible(true);

        for (int i1 = 0; i1 < NUM_COLUMNS; i1++) {
            TableColumn tableColumn = new TableColumn(table, SWT.NONE);
            tableColumn.setText(Character.toString((char) ('A' + i1)));
            tableColumn.setWidth(60);
        }
        WritableList<Object> list = new WritableList<>();
        for (int i2 = 0; i2 < NUM_ROWS; i2++) {
            list.add(new Object());
            for (int j = 0; j < NUM_COLUMNS; j++) {
                cellFormulas[i2][j] = new WritableValue<>();
                cellValues[i2][j] = new ComputedCellValue(cellFormulas[i2][j]);
                if (!FUNKY_FORMULAS || i2 == 0 || j == 0) {
                    cellFormulas[i2][j].setValue("");
                } else {
                    cellFormulas[i2][j]
                            .setValue("=" + cellReference(i2 - 1, j) + "+" + cellReference(i2, j - 1));
                }
            }
        }

        new TableUpdater<Object>(table, list) {
            @Override
            protected void updateItem(int rowIndex, TableItem item, Object element) {
                if (DEBUG_LEVEL >= 1) {
                    System.out.println("updating row " + rowIndex);
                }
                for (int j = 0; j < NUM_COLUMNS; j++) {
                    item.setText(j, cellValues[rowIndex][j].getValue());
                }
            }
        };

        if (FUNKY_COUNTER) {
            // counter in A1
            display.asyncExec(new Runnable() {
                @Override
                public void run() {
                    cellFormulas[0][1].setValue("" + counter++);
                    display.timerExec(COUNTER_UPDATE_DELAY, this);
                }
            });
        }

        // create a TableCursor to navigate around the table
        final TableCursor cursor = new TableCursor(table, SWT.NONE);
        // create an editor to edit the cell when the user hits "ENTER"
        // while over a cell in the table
        final ControlEditor editor = new ControlEditor(cursor);
        editor.grabHorizontal = true;
        editor.grabVertical = true;

        cursor.addSelectionListener(new SelectionAdapter() {
            // when the TableEditor is over a cell, select the
            // corresponding row
            // in
            // the table
            @Override
            public void widgetSelected(SelectionEvent e) {
                table.setSelection(new TableItem[] { cursor.getRow() });
            }

            // when the user hits "ENTER" in the TableCursor, pop up a
            // text
            // editor so that
            // they can change the text of the cell
            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
                final Text text = new Text(cursor, SWT.NONE);
                TableItem row = cursor.getRow();
                int rowIndex = table.indexOf(row);
                int columnIndex = cursor.getColumn();
                text.setText(cellFormulas[rowIndex][columnIndex].getValue());
                text.addKeyListener(new KeyAdapter() {
                    @Override
                    public void keyPressed(KeyEvent e) {
                        // close the text editor and copy the data over
                        // when the user hits "ENTER"
                        if (e.character == SWT.CR) {
                            TableItem row = cursor.getRow();
                            int rowIndex = table.indexOf(row);
                            int columnIndex = cursor.getColumn();
                            cellFormulas[rowIndex][columnIndex].setValue(text.getText());
                            text.dispose();
                        }
                        // close the text editor when the user hits
                        // "ESC"
                        if (e.character == SWT.ESC) {
                            text.dispose();
                        }
                    }
                });
                editor.setEditor(text);
                text.setFocus();
            }
        });
        // Hide the TableCursor when the user hits the "MOD1" or "MOD2"
        // key.
        // This alows the user to select multiple items in the table.
        cursor.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.keyCode == SWT.MOD1 || e.keyCode == SWT.MOD2 || (e.stateMask & SWT.MOD1) != 0
                        || (e.stateMask & SWT.MOD2) != 0) {
                    cursor.setVisible(false);
                }
            }
        });
        // Show the TableCursor when the user releases the "MOD2" or
        // "MOD1" key.
        // This signals the end of the multiple selection task.
        table.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.keyCode == SWT.MOD1 && (e.stateMask & SWT.MOD2) != 0)
                    return;
                if (e.keyCode == SWT.MOD2 && (e.stateMask & SWT.MOD1) != 0)
                    return;
                if (e.keyCode != SWT.MOD1 && (e.stateMask & SWT.MOD1) != 0)
                    return;
                if (e.keyCode != SWT.MOD2 && (e.stateMask & SWT.MOD2) != 0)
                    return;

                TableItem[] selection = table.getSelection();
                TableItem row = (selection.length == 0) ? table.getItem(table.getTopIndex()) : selection[0];
                table.showItem(row);
                cursor.setSelection(row, 0);
                cursor.setVisible(true);
                cursor.setFocus();
            }
        });

        GridLayoutFactory.fillDefaults().generateLayout(shell);
        shell.setSize(400, 300);
        shell.open();

        // The SWT event loop
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    });
    display.dispose();
}

From source file:org.eclipse.jface.examples.databinding.snippets.Snippet016TableUpdater.java

License:Open Source License

protected static Shell createShell(final Display display) {
    Shell shell = new Shell();
    Table t = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.VIRTUAL);
    t.setHeaderVisible(true);//from  w  ww. ja  va  2  s .co  m
    createColumn(t, "Values");
    t.setLinesVisible(true);
    final WritableList<Stuff> list = new WritableList<>();
    new TableUpdater<Stuff>(t, list) {
        @Override
        protected void updateItem(int index, TableItem item, Stuff element) {
            item.setText(element.toString());
        }
    };
    display.timerExec(2000, new Runnable() {
        @Override
        public void run() {
            list.add(new Stuff(display));
            display.timerExec(2000, this);
        }
    });
    return shell;
}