Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » TableExample from Tobias Weih
TableExample from Tobias Weih [message #445734] Wed, 10 November 2004 07:46
Michael Seele is currently offline Michael SeeleFriend
Messages: 124
Registered: July 2009
Senior Member
hi,
i've searched on the eclipse homepage for a good swt-table + tableCursor +
ControlEditor example and found code from Tobias Weih, that he posted Fri,
23 Apr 2004 16:37:36 in the eclipse.platform newsgroup! when i copy the
code in my project, i got following errors: the classes CommandThread,
Form and Command will not be found! can somebody help me? this is the
code, i've been talking:

public class TableModifier extends TableCursor {

private Table editedTable;

private ControlEditor editor = null;

private CommandThread cmdThread;

private Form form;

protected TableModifier cursor = null;

private boolean editable_flag;

private boolean newLineButton_flag;

private int newLinePosition = SWT.NULL;

private boolean editorOpened = false;

private Widget editorWidget = null;

public static final int EMPTY_START = 0;

public static final int CONTENT_START = 1;

private Random randomizer = new Random();

/**
* @param table
* @param application reference to XinityBaseClient application
* @param style
*/

public TableModifier(Table table, CommandThread cmdThread, Form form, int
style) {
super(table, style);
editedTable = table;
this.cmdThread = cmdThread;
this.form = form;
cursor = this;

addCursorListeners();
addColumnSorters();
editor = new ControlEditor(cursor);
editor.grabHorizontal = true;
editor.grabVertical = true;

String editable_str = (String) editedTable.getData("editable");
editable_flag = false;
if ("true".equals(editable_str)) {
editable_flag = true;
}

String newline_str = (String) editedTable.getData("newline");
newLineButton_flag = false;
if ("first".equals(newline_str)) {
newLineButton_flag = true;
newLinePosition = SWT.TOP;
}
if ("last".equals(newline_str)) {
newLineButton_flag = true;
newLinePosition = SWT.BOTTOM;
}
}

/**
* */
private void addColumnSorters() {
for (int i = 0; i < editedTable.getColumnCount(); i++) {
TableColumn column = editedTable.getColumn(i);
// column.addSelectionListener(new TableSorter(i));
}

}

/**
* called from constructor, adds listeners to the cursor only code
beautify
*/
private void addCursorListeners() {
addSelectionListener(new SelectionAdapter() {

// when the TableEditor is over a cell, select the corresponding row in
// the table
public void widgetSelected(SelectionEvent e) {
if (editorOpened && editorWidget != null) {
editorWidget.dispose();
editorWidget = null;
editorOpened = false;
}
editedTable.setSelection(new TableItem[] {cursor.getRow()});

// if(newLineButton_flag && cursorOnCreateNewLine()){
// cursor.setSelection(cursor.getRow(),0);
cursor.redraw();
// }else{
// cursor.redraw();
// }
}

public void widgetDefaultSelected(SelectionEvent e) {
// what to do when Enter is pressed editedTable.setSelection(new
TableItem[] {cursor.getRow()});
if (newLineButton_flag && cursorOnCreateNewLine()) {
createNewLine();
return;
}

boolean buttonColumn = false;
TableItem row = getRow();
int column = getColumn();
try {
if ("button".equals(editedTable.getColumn(column).getData("control ")))
buttonColumn = true;
} catch (Exception exception) {}
if (buttonColumn) {
openEditor(getRowIndex(), column, CONTENT_START, '\0');
} else {
if (editable_flag) {
//move to the next row, first column
moveCursor(SWT.CR);
}
}
}
});
addMouseListener(new MouseListener() {

public void mouseDoubleClick(MouseEvent arg0) {
editedTable.setSelection(new TableItem[] {cursor.getRow()});
if (newLineButton_flag && cursorOnCreateNewLine()) {
//create new line
createNewLine();
}
// }else{
//openEditor
openEditor(getRowIndex(), getColumn(), CONTENT_START, '\0');
// }
}

public void mouseDown(MouseEvent arg0) {}

public void mouseUp(MouseEvent arg0) {}

});

addListener(SWT.DefaultSelection, new Listener() {

public void handleEvent(Event arg0) {
if (!editable_flag && cursor != null && !cursor.isDisposed()) {
cursor.getRow().notifyListeners(SWT.DefaultSelection, arg0);
}
}
});

addListener(SWT.MouseDoubleClick, new Listener() {

public void handleEvent(Event arg0) {
if (!editable_flag && cursor != null && !cursor.isDisposed()) {
cursor.getRow().notifyListeners(SWT.MouseDoubleClick, arg0);
}
}
});

addListener(SWT.Selection, new Listener() {

public void handleEvent(Event arg0) {
if (!editable_flag && cursor != null && !cursor.isDisposed()) {
cursor.getRow().notifyListeners(SWT.Selection, arg0);
}
}
});

addKeyListener(new KeyListener() {

public void keyPressed(KeyEvent arg0) {
if (editable_flag && arg0.keyCode == 16777227 && arg0.stateMask == 0) {
if (cursorOnCreateNewLine()) {
createNewLine();
}
// if(!newLineButton_flag || !cursorOnCreateNewLine()){
//openEditor
openEditor(getRowIndex(), getColumn(), CONTENT_START, '\0');
// }
} else if (editable_flag && arg0.keyCode < 0x10000 && arg0.keyCode >
0x1f || arg0.keyCode == 0x00 && (arg0.stateMask == 0 || arg0.stateMask ==
SWT.SHIFT)) {
if (cursorOnCreateNewLine()) {
createNewLine();
}
// if(!newLineButton_flag || !cursorOnCreateNewLine()){
//openEditor
openEditor(getRowIndex(), getColumn(), EMPTY_START, arg0.character);
// }
}
// F2 to open editor
}

public void keyReleased(KeyEvent arg0) {}

});

addTraverseListener(new TraverseListener() {

public void keyTraversed(TraverseEvent arg0) {
if (arg0.detail == SWT.TRAVERSE_TAB_NEXT && arg0.stateMask == 0) {
//move to the next cell
cursor.moveCursor(SWT.RIGHT);
arg0.doit = false;
}
if (arg0.detail == SWT.TRAVERSE_TAB_PREVIOUS && arg0.stateMask ==
131072) {
cursor.moveCursor(SWT.LEFT);
arg0.doit = false;
}

}
});

addFocusListener(new FocusListener() {

public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
cursor.setVisible(true);
cursor.redraw();
}

public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
}
});
}

