[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.platform.swt] Table cell editing like Excel
|
Dear Community,
we have to implement a table cell editing mode like Excel does:
1. activating the cell editor with any numeric key
2. set the used key in the current cell
3. changing to the next cell below current cell when a CR is pressed.
I prepared a code snippet. Topic 1 and 2 are mostly working, but it
isn't clear how to solve 3. (using a olumnViewerEditorActivationListener
??).
Any comments are welcome. Maybe we can use this snippet as a starter for
an additional JFaceSnippet.
Regards,
H. Steenblock
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationListener;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.ColumnViewerEditorDeactivationEvent;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.nebula.jface.gridviewer.GridTableViewer;
import org.eclipse.nebula.jface.gridviewer.GridViewerColumn;
import org.eclipse.nebula.jface.gridviewer.GridViewerEditor;
import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Snippet052DouleClickCellEditorModified
{
private Pattern m_IsValidPattern = null;
public class MyModel
{
private String sCounter = "";
public MyModel(){
}
public void setCounter(String sCounter)
{
this.sCounter = sCounter;
}
public String toString()
{
return sCounter;
}
}
public class MyTextCellEditor extends TextCellEditor
{
/**
* search pattern is Numeric character
*/
private Pattern m_IsValidPattern = null;
public MyTextCellEditor(Grid parent) {
super(parent);
m_IsValidPattern = Pattern.compile("[\\w+-\\.]");
}
@Override
public void activate(ColumnViewerEditorActivationEvent
activationEvent) {
// set the activation character
String s1 = String.valueOf( activationEvent.character );
Matcher matcher = m_IsValidPattern.matcher( s1 );
if (matcher.matches()) {
doSetValue(s1);
}
super.activate(activationEvent);
}
}
public Snippet052DouleClickCellEditorModified(Shell shell)
{
/* allow numbers and characters */
m_IsValidPattern = Pattern.compile("[\\w+-\\.]");
final GridTableViewer v = new GridTableViewer(shell, SWT.BORDER
| SWT.H_SCROLL | SWT.V_SCROLL);
v.setCellEditors(new CellEditor[] {
new MyTextCellEditor(v.getGrid()),
new MyTextCellEditor(v.getGrid())
});
v.setCellModifier(new ICellModifier() {
public boolean canModify(Object element, String property) {
return true;
}
public Object getValue(Object element, String property) {
return element.toString();
}
public void modify(Object element, String property, Object
value) {
if (element instanceof GridItem) {
GridItem item = (GridItem)element;
MyModel model = (MyModel)(item.getData());
model.setCounter(value.toString());
v.update(model, null);
}
}
});
ColumnViewerEditorActivationListener listener = new
ColumnViewerEditorActivationListener() {
public void afterEditorActivated(
ColumnViewerEditorActivationEvent event)
{
}
public void afterEditorDeactivated(
ColumnViewerEditorDeactivationEvent event)
{
// how to switch to the next cell?
}
public void beforeEditorActivated(
ColumnViewerEditorActivationEvent event)
{
}
public void beforeEditorDeactivated(
ColumnViewerEditorDeactivationEvent event)
{
}
};
v.setContentProvider(new ArrayContentProvider());
v.setColumnProperties(new String[] { "1", "2" });
v.getGrid().setCellSelectionEnabled(true);
v.getGrid().setLinesVisible(true);
v.getGrid().setHeaderVisible(true);
ColumnViewerEditorActivationStrategy actSupport = new
ColumnViewerEditorActivationStrategy(
v) {
protected boolean isEditorActivationEvent(
ColumnViewerEditorActivationEvent event)
{
boolean result;
String s1 = String.valueOf( event.character );
Matcher matcher = m_IsValidPattern.matcher( s1 );
boolean bIsCharKey = matcher.matches();
boolean bEnableKey =
(event.keyCode == SWT.CR) ||
(event.keyCode == SWT.KEYPAD_CR) ||
(event.keyCode == SWT.F2) ||
bIsCharKey;
result = event.eventType ==
ColumnViewerEditorActivationEvent.TRAVERSAL
|| event.eventType ==
ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
|| (event.eventType ==
ColumnViewerEditorActivationEvent.KEY_PRESSED && bEnableKey)
|| event.eventType ==
ColumnViewerEditorActivationEvent.PROGRAMMATIC;
return result;
}
};
GridViewerEditor.create(v, actSupport,
ColumnViewerEditor.TABBING_HORIZONTAL
| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
| ColumnViewerEditor.TABBING_VERTICAL
| ColumnViewerEditor.KEYBOARD_ACTIVATION);
v.getColumnViewerEditor().addEditorActivationListener(listener);
GridViewerColumn column = new GridViewerColumn(v, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setMoveable(true);
column.getColumn().setText("Column 1");
column.setLabelProvider(new ColumnLabelProvider());
column = new GridViewerColumn(v, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setMoveable(true);
column.getColumn().setText("Column 2");
column.setLabelProvider(new ColumnLabelProvider());
MyModel[] model = createModel();
v.setInput(model);
}
private MyModel[] createModel() {
MyModel[] elements = new MyModel[10];
for (int i = 0; i < 10; i++)
{
elements[i] = new MyModel();
if (i<3){
Double h = Double.valueOf(i);
elements[i].setCounter( h.toString() );
}
}
return elements;
}
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new Snippet052DouleClickCellEditorModified(shell);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}