001: package org.swingml.tablebrowser.ext;
002:
003: import java.text.*;
004: import java.util.*;
005:
006: import org.swingml.action.*;
007: import org.swingml.model.*;
008: import org.swingml.system.*;
009: import org.swingml.util.*;
010:
011: public class TableBrowserModel extends JTableModel implements
012: TableBrowserRemoteActionSubject {
013:
014: public static final int SELECTION_STYLE_DEFAULT = 0;
015: public static final int SELECTION_STYLE_MULTI_CELL = 4;
016: public static final int SELECTION_STYLE_MULTI_ROW = 2;
017: public static final int SELECTION_STYLE_SINGLE_CELL = 3;
018: public static final int SELECTION_STYLE_SINGLE_ROW = 1;
019:
020: private boolean autoFit = false;
021: private String cellManager;
022: private String[] columnNames;
023: private BrowserContract contract;
024: private boolean isStartEditCalculated = false;
025: private boolean postColumnState;
026: private boolean rowHeaderVisible = false;
027: private int selectionStyle;
028: private int startEditCellColumn = -1;
029: private int startEditCellRow = -1;
030: private TableBrowser tableBrowser;
031: private Object[][] values;
032:
033: public TableBrowserModel() {
034: super ();
035: }
036:
037: public void addChild(Object aChild) {
038: super .addChild(aChild);
039:
040: if (aChild instanceof ErrorHandlerModel) {
041: // add it so it can be 'rendered' later and created
042: getChildren().add(aChild);
043: }
044: }
045:
046: private void calculateStartEdit() {
047: for (int row = 0; row < getRowCount(); row++) {
048: for (int column = 0; column < getColumnCount(); column++) {
049: if (getDataModel(row, column).isStartEdit()) {
050: startEditCellRow = row;
051: startEditCellColumn = column;
052: this .isStartEditCalculated = true;
053: return;
054: }
055: }
056: }
057: this .isStartEditCalculated = true;
058: }
059:
060: public Object[][] data() {
061: // create two dimensional array and apply to contract
062: Object[][] v = new Object[getRows().size()][getColumns().size()];
063: for (int i = getRows().size() - 1; i >= 0; i--) {
064: TableRowModel trw = (TableRowModel) getRows().get(i);
065: List cols = trw.getChildren();
066: for (int j = cols.size() - 1; j >= 0; j--) {
067: v[i][j] = cols.get(j);
068: }
069: }
070: return v;
071: }
072:
073: public List extractColumnNames() {
074: List c = getColumns();
075: columnNames = new String[c.size()];
076: for (int i = c.size() - 1; i >= 0; i--) {
077: TableColumnModel tcm = (TableColumnModel) c.get(i);
078: columnNames[i] = tcm.getText();
079: }
080: return Arrays.asList(columnNames);
081: }
082:
083: public String getCellManager() {
084: return cellManager;
085: }
086:
087: public int getColumnCount() {
088: int result = 1;
089: if (columnNames != null) {
090: result = columnNames.length;
091: }
092:
093: return result;
094: }
095:
096: public String getColumnName(int col) {
097: return columnNames[col];
098: }
099:
100: /**
101: * Returns the columnNames.
102: */
103: public String[] getColumnNames() {
104: return columnNames;
105: }
106:
107: public BrowserContract getContract() {
108: return contract;
109: }
110:
111: public TableDataModel getModelAt(int row, int col) {
112: TableDataModel result = null;
113: BrowserContract aContract = getContract();
114: result = (TableDataModel) aContract.getModelAt(row, col);
115: return result;
116: }
117:
118: public int getRowCount() {
119: int result = 1;
120: if (values != null) {
121: result = values.length;
122: }
123: return result;
124: }
125:
126: /**
127: * Get the cell/row selection style (single/multiple, entire row or just a
128: * cell, etc)
129: */
130: public int getSelectionStyle() {
131: return selectionStyle;
132: }
133:
134: public int getStartEditCellColumn() {
135: if (!this .isStartEditCalculated) {
136: calculateStartEdit();
137: }
138: return startEditCellColumn;
139: }
140:
141: public int getStartEditCellRow() {
142: if (!this .isStartEditCalculated) {
143: calculateStartEdit();
144: }
145: return startEditCellRow;
146: }
147:
148: public TableBrowser getTableBrowser() {
149: return tableBrowser;
150: }
151:
152: public Object getValueAt(int row, int col) {
153: return values[row][col];
154: }
155:
156: public Object[][] getValues() {
157: return values;
158: }
159:
160: public boolean hasStartEditCell() {
161: if (!this .isStartEditCalculated) {
162: calculateStartEdit();
163: }
164: return startEditCellRow != -1;
165: }
166:
167: public boolean isAutoFit() {
168: return autoFit;
169: }
170:
171: public boolean isCellEditable(int aRowIndex, int aColumnIndex) {
172: CellDataValue cd = (CellDataValue) getValueAt(aRowIndex,
173: aColumnIndex);
174: return cd.isEditable();
175: }
176:
177: public boolean isPostColumnState() {
178: return postColumnState;
179: }
180:
181: public boolean isRowHeaderVisible() {
182: return rowHeaderVisible;
183: }
184:
185: public void setAutoFit(boolean fit) {
186: this .autoFit = fit;
187: }
188:
189: public void setCellEditable(int row, int column, boolean editable) {
190: ((CellDataValue) getValueAt(row, column)).setEditable(editable);
191: }
192:
193: public void setCellManager(String className) {
194: cellManager = className;
195: }
196:
197: /**
198: * Sets the columnNames.
199: */
200: public void setColumnNames(String[] names) {
201: this .columnNames = names;
202: }
203:
204: /**
205: * Sets the contract.
206: */
207: public void setContract(BrowserContract aContract) {
208: this .contract = aContract;
209: }
210:
211: public void setPostColumnState(boolean b) {
212: this .postColumnState = b;
213: }
214:
215: public void setRowHeaderVisible(boolean visible) {
216: this .rowHeaderVisible = visible;
217: }
218:
219: /**
220: * Set the cell/row selection style (single/multiple, entire row or just a
221: * cell, etc)
222: */
223: public void setSelectionStyle(int style) {
224: this .selectionStyle = style;
225: }
226:
227: public void setTableBrowser(TableBrowser aBrowser) {
228: this .tableBrowser = aBrowser;
229: }
230:
231: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
232: // pass on through to contract
233: if (aValue != null) {
234: CellDataValue cdv = (CellDataValue) getValueAt(rowIndex,
235: columnIndex);
236: String stringValue = aValue.toString();
237: // validation rules can be applied here
238: String validatedValue = getContract().editValue(
239: cdv.getColumnName(), cdv.getRow(), stringValue);
240: if (validatedValue != null) {
241: if (aValue instanceof DateConverter) {
242: DateConverter converter = (DateConverter) aValue;
243: try {
244: if (converter.convert(stringValue) != null) {
245: converter.setPattern(stringValue);
246: cdv.setValue(validatedValue);
247: cdv.setDisplayValue(stringValue);
248: }
249: } catch (ParseException e) {
250: SwingMLLogger.getInstance().log(e);
251: }
252: } else if (aValue instanceof NumberConverter) {
253: cdv.setValue(stringValue);
254: cdv.setDisplayValue(stringValue);
255: } else if (aValue instanceof ItemModel) {
256: ItemModel item = (ItemModel) aValue;
257: cdv.setValue(item.getValue());
258: cdv.setDisplayValue(item.getText());
259: } else if (aValue instanceof CellDataValue) {
260: CellDataValue aCellValue = (CellDataValue) aValue;
261: if (aCellValue != null) {
262: cdv.setValue(aCellValue.getValue());
263: cdv.setDisplayValue(validatedValue);
264: } else {
265: cdv.setValue(validatedValue);
266: cdv.setDisplayValue(validatedValue);
267: }
268: } else {
269: cdv.setValue(validatedValue);
270: cdv.setDisplayValue(validatedValue);
271: }
272:
273: cdv.setDirty(true);
274: }
275: }
276: }
277:
278: public void setValues(Object[][] someValues) {
279: this .values = someValues;
280: }
281:
282: public void update(String rowId, String colId, Object value) {
283: try {
284: int rowIndex = Integer.parseInt(rowId);
285: int columnIndex = Integer.parseInt(colId);
286:
287: CellDataValue cellDataValue = getTableBrowser().valueAt(
288: rowIndex, columnIndex);
289: cellDataValue.setValue(value.toString());
290: } catch (NumberFormatException e) {
291: throw new RuntimeException(
292: "Col/Row index value for table update must be an integer index");
293: }
294: }
295:
296: }
|