Example usage for org.eclipse.jface.viewers ViewerCell getElement

List of usage examples for org.eclipse.jface.viewers ViewerCell getElement

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers ViewerCell getElement.

Prototype

public Object getElement() 

Source Link

Document

Get the element this row represents.

Usage

From source file:ca.uvic.chisel.javasketch.ui.internal.views.TraceNavigator.java

License:Open Source License

/**
 * @param cell/*w  w  w .  ja v  a 2  s  .  co m*/
 * @param data
 */
public void handleDoubleClick(ViewerCell cell, MouseEvent event) {
    IHandlerService hs = (IHandlerService) SketchPlugin.getDefault().getWorkbench()
            .getService(IHandlerService.class);
    if (cell != null) {
        if (cell.getElement() instanceof IThread) {
            if (hs != null) {
                try {
                    Event e = new Event();
                    e.button = event.button;
                    e.x = event.x;
                    e.y = event.y;
                    e.type = SWT.MouseDoubleClick;
                    e.data = event.data;
                    hs.executeCommand(OpenIThreadCommandHandler.COMMAND_ID, e);

                } catch (CommandException ex) {
                    SketchPlugin.getDefault().log(ex);
                }
            }
        } else {
            viewer.setExpandedState(cell.getElement(), !viewer.getExpandedState(cell.getElement()));
        }
    }
}

From source file:ca.uvic.chisel.javasketch.ui.internal.views.TraceNavigatorLabelProvider.java

License:Open Source License

@Override
public void update(ViewerCell cell) {
    Object element = cell.getElement();
    IProgramSketch sketch = null;/*from   ww  w  . j  a va 2s.  c o m*/
    Image image = null;
    if (element instanceof IProgramSketch) {
        sketch = (IProgramSketch) element;
    } else if (element instanceof IThread) {
        IThread thread = (IThread) element;
        sketch = SketchPlugin.getDefault().getSketch(thread);
    }
    switch (cell.getColumnIndex()) {
    case TraceNavigator.ACTIVE_TRACE_COLUMN:
        if (sketch != null) {
            if (sketch.equals(SketchPlugin.getDefault().getActiveSketch())) {
                cell.setImage(SketchPlugin.getDefault().getImageRegistry()
                        .get(ISketchImageConstants.ICON_TRACE_ACTIVE));
            } else {
                cell.setImage(SketchPlugin.getDefault().getImageRegistry()
                        .get(ISketchImageConstants.ICON_TRACE_INACTIVE));
            }
        }
        break;
    case TraceNavigator.LABEL_COLUMN:
        cell.setText(getText(element));
        cell.setImage(getImage(element));
        break;
    case TraceNavigator.VISIBLE_TRACE_COLUMN:
        if (sketch != null) {
            IDegreeOfInterest doi = SketchPlugin.getDefault().getDOI();
            IProgramSketch activeSketch = SketchPlugin.getDefault().getActiveSketch();
            if (activeSketch != null) {
                if (activeSketch.getTracedLaunchConfiguration().equals(sketch.getTracedLaunchConfiguration())) {
                    TraceNavigator navigator = (TraceNavigator) PlatformUI.getWorkbench()
                            .getActiveWorkbenchWindow().getActivePage().findView(TraceNavigator.VIEW_ID);
                    if (navigator != null) {
                        if (doi.isSketchHidden(sketch)) {
                            image = SketchPlugin.getDefault().getImageRegistry()
                                    .get(ISketchImageConstants.ICON_ELEMENT_FILTERED);
                        } else {
                            image = SketchPlugin.getDefault().getImageRegistry()
                                    .get(ISketchImageConstants.ICON_ELEMENT_VISIBLE);
                        }
                    }
                }
            }
        }
        cell.setImage(image);
        break;
    }
}

From source file:cc.xuloo.rcputils.properties.PropertyCellLabelProvider.java

License:Open Source License

