001: package fr.aliacom.form.swt.ui;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005:
006: import org.apache.log4j.Logger;
007: import org.eclipse.swt.SWT;
008: import org.eclipse.swt.events.SelectionEvent;
009: import org.eclipse.swt.events.SelectionListener;
010: import org.eclipse.swt.widgets.Composite;
011: import org.eclipse.swt.widgets.Table;
012: import org.eclipse.swt.widgets.TableColumn;
013: import org.eclipse.swt.widgets.TableItem;
014:
015: import fr.aliacom.commands.Command;
016: import fr.aliacom.common.ui.ITable;
017: import fr.aliacom.common.ui.table.BeanPropertiesMapping;
018: import fr.aliacom.common.ui.table.ColumnView;
019: import fr.aliacom.common.ui.table.DefaultCellRenderer;
020: import fr.aliacom.common.ui.table.ICellRenderer;
021: import fr.aliacom.common.ui.table.TableModel;
022:
023: /**
024: * @author tom
025: *
026: * (C) 2001, 2003 Thomas Cataldo
027: */
028: public final class SWTTable implements ITable {
029:
030: private SWTTableCell cell;
031: private SWTTableCursor cursor;
032: private DefaultCellRenderer defaultRenderer;
033: private boolean needsEditing;
034: private Command onSelect;
035: private Table table;
036: private TableModel tableModel;
037: private Logger log;
038:
039: /** ArrayList<TableColumn> */
040: private ArrayList viewModel;
041:
042: /** HashMap<String, ColumnView> */
043: private HashMap viewsStore;
044:
045: /**
046: * Constructor SWTTable.
047: * @param composite
048: * @param tableModel
049: */
050: public SWTTable(Composite composite, TableModel tableModel) {
051: log = Logger.getLogger(SWTTable.class);
052: final int style = SWT.BORDER | SWT.FULL_SELECTION
053: | SWT.HIDE_SELECTION;
054: table = new Table(composite, style);
055: this .tableModel = tableModel;
056: tableModel.addTableModelListener(this );
057: this .viewModel = new ArrayList();
058: this .viewsStore = new HashMap();
059: this .needsEditing = false;
060: this .cell = new SWTTableCell();
061: this .defaultRenderer = new DefaultCellRenderer();
062: }
063:
064: /**
065: * @see fr.aliacom.common.ui.ITable#addColumnView(ColumnView)
066: */
067: public void addColumnView(ColumnView view) {
068: viewsStore.put(view.getProperty(), view);
069: if (view.getEditor() != null) {
070: needsEditing = true;
071: }
072: }
073:
074: /**
075: * Method addEditingSupport.
076: */
077: private void addEditingSupport() {
078: log.debug("Adding editing support to table");
079: cursor = new SWTTableCursor(this );
080: }
081:
082: /**
083: * @see fr.aliacom.common.ui.ITable#addRow(Object)
084: */
085: public void addRow(Object r) {
086: tableModel.addRow(r);
087: }
088:
089: public void createUIFromModel() {
090: BeanPropertiesMapping mapping = tableModel.getColumnModel();
091: TableColumn tc;
092: for (int i = 0, n = mapping.getPropertiesCount(); i < n; i++) {
093: tc = new TableColumn(table, SWT.NULL);
094: tc.setText(mapping.getTitle(i));
095: viewModel.add(tc);
096: }
097: if (needsEditing) {
098: addEditingSupport();
099: }
100: table.setHeaderVisible(true);
101: if (onSelect != null) {
102: table.addSelectionListener(new SelectionListener() {
103: public void widgetSelected(SelectionEvent e) {
104: log.debug("Running onSelect action");
105: onSelect.run();
106: }
107:
108: public void widgetDefaultSelected(SelectionEvent e) {
109: }
110: });
111: }
112: repack();
113: }
114:
115: /**
116: * Method getColumnView.
117: * @param column
118: * @return ColumnView
119: */
120: public ColumnView getColumnView(int column) {
121: return (ColumnView) viewsStore.get(tableModel.getColumnModel()
122: .getProperty(column));
123: }
124:
125: /**
126: * @see fr.aliacom.common.ui.ITable#getModel()
127: */
128: public TableModel getModel() {
129: return tableModel;
130: }
131:
132: /**
133: * @see fr.aliacom.form.common.IFormComponent#getNativeWidget()
134: */
135: public Object getNativeWidget() {
136: return table;
137: }
138:
139: private ICellRenderer getRenderer(int column) {
140: ICellRenderer ret = null;
141: ColumnView view = (ColumnView) viewsStore.get(tableModel
142: .getColumnModel().getProperty(column));
143: if (view == null || (ret = view.getRenderer()) == null) {
144: ret = defaultRenderer;
145: }
146: return ret;
147: }
148:
149: /**
150: * @see fr.aliacom.common.ui.ITable#getRow(int)
151: */
152: public Object getRow(int rowIndex) {
153: return tableModel.getRow(rowIndex);
154: }
155:
156: /**
157: * @see fr.aliacom.common.ui.ITable#getRows()
158: */
159: public ArrayList getRows() {
160: return tableModel.getRows();
161: }
162:
163: /**
164: * @see fr.aliacom.common.ui.ITable#getSelectedValue()
165: */
166: public Object getSelectedValue() {
167: if (table.getSelectionIndex() >= 0) {
168: return tableModel.getValueAt(table.getSelectionIndex(), 0);
169: } else {
170: return null;
171: }
172: }
173:
174: /**
175: * @see fr.aliacom.common.ui.ITable#isHidden(String)
176: */
177: public boolean isHidden(String column) {
178: return false;
179: }
180:
181: /**
182: * Method refillTable.
183: */
184: private void refillTable() {
185: log.debug("**** REFILLING TABLE ****");
186: table.removeAll();
187: appendRows(0, tableModel.getRowCount());
188: repack();
189: }
190:
191: /**
192: * @see fr.aliacom.common.ui.ITable#removeRow(int)
193: */
194: public Object removeRow(int rowIndex) {
195: return tableModel.removeRow(rowIndex);
196: }
197:
198: /**
199: * @see fr.aliacom.common.ui.ITable#removeRow(Object)
200: */
201: public Object removeRow(Object row) {
202: return tableModel.removeRow(row);
203: }
204:
205: private void repack() {
206: for (int i = 0, n = viewModel.size(); i < n; i++) {
207: ((TableColumn) viewModel.get(i)).pack();
208: }
209: }
210:
211: /**
212: * @see fr.aliacom.form.common.IFormComponent#reset()
213: */
214: public void reset() {
215: }
216:
217: /**
218: * @see fr.aliacom.common.ui.ITable#setEnabled(boolean)
219: */
220: public void setEnabled(boolean b) {
221: }
222:
223: /**
224: * @see fr.aliacom.common.ui.ITable#setHidden(String, boolean)
225: */
226: public void setHidden(String column, boolean b) {
227: }
228:
229: /**
230: * @see fr.aliacom.common.ui.ITable#setOnSelectAction(fr.aliacom.commands.Command)
231: */
232: public void setOnSelectAction(Command c) {
233: onSelect = c;
234: }
235:
236: /**
237: * @see fr.aliacom.common.ui.ITable#setRows(ArrayList)
238: */
239: public void setRows(ArrayList rows) {
240: tableModel.setRows(rows);
241: }
242:
243: /**
244: * @see fr.aliacom.form.common.IFormComponent#setValueBean(Object)
245: */
246: public void setValueBean(Object bean) {
247: setRows((ArrayList) bean);
248: }
249:
250: /**
251: * @see fr.aliacom.common.ui.table.ITableModelListener#tableCellUpdated(int, int)
252: */
253: public void tableCellUpdated(int row, int col) {
254: log.debug("tableCellUpdated(" + row + ", " + col + ")");
255: TableItem it = table.getItem(row);
256: Object value = tableModel.getValueAt(row, col + 1);
257: cell.setItem(it, col);
258: // TODO renderer can use data from other from other columns
259: getRenderer(col).render(value, tableModel.getValueAt(row, 0),
260: cell);
261: ((TableColumn) viewModel.get(col)).pack();
262: }
263:
264: /**
265: * @see fr.aliacom.common.ui.table.ITableModelListener#tableDataChanged()
266: */
267: public void tableDataChanged() {
268: refillTable();
269: }
270:
271: /**
272: * @see fr.aliacom.common.ui.table.ITableModelListener#tableRowsDeleted(int, int)
273: */
274: public void tableRowsDeleted(int start, int end) {
275: table.remove(start, end);
276: }
277:
278: /**
279: * @see fr.aliacom.common.ui.table.ITableModelListener#tableRowsInserted(int, int)
280: */
281: public void tableRowsInserted(final int first, final int last) {
282: log.debug("tableRowsInserted(" + first + ", " + last + ")");
283: appendRows(first, last);
284: repack();
285: }
286:
287: /**
288: * @see fr.aliacom.common.ui.table.ITableModelListener#tableRowsUpdated(int, int)
289: */
290: public void tableRowsUpdated(int first, int last) {
291: for (int i = first; i < last; i++) {
292: for (int j = 0; j < tableModel.getColumnCount(); j++) {
293: tableCellUpdated(i, j);
294: }
295: }
296: }
297:
298: private void appendRows(int first, int last) {
299: TableItem it = null;
300: for (int i = first; i < last; i++) {
301: it = new TableItem(table, SWT.NULL, i);
302: for (int j = 1, k = tableModel.getColumnCount(); j <= k; j++) {
303: Object value = tableModel.getValueAt(i, j);
304: cell.setItem(it, j - 1);
305: getRenderer(j - 1).render(value,
306: tableModel.getValueAt(i, 0), cell);
307: }
308: }
309: }
310:
311: }
|