/**
* Open ControlEditor (Text, Combo, ...) above specified * <b>row </b>
and <b>column </b> of the table or execute
* command if buttons are in this column. *
*
* @param row of the selected cell
* @param column of the selected cell
* @param mode of editor opening (EMPTY_START, CONTENT_START, ...)
* @param character that has ben pressed (used for EMPTY_START to insert
first char)
*/
private void openEditor(final int rowNum, final int column, int mode,
char character) {
final TableItem item = editedTable.getItem(rowNum);
String columnType = null;
TableColumn currentColumn = editedTable.getColumn(column);
if (currentColumn != null) {
columnType = (String) currentColumn.getData("control");
}

if (editable_flag && "input".equals(columnType) && mode ==
CONTENT_START) {
editorOpened = true;
//Edit operation requested => open editor
final Text text = new Text(cursor, SWT.NONE);
editorWidget = text;
text.setText(item.getText(column));
text.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {
// close the text editor and copy the data over // when the user hits
"ENTER"
if (e.character == SWT.CR) {
TableItem row = cursor.getRow();
row.setText(column, text.getText());
text.dispose();
editorOpened = false;
editorWidget = null;
}
// close the text editor when the user hits "ESC" without copying data
if (e.character == SWT.ESC) {
text.dispose();
editorOpened = false;
editorWidget = null;
}
}
});