@Override
public void update(ViewerCell cell) {
    try {/*from  w ww.  j  a  v a2  s. c  om*/
        Object rawValue = null;
        if (valueHandler != null) {
            rawValue = valueHandler.getValue(cell.getElement());
            Object formattedValue = rawValue;
            if (valueFormatter != null) {
                formattedValue = valueFormatter.format(rawValue);
            }
            cell.setText(String.valueOf(formattedValue));
        }
        if (cellFormatter != null) {
            cellFormatter.formatCell(cell, rawValue);
        }
    } catch (Exception e) {
        //RcpUtilsPlugin.logException(e);
    }
}

From source file:ch.elexis.core.ui.contacts.preferences.BezugsKontaktSettings.java

License:Open Source License

@Override
protected Control createContents(Composite parent) {
    updateExistingEntriesIds.clear();/*  ww  w . j  a  v a  2 s . c o  m*/
    Composite container = new Composite(parent, SWT.NULL);
    container.setLayout(new GridLayout(1, false));

    Group group = new Group(container, SWT.NONE);
    group.setLayout(new GridLayout(1, false));
    group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    group.setText(Messages.Bezugskontakt_Definition);

    Composite composite = new Composite(group, SWT.NONE);
    GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    gd_composite.heightHint = 300;
    composite.setLayoutData(gd_composite);
    TableColumnLayout tcl_composite = new TableColumnLayout();
    composite.setLayout(tcl_composite);

    tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
    tableViewer.setContentProvider(ArrayContentProvider.getInstance());

    tableBezugsKontaktRelations = tableViewer.getTable();
    tableBezugsKontaktRelations.setHeaderVisible(true);
    tableBezugsKontaktRelations.setLinesVisible(true);

    if (allowEditing) {
        Menu menu = new Menu(tableBezugsKontaktRelations);
        tableBezugsKontaktRelations.setMenu(menu);

        MenuItem mntmAddBezugsKontaktRelation = new MenuItem(menu, SWT.NONE);
        mntmAddBezugsKontaktRelation.setText(Messages.Bezugskontakt_Add);
        mntmAddBezugsKontaktRelation.setImage(Images.IMG_NEW.getImage());
        mntmAddBezugsKontaktRelation.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                BezugsKontaktRelation bezugsKontaktRelation = new BezugsKontaktRelation("",
                        RelationshipType.AGENERIC, RelationshipType.AGENERIC);
                tableViewer.add(bezugsKontaktRelation);
                tableViewer.setSelection(new StructuredSelection(bezugsKontaktRelation));
            }
        });

        MenuItem mntmRemoveBezugsKontaktRelation = new MenuItem(menu, SWT.NONE);
        mntmRemoveBezugsKontaktRelation.setText(Messages.Bezugskontakt_Delete);
        mntmRemoveBezugsKontaktRelation.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                int selectionsIdx = tableViewer.getTable().getSelectionIndex();
                if (selectionsIdx != -1) {
                    boolean ret = MessageDialog.openQuestion(UiDesk.getTopShell(),
                            Messages.Bezugskontakt_ConfirmDelete, Messages.Bezugskontakt_ConfirmDeleteText);
                    if (ret) {
                        tableViewer.getTable().remove(selectionsIdx);
                    }
                }
            }
        });
    }

    TableViewerColumn viewCol = new TableViewerColumn(tableViewer, SWT.NONE);
    TableColumn col = viewCol.getColumn();
    tcl_composite.setColumnData(col, new ColumnWeightData(1, 140));
    col.setText(Messages.BezugsKonktat_Reference);
    col.setToolTipText(Messages.Bezugskontakt_ReferenceTooltip);
    viewCol.setLabelProvider(new CellLabelProvider() {

        @Override
        public void update(ViewerCell cell) {
            BezugsKontaktRelation s = (BezugsKontaktRelation) cell.getElement();
            if (s == null)
                return;
            cell.setText(s.getName());
        }
    });
    viewCol.setEditingSupport(new EditingSupport(tableViewer) {

        @Override
        protected void setValue(Object element, Object value) {
            if (element instanceof BezugsKontaktRelation) {
                String newName = String.valueOf(value);
                BezugsKontaktRelation tableData = null;
                for (TableItem tableItem : tableViewer.getTable().getItems()) {
                    tableData = (BezugsKontaktRelation) tableItem.getData();
                    if (tableData != null && !tableData.equals(element) && !tableData.getName().isEmpty()
                            && newName.equalsIgnoreCase(tableData.getName())) {
                        MessageDialog.openError(UiDesk.getTopShell(), "",
                                Messages.Bezugskontakt_NameMustBeUnique);
                        return;
                    }
                }
                BezugsKontaktRelation bezugsKontaktRelation = (BezugsKontaktRelation) element;
                if (!bezugsKontaktRelation.getName().equals(newName)) {
                    bezugsKontaktRelation.setName(newName);
                    getViewer().update(bezugsKontaktRelation, null);
                    openConfirmUpdateExistingData(bezugsKontaktRelation);
                }
            }

        }

        @Override
        protected Object getValue(Object element) {
            if (element instanceof BezugsKontaktRelation) {
                return ((BezugsKontaktRelation) element).getName();
            }
            return null;
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            return new TextCellEditor(tableViewer.getTable());
        }

        @Override
        protected boolean canEdit(Object element) {
            return allowEditing;
        }
    });

    viewCol = new TableViewerColumn(tableViewer, SWT.NONE);
    col = viewCol.getColumn();
    tcl_composite.setColumnData(col, new ColumnWeightData(0, 140));
    col.setText(Messages.Bezugskontakt_RelationFrom);
    viewCol.setLabelProvider(new CellLabelProvider() {

        @Override
        public void update(ViewerCell cell) {
            BezugsKontaktRelation s = (BezugsKontaktRelation) cell.getElement();
            if (s == null)
                return;
            cell.setText(LocalizeUtil.getLocaleText(s.getDestRelationType()));
        }
    });
    viewCol.setEditingSupport(new EditingSupport(tableViewer) {

        @Override
        protected void setValue(Object element, Object value) {
            if (element instanceof BezugsKontaktRelation && value instanceof Integer) {
                BezugsKontaktRelation bezugsKontaktRelation = (BezugsKontaktRelation) element;
                RelationshipType[] allRelationshipTypes = RelationshipType.values();
                if ((int) value != -1 && !bezugsKontaktRelation.getDestRelationType()
                        .equals(allRelationshipTypes[(int) value])) {
                    bezugsKontaktRelation.setDestRelationType(allRelationshipTypes[(int) value]);
                    getViewer().update(bezugsKontaktRelation, null);
                    openConfirmUpdateExistingData(bezugsKontaktRelation);
                }
            }

        }

        @Override
        protected Object getValue(Object element) {
            if (element instanceof BezugsKontaktRelation) {
                BezugsKontaktRelation bezugsKontaktRelation = (BezugsKontaktRelation) element;
                RelationshipType relationshipType = bezugsKontaktRelation.getDestRelationType();
                if (relationshipType != null) {
                    return relationshipType.getValue();
                }
            }

            return 0;
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            return new ComboBoxCellEditor(tableViewer.getTable(), BezugsKontaktAuswahl.getBezugKontaktTypes(),
                    SWT.NONE);
        }

        @Override
        protected boolean canEdit(Object element) {
            return allowEditing;
        }
    });

    viewCol = new TableViewerColumn(tableViewer, SWT.NONE);
    col = viewCol.getColumn();
    tcl_composite.setColumnData(col, new ColumnWeightData(0, 140));
    col.setText(Messages.Bezugskontakt_RelationTo);

    viewCol.setLabelProvider(new CellLabelProvider() {

        @Override
        public void update(ViewerCell cell) {
            BezugsKontaktRelation s = (BezugsKontaktRelation) cell.getElement();
            if (s == null)
                return;
            cell.setText(LocalizeUtil.getLocaleText(s.getSrcRelationType()));
        }
    });
    viewCol.setEditingSupport(new EditingSupport(tableViewer) {

        @Override
        protected void setValue(Object element, Object value) {
            if (element instanceof BezugsKontaktRelation && value instanceof Integer) {
                BezugsKontaktRelation bezugsKontaktRelation = (BezugsKontaktRelation) element;
                RelationshipType[] allRelationshipTypes = RelationshipType.values();
                if ((int) value != -1 && !bezugsKontaktRelation.getSrcRelationType()
                        .equals(allRelationshipTypes[(int) value])) {
                    bezugsKontaktRelation.setSrcRelationType(allRelationshipTypes[(int) value]);
                    getViewer().update(bezugsKontaktRelation, null);
                    openConfirmUpdateExistingData(bezugsKontaktRelation);
                }
            }
        }

        @Override
        protected Object getValue(Object element) {
            if (element instanceof BezugsKontaktRelation) {
                BezugsKontaktRelation bezugsKontaktRelation = (BezugsKontaktRelation) element;
                RelationshipType relationshipType = bezugsKontaktRelation.getSrcRelationType();
                if (relationshipType != null) {
                    return relationshipType.getValue();
                }
            }

            return 0;
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            return new ComboBoxCellEditor(tableViewer.getTable(), BezugsKontaktAuswahl.getBezugKontaktTypes(),
                    SWT.NONE);
        }

        @Override
        protected boolean canEdit(Object element) {
            return allowEditing;
        }
    });
    ;
    tableViewer
            .setInput(loadBezugKonkaktTypes(CoreHub.globalCfg.get(Patientenblatt2.CFG_BEZUGSKONTAKTTYPEN, "")));
    return container;
}

