如何在JFace TableViewerColumn
中设置要输入的最大字符数?有没有办法获得控制并投射它?
所以,这里(几乎)完整的课程:
public class CustomerTab extends Composite implements ITableLabelProvider {
org.eclipse.swt.layout.GridLayout layout = null;
Table table = null;
GridData griddata = null;
TableViewer viewer = null;
private CustomerSearchFilter filter;
public CustomerTab(Composite arg1, int arg2) throws SQLException {
super(arg1, arg2);
layout = new org.eclipse.swt.layout.GridLayout(GridData.FILL_BOTH,
false);
layout.numColumns = 1;
this.setLayout(layout);
Button button = new Button(this, SWT.PUSH);
button.setText("New customer");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
WizardDialog wizardDialog = new WizardDialog(ApplicationRunner
.getApp().getShell(), new NewCustomerWizard());
if (wizardDialog.open() == Window.OK) {
System.out.println("Ok pressed");
try {
viewer.setInput(CustomerDAO.getInstance().getAll());
viewer.refresh();
} catch (SQLException e1) {
// TODO something
}
} else {
System.out.println("Cancel pressed");
}
}
});
final Text searchText = new Text(this, SWT.BORDER | SWT.SEARCH);
searchText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
| GridData.HORIZONTAL_ALIGN_FILL));
addTable();
// Support the search
searchText.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent ke) {
filter.setSearchText(searchText.getText());
viewer.refresh();
}
});
filter = new CustomerSearchFilter();
viewer.addFilter(filter);
}
public void addTable() throws SQLException {
viewer = new TableViewer(this, SWT.BORDER | SWT.MULTI
| SWT.FULL_SELECTION);
final Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
createColumns(this, viewer);
viewer.setContentProvider(new ArrayContentProvider());
viewer.setInput(CustomerDAO.getInstance().getAll());
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 2;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
viewer.getControl().setLayoutData(gridData);
}
private void createColumns(final Composite parent, final TableViewer viewer) {
String[] titles = { "ID", "Firma", "Anrede", "Vorname", "Nachname",
"Strasse", "Plz", "Ort", "Tel. privat", "Tel. gesch.", " " };
int[] bounds = { 30, 130, 50, 100, 100, 100, 50, 100, 100, 100, 30 };
// Col 1, ID
TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
Customer p = (Customer) element;
return String.valueOf(p.getId());
}
});
col.setEditingSupport(new TestFirstNameEditingSupport(viewer));
// other columns here...
// Col 4, Firstname
col = createTableViewerColumn(titles[3], bounds[3], 3);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
Customer p = (Customer) element;
return p.getFirstname();
}
});
col.setEditingSupport(new TestFirstNameEditingSupport(viewer));
// and the rest of the columns here
}
private TableViewerColumn createTableViewerColumn(String title, int bound,
final int colNumber) {
final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
column.setText(title);
column.setWidth(bound);
column.setResizable(true);
column.setMoveable(true);
return viewerColumn;
}
// LabelProvider
@Override
public void addListener(ILabelProviderListener arg0) {
}
@Override
public boolean isLabelProperty(Object arg0, String arg1) {
return false;
}
@Override
public void removeListener(ILabelProviderListener arg0) {
}
@Override
public Image getColumnImage(Object arg0, int arg1) {
return null;
}
@Override
public String getColumnText(Object arg0, int arg1) {
String result = "";
switch (arg1) {
case 0:
result = "ID";
break;
...
case 4:
result = "Firstname";
break;
...
default:
// should not get here
result = "";
}
return result;
}
static class TestFirstNameEditingSupport extends EditingSupport {
private final TableViewer viewer;
private TextCellEditor editor = null;
public TestFirstNameEditingSupport(TableViewer viewer) {
super(viewer);
this.viewer = viewer;
editor = new TextCellEditor(viewer.getTable());
Text text = (Text) editor.getControl();
// set maximal length HERE
text.setTextLimit(3);
}
public CellEditor getMyCellEditor(Object element) {
return editor;
}
@Override
protected CellEditor getCellEditor(Object element) {
return new TextCellEditor(viewer.getTable());
}
@Override
protected boolean canEdit(Object element) {
return true;
}
@Override
protected Object getValue(Object element) {
return ((de.devision.data.bean.Customer) element).getVorname();
}
@Override
protected void setValue(Object element, Object value) {
if (String.valueOf(value).length() <= 2 || String.valueOf(value).equals("")) {
MessageDialog.openInformation(ApplicationRunner.getApp().getShell(), "to short!", "at least 3 characters!");
return;
}
((Customer) element).setVorname(String.valueOf(value));
try {
CustomerDAO.getInstance().update(((Customer) element));
} catch (SQLException e) {
// TODO maybe later
}
viewer.refresh();
}
}
}
答案 0 :(得分:2)
好的,终于搞定了:
private static void createViewer(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
createColumns(parent, viewer);
final Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
viewer.setContentProvider(new ArrayContentProvider());
// Get the content for the viewer, setInput will call getElements in the
// contentProvider
viewer.setInput(ModelProvider.INSTANCE.getPersons());
// Set the sorter for the table
// Layout the viewer
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 2;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
viewer.getControl().setLayoutData(gridData);
comparator = new PersonViewerComparator();
viewer.setComparator(comparator);
}
// This will create the columns for the table
private static void createColumns(final Composite parent,
final TableViewer viewer) {
String[] titles = { "First name", "Last name", "Gender", "Married" };
int[] bounds = { 100, 100, 100, 100 };
// First column is for the first name
TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
Person p = (Person) element;
return p.getFirstName();
}
});
col.setEditingSupport(new CheckLengthEditingSupport(viewer));
// Second column is for the last name
col = createTableViewerColumn(titles[1], bounds[1], 1);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
Person p = (Person) element;
return p.getLastName();
}
});
// Now the gender
col = createTableViewerColumn(titles[2], bounds[2], 2);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
Person p = (Person) element;
return p.getGender();
}
});
// // Now the status married
col = createTableViewerColumn(titles[3], bounds[3], 3);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return "";
}
@Override
public Image getImage(Object element) {
if (((Person) element).isMarried()) {
return CHECKED;
} else {
return UNCHECKED;
}
}
});
}
static class CheckLengthEditingSupport extends EditingSupport {
private final TableViewer viewer;
private TextCellEditor editor = null;
public FirstNameEditingSupport(TableViewer viewer) {
super(viewer);
this.viewer = viewer;
editor = new TextCellEditor(viewer.getTable());
Text text = (Text) editor.getControl();
// set maximal length HERE
text.setTextLimit(3);
}
@Override
protected CellEditor getCellEditor(Object element) {
return editor;
}
@Override
protected boolean canEdit(Object element) {
return true;
}
@Override
protected Object getValue(Object element) {
return ((YourObject) element).getName();
}
@Override
protected void setValue(Object element, Object value) {
((YourObject) element).setName(String.valueOf(value));
viewer.update(element, null);
}
}
您必须在TextCellEditor
类中设置EditingSupport
,并使用protected CellEditor getCellEditor(Object element)
方法将其返回。
顺便说一句:如果您想要“少于x个字符”的其他限制,只需向VerifyListener
中的text
添加TextCellEditor
即可。