Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You do not need use the JFace Databinding Framework in <code>TableViewer</code>. Manipulation the structured data is simpler then SWT controls, such <code>TableViewer</code>, <code>ListViewer</code> and <code>TreeViewer</code>. You can use those viewer in the same way:</p> <ul> <li>create viewer</li> <li>set content provider</li> <li>set label provider (suggested)</li> <li>set filter (optional)</li> <li>set sorter (optional)</li> </ul> <p>After the viewer created, just invoke <code>viewer.setInput(data)</code> to put all the things to your viewer.</p> <p>There are a list of model:</p> <pre><code>TableViewer tableViewer = new TableViewer(parent); Table table = tableViewer.getTable(); table.setHeaderVisible(true); table.setLinesVisible(true);` for (int i = 0; i &lt; COLUMN_NAMES.length; i++) { TableColumn tableColumn = new TableColumn(table, SWT.LEFT); tableColumn.setText(COLUMN_NAMES[i]); tableColumn.setWidth(COLUMN_WIDTHS[i]); } tableViewer.setContentProvider(new ModelContentProvider()); tableViewer.setLabelProvider(new ModelLabelProvider()); tableViewer.setInput(models); </code></pre> <p>The magic happens in the content provider:</p> <pre><code>class ModelContentProvider implements IStructuredContentProvider { @SuppressWarnings("unchecked") @Override public Object[] getElements(Object inputElement) { // The inputElement comes from view.setInput() if (inputElement instanceof List) { List models = (List) inputElement; return models.toArray(); } return new Object[0]; } /* ... other methods */ } </code></pre> <p>Each model will become a <code>TableItem</code> and the model in the <code>TableItem(item.getData())</code>.</p> <p>However, a table composed by many columns, you need the <code>LabelProvider</code> to help you mapping the property of model to the <code>TableItem</code>:</p> <pre><code>class ModelLabelProvider extends LabelProvider implements ITableLabelProvider { @Override public Image getColumnImage(Object element, int columnIndex) { // no image to show return null; } @Override public String getColumnText(Object element, int columnIndex) { // each element comes from the ContentProvider.getElements(Object) if (!(element instanceof Model)) { return ""; } Model model = (Model) element; switch (columnIndex) { case 0: return model.getFoo(); case 1: return model.getBar(); default: break; } return ""; } } </code></pre> <p>The propagation of models to viewer is easy. If you will propagate viewer to the binded model, using the <code>CellEditor</code> is simple as well. To use <code>CellEditor</code>, you need set the column properties, cell editors and cell modifier to <code>TableViewer</code>:</p> <pre><code>tableViewer.setColumnProperties(COLUMNS_PROPERTIES); tableViewer.setCellEditors(new CellEditor[] { new TextCellEditor(table), new TextCellEditor(table) }); tableViewer.setCellModifier(new ModelCellModifier(tableViewer)); </code></pre> <p>The CellModifier likes this:</p> <pre><code>class ModelCellModifier implements ICellModifier { TableViewer viewer; public ModelCellModifier(TableViewer viewer) { this.viewer = viewer; } @Override public boolean canModify(Object element, String property) { // property is defined by viewer.setColumnProperties() // allow the FOO column can be modified. return "foo_prop".equals(property); } @Override public Object getValue(Object element, String property) { if ("foo_prop".equals(property)) { return ((Model) element).getFoo(); } if ("bar_prop".equals(property)) { return ((Model) element).getBar(); } return ""; } @Override public void modify(Object element, String property, Object value) { if ("foo_prop".equals(property)) { TableItem item = (TableItem) element; ((Model) item.getData()).setFoo("" + value); // refresh the viewer to show the changes to our user. viewer.refresh(); } } } </code></pre> <p>Everything is simple but there are many steps to make all together.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload