【发布时间】:2015-07-05 22:06:33
【问题描述】:
我正在开发一个 Eclipse 产品,它将打开一个向导,该向导的内容取决于以前的用户选择。我创建了几个 WizardPages,它们读取全局参数,然后显示相应的内容。
但我正在努力使用最新的页面,该页面应该显示一个包含任意数量的列和任意数量的行的表格。
因为表格的内容取决于用户在之前页面上的输入,所以页面的创建不是在public void createControl(Composite parent) 中完成的。在这个函数中,我创建了一个全局 composite,它将托管在页面可见时创建的表,因为此时需要的信息可用。
列的数量和标题取决于用户输入,就像行数一样。
我添加了一个示例,其中表应该有 3 列和 ~15 行。除了最后一列之外的每个单元格都包含一个复选框。最后一列的单元格包含文本。但是正如您所看到的,WizardPage 的大部分内容是灰色的,并且在底部有一个可以看到表格的小区域。看起来好像有某种覆盖。此外,整个表格看起来像是向左移动了,因为您只能在示例图片上看到两列。
如果我在 ScrolledComposite 中添加所有内容,那么即使是小区域也不可见。 (稍后我将更改为 ScrolledComposite,因为表格可能会变得非常大。)我调试了项目,我可以看到页面构建正确,复合 composite 仅托管表格而没有其他元素。创建的所有列标题和行数都正确,但我无法正确显示表格。
感谢您的每一个建议。
代码如下:
public class selectionListPage extends CoevolutionDecisionPage implements SelectionListener {
private ScrolledComposite scrolledComposite;
private Composite composite;
private Table table;
private HashMap<String, String> parameters;
public selectionListPage(String name, HashMap<String, String> parameters) {
super(name);
this.setTitle(name);
this.parameters = parameters;
}
@Override
public void createControl(Composite parent) {
composite = new Composite(parent, SWT.NULL);
composite.setLayout(new GridLayout());
setControl(composite);
}
@Override
public void setVisible(boolean visible) {
super.setVisible(visible);
if (visible) {
onEnterPage();
}
}
public void onEnterPage() {
CoevolutionWizard wizard = (CoevolutionWizard)getWizard();
// this list will contain all titles for all columns
ArrayList<String> titles = new ArrayList<String> ();
/* read titles from wizard */
// create table for data
table = new Table(composite, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
table.setLinesVisible (true);
table.setHeaderVisible (true);
table.setVisible(true);
// set layout data so that all cells are equal
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
table.setLayoutData(data);
for (int i = 0; i < titles.size(); i++) {
TableColumn column = new TableColumn (table, SWT.NONE);
column.setText (titles.get(i));
}
// add table column for attributes
TableColumn column = new TableColumn (table, SWT.NONE);
column.setText ("Attribute");
// this list will contain all relevant elements for all inspected elements
ArrayList<ArrayList<String>> allElements = new ArrayList<ArrayList<String>> ();
/* read data from wizard */
// this counter indicates which column is default value for items
int elementCount = 0;
// every element has an own element of nested elements
for (ArrayList<String> list : allElements) {
// iterate over all nested elements
for (String element : list) {
// each row has one tableItem which contains all cells and meta data about items
TableItem item = new TableItem (table, SWT.NONE);
// for every column a cell will be created
for (int j = 0; j < titles.size()-1; j++) {
// create a checkbutton
Button checkButton = new Button(table, SWT.CHECK);
TableEditor editor = new TableEditor(table);
// pack button for correct display size and set listener
checkButton.pack();
checkButton.addSelectionListener(this);
// set size and alignment options for this cell
editor.minimumWidth = checkButton.getSize().x;
editor.horizontalAlignment = SWT.CENTER;
// add button to meta data of table item
item.setData("checkButton" + j, checkButton);
// add element to table
editor.setEditor(checkButton, item, j);
}
// add text
item.setText (titles.size(), titles.get(titles.size()-1));
// add additional meta data
item.setData("name", titles.get(titles.size()-1));
item.setData("default", titles.get(elementCount));
}
elementCount++;
}
for (int j = 0; j < titles.size(); j++) {
table.getColumn(j).pack();
}
composite.pack();
// proceeding from this page is possible at every moment
this.setPageComplete(true);
}
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("click");
}
编辑:添加了 CoevolutionDecisionPage 的代码
/**
* Superclass for all decision pages of the refactoring wizard.
*/
public abstract class CoevolutionDecisionPage extends WizardPage {
/**
* Creates a decision page for the refactoring wizard.
*
* @param refElem
* Refactoring element.
* @param decision
* Decision to show
*/
public CoevolutionDecisionPage(String name) {
super(name);
this.setPageComplete(false);
}
/**
* Is called as soon as data to execute the query delivering elements to
* decide is available. This is important because a decision can depend on
* previous decisions.
*/
abstract public void updateData();
}
Edit2:我没有解决问题,但我认为我有一些解决方案的相关信息(至少我希望如此)。
在沮丧的时刻,我将 Layout 从 createControl(Composite parent) 内部的复合父级更改为 FillLayout,然后将 Wizard 分为与 WizardPages 一样多的相等“列”。每列都包含正确的 WizardPage 并在正确的时间显示它。所以 FillLayout 的行为是正确的。但这当然不是一个长期的解决方案,但让我可以从事其他任务。它允许我验证表的正确创建。
所以我在不工作的页面之后添加了另一个 WizardPage。此页面仅包含一些标签(它是最后一页并显示摘要)。但是这个页面显示了与我的selectionListPage 一样的奇怪行为(如果父布局未设置为 FillLayout):它是“覆盖的”。
所以我认为问题出在前几页中。我总共有四个 WizardPages:
- 欢迎页面显示从“全局”参数读取的一些信息(仅文本)。
- 用户必须在任意数量的文本字段中输入名称的页面(动态创建和删除,因此每时每刻只存在一个空文本字段)
- 所描述的 selectionListPage 包含一个表格。
- 摘要页面。
第一个页面的行为与预期相同,但其他两个页面可能未显示。
【问题讨论】:
-
为什么要延迟创建表,直到 setVisible。为什么不在正常的地方建表,直接把数据放到setVisible里的表里。
-
因为事先不知道行数和列数。
-
我将表的创建移动到
createControl()并在 onEnterPage() 中用列和 TableItems 填充它,但它没有任何改变。 -
我也将布局从复合更改为 FillLayout,因为只有一个孩子(表格),但这也无济于事。
-
在快速测试中,这段代码似乎对我有用。
CoevolutionDecisionPage是做什么的?它是否以某种方式干扰?
标签: java eclipse swt visibility wizard