001: package org.wingx;
002:
003: import java.util.StringTokenizer;
004:
005: import org.wings.*;
006: import org.wings.event.SMouseListener;
007: import org.wings.event.SMouseEvent;
008: import org.wings.table.*;
009: import org.wingx.treetable.*;
010: import org.wingx.table.EditableTableCellRenderer;
011:
012: import javax.swing.table.TableModel;
013:
014: public class XTreeTable extends STable {
015: public XTreeTable(XTreeTableModel model) {
016: this ();
017: setModel(model);
018: }
019:
020: public XTreeTable() {
021: setEditable(true);
022: setSelectionMode(STable.NO_SELECTION);
023: }
024:
025: public void setModel(TableModel tm) {
026: super .setModel(tm);
027: }
028:
029: public void createDefaultColumnsFromModel() {
030: super .createDefaultColumnsFromModel();
031: TableModel tm = getModel();
032:
033: if (tm instanceof XTreeTableModel) {
034: columnModel.getColumn(0).setCellRenderer(
035: new TreeColumnRenderer());
036: boolean registered = false;
037: Object[] list = getListenerList();
038: for (int i = 0; i < list.length; i++) {
039: Object o = list[i];
040: if (o instanceof ExpansionListener) {
041: registered = true;
042: break;
043: }
044: }
045:
046: if (!registered) {
047: addMouseListener(new ExpansionListener());
048: }
049: }
050: }
051:
052: public STableCellRenderer getCellRenderer(int row, int col) {
053: STableColumnModel columnModel = getColumnModel();
054: if (columnModel != null) {
055: STableColumn column = columnModel.getColumn(col);
056: if (column != null) {
057: STableCellRenderer renderer = column.getCellRenderer();
058: if (renderer != null)
059: return renderer;
060: }
061: }
062:
063: XTreeTableNode rowNode = (XTreeTableNode) getTreeTableModel()
064: .getNodeModel().get(row);
065: if (rowNode.getNodeClass() != null)
066: return getDefaultRenderer(rowNode.getNodeClass());
067: else
068: return getDefaultRenderer(getColumnClass(col));
069: }
070:
071: public STableCellEditor getCellEditor(int row, int col) {
072: STableColumnModel columnModel = getColumnModel();
073: if (columnModel != null) {
074: STableColumn column = columnModel.getColumn(col);
075: if (column != null) {
076: STableCellEditor editor = column.getCellEditor();
077: if (editor != null)
078: return editor;
079: }
080: }
081:
082: XTreeTableNode rowNode = (XTreeTableNode) getTreeTableModel()
083: .getNodeModel().get(row);
084: if (rowNode.getNodeClass() != null) {
085: return getDefaultEditor(rowNode.getNodeClass());
086: } else
087: return getDefaultEditor(getColumnClass(col));
088: }
089:
090: public boolean isCellEditable(int row, int col) {
091: return col == 0;
092: }
093:
094: public void processLowLevelEvent(String action, String[] values) {
095: // delegate events to editors
096: if (action.indexOf("_") != -1) {
097: StringTokenizer tokens = new StringTokenizer(action, "_");
098: tokens.nextToken(); // tableName
099: int row = Integer.parseInt(tokens.nextToken()); // row
100: int col = Integer.parseInt(tokens.nextToken()); // col
101:
102: STableCellRenderer cellRenderer = getCellRenderer(row, col);
103: if (cellRenderer instanceof EditableTableCellRenderer) {
104: EditableTableCellRenderer editableCellRenderer = (EditableTableCellRenderer) cellRenderer;
105: editableCellRenderer.getLowLevelEventListener(this ,
106: row, col).processLowLevelEvent(action, values);
107: Object value = editableCellRenderer.getValue();
108: getTreeTableModel().setValueAt(value, row, col);
109: }
110: } else
111: super .processLowLevelEvent(action, values);
112: }
113:
114: /**
115: * @return Returns the model casted as a XTreeTableModel
116: */
117: public XTreeTableModel getTreeTableModel() {
118: return (XTreeTableModel) getModel();
119: }
120:
121: class ExpansionListener implements SMouseListener {
122: public void mouseClicked(SMouseEvent e) {
123: int row = XTreeTable.this .rowAtPoint(e.getPoint());
124: int col = XTreeTable.this .columnAtPoint(e.getPoint());
125: if (col == 0) {
126: getTreeTableModel().toggleExpanded(row);
127: e.consume();
128: }
129: }
130: }
131: }
|