001: package fr.aliacom.form.swt.ui;
002:
003: import org.apache.log4j.Logger;
004: import org.eclipse.swt.SWT;
005: import org.eclipse.swt.custom.TableEditor;
006: import org.eclipse.swt.graphics.Point;
007: import org.eclipse.swt.graphics.Rectangle;
008: import org.eclipse.swt.widgets.Control;
009: import org.eclipse.swt.widgets.Event;
010: import org.eclipse.swt.widgets.Listener;
011: import org.eclipse.swt.widgets.Table;
012: import org.eclipse.swt.widgets.TableItem;
013:
014: import fr.aliacom.common.ui.table.ColumnView;
015: import fr.aliacom.common.ui.table.ICellEditor;
016: import fr.aliacom.common.ui.table.ICellEditorListener;
017:
018: /**
019: * @author tom
020: *
021: * (C) 2001, 2003 Thomas Cataldo
022: */
023: public final class SWTTableCursor implements ICellEditorListener {
024:
025: private SWTTable swtTable;
026: private Table table;
027: private TableEditor editor;
028: private ICellEditor cellEditor;
029: private boolean editing;
030: private static final Logger log = Logger
031: .getLogger(SWTTableCursor.class);
032:
033: public SWTTableCursor(SWTTable swtTable) {
034: this .swtTable = swtTable;
035: this .table = (Table) swtTable.getNativeWidget();
036:
037: this .editor = new TableEditor(this .table);
038: this .editor.grabHorizontal = true;
039: this .editor.grabVertical = true;
040: this .editor.horizontalAlignment = SWT.LEFT;
041: this .editing = false;
042:
043: final SWTTable swt = swtTable;
044: table.addListener(SWT.MouseDown, new Listener() {
045: public void handleEvent(Event event) {
046: log.debug("mouse down");
047: editingStopped();
048: Point pt = new Point(event.x, event.y);
049: TableItem item = table.getItem(pt);
050: if (item == null)
051: return;
052: for (int column = 0, n = swt.getModel()
053: .getColumnCount(); column < n; column++) {
054: Rectangle rect = item.getBounds(column);
055: if (rect.contains(pt)) {
056: int row = table.indexOf(item);
057: editCellAt(row, column);
058: break;
059: }
060: }
061: }
062: });
063: }
064:
065: private void editCellAt(int row, int column) {
066: TableItem it = table.getItem(row);
067: if (it == null) {
068: log.debug("row is null");
069: return;
070: }
071:
072: System.out.println("Column is " + column);
073: ColumnView view = swtTable.getColumnView(column);
074: if (view == null) {
075: log.debug("view is null");
076: return;
077: }
078: cellEditor = view.getEditor();
079: if (cellEditor == null) {
080: log.debug("cellEditor is null");
081: return;
082: }
083: Control oldEditor = editor.getEditor();
084: if (oldEditor != null) {
085: oldEditor.dispose();
086: }
087: editing = true;
088: cellEditor = (ICellEditor) cellEditor.clone();
089: cellEditor.addCellEditorListener(this );
090: Control newEditor = (Control) cellEditor.getNativeWidget();
091: log.debug("The new editor is " + newEditor);
092: newEditor.setFocus();
093: editor.setEditor(newEditor, it, column);
094: cellEditor.setValueBean(swtTable.getModel().getValueAt(row, 0));
095: }
096:
097: /**
098: * Method setCellEditor.
099: * @param cellEditor
100: */
101:
102: /**
103: * @see fr.aliacom.common.ui.table.ICellEditorListener#editingStopped()
104: */
105: public void editingStopped() {
106: if (editing) {
107: log.debug("SWTTableCursor : editingStopped...");
108: Control ctrl = editor.getEditor();
109: if (ctrl != null) {
110: ctrl.dispose();
111: }
112: swtTable.tableRowsUpdated(table.getSelectionIndex(), table
113: .getSelectionIndex() + 1);
114: cellEditor.removeCellEditorListener(this );
115: editing = false;
116: }
117: }
118:
119: }
|