From source file:cideplus.ui.astview.ASTViewLabelProvider.java

License:Open Source License

@Override
public void update(final ViewerCell cell) {
    Object element = cell.getElement();
    //cell.setStyleRanges(new StyleRange[]{new stylera});
    String text = getText(element);
    cell.setText(text);/*from www .j  a  v a2  s. c o  m*/
    Image image = getImage(element);
    cell.setImage(image);
    if (fullBackgroundColor) {
        cell.setBackground(getBackground(element));
    }
    cell.setForeground(getForeground(element));
    cell.setFont(getFont(element));
    cell.setStyleRanges(new StyleRange[] {
            new StyleRange(0, text.length(), getForeground(element), getBackground(element)) });
}

From source file:com.android.ddmuilib.logcat.LogCatMessageLabelProvider.java

License:Apache License

@Override
public void update(ViewerCell cell) {
    Object element = cell.getElement();
    if (!(element instanceof LogCatMessage)) {
        return;/* ww  w. j  a v a 2 s  . co m*/
    }
    LogCatMessage m = (LogCatMessage) element;

    String text = getCellText(m, cell.getColumnIndex());
    cell.setText(text);
    cell.setFont(mLogFont);
    cell.setForeground(getForegroundColor(m));
}

From source file:com.android.ide.eclipse.adt.internal.wizards.newxmlfile.AddTranslationDialog.java

