2017-12-26 27 views
0

테이블 뷰어를 편집 가능하게 만들고 싶습니다. 나는 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

몇 가지 코드를 알려주시겠습니까? – Aman

+0

이 메서드는 List를 반환합니다. DataTypeFactory.getInstance(). getCastList (wizard.getSourceInfo(). getDBType()) – khunnie

답변

1

당신은 그냥 문자열 배열을 사용하지 않도록 입력의 디자인을 변경해야합니다. 열 데이터를 포함하는 각 행에 대해 클래스를 사용하고 해당 클래스에 get 및 set 메서드를 추가합니다.

전달할 데이터를 setInput으로 변경하거나이 클래스를 사용하거나 사용자 지정 콘텐츠 공급자를 사용하여 변환 할 수 있습니다.

+0

답장을 보내 주셔서 감사합니다. 데이터를 가져 오는 데 사용한 메서드를 호출하는 대신 데이터 리소스에 대한 새 클래스를 만들어야한다는 뜻입니까? – khunnie

+0

당신이 이것을하는 곳은 당신에게 달려 있습니다. 'getCastList'를 변경하여 행 클래스의 목록을 반환하거나'setInput'을 호출하기 전에 목록을 변경하거나 콘텐츠 공급자가 클래스 인스턴스를 만들도록 할 수 있습니다. 그것은 당신이 가장 잘 이해하는 프로그램에 달려 있습니다. –

+0

다시 한번 감사드립니다. 하지만 또 다른 질문이 있습니다. 문자열 배열에 저장된 데이터는 매핑 관계가 있습니다. 목록을 변경하면이 관계를 유지하는 방법? – khunnie