表查看器可编辑字符串数组项

时间:2017-12-26 09:24:05

标签: java swt jface

我想让我的桌面浏览器可编辑。我决定使用setEditingSupport和EdittingSupport。但表内容存储在一个字符串数组中,该数组没有setter和getter。如何在类中的getValue()和setValue()中编写代码扩展EdittingSupport? tableviewer的代码如下所示:

public class DatatypePage extends WizardPage

{

public static final String NAME = "type";
private MigratorWizard wizard;  

private TableViewer tableViewer;

protected DatatypePage(MigratorWizard wizard) 
{
    super(NAME);
    this.wizard = wizard;
    setTitle("Data Type");
    setDescription("This is a data type page");
}

@Override
public void createControl(Composite parent) 
{
    Composite compositeContent = new Composite(parent, SWT.NONE);
    setControl(compositeContent);

    //super.createControl(parent);
    //updatePage(rbtnDatatype);

    compositeContent.setLayout(new FormLayout());

    Button btnEditConstraint = new Button(compositeContent, SWT.NONE);
    FormData fd_btnEditConstraint = new FormData();
    fd_btnEditConstraint.left = new FormAttachment(0, 287);
    btnEditConstraint.setLayoutData(fd_btnEditConstraint);
    btnEditConstraint.setText("Edit Constraint");

    Button btnAddConstraint = new Button(compositeContent, SWT.NONE);
    fd_btnEditConstraint.top = new FormAttachment(btnAddConstraint, 0, SWT.TOP);
    fd_btnEditConstraint.right = new FormAttachment(btnAddConstraint, -63);
    FormData fd_btnAddConstraint = new FormData();
    fd_btnAddConstraint.bottom = new FormAttachment(100);
    fd_btnAddConstraint.left = new FormAttachment(0, 500);
    btnAddConstraint.setLayoutData(fd_btnAddConstraint);
    btnAddConstraint.setText("Add Constraint");
    btnAddConstraint.addSelectionListener(new SelectionAdapter() {
        @Override 
        public void widgetSelected(SelectionEvent e) {

        }
    });

    Button btnDeleteConstraint = new Button(compositeContent, SWT.NONE);
    fd_btnAddConstraint.right = new FormAttachment(btnDeleteConstraint, -60);

    FormData fd_btnDeleteConstraint = new FormData();
    fd_btnDeleteConstraint.left = new FormAttachment(0, 710);
    fd_btnDeleteConstraint.right = new FormAttachment(100, -2);
    fd_btnDeleteConstraint.bottom = new FormAttachment(100);
    btnDeleteConstraint.setLayoutData(fd_btnDeleteConstraint);
    btnDeleteConstraint.setText("Delete Constraint");

    btnDeleteConstraint.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            ISelection selection = tableViewer.getSelection();
            logger.debug("datatype selected");
            if (selection != null || selection instanceof IStructuredSelection) {
                IStructuredSelection sel = (IStructuredSelection) selection;
                Iterator iterator = sel.iterator();
                while(iterator.hasNext()) {
                    Object obj = iterator.next();
                    tableViewer.remove(obj);
                }


            }

        }
    });


    tableViewer = new TableViewer(compositeContent, SWT.MULTI | SWT.H_SCROLL
            | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
    Table table = tableViewer.getTable();
    FormData fd_table = new FormData();
    fd_table.bottom = new FormAttachment(btnEditConstraint, -6);
    fd_table.top = new FormAttachment(0);
    fd_table.left = new FormAttachment(0);
    fd_table.right = new FormAttachment(100);
    table.setLayoutData(fd_table);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    TableViewerColumn tcolOracle = new TableViewerColumn(tableViewer, SWT.NONE);
    TableColumn tcOracle = tcolOracle.getColumn();
    tcOracle.setText("oracle");
    tcOracle.setWidth(300);
    tcolOracle.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            String[] t = (String[]) element;
            return t[0];
        }
    });
    tcolOracle.setEditingSupport(new FirstColEdittingSupport(tableViewer));

    TableViewerColumn tcolHighgo = new TableViewerColumn(tableViewer, SWT.NONE);
    TableColumn tcHighgo = tcolHighgo.getColumn();
    tcHighgo.setText("hgdb");
    tcHighgo.setWidth(300);
    tcolHighgo.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            String[] t = (String[]) element;
            return t[1];
        }
    });


    initConfig();


}

private void initConfig()
{
    tableViewer.setContentProvider(new ArrayContentProvider()); 
    //tableViewer.setInput(DataTypeFactory.getInstance().getCastList(wizard.getSourceInfo().getDBType()));
    // make the selection available to other views
    // getSite().setSelectionProvider(tableViewer);
}


public void update()
{
    logger.debug("sourceDB=" + wizard.getSourceInfo().getDBType());
    tableViewer.setInput(DataTypeFactory.getInstance().getCastList(wizard.getSourceInfo().getDBType()));
    logger.debug("dataType = "+ wizard.getSourceInfo().getDBType());
    tableViewer.refresh();

    // tableViewer.getTable().selectAll();
}

}

1 个答案:

答案 0 :(得分:1)

您必须更改输入的设计,以便它不仅使用字符串数组。对包含列数据的每一行使用一个类,并将get和set方法添加到该类中。

您可以更改传递给setInput的数据以使用此类,也可以使用自定义内容提供商进行转换。

相关问题