001: package org.swingml.treetablebrowser.ext;
002:
003: import java.awt.*;
004: import java.text.*;
005: import java.util.*;
006: import java.util.List;
007:
008: import org.swingml.*;
009: import org.swingml.action.*;
010: import org.swingml.event.*;
011: import org.swingml.system.*;
012: import org.swingml.util.*;
013:
014: public class TreeTableBrowserModel extends AbstractTreeTableModel
015: implements TableBrowserRemoteActionSubject {
016:
017: private static final Map expanded = new HashMap();
018: private TreeTableBrowser browser;
019: private Container container;
020: private String expandToNodeName;
021: private boolean handlingEvent;
022: private int height;
023: private int postColumn;
024: private int[] postColumns; // Added to support the submission of several column values
025: private String postStyle;
026: private List treeTableColumns;
027: private TreeTableBrowserTreeModel treeTableTree;
028: private int width;
029:
030: public TreeTableBrowserModel() {
031: super (null, null);
032: }
033:
034: public TreeTableBrowserModel(Object aRoot, Container aContainer) {
035: super (aRoot, aContainer);
036: }
037:
038: public void addChild(Object aChild) {
039: if (aChild instanceof TreeTableBrowserColumnModel) {
040: TreeTableBrowserColumnModel aTreeTableColumnModel = (TreeTableBrowserColumnModel) aChild;
041: this .getTreeTableColumns().add(aTreeTableColumnModel);
042: } else if (aChild instanceof TreeTableBrowserTreeModel) {
043: TreeTableBrowserTreeModel aTreeTableTreeModel = (TreeTableBrowserTreeModel) aChild;
044: this .treeTableTree = aTreeTableTreeModel;
045: super .setRoot(aTreeTableTreeModel);
046: }
047: }
048:
049: public void applyFilters() {
050: treeTableTree.setModel(this );
051: treeTableTree.applyFilters();
052: }
053:
054: public void collapse(final String s) {
055: List l = (List) expanded.get(getName());
056: if (null != l) {
057: l.remove(s);
058: }
059: }
060:
061: public void expand(final String s) {
062: List l = (List) expanded.get(getName());
063: if (l == null) {
064: l = new ArrayList();
065: expanded.put(getName(), l);
066: }
067:
068: if (!l.contains(s)) {
069: l.add(s);
070: }
071: }
072:
073: public TreeTableBrowser getBrowser() {
074: return browser;
075: }
076:
077: public Object getChild(Object parent, int index) {
078: SwingMLModel theSwingMLModel = (SwingMLModel) parent;
079: return theSwingMLModel.getChildren().get(index);
080: }
081:
082: public int getChildCount(Object parent) {
083: SwingMLModel theSwingMLModel = (SwingMLModel) parent;
084: return theSwingMLModel.getChildren().size();
085: }
086:
087: public Class getColumnClass(int column) {
088: Class classType = null;
089: if (column == 0) {
090: classType = TreeTableBrowserModelInterface.class;
091: } else {
092: classType = String.class;
093: }
094: return classType;
095: }
096:
097: public int getColumnCount() {
098: return getTreeTableColumns().size();
099: }
100:
101: public String getColumnName(int column) {
102: TreeTableBrowserColumnModel theTreeTableColumn = (TreeTableBrowserColumnModel) getTreeTableColumns()
103: .get(column);
104: return theTreeTableColumn.getText();
105: }
106:
107: public List getColumns() {
108: return getTreeTableColumns();
109: }
110:
111: public Container getContainer() {
112: return container;
113: }
114:
115: public List getExpanded() {
116: List l = (List) expanded.get(getName());
117: if (null == l) {
118: l = new ArrayList(0);
119: }
120: return l;
121: }
122:
123: public String getExpandToNodeName() {
124: return expandToNodeName;
125: }
126:
127: public int getHeight() {
128: return height;
129: }
130:
131: /**
132: * @return Returns the postColumn.
133: */
134: public int getPostColumn() {
135: return postColumn;
136: }
137:
138: /**
139: * @return Returns the postColumns.
140: */
141: public int[] getPostColumns() {
142: return postColumns;
143: }
144:
145: /**
146: * @return Returns the postStyle.
147: */
148: public String getPostStyle() {
149: return postStyle;
150: }
151:
152: public List getTreeTableColumns() {
153: if (this .treeTableColumns == null) {
154: this .treeTableColumns = new ArrayList();
155: }
156: return this .treeTableColumns;
157: }
158:
159: public TreeTableBrowserTreeModel getTreeTableTree() {
160: return this .treeTableTree;
161: }
162:
163: public Object getValueAt(Object node, int column) {
164: Object theValue = null;
165: if (node instanceof TreeTableBrowserTreeModel) {
166: TreeTableBrowserTreeModel theTreeTableTreeModel = (TreeTableBrowserTreeModel) node;
167: theValue = theTreeTableTreeModel.getTreeTableDatas().get(
168: column);
169: theTreeTableTreeModel.setModel(this );
170: } else if (node instanceof TreeTableBrowserNodeModel) {
171: TreeTableBrowserNodeModel theTreeTableNodeModel = (TreeTableBrowserNodeModel) node;
172: theValue = theTreeTableNodeModel.getTreeTableDatas().get(
173: column);
174: // apply column converter if any...this is found
175: }
176: TreeTableBrowserColumnModel columnModel = (TreeTableBrowserColumnModel) getTreeTableColumns()
177: .get(column);
178: if (columnModel.getType() instanceof DateConverter) {
179: DateConverter converter = (DateConverter) columnModel
180: .getType();
181: TreeTableBrowserDataModel dataModel = (TreeTableBrowserDataModel) theValue;
182: try {
183: String displayValue = converter
184: .displayValueString(dataModel.getText());
185: dataModel.setDisplayValue(displayValue);
186: } catch (ParseException e) {
187: SwingMLLogger.getInstance().log(
188: "Display Pattern for column "
189: + columnModel.getName()
190: + " is not valid data formate");
191: }
192: }
193:
194: return theValue;
195: }
196:
197: public int getWidth() {
198: return width;
199: }
200:
201: public boolean isHandlingEvent() {
202: return handlingEvent;
203: }
204:
205: public int locateColumnIndex(String colId) {
206: Iterator itr = getTreeTableColumns().iterator();
207: int index = 0;
208: while (itr.hasNext()) {
209: TreeTableBrowserColumnModel cm = (TreeTableBrowserColumnModel) itr
210: .next();
211: if (cm.getText().equalsIgnoreCase(colId)) {
212: return index;
213: }
214: index++;
215: }
216: return -1;
217: }
218:
219: public String locateColumnName(int col) {
220: return getColumnName(col);
221: }
222:
223: public SwingMLModel locateRow(String id) {
224: SwingMLModel result = null;
225: Iterator itr = getChildren().iterator();
226: while (itr.hasNext()) {
227: SwingMLModel model = (SwingMLModel) itr.next();
228: if (model instanceof TreeTableBrowserTreeModel) {
229: result = locateRow(id);
230: }
231: if (model.getName().equalsIgnoreCase(id)) {
232: return model;
233: }
234: }
235: return result;
236: }
237:
238: public void setBrowser(TreeTableBrowser aBrowser) {
239: this .browser = aBrowser;
240: }
241:
242: public void setContainer(Container aContainer) {
243: this .container = aContainer;
244: }
245:
246: public void setExpandToNodeName(final String s) {
247: expandToNodeName = s;
248: }
249:
250: public void setHandlingEvent(boolean handlingEvent) {
251: this .handlingEvent = handlingEvent;
252: }
253:
254: public void setHeight(int aHeight) {
255: this .height = aHeight;
256: }
257:
258: public void setPostColumn(int column) {
259: this .postColumn = column;
260: }
261:
262: public void setPostColumns(int[] columns) {
263: this .postColumns = columns;
264: }
265:
266: public void setPostColumns(String columns) {
267: if (columns == null || columns.trim().length() == 0) {
268: setPostColumns((int[]) null);
269: } else {
270: StringTokenizer tokenizer = new StringTokenizer(columns,
271: ", ");
272: int[] temp = new int[tokenizer.countTokens()];
273: int i = 0;
274: while (tokenizer.hasMoreTokens()) {
275: temp[i++] = Integer.parseInt(tokenizer.nextToken());
276: }
277: setPostColumns(temp);
278: }
279: }
280:
281: public void setPostStyle(String style) {
282: this .postStyle = style;
283: }
284:
285: public void setTreeTableColumns(List aTreeTableColumns) {
286: this .treeTableColumns = aTreeTableColumns;
287: }
288:
289: public void setTreeTableTree(
290: TreeTableBrowserTreeModel aTreeTableTree) {
291: this .treeTableTree = aTreeTableTree;
292: }
293:
294: /**
295: * Update the node value and fire an Event to nofify listeners
296: *
297: * ActionListener.actionPerformed.cellValueChanged
298: */
299: public void setValueAt(Object aValue, Object node, int column) {
300: if (node instanceof TreeTableBrowserNodeModel) {
301: TreeTableBrowserNodeModel m = (TreeTableBrowserNodeModel) node;
302: m.updateColumn(column, aValue);
303: }
304:
305: if (!isHandlingEvent()) {
306: setHandlingEvent(true);
307: EventHandler.getInstance().handleEvent(
308: new TreeTableBrowserCellUpdateModel(this , node,
309: aValue, column),
310: Constants.ACTION_PERFORMED_CELL_VALUE_CHANGED,
311: getBrowser().getTable());
312: }
313: }
314:
315: public void setWidth(int aWidth) {
316: this .width = aWidth;
317: }
318:
319: public void sort() {
320: treeTableTree.setModel(this );
321: treeTableTree.sort();
322: }
323:
324: public List treeNodes() {
325: return treeTableTree.getOriginalChildren();
326: }
327:
328: public void update(String rowId, String colId, Object value) {
329: // locate node and apply new value
330: // locate row nod in tree model
331: TreeTableBrowserTreeModel tm = (TreeTableBrowserTreeModel) getRoot();
332: SwingMLModel model = tm.locateRow(rowId);
333: if (model instanceof TreeTableBrowserNodeModel) {
334: TreeTableBrowserNodeModel m = (TreeTableBrowserNodeModel) model;
335: m.updateColumn(locateColumnIndex(colId), value);
336: }
337: browser.refresh();
338: }
339:
340: }
|