text.addListener(SWT.Traverse, new Listener() {

public void handleEvent(Event arg0) {
if (arg0.detail == SWT.TRAVERSE_TAB_NEXT) {
//save and move selection right
TableItem row = cursor.getRow();
row.setText(column, text.getText());
text.dispose();
cursor.moveCursor(SWT.RIGHT);
arg0.doit = false;
}
if (arg0.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
//save and move selection left
TableItem row = cursor.getRow();
row.setText(column, text.getText());
text.dispose();
cursor.moveCursor(SWT.LEFT);
arg0.doit = false;
}
}
});

// text.addModifyListener(new ModifyListener(){
//
// public void modifyText(ModifyEvent arg0) {
// item.setText(column,text.getText());
// }
// });
text.addDisposeListener(new DisposeListener() {

public void widgetDisposed(DisposeEvent arg0) {
item.setText(column, text.getText());
}
});

editor.setEditor(text);
text.selectAll();
text.setFocus();
}

if (editable_flag && "input".equals(columnType) && mode == EMPTY_START) {
editorOpened = true;
//Edit operation requested => open editor
final Text text = new Text(cursor, SWT.NONE);
editorWidget = text;
text.setText(character + "");
text.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {
// close the text editor and copy the data over // when the user hits
"ENTER"
if (e.character == SWT.CR) {
TableItem row = cursor.getRow();
row.setText(column, text.getText());
text.dispose();
editorOpened = false;
editorWidget = null;
//move selection down and on the first column
cursor.moveCursor(SWT.CR);
}

// close the text editor when the user hits "ESC" without copying data
if (e.character == SWT.ESC) {
text.dispose();
editorOpened = false;
editorWidget = null;
}
}
});
text.addListener(SWT.Traverse, new Listener() {

public void handleEvent(Event arg0) {
if (arg0.detail == SWT.TRAVERSE_TAB_NEXT) {
//save and move selection right
TableItem row = cursor.getRow();
row.setText(column, text.getText());
text.dispose();
cursor.moveCursor(SWT.RIGHT);
arg0.doit = false;
}
if (arg0.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
//save and move selection left
TableItem row = cursor.getRow();
row.setText(column, text.getText());
text.dispose();
cursor.moveCursor(SWT.LEFT);
arg0.doit = false;
}
//arrow left
if (arg0.keyCode == 16777219) {
//save and move selection left
TableItem row = cursor.getRow();
row.setText(column, text.getText());
text.dispose();
cursor.moveCursor(SWT.LEFT);
arg0.doit = false;
}
//arrow right
if (arg0.keyCode == 16777220) {
//save and move selection right
TableItem row = cursor.getRow();
row.setText(column, text.getText());
text.dispose();
cursor.moveCursor(SWT.RIGHT);
arg0.doit = false;
}
//arrow up
if (arg0.keyCode == 16777217) {
//save and move selection up
TableItem row = cursor.getRow();
row.setText(column, text.getText());
text.dispose();
cursor.moveCursor(SWT.UP);
arg0.doit = false;
}
//arrow down
if (arg0.keyCode == 16777218) {
//save and move selection down
TableItem row = cursor.getRow();
row.setText(column, text.getText());
text.dispose();
cursor.moveCursor(SWT.DOWN);
arg0.doit = false;
}

}

});

