显示CheckBoxTable中EMF模型的所有枚举?

时间:2012-06-19 15:04:34

标签: eclipse eclipse-plugin eclipse-emf eclipse-emf-ecore emf

我试图在Eclipse页面中显示一个CheckBoxTable,它允许用户选择许多项目中的任何一项 - 可用的项目来自EMF模型并且是枚举。

我已经正确设置了内容提供商和标签提供商(我认为),但我无法确定如何设置输入以显示完整的枚举列表。

所以说我的模型有一个名为MyEnum的枚举,其值为ONE,TWO和THREE - 我希望能够将这三个枚举显示给用户作为复选框。

我需要在查看器上调用setInput(...)但是我应该将其传递给它以获取这些枚举?

2 个答案:

答案 0 :(得分:0)

虽然我从来没有为CheckboxTableViewer完成此操作,但我已将EEnum设置为StructuredViewer等其他ComboViewer类的值的来源。我所做的是创建一个自定义IStructuredContentProvider,它是ArrayList的子类,并将EEnum作为构造函数参数(调用此类EEnumContentProvider)。在构造函数中,我遍历EEnum的{​​{1}}并在每个getELiterals()值上调用add()。像这样:

getInstance()

您可以通过返回public EEnumContentProvider(EEnum source) { List<EEnumLiteral> literals = source.getELiterals(); for (EEnumLiteral aLiteral : literals) { add(aLiteral.getInstance()); } } 的结果轻松实现IStructuredContentProvider.getElements(Object)而您不关心toArray(),因为内容不是基于输入,而是静态的

然后,您可以将IContentProvider.setInput()的实例设置为查看者的内容提供者。

答案 1 :(得分:0)

只需要获取文字并将它们添加到控件中,如下所示:

/* Populate the Combo Box with the Literals */    
EEnum cFEnum = Package.Literals.LITERAL_ENUMERATION;
/* 
 * Add an EMPTY item value so that the user can disable the specific 
 * feature 
 */
this.cmbNames.add( EMPTY_STRING );

/*
 * Add the  Enumeration Literals to the 
 * appropriate SWT Combo widget.
 */
for (int i=0; i<cFEnum.getELiterals().size(); i++){         
  this.cmbNames.add( cFEnum.getEEnumLiteral( i ).toString() );
}
cFEnum = null;


String[] sortedTypes = this.cmbNames.getItems();
Arrays.sort( sortedTypes );
this.cmbNames.setItems( sortedTypes );