License:Open Source License

/** Populates the table with keys and default strings */
private void fillStrings() {
    ResourceManager manager = ResourceManager.getInstance();
    ProjectResources resources = manager.getProjectResources(mProject);
    mExistingLanguages = resources.getLanguages();

    Collection<ResourceItem> items = resources.getResourceItemsOfType(ResourceType.STRING);

    ResourceItem[] array = items.toArray(new ResourceItem[items.size()]);
    Arrays.sort(array);/*from w  w w . j  a v a2  s .c  o  m*/

    // TODO: Read in the actual XML files providing the default keys here
    // (they can be obtained via ResourceItem.getSourceFileList())
    // such that we can read all the attributes associated with each
    // item, and if it defines translatable=false, or the filename is
    // donottranslate.xml, we can ignore it, and in other cases just
    // duplicate all the attributes (such as "formatted=true", or other
    // local conventions such as "product=tablet", or "msgid="123123123",
    // etc.)

    mTranslations = Maps.newHashMapWithExpectedSize(items.size());
    IBaseLabelProvider labelProvider = new CellLabelProvider() {
        @Override
        public void update(ViewerCell cell) {
            Object element = cell.getElement();
            int index = cell.getColumnIndex();
            ResourceItem item = (ResourceItem) element;
            switch (index) {
            case KEY_COLUMN: {
                // Key
                cell.setText(item.getName());
                return;
            }
            case DEFAULT_TRANSLATION_COLUMN: {
                // Default translation
                ResourceValue value = item.getResourceValue(ResourceType.STRING, mConfiguration, false);

                if (value != null) {
                    cell.setText(value.getValue());
                    return;
                }
                break;
            }
            case NEW_TRANSLATION_COLUMN: {
                // New translation
                String translation = mTranslations.get(item.getName());
                if (translation != null) {
                    cell.setText(translation);
                    return;
                }
                break;
            }
            default:
                assert false : index;
            }
            cell.setText("");
        }
    };

    mTableViewer.setLabelProvider(labelProvider);
    mTableViewer.setContentProvider(new ArrayContentProvider());
    mTableViewer.setInput(array);
}