// text.addModifyListener(new ModifyListener(){
//
// public void modifyText(ModifyEvent arg0) {
// item.setText(column,text.getText());
// }
// });
text.addDisposeListener(new DisposeListener() {

public void widgetDisposed(DisposeEvent arg0) {
try {
item.setText(column, text.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
});

editor.setEditor(text);
// text.selectAll();
text.setSelection(1, 1);
text.setFocus();
}
if ("button".equals(columnType)) {
//command execution requested
Command command = new Command("");

command.putAll((HashMap) currentColumn.getData("commandVars"));
//replace all variables in the commandVars
//get data for table
//find corresponding HashMap (_#RowIdentifier)
String dataref = (String) editedTable.getData("ref");
if (dataref != null) {
ArrayList data = (ArrayList) form.data.get(dataref);
if (data != null) {
// HashMap data4selectedLine = (HashMap)
data.get(editedTable.indexOf(cursor.getRow()));
long rowId = 0;
try {
rowId = ((Long)
cursor.getRow().getData("_#RowIdentifier")).longValue();
} catch (Exception exc) {}
HashMap data4selectedLine = null;
for (int i = 0; i < data.size(); i++) {
HashMap current = (HashMap) data.get(i);
long hashId = 0;
try {
hashId = ((Long) current.get("_#RowIdentifier")).longValue();
} catch (Exception exc) {}
if (hashId == rowId) {
data4selectedLine = current;
}
}
if (data4selectedLine != null) {
for (Iterator i = command.entrySet().iterator(); i.hasNext();) {
java.util.Map.Entry element = (java.util.Map.Entry) i.next();
String key = (String) element.getKey();
Object value = element.getValue();
if (value instanceof String && ((String) value).startsWith("${")) {
String key2lookup = ((String) value).replaceAll("[\\{\\}\\$]", "");
command.put(key, data4selectedLine.get(key2lookup));
}
}
}
}
}
cmdThread.sendCommand(command);

}
if (editable_flag && "select1".equals(columnType)) {
editorOpened = true;
//create ComboBox as editor
//Edit operation requested => open editor

// The control that will be the editor must be a child of the Cursor
final CCombo combo = new CCombo(cursor, SWT.NONE);
editorWidget = combo;
//fill it with list from FormLists
//find listdata reference
String ref = (String) currentColumn.getData("ref");
ArrayList selectList = null;
if (ref != null) {
selectList = (ArrayList) form.lists.get(ref);
}
int listItem = 0;
while (selectList != null && listItem < selectList.size()) {
combo.add((String) ((HashMap)
selectList.get(listItem++)).get("__text__"));
}
combo.setData("previous", item.getText(column));
// Assign focus to the combo control
if (mode == CONTENT_START) {
combo.setText(item.getText(column));
combo.setFocus();
}
if (mode == EMPTY_START) {
combo.setFocus();
combo.setText(character + "");
}

//finish editing and copy data - pressed enter
combo.addListener(SWT.KeyDown, new Listener() {

public void handleEvent(Event e) {
if (e.character == SWT.CR) {
TableItem row = cursor.getRow();
row.setText(column, combo.getText());
combo.dispose();
cursor.setFocus();
editorOpened = false;
editorWidget = null;
}
//finish editing without copying data - pressed Escape
if (e.character == SWT.ESC) {
String previous = (String) combo.getData("previous");
if (previous != null) {
TableItem row = cursor.getRow();
row.setText(column, previous);
}
combo.dispose();
cursor.setFocus();
editorOpened = false;
editorWidget = null;
}
}
});
combo.addListener(SWT.Traverse, new Listener() {

public void handleEvent(Event arg0) {
if (arg0.detail == SWT.TRAVERSE_TAB_NEXT) {
//save and move selection right
TableItem row = cursor.getRow();
row.setText(column, combo.getText());
combo.dispose();
cursor.moveCursor(SWT.RIGHT);
arg0.doit = false;
}
if (arg0.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
//save and move selection left
TableItem row = cursor.getRow();
row.setText(column, combo.getText());
combo.dispose();
cursor.moveCursor(SWT.LEFT);
arg0.doit = false;
}
}

});

combo.addModifyListener(new ModifyListener() {

public void modifyText(ModifyEvent arg0) {
item.setText(column, combo.getText());
}
});

// Open the combo editor in the doubleclicked column of the selected row
editor.setEditor(combo);
}
}

private void createNewLine() {
TableItem item = null;
if (newLinePosition == SWT.BOTTOM) {
item = new TableItem(editedTable, SWT.NONE, editedTable.getItemCount()
- 1);
}
if (newLinePosition == SWT.TOP) {
item = new TableItem(editedTable, SWT.NONE, 1);
setSelection(1, getColumn());
editedTable.setSelection(1);
}
if (item != null) {
//crate unic row identifier
item.setData("_#RowIdentifier", new Long(randomizer.nextLong()));
//go through all columns and if button type found, set item text to
button text
TableColumn columns[] = editedTable.getColumns();
for (int i = 0; i < editedTable.getColumnCount(); i++) {
String control = (String) columns[i].getData("control");
if ("button".equals(control)) {
item.setText(i, (String) columns[i].getData("name"));
}
}
cursor.redraw();
}
}

/**
* Moves cursor to the next cell in the given direction. Rollover is
enabled means * down on the last line -> first
* line left on the last column -> first column on the next line etc.
"newLine" line check means if cursor moves to
* the line for creating * new lines, moves to the first column *
*
* @param direction can be {SWT.DOWN, SWT.UP, SWT.LEFT, SWT.RIGHT,
SWT.CR} * SWT.CR direction is next line down,
* first column
*/
public void moveCursor(int direction) {
int newRow = 0, newColumn = 0;

// TableItem row = getRow();
int column = getColumn();
int columnCount = editedTable.getColumnCount();

switch (direction) {
case SWT.LEFT:
newColumn = column - 1;
newRow = getRowIndex();
if (newColumn < 0) {
newColumn = columnCount - 1;
newRow--;
}
if (newRow < 0) {
newRow = editedTable.getItemCount() - 1;
}
editedTable.setSelection(newRow);
cursor.setSelection(newRow, newColumn);
break;
case SWT.RIGHT:
newColumn = column + 1;
newRow = getRowIndex();
if (newColumn == columnCount) {
newColumn = 0;
newRow++;
}
if (newRow == editedTable.getItemCount()) {
newRow = 0;
}
editedTable.setSelection(newRow);
cursor.setSelection(newRow, newColumn);
break;
case SWT.UP:
newColumn = column;
newRow = getRowIndex() - 1;
if (newRow < 0) {
newRow = editedTable.getItemCount() - 1;
}
editedTable.setSelection(newRow);
cursor.setSelection(newRow, newColumn);
break;
case SWT.DOWN:
newColumn = column;
newRow = getRowIndex() + 1;
if (newRow == editedTable.getItemCount()) {
newRow = 0;
}
editedTable.setSelection(newRow);
cursor.setSelection(newRow, newColumn);
break;
case SWT.CR:
newColumn = 0;
newRow = getRowIndex() + 1;
if (newRow == editedTable.getItemCount()) {
newRow = 0;
}
editedTable.setSelection(newRow);
cursor.setSelection(newRow, newColumn);
break;
}

//test for "newLine"
// if(newLineButton_flag && newLinePosition == SWT.TOP){
// if(newRow == 0){
// newColumn = 0;
// cursor.setSelection(newRow,newColumn); // }
// }
// if(newLineButton_flag && newLinePosition == SWT.BOTTOM){
// if(newRow == editedTable.getItemCount()-1){
// newColumn = 0;
// cursor.setSelection(newRow,newColumn); // }
// }
}

/**
* Returns true, if newLineButton is udes and cursor is over it.
Automaticly determins, if newLineButton is first
* line or last line
*
* @return
*/
private boolean cursorOnCreateNewLine() {

boolean onNewLine = false;
if (newLineButton_flag && newLinePosition == SWT.BOTTOM && getRowIndex()
== editedTable.getItemCount() - 1)
onNewLine = true;
if (newLineButton_flag && newLinePosition == SWT.TOP && getRowIndex() ==
0)
onNewLine = true;

return onNewLine;
}

public int getRowIndex() {
if (editedTable == null)
return -1;
return editedTable.indexOf(cursor.getRow());
}

public void setVisible(boolean arg) {
try {
super.setVisible(arg);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
if (editorOpened && editorWidget != null) {

editorWidget.dispose();
editorWidget = null;
}
}

public void setSelection(int row, int column) {
if (row < 0 || row >= editedTable.getItemCount() || column < 0 || column
>= editedTable.getColumnCount())
return;
super.setSelection(row, column);
}
}
Previous Topic:org.eclipse.swt.opengl/gtk/eclipse/swt/opengl/*
Next Topic:[newbie] Setting the size of a Tree
Goto Forum:
  


Current Time: Wed Nov 25 20:58:26 GMT 2020

Powered by FUDForum. Page generated in 0.02053 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top