如何设置在 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();
}
}
}