RCP/SWT & JFACE

Tableviwer와 편집모드, 컨텍스트 메뉴 생성

반응형

package Table;


import org.eclipse.jface.action.Action;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.action.MenuManager;

import org.eclipse.jface.viewers.CellEditor;

import org.eclipse.jface.viewers.ColumnWeightData;

import org.eclipse.jface.viewers.ComboBoxCellEditor;

import org.eclipse.jface.viewers.ICellModifier;

import org.eclipse.jface.viewers.ILabelProviderListener;

import org.eclipse.jface.viewers.IStructuredContentProvider;

import org.eclipse.jface.viewers.ITableLabelProvider;

import org.eclipse.jface.viewers.TableLayout;

import org.eclipse.jface.viewers.TableViewer;

import org.eclipse.jface.viewers.TextCellEditor;

import org.eclipse.jface.viewers.Viewer;

import org.eclipse.swt.SWT;

import org.eclipse.swt.graphics.Image;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Menu;

import org.eclipse.swt.widgets.Table;

import org.eclipse.swt.widgets.TableColumn;

import org.eclipse.swt.widgets.TableItem;


public class Ch9Table extends Composite {

private static final Object[] CONTENT = new Object[]{

new EditableTableItem("item 1", new Integer(0)),

new EditableTableItem("item 2", new Integer(1))

};

private static final String[] VALUE_SET = new String[]{

"xxx", "yyy","zzz"

};

private static final String NAME_PROPERTY = "name";

private static final String VALUE_PROPERTY = "value";

private TableViewer viewer;

public Ch9Table(Composite parent){

super(parent, SWT.NULL);

buildControls();

}

private class NewRowAction extends Action{

public NewRowAction(){

super("Insert New Row");

}

public void run(){

EditableTableItem newItem = new EditableTableItem("new row", new Integer(2));

viewer.add(newItem);

}

}



protected void buildControls(){

FillLayout compositeLayout = new FillLayout();

setLayout(compositeLayout);

final Table table = new Table(this, SWT.FULL_SELECTION);

viewer = buildAndLayoutTable(table); //table 생성

attachContentProvider(viewer);

attachLabelProvider(viewer); //data 제공

attachCellEditors(viewer, table); //Data Editor 제공

MenuManager popupMenu = new MenuManager();//  팝업 메뉴 생성

IAction newRowAction = new NewRowAction();

popupMenu.add(newRowAction);

Menu menu = popupMenu.createContextMenu(table);

table.setMenu(menu);

viewer.setInput(CONTENT);

}

private void attachLabelProvider(TableViewer viewer){

viewer.setLabelProvider(new ITableLabelProvider(){

public Image getColumnImage(Object element, int columnIndex){

return null;

}

public String getColumnText(Object element, int columnIndex){

switch(columnIndex){

case 0:

return ((EditableTableItem)element).name;

case 1:

Number index = ((EditableTableItem)element).value;

return VALUE_SET[index.intValue()];

default:

return "Invalid column: " + columnIndex;

}

}

public void addListener(ILabelProviderListener listener){

}

public void dispose(){

}

public boolean isLabelProperty(Object element, String property){

return false;

}

public void removeListener(ILabelProviderListener lpl){

}

});

}

private void attachContentProvider(TableViewer viewer){

viewer.setContentProvider(new IStructuredContentProvider(){

public Object[] getElements(Object inputElement){

return (Object[])inputElement;

}

public void dispose(){

}

public void inputChanged(Viewer viewer, Object oldInput, Object newInput){

}

});

}

private TableViewer buildAndLayoutTable(final Table table){

TableViewer tableViewer = new TableViewer(table);

TableLayout layout = new TableLayout();

layout.addColumnData(new ColumnWeightData(50, 75, true));

layout.addColumnData(new ColumnWeightData(50, 75, true));

table.setLayout(layout);

TableColumn nameColumn = new TableColumn(table, SWT.CENTER);

nameColumn.setText("Name");

TableColumn valColumn = new TableColumn(table, SWT.CENTER);

valColumn.setText("value");

table.setHeaderVisible(true);

return tableViewer;

}

private void attachCellEditors(final TableViewer viewer, Composite parent){

viewer.setCellModifier(new ICellModifier(){

public boolean canModify(Object element, String property){

return true;

}

public Object getValue(Object element, String property){

if(NAME_PROPERTY.equals(property))

return ((EditableTableItem)element).name;

else

return ((EditableTableItem)element).value;

}

public void modify(Object element, String property, Object value){

TableItem tableItem = (TableItem)element;

EditableTableItem data = (EditableTableItem)tableItem.getData();

if(NAME_PROPERTY.equals(property))

data.name = value.toString();

else

data.value = (Integer)value;

viewer.refresh(data);

}

});

viewer.setCellEditors(new CellEditor[]{

new TextCellEditor(parent), new ComboBoxCellEditor(parent, VALUE_SET)

});

viewer.setColumnProperties(new String[] { NAME_PROPERTY, VALUE_PROPERTY});

}

}

class EditableTableItem{

public String name;

public Integer value;

public EditableTableItem(String n, Integer v){

name=n;

value=v;

}

}


반응형

'RCP > SWT & JFACE' 카테고리의 다른 글

RCP 프레임워크의 Extension and Extension Points  (0) 2016.12.23
현재 화면의 활성화된 shell 얻는 방법  (0) 2016.12.23
JFace의 treeList 예제  (0) 2016.12.23
JFACE TreeViewer 예제  (0) 2016.12.23
SashForm 소개와 예제  (3) 2016.12.22