我正在实施一个JFace TableViewer
,其中一些列为ComboBoxViewerCellEditor
。
我正在为每个列设置ColumnLabelProvider
,除了使用组合框的列之外,我还设置了EditingSupport
。
EditingSupport
包含LabelProvider
内的ComboBoxViewerCellEditor
封装。
所有实现都基于JFace MVC - 即没有实现事件侦听器的查看者和提供者。
行为如下:
1.启动表格显示的值OK - 即列标签提供商提供正确的数据
2.在列中启动组合框,下拉列表中的值显示为OK,并且选择值工作正常 - 即组合框显示新值并且数据模型更改为新值
3.离开组合框时,组合框中显示的值(标签)返回列标签提供者提供的值(#1)。
似乎我不应该同时使用标签提供程序或以某种方式刷新列标签提供程序中的数据。
我正在附加我的代码,请查看同时包含columnViewerTargetObjectModelAttr
和ColumnLabelProvider
的columnViewer EditingSupport
。
我会感激任何建议, 谢谢。 亚龙。
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import myPackage.DataObjectAssociation;
import myPackage.DataObjectAssociations;
import myPackage.DataObjectAttribute;
import myPackage.DataObjectAttributeUI;
import myPackage.DataObjectUI;
public class ObjectModelAssociationPageMVC extends
ObjectModaelGenerationWizardPage {
private TableViewer omTableViewer;
private TableViewer assocTableViewer;
private Composite container;
public ObjectModelAssociationPageMVC(ISelection selection,
HanaServicesAdapter hanaServicesAdapter) {
super("wizardPage", selection, hanaServicesAdapter);
setTitle("Generate Object Model");
setDescription("This wizard generates object models based on selected tables and views");
this.hanaServicesAdapter = hanaServicesAdapter;
}
@Override
public void createControl(Composite parent) {
// main container
this.container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout(1, true);
container.setLayout(layout);
// om container
Composite omContainer = new Composite(container, SWT.NULL);
omContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
1, 1));
GridLayout omLayout = new GridLayout(1, true);
omContainer.setLayout(omLayout);
// ----- Objects List Label ------
Label objectModelLabel = new Label(omContainer, SWT.NULL);
objectModelLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
false, 1, 1));
objectModelLabel.setText("Create Object Models Association:");
// ----- Objects List ------
omTableViewer = new TableViewer(omContainer, SWT.BORDER | SWT.SELECTED);
omTableViewer.setContentProvider(new ArrayContentProvider());
omTableViewer
.addSelectionChangedListener(new ObjectModelTableViewerSelectionListener());
Table omTable = omTableViewer.getTable();
omTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1,
1));
omTable.setLinesVisible(true);
omTable.setHeaderVisible(true);
GridData omTableGridData = new GridData(SWT.FILL, SWT.FILL, true,
false, 1, 1);
omTableGridData.heightHint = omTable.getItemHeight() * 10
+ omTable.getHeaderHeight();
omTable.setLayoutData(omTableGridData);
// Column Object Name
TableViewerColumn col = new TableViewerColumn(omTableViewer, SWT.NONE);
col.getColumn().setText("Object Model Name");
col.getColumn().setWidth(200);
col.setLabelProvider(new ObjectModelNameColumnLabelProvider());
// Associations List Container
Composite assocContainer = new Composite(container, SWT.NULL);
assocContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
false, 1, 1));
GridLayout assocLayout = new GridLayout(2, false);
assocContainer.setLayout(assocLayout);
// ----- Associations List Label ------
Label assocLabel = new Label(assocContainer, SWT.NULL);
assocLabel.setText("Associations:");
assocLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
2, 1));
// Associations Table Container
Composite assocTableContainer = new Composite(assocContainer, SWT.NULL);
assocTableContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
true, false, 1, 1));
GridLayout assocTableLayout = new GridLayout(1, true);
assocTableContainer.setLayout(assocTableLayout);
// Associations add/remove buttons container
Composite assocButtonsContainer = new Composite(assocContainer,
SWT.NULL);
assocButtonsContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
true, false, 1, 1));
GridLayout assocButtonsLayout = new GridLayout(1, true);
assocButtonsContainer.setLayout(assocButtonsLayout);
// ----- Associations List ------
assocTableViewer = new TableViewer(assocTableContainer, SWT.BORDER
| SWT.MULTI | SWT.FULL_SELECTION);
assocTableViewer.setContentProvider(new ArrayContentProvider());
Table assocTable = assocTableViewer.getTable();
GridData assocTableGridData = new GridData(SWT.FILL, SWT.FILL, true,
false, 1, 1);
assocTableGridData.heightHint = assocTable.getItemHeight() * 10
+ assocTable.getHeaderHeight();
assocTable.setLayoutData(assocTableGridData);
assocTable.setLinesVisible(false);
assocTable.setHeaderVisible(true);
TableViewerColumn columnViewerAssocName = new TableViewerColumn(
assocTableViewer, SWT.NONE);
columnViewerAssocName.getColumn().setText("Association Name");
columnViewerAssocName
.setLabelProvider(new AssociationNameColumnLabelProvider());
columnViewerAssocName.getColumn().pack();
TableViewerColumn columnViewerSourceObjectModelAttr = new TableViewerColumn(
assocTableViewer, SWT.NONE);
columnViewerSourceObjectModelAttr.getColumn().setText(
"Source Object Model Attribute");
columnViewerSourceObjectModelAttr
.setLabelProvider(new SourceObjectModelAttributeNameColumnLabelProvider());
columnViewerSourceObjectModelAttr.getColumn().pack();
TableViewerColumn columnViewerTargetObjectModel = new TableViewerColumn(
assocTableViewer, SWT.NONE);
columnViewerTargetObjectModel.getColumn()
.setText("Target Object Model");
columnViewerTargetObjectModel
.setLabelProvider(new TargetObjectModelNameColumnLabelProvider());
EditingSupport targetObjectModelCulomnEditingSupport = new TargetObjectModelColumnEditingSupport(
columnViewerTargetObjectModel.getViewer());
columnViewerTargetObjectModel
.setEditingSupport(targetObjectModelCulomnEditingSupport);
columnViewerTargetObjectModel.getColumn().pack();
TableViewerColumn columnViewerTargetObjectModelAttr = new TableViewerColumn(
assocTableViewer, SWT.NONE);
columnViewerTargetObjectModelAttr.getColumn().setText(
"Target Object Model Attribute");
columnViewerTargetObjectModelAttr
.setLabelProvider(new TargetObjectModelAttributeNameColumnLabelProvider());
EditingSupport targetObjectModelAttributeColumnEditingSupport = new TargetObjectModelAttributeColumnEditingSupport(
columnViewerTargetObjectModel.getViewer());
columnViewerTargetObjectModelAttr
.setEditingSupport(targetObjectModelAttributeColumnEditingSupport);
columnViewerTargetObjectModelAttr.getColumn().pack();
TableViewerColumn columnViewerAssocCardinality = new TableViewerColumn(
assocTableViewer, SWT.NONE);
columnViewerAssocCardinality.getColumn().setText("Cardinality");
columnViewerAssocCardinality
.setLabelProvider(new CardinalityColumnLabelProvider());
columnViewerAssocCardinality.getColumn().pack();
assocTable.pack();
// ----- Associations List Buttons ------
Button addAssocButton = new Button(assocButtonsContainer, SWT.PUSH);
addAssocButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
false, 1, 1));
addAssocButton.setText("Add");
Button removeAssocButton = new Button(assocButtonsContainer, SWT.PUSH);
removeAssocButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
false, 1, 1));
removeAssocButton.setText("Remove");
// ------------ Create Objects ------------------
setControl(container);
}
class ObjectModelTableViewerSelectionListener implements
ISelectionChangedListener {
@Override
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection;
DataObjectUI dataObjectUI;
DataObjectAssociations dataObjectAssociations;
DataObjectAssociation[] dataObjectAssociation = null;
selection = (IStructuredSelection) omTableViewer.getSelection();
dataObjectUI = (DataObjectUI) selection.getFirstElement();
dataObjectAssociations = dataObjectUI.getAssociations();
if (dataObjectAssociations != null) {
List<DataObjectAssociation> list = dataObjectAssociations
.getAssociation();
dataObjectAssociation = list
.toArray(new DataObjectAssociation[list.size()]);
}
assocTableViewer.setInput(dataObjectAssociation);
// cellEditor.setInput(hanaServicesAdapter.getSelectedTablesViews()
// .toArray());
}
}
@Override
public void setVisible(boolean visible) {
super.setVisible(visible);
if (visible) {
omTableViewer.setContentProvider(new ArrayContentProvider());
omTableViewer.setInput(hanaServicesAdapter.getSelectedTablesViews()
.toArray());
}
}
class ObjectModelNameColumnLabelProvider extends ColumnLabelProvider {
@Override
public String getText(Object element) {
DataObjectUI dataObjectUI = (DataObjectUI) element;
return dataObjectUI.getFileName();
}
}
class AssociationNameColumnLabelProvider extends ColumnLabelProvider {
@Override
public String getText(Object element) {
DataObjectAssociation dataObjectAssociation = (DataObjectAssociation) element;
return dataObjectAssociation.getName();
}
}
class TargetObjectModelNameColumnLabelProvider extends ColumnLabelProvider {
@Override
public String getText(Object element) {
DataObjectAssociation dataObjectAssociation = (DataObjectAssociation) element;
return dataObjectAssociation.getTarget();
}
}
class TargetObjectModelAttributeNameColumnLabelProvider extends
ColumnLabelProvider {
@Override
public String getText(Object element) {
DataObjectAssociation dataObjectAssociation = (DataObjectAssociation) element;
return dataObjectAssociation.getTargetAttribute();
}
}
class SourceObjectModelAttributeNameColumnLabelProvider extends
ColumnLabelProvider {
@Override
public String getText(Object element) {
DataObjectAssociation dataObjectAssociation = (DataObjectAssociation) element;
return dataObjectAssociation.getSourceAttribute();
}
}
class CardinalityColumnLabelProvider extends ColumnLabelProvider {
@Override
public String getText(Object element) {
DataObjectAssociation dataObjectAssociation = (DataObjectAssociation) element;
return dataObjectAssociation.getCardinality().toString();
}
}
public final class TargetObjectModelColumnEditingSupport extends
EditingSupport {
private ComboBoxViewerCellEditor cellEditor;
private TargetObjectModelColumnEditingSupport(ColumnViewer viewer) {
super(viewer);
}
@Override
protected CellEditor getCellEditor(Object element) {
cellEditor = new ComboBoxViewerCellEditor((Composite) getViewer()
.getControl(), SWT.READ_ONLY);
cellEditor.setLabelProvider(new LabelProvider() {
public String getText(Object element) {
return ((DataObjectUI) element).getFileName();
}
});
cellEditor.setContentProvider(new ArrayContentProvider());
String target = ((DataObjectAssociation) element).getTarget();
CCombo combo = cellEditor.getViewer().getCCombo();
// combo.select(combo.indexOf(target));
combo.setText(target);
cellEditor.setInput(hanaServicesAdapter.getSelectedTablesViews()
.toArray());
return cellEditor;
}
@Override
protected boolean canEdit(Object element) {
return true;
}
@Override
protected Object getValue(Object element) {
if (element instanceof DataObjectAssociation) {
DataObjectAssociation data = (DataObjectAssociation) element;
return data.getTarget();
}
return null;
}
@Override
protected void setValue(Object element, Object value) {
if (element instanceof DataObjectAssociation
&& value instanceof DataObjectUI) {
DataObjectAssociation data = (DataObjectAssociation) element;
String newValue = ((DataObjectUI) value).getFileName();
/* only set new value if it differs from old one */
if (!data.getTarget().equals(newValue)) {
data.setTarget(newValue);
}
}
}
}
public final class TargetObjectModelAttributeColumnEditingSupport extends
EditingSupport {
private ComboBoxViewerCellEditor cellEditor;
private TargetObjectModelAttributeColumnEditingSupport(
ColumnViewer viewer) {
super(viewer);
}
@Override
protected CellEditor getCellEditor(Object element) {
cellEditor = new ComboBoxViewerCellEditor((Composite) getViewer()
.getControl(), SWT.READ_ONLY);
cellEditor.setLabelProvider(new LabelProvider() {
public String getText(Object element) {
return ((DataObjectAttributeUI) element).getName();
}
});
cellEditor.setContentProvider(new ArrayContentProvider());
CCombo combo = cellEditor.getViewer().getCCombo();
// String targetObjectModel = targetObjectCombo.getText();
DataObjectUI dataObjectUI = hanaServicesAdapter
.getDataObjectUI(((DataObjectAssociation) element)
.getTarget());
cellEditor.setInput(dataObjectUI.getSelectedAttributesUI()
.toArray());
String attribute = ((DataObjectAssociation) element)
.getTargetAttribute();
combo.setText(attribute);
return cellEditor;
}
@Override
protected boolean canEdit(Object element) {
return true;
}
@Override
protected Object getValue(Object element) {
if (element instanceof DataObjectAssociation) {
DataObjectAssociation data = (DataObjectAssociation) element;
return data.getTargetAttribute();
}
return null;
}
@Override
protected void setValue(Object element, Object value) {
if (element instanceof DataObjectAssociation
&& value instanceof DataObjectAttributeUI) {
DataObjectAssociation data = (DataObjectAssociation) element;
String newValue = ((DataObjectAttributeUI) value).getName();
/* only set new value if it differs from old one */
if (!data.getTargetAttribute().equals(newValue)) {
data.setTargetAttribute(newValue);
}
}
}
}
}
答案 0 :(得分:0)
您可能想尝试更新setValue()。
if (!data.getTargetAttribute().equals(newValue)) {
data.setTargetAttribute(newValue);
update(element, null);
}