From source file:com.android.ide.eclipse.gltrace.editors.StateLabelProvider.java

License:Apache License

@Override
public void update(ViewerCell cell) {
    Object element = cell.getElement();
    if (!(element instanceof IGLProperty)) {
        return;/*from w  w  w  .  j a v  a2  s.c  o  m*/
    }

    IGLProperty prop = (IGLProperty) element;

    String text = getColumnText(prop, cell.getColumnIndex());
    cell.setText(text);

    if (mChangedProperties != null && mChangedProperties.contains(prop)) {
        cell.setForeground(mHighlightForegroundColor);
    } else {
        cell.setForeground(mNormalForegroundColor);
    }
}

From source file:com.android.sdkuilib.internal.widgets.AvdCreationDialog.java

License:Apache License

/**
 * Creates the UI for the hardware properties table.
 * This creates the {@link Table}, and several viewers ({@link TableViewer},
 * {@link TableViewerColumn}) and adds edit support for the 2nd column
 *//* w ww  . j  av  a2s .c  o  m*/
private void createHardwareTable(Composite parent) {
    final Table hardwareTable = new Table(parent, SWT.SINGLE | SWT.FULL_SELECTION);
    GridData gd = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
    gd.widthHint = 200;
    gd.heightHint = 100;
    hardwareTable.setLayoutData(gd);
    hardwareTable.setHeaderVisible(true);
    hardwareTable.setLinesVisible(true);

    // -- Table viewer
    mHardwareViewer = new TableViewer(hardwareTable);
    mHardwareViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            // it's a single selection mode, we can just access the selection index
            // from the table directly.
            mDeleteHardwareProp.setEnabled(hardwareTable.getSelectionIndex() != -1);
        }
    });

    // only a content provider. Use viewers per column below (for editing support)
    mHardwareViewer.setContentProvider(new IStructuredContentProvider() {
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            // we can just ignore this. we just use mProperties directly.
        }

        public Object[] getElements(Object arg0) {
            return mProperties.keySet().toArray();
        }

        public void dispose() {
            // pass
        }
    });

    // -- column 1: prop abstract name
    TableColumn col1 = new TableColumn(hardwareTable, SWT.LEFT);
    col1.setText("Property");
    col1.setWidth(150);
    TableViewerColumn tvc1 = new TableViewerColumn(mHardwareViewer, col1);
    tvc1.setLabelProvider(new CellLabelProvider() {
        @Override
        public void update(ViewerCell cell) {
            String name = cell.getElement().toString();
            HardwareProperty prop = mHardwareMap.get(name);
            if (prop != null) {
                cell.setText(prop.getAbstract());
            } else {
                cell.setText(String.format("%1$s (Unknown)", name));
            }
        }
    });

    // -- column 2: prop value
    TableColumn col2 = new TableColumn(hardwareTable, SWT.LEFT);
    col2.setText("Value");
    col2.setWidth(50);
    TableViewerColumn tvc2 = new TableViewerColumn(mHardwareViewer, col2);
    tvc2.setLabelProvider(new CellLabelProvider() {
        @Override
        public void update(ViewerCell cell) {
            String value = mProperties.get(cell.getElement());
            cell.setText(value != null ? value : "");
        }
    });

    // add editing support to the 2nd column
    tvc2.setEditingSupport(new EditingSupport(mHardwareViewer) {
        @Override
        protected void setValue(Object element, Object value) {
            String hardwareName = (String) element;
            HardwareProperty property = mHardwareMap.get(hardwareName);
            switch (property.getType()) {
            case INTEGER:
                mProperties.put((String) element, (String) value);
                break;
            case DISKSIZE:
                if (HardwareProperties.DISKSIZE_PATTERN.matcher((String) value).matches()) {
                    mProperties.put((String) element, (String) value);
                }
                break;
            case BOOLEAN:
                int index = (Integer) value;
                mProperties.put((String) element, HardwareProperties.BOOLEAN_VALUES[index]);
                break;
            }
            mHardwareViewer.refresh(element);
        }

        @Override
        protected Object getValue(Object element) {
            String hardwareName = (String) element;
            HardwareProperty property = mHardwareMap.get(hardwareName);
            String value = mProperties.get(hardwareName);
            switch (property.getType()) {
            case INTEGER:
                // intended fall-through.
            case DISKSIZE:
                return value;
            case BOOLEAN:
                return HardwareProperties.getBooleanValueIndex(value);
            }

            return null;
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            String hardwareName = (String) element;
            HardwareProperty property = mHardwareMap.get(hardwareName);
            switch (property.getType()) {
            // TODO: custom TextCellEditor that restrict input.
            case INTEGER:
                // intended fall-through.
            case DISKSIZE:
                return new TextCellEditor(hardwareTable);
            case BOOLEAN:
                return new ComboBoxCellEditor(hardwareTable, HardwareProperties.BOOLEAN_VALUES,
                        SWT.READ_ONLY | SWT.DROP_DOWN);
            }
            return null;
        }

        @Override
        protected boolean canEdit(Object element) {
            String hardwareName = (String) element;
            HardwareProperty property = mHardwareMap.get(hardwareName);
            return property != null;
        }
    });

    mHardwareViewer.setInput(mProperties);
}

