swt/jface EditingSupportを使用して同じ列に異なるタイプのエディタCellEditorを提供


カスタムMyEditingSupportはEditingSupportを実現し、コードは以下の通りである.

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;

public class MyEditingSupport extends EditingSupport {
	private static final String[] CROPPRING_SEASONS = { "1", "2" ,"3", "4", "5" };
	private static final String[] GENERATION = { "F1DH", "F2" ,"F3", "F4", "F5" ,"F6", "F7" ,"F8"};
	private static final String[] ROUNDS_INTER = { "1", "2" ,"3", "4:3" };
	private static final String[] ROUNDS_SELFING = { "1", "2" ,"3", "4", "5" ,"6" ,"7" ,"8:5" };
	private CellEditor[] editors;
	private StructuredViewer viewer;

	public MyEditingSupport(TableViewer viewer) {
		super(viewer);
		this.viewer = viewer;
		editors = new CellEditor[10];
		editors[0] = new ComboBoxCellEditor(viewer.getTable(), CROPPRING_SEASONS, SWT.READ_ONLY);
		editors[1] = new TextCellEditor(viewer.getTable());
		editors[2] = new ComboBoxCellEditor(viewer.getTable(), GENERATION, SWT.READ_ONLY);
		editors[3] = new ComboBoxCellEditor(viewer.getTable(), GENERATION, SWT.READ_ONLY);
		editors[4] = new ComboBoxCellEditor(viewer.getTable(), ROUNDS_INTER, SWT.READ_ONLY);
		editors[5] = new ComboBoxCellEditor(viewer.getTable(), ROUNDS_SELFING, SWT.READ_ONLY);
		editors[6] = new TextCellEditor(viewer.getTable());
		editors[7] = new TextCellEditor(viewer.getTable());
		editors[8] = new TextCellEditor(viewer.getTable());
		editors[9] = new TextCellEditor(viewer.getTable());
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		InputInfoItem item = (InputInfoItem) element;
		for(int i = 1 ; i<= 10 ; i++){
			if(item.getId().equals(String.valueOf(i)))
				return editors[i-1];
		}
		/*if (item.getId().equals("1")) {
			return editors[0];
		} else if (item.getId().equals("2")) {
			return editors[1];
		} */
		return null;
	}

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

	@Override
	protected Object getValue(Object element) {
		InputInfoItem item = (InputInfoItem) element;
		if (item.getId().equals("1")) {
			for(int i = 0 ; i<CROPPRING_SEASONS.length ; i++){
				if(CROPPRING_SEASONS[i].equals(item.getValue()))
					return new Integer(i);
			}
        } else if (item.getId().equals("2")) {
        	return item.getValue();
        } else if (item.getId().equals("3")) {
        	for(int i = 0 ; i<GENERATION.length ; i++){
				if(GENERATION[i].equals(item.getValue()))
					return new Integer(i);
			}
        } else if (item.getId().equals("4")) {
        	for(int i = 0 ; i<GENERATION.length ; i++){
				if(GENERATION[i].equals(item.getValue()))
					return new Integer(i);
			}
        } else if (item.getId().equals("5")) {
        	for(int i = 0 ; i<ROUNDS_INTER.length ; i++){
				if(ROUNDS_INTER[i].equals(item.getValue()))
					return new Integer(i);
			}
        } else if (item.getId().equals("6")) {
        	for(int i = 0 ; i<ROUNDS_SELFING.length ; i++){
				if(ROUNDS_SELFING[i].equals(item.getValue()))
					return new Integer(i);
			}
        } else if (item.getId().equals("7")) {
        	return item.getValue();
        } else if (item.getId().equals("8")) {
        	return item.getValue();
        } else if (item.getId().equals("9")) {
        	return item.getValue();
        } else if (item.getId().equals("10")) {
        	return item.getValue();
        }
		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {
		InputInfoItem item = (InputInfoItem) element;
		if(item.getId().equals("1")) {
			item.setValue(CROPPRING_SEASONS[Integer.parseInt(value.toString())]);
		} else if(item.getId().equals("2")) {
			item.setValue(value.toString());
		} else if(item.getId().equals("3")) {
			item.setValue(GENERATION[Integer.parseInt(value.toString())]);
		} else if(item.getId().equals("4")) {
			item.setValue(GENERATION[Integer.parseInt(value.toString())]);
		} else if(item.getId().equals("5")) {
			item.setValue(ROUNDS_INTER[Integer.parseInt(value.toString())]);
		} else if(item.getId().equals("6")) {
			item.setValue(ROUNDS_SELFING[Integer.parseInt(value.toString())]);
		} else if(item.getId().equals("7")) {
			item.setValue(value.toString());
		} else if(item.getId().equals("8")) {
			item.setValue(value.toString());
		} else if(item.getId().equals("9")) {
			item.setValue(value.toString());
		} else if(item.getId().equals("10")) {
			item.setValue(value.toString());
		} 
        this.viewer.refresh();
	}
}

表は3列、10行あり、3列目にEditingSupportを追加

tableViewer = new TableViewer(this,SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(60);
		tableColumn.setText("ID");
		TableColumn tableColumn1 = new TableColumn(table, SWT.NONE);
		tableColumn1.setWidth(220);
		tableColumn1.setText("Name");
		TableColumn tableColumn2 = new TableColumn(table, SWT.NONE);
		tableColumn2.setWidth(80);
		tableColumn2.setText("Value");
		this.setLayout(new FillLayout());
		this.setBackground(new Color(Display.getCurrent(), 255, 255, 255));
		

		[color=red]TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer,tableColumn2);
	    tableViewerColumn.setEditingSupport(new MyEditingSupport(tableViewer));[/color]		
	    tableViewer.setLabelProvider();
	    tableViewer.setContentProvider();
		
	    tableViewer.setInput(inputInfoList);