我有一个树视图,其中一列包含字符串。 我做了一个“添加按钮”。
我想要的是当用户添加一行时,我想将“编辑光标”移动到新行。
我有两个问题:
使用
添加新数据时tableViewer.add()
tableViewer.getTable().getItemCount()
未更新!所以我不能选择最后一行...
另外,当我设置ColumnViewerEditorActivationStrategy
时。我无法再使用tableViewer.setSelection()
这里是详细的源代码
public class CompositeTableViewer extends Composite {
private TableViewer tableViewer;
private List<String> columnNames;
int correctionSelection = 0;
public CompositeTableViewer(Composite parent, int style, List<String> columnNames, boolean rowAddable)
{
this(parent, style, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER, columnNames, rowAddable);
}
public CompositeTableViewer(Composite parent, int style, int tableViewerStyle, List<String> columnNames, boolean rowAddable)
{
super(parent, style);
GridLayout layout = new GridLayout(3, false);
layout.marginWidth = 4;
this.setLayout(layout);
this.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
setTableViewer(new TableViewer(this, tableViewerStyle));
TextCellEditor columnEditor = new TextCellEditor(tableViewer.getTable());
CellEditor[] editors = { columnEditor };
this.columnNames = columnNames;
tableViewer.setColumnProperties(this.columnNames.toArray(new String[this.columnNames.size()]));
tableViewer.setContentProvider(new ArrayContentProvider());
tableViewer.setLabelProvider(new LabelProvider());
tableViewer.setCellEditors(editors);
tableViewer.setCellModifier(new StringListCellModifier(this));
//here I restricted the edit activation to double-click
changeColumnViewerEditorActivationStrategy();
final Table table = tableViewer.getTable();
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
table.setLinesVisible(true);
createButtons(this, rowAddable);
}
public void setInput(List<Object> dataList)
{
this.tableViewer.setInput(dataList);
}
/**
* change the edition activation to double click
* otherwise that's block multi selection :/
*
*/
public TableViewerFocusCellManager changeColumnViewerEditorActivationStrategy()
{
TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(tableViewer, new FocusCellOwnerDrawHighlighter(tableViewer));
ColumnViewerEditorActivationStrategy activationSupport = new ColumnViewerEditorActivationStrategy(tableViewer)
{
protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event)
{
// Enable editor only with mouse double click
if (event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION)
{
EventObject source = event.sourceEvent;
if (source instanceof MouseEvent && ((MouseEvent) source).button == 3)
return false;
return true;
}
return false;
}
};
TableViewerEditor.create(tableViewer, focusCellManager, activationSupport, ColumnViewerEditor.TABBING_HORIZONTAL
| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);
return focusCellManager;
}
/**
* Add the "Add" and "Delete" buttons
*
* @param parent
* the parent composite
*/
private void createButtons(Composite parent, boolean rowAddable)
{
// Create and configure the "Add" button
Button add = new Button(parent, SWT.PUSH | SWT.CENTER);
add.setText("Add");
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
gridData.widthHint = 80;
add.setLayoutData(gridData);
add.setEnabled(rowAddable);
add.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
tableViewer.add("-_-"); // suppose to be an empty string
int selection = CompositeTableViewer.this.tableViewer.getTable().getItemCount();
CompositeTableViewer.this.tableViewer.setSelection(
new StructuredSelection(tableViewer.getElementAt(selection - 1 + correctionSelection)), true);
tableViewer.setSelection(new StructuredSelection(CompositeTableViewer.this.tableViewer.getElementAt(selection - 1)), true);
}
});
// Create and configure the "Delete" button
Button delete = new Button(parent, SWT.PUSH | SWT.CENTER);
delete.setText("Delete");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
gridData.widthHint = 80;
delete.setLayoutData(gridData);
delete.addSelectionListener(new SelectionAdapter()
{
// Remove the selection and refresh the view
public void widgetSelected(SelectionEvent e)
{
Object[] dataTable = (Object[]) ((IStructuredSelection) tableViewer.getSelection()).toArray();
if (dataTable != null)
{
tableViewer.remove(dataTable);
}
}
});
}
}
修改 问题是 ArraycontentProvider 不能用于可编辑的表。 我不应该使用列表作为模型。我换了一个List。 我在评论我的新源代码
答案 0 :(得分:0)
我导入了你的代码并对其进行了一些修改,而且我看到的一些东西似乎是错误的。
首先,ArraycontentProvider在java doc中说明了这一点:
IStructuredContentProvider的这个实现处理案例 其中查看器输入是一个不变的数组或集合 元素
因此,您的首要任务是编写自己的内容提供商。这很重要,因为您需要更新已设置的输入,此内容提供商可能不会为您执行此操作。
其次,您需要添加列以使其可编辑。例如:
TableViewerColumn c1 = new TableViewerColumn(tableViewer, tableViewerStyle);
c1.setLabelProvider(new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
}
});
c1.getColumn().setWidth(200);
c1.setEditingSupport(new FirstNameEditingSupport(tableViewer));
这将使您的专栏可以使用专用编辑器进行编辑。 (在这种情况下的文字。我从一个很好的教程中偷了编辑器,你可以在这里找到:
http://www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html#jfaceeditor
现在通过这些更改,我设法解决了添加问题。 您需要有一个可以更新的模型对象。就我而言,我创建了:
public static class TestObject {
public TestObject(String text) {
this.text = text;
}
String text;
}
这是必需的,因为在编辑器中,您必须使用编辑的内容更新查看器的内容。
我没做的是创建一个合适的ContentProvider,所以在我的例子中我永远不会坚持我的更新。但是,添加工作,双击工作和编辑工作。
现在选择。 tableviewer add方法说:
这个方法应该在单个时调用(由内容提供者提供) 元素已添加到模型中,以便使查看者能够 准确反映模型。此方法仅影响查看者,而不是 该模型。请注意,还有另一种有效的方法 处理同时添加多个元素。
这将返回给您的内容提供商。它需要处理已添加/编辑/删除内容的事实并相应地更新模型对象。
这应该让你现在开始。看看Lars vogel的教程,它非常好,清楚该怎么做。让我知道是否有更多问题,我会尝试创建一个更好的例子。
阿图尔
P.S。:复制/粘贴您的代码并没有为我编译。尝试为您的问题提供一个迷你独立示例。这样就可以更轻松地对其进行编码并为您提供更详细的指针。
答案 1 :(得分:0)
这是我的新版本
package features.dataManagement.entity.table.model;
public class RowStringModel
{
private String content;
public RowStringModel(String content)
{
super();
this.content = content;
}
public String getContent()
{
return content;
}
public void setContent(String content)
{
this.content = content;
}
}
RowStringModel
package features.dataManagement.entity.table.edit;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import features.dataManagement.entity.table.model.RowStringModel;
public class StringEditingSupport extends EditingSupport
{
private final TableViewer viewer;
private final CellEditor editor;
public StringEditingSupport(TableViewer viewer)
{
super(viewer);
this.viewer = viewer;
this.editor = new TextCellEditor(viewer.getTable());
}
@Override
protected CellEditor getCellEditor(Object element) {
return editor;
}
@Override
protected boolean canEdit(Object element) {
return true;
}
@Override
protected Object getValue(Object element) {
return ((RowStringModel) element).getContent();
}
@Override
protected void setValue(Object element, Object userInputValue) {
((RowStringModel) element).setContent(String.valueOf(userInputValue));
viewer.update(element, null);
}
}
StringEditingSupport
package features.dataManagement.entity.table.model;
import java.util.List;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
public class RowStringContentProvider implements IStructuredContentProvider, IItemModelListViewer<RowStringModel>
{
private TableViewer tableViewer;
private List<RowStringModel> modelList;
public RowStringContentProvider(TableViewer tableViewer, List<RowStringModel> modelList)
{
this.tableViewer = tableViewer;
this.modelList = modelList;
}
public void inputChanged(Viewer v, Object oldInput, Object newInput)
{
}
public void dispose()
{
}
// Return the tasks as an array of Objects
public Object[] getElements(Object parent)
{
return modelList.toArray();
}
public void addItemModel(RowStringModel itemModel)
{
tableViewer.add(itemModel);
}
@Override
public void removeItemModel(RowStringModel itemModel)
{
tableViewer.remove(itemModel);
}
@Override
public void updateItemModel(RowStringModel itemModel)
{
tableViewer.update(itemModel, null);
}
}
RowStringContentProvider
buffer-quit-function