From source file:com.android.sdkuilib.internal.widgets.LegacyAvdEditDialog.java

License:Apache License

/**
 * Creates the UI for the hardware properties table.
 * This creates the {@link Table}, and several viewers ({@link TableViewer},
 * {@link TableViewerColumn}) and adds edit support for the 2nd column
 *//*w ww.j  a v  a2  s . c  o  m*/
private void createHardwareTable(Composite parent) {
    final Table hardwareTable = new Table(parent, SWT.SINGLE | SWT.FULL_SELECTION);
    GridData gd = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
    gd.widthHint = 200;
    gd.heightHint = 100;
    hardwareTable.setLayoutData(gd);
    hardwareTable.setHeaderVisible(true);
    hardwareTable.setLinesVisible(true);

    // -- Table viewer
    mHardwareViewer = new TableViewer(hardwareTable);
    mHardwareViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            // it's a single selection mode, we can just access the selection index
            // from the table directly.
            mDeleteHardwareProp.setEnabled(hardwareTable.getSelectionIndex() != -1);
        }
    });

    // only a content provider. Use viewers per column below (for editing support)
    mHardwareViewer.setContentProvider(new IStructuredContentProvider() {
        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            // we can just ignore this. we just use mProperties directly.
        }

        @Override
        public Object[] getElements(Object arg0) {
            return mProperties.keySet().toArray();
        }

        @Override
        public void dispose() {
            // pass
        }
    });

    // -- column 1: prop abstract name
    TableColumn col1 = new TableColumn(hardwareTable, SWT.LEFT);
    col1.setText("Property");
    col1.setWidth(150);
    TableViewerColumn tvc1 = new TableViewerColumn(mHardwareViewer, col1);
    tvc1.setLabelProvider(new CellLabelProvider() {
        @Override
        public void update(ViewerCell cell) {
            String name = cell.getElement().toString();
            HardwareProperty prop = mHardwareMap.get(name);
            if (prop != null) {
                cell.setText(prop.getAbstract());
            } else {
                cell.setText(String.format("%1$s (Unknown)", name));
            }
        }
    });

    // -- column 2: prop value
    TableColumn col2 = new TableColumn(hardwareTable, SWT.LEFT);
    col2.setText("Value");
    col2.setWidth(50);
    TableViewerColumn tvc2 = new TableViewerColumn(mHardwareViewer, col2);
    tvc2.setLabelProvider(new CellLabelProvider() {
        @Override
        public void update(ViewerCell cell) {
            String value = mProperties.get(cell.getElement());
            cell.setText(value != null ? value : "");
        }
    });

    // add editing support to the 2nd column
    tvc2.setEditingSupport(new EditingSupport(mHardwareViewer) {
        @Override
        protected void setValue(Object element, Object value) {
            String hardwareName = (String) element;
            HardwareProperty property = mHardwareMap.get(hardwareName);
            int index;
            switch (property.getType()) {
            case INTEGER:
                mProperties.put((String) element, (String) value);
                break;
            case DISKSIZE:
                if (HardwareProperties.DISKSIZE_PATTERN.matcher((String) value).matches()) {
                    mProperties.put((String) element, (String) value);
                }
                break;
            case BOOLEAN:
                index = (Integer) value;
                mProperties.put((String) element, HardwareProperties.getBooleanValue(index));
                break;
            case STRING_ENUM:
            case INTEGER_ENUM:
                // For a combo, value is the index of the enum to use.
                index = (Integer) value;
                String[] values = property.getEnum();
                if (values != null && values.length > index) {
                    mProperties.put((String) element, values[index]);
                }
                break;
            }
            mHardwareViewer.refresh(element);
        }

        @Override
        protected Object getValue(Object element) {
            String hardwareName = (String) element;
            HardwareProperty property = mHardwareMap.get(hardwareName);
            String value = mProperties.get(hardwareName);
            switch (property.getType()) {
            case INTEGER:
                // intended fall-through.
            case DISKSIZE:
                return value;
            case BOOLEAN:
                return HardwareProperties.getBooleanValueIndex(value);
            case STRING_ENUM:
            case INTEGER_ENUM:
                // For a combo, we need to return the index of the value in the enum
                String[] values = property.getEnum();
                if (values != null) {
                    for (int i = 0; i < values.length; i++) {
                        if (values[i].equals(value)) {
                            return i;
                        }
                    }
                }
            }

            return null;
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            String hardwareName = (String) element;
            HardwareProperty property = mHardwareMap.get(hardwareName);
            switch (property.getType()) {
            // TODO: custom TextCellEditor that restrict input.
            case INTEGER:
                // intended fall-through.
            case DISKSIZE:
                return new TextCellEditor(hardwareTable);
            case BOOLEAN:
                return new ComboBoxCellEditor(hardwareTable, new String[] {
                        HardwareProperties.getBooleanValue(0), HardwareProperties.getBooleanValue(1), },
                        SWT.READ_ONLY | SWT.DROP_DOWN);
            case STRING_ENUM:
            case INTEGER_ENUM:
                String[] values = property.getEnum();
                if (values != null && values.length > 0) {
                    return new ComboBoxCellEditor(hardwareTable, values, SWT.READ_ONLY | SWT.DROP_DOWN);
                }
            }
            return null;
        }

        @Override
        protected boolean canEdit(Object element) {
            String hardwareName = (String) element;
            HardwareProperty property = mHardwareMap.get(hardwareName);
            return property != null;
        }
    });

    mHardwareViewer.setInput(mProperties);
}