001: package org.swingml.treetablebrowser.ext;
002:
003: import java.awt.*;
004: import java.net.*;
005: import java.util.*;
006: import java.util.List;
007:
008: import javax.swing.*;
009:
010: import org.swingml.*;
011: import org.swingml.system.*;
012:
013: public class TreeTableBrowserTreeModel extends SwingMLModel {
014:
015: private static SwingMLModel locateRow(final List l, final String id) {
016: for (Iterator i = l.iterator(); i.hasNext();) {
017: SwingMLModel smlm = (SwingMLModel) i.next();
018: if (id.equalsIgnoreCase(smlm.getName())) {
019: return smlm;
020: }
021: if (smlm instanceof TreeTableBrowserTreeModel) {
022: smlm = locateRow(((TreeTableBrowserTreeModel) smlm)
023: .getChildren(), id);
024: if (null != smlm) {
025: return smlm;
026: }
027: }
028: }
029: return null;
030: }
031:
032: private Icon closedFolderIcon;
033: private Icon leafIcon;
034: private TreeTableBrowserModel model;
035: private Icon openFolderIcon;
036: private List originalChildren;
037: private List treeTableDatas;
038:
039: public TreeTableBrowserTreeModel(Container aContainer) {
040: super ();
041: }
042:
043: public void addChild(Object aChild) {
044: if (aChild instanceof TreeTableBrowserDataModel) {
045: TreeTableBrowserDataModel aTreeTableDataModel = (TreeTableBrowserDataModel) aChild;
046: getTreeTableDatas().add(aTreeTableDataModel);
047: } else {
048: super .addChild(aChild);
049: }
050: }
051:
052: public void applyFilters() {
053: if (originalChildren == null) {
054: // want to keep a copy of the orignal child objects
055: originalChildren = new ArrayList(getChildren());
056: }
057: // collect nodes
058: Iterator itr = originalChildren.iterator();
059: // clear children
060: getChildren().clear();
061: ArrayList nodes = new ArrayList();
062: while (itr.hasNext()) {
063: Object originalChild = itr.next();
064: if (originalChild instanceof TreeTableBrowserTreeModel) {
065: TreeTableBrowserTreeModel originalChildModel = (TreeTableBrowserTreeModel) originalChild;
066: originalChildModel.setModel(getModel());
067: originalChildModel.applyFilters();
068: getChildren().add(originalChild);
069: } else if (originalChild instanceof TreeTableBrowserNodeModel) {
070: nodes.add(originalChild);
071: }
072: }
073: // filter
074: Object[][] objects = convertToArray(nodes);
075: // filter nodes
076: getModel().getBrowser().filter.filterAll(true, objects);
077: // get filtered CDTATA nodes
078: objects = getModel().getBrowser().filter.getActiveData();
079: // Create a new list of Nodes from the filtered object[][]. and add to tree. The old
080: // nodes have been deleted.
081: ArrayList newOrder = new ArrayList();
082: for (int row = 0; row < objects.length; row++) {
083: // NOTE: THis should be refactored to use a common utility to clone nodes
084: TreeTableBrowserNodeModel newNode = new TreeTableBrowserNodeModel(
085: getContainer());
086: newNode.setParent(this );
087: // setup newNode's listeners
088: TreeTableBrowserNodeModel originalNode = (TreeTableBrowserNodeModel) ((TreeTableBrowserDataModel) objects[row][0])
089: .getParent();
090: List listeners = originalNode.getListeners();
091: if (listeners != null) {
092: Iterator schmiterator = listeners.iterator();
093: while (schmiterator.hasNext()) {
094: newNode.addListener(schmiterator.next());
095: }
096: }
097:
098: int numColumns = objects[row].length;
099: for (int column = 0; column < numColumns - 1; column++) {
100: TreeTableBrowserDataModel dataModel = (TreeTableBrowserDataModel) objects[row][column];
101: newNode.addChild(dataModel);
102: }
103: // set the node id field, this is obtained from the last column of the array
104: newNode.setName((String) objects[row][numColumns - 1]);
105: newOrder.add(newNode);
106: }
107: // replace nodes with new list
108: getChildren().addAll(0, newOrder);
109: }
110:
111: private Object[][] convertToArray(List nodes) {
112: int cols = getModel().getColumnCount() + 1;
113: Object[][] results = new Object[nodes.size()][cols];
114: for (int i = 0; i < nodes.size(); i++) {
115: TreeTableBrowserNodeModel node = (TreeTableBrowserNodeModel) nodes
116: .get(i);
117: // columns
118: List columnData = node.getTreeTableDatas();
119: for (int j = 0; j < columnData.size(); j++) {
120: results[i][j] = columnData.get(j);
121: }
122: // add model name to last col
123: results[i][cols - 1] = node.getName();
124: }
125: return results;
126: }
127:
128: public Icon getClosedFolderIcon() {
129: return closedFolderIcon;
130: }
131:
132: public Icon getLeafIcon() {
133: return leafIcon;
134: }
135:
136: /**
137: * Returns the model.
138: * @return TreeTableModel
139: */
140: public TreeTableBrowserModel getModel() {
141: return model;
142: }
143:
144: public Icon getOpenFolderIcon() {
145: return openFolderIcon;
146: }
147:
148: /**
149: * @return Returns the originalChildren.
150: */
151: public List getOriginalChildren() {
152: return originalChildren;
153: }
154:
155: public List getTreeTableDatas() {
156: if (this .treeTableDatas == null) {
157: this .treeTableDatas = new ArrayList();
158: }
159: return this .treeTableDatas;
160: }
161:
162: public SwingMLModel locateRow(String id) {
163: if (id.equalsIgnoreCase(getName())) {
164: return this ;
165: }
166: return locateRow(getChildren(), id);
167: }
168:
169: public void setClosedFolderIcon(Icon icon) {
170: this .closedFolderIcon = icon;
171: }
172:
173: public void setClosedFolderIcon(String url) {
174: try {
175: setClosedFolderIcon(new ImageIcon(new URL(url)));
176: } catch (MalformedURLException x) {
177: /* ignore and use default */
178: SwingMLLogger.getInstance().log(
179: "Unable to use URL: '" + url + "'.");
180: SwingMLLogger.getInstance().log(x);
181: }
182: }
183:
184: public void setLeafIcon(Icon icon) {
185: this .leafIcon = icon;
186: }
187:
188: public void setLeafIcon(String url) {
189: try {
190: setLeafIcon(new ImageIcon(new URL(url)));
191: } catch (MalformedURLException x) {
192: /* ignore and use default */
193: SwingMLLogger.getInstance().log(
194: "Unable to use URL: '" + url + "'.");
195: SwingMLLogger.getInstance().log(x);
196: }
197: }
198:
199: /**
200: * Sets the model.
201: * @param model The model to set
202: */
203: public void setModel(TreeTableBrowserModel model) {
204: this .model = model;
205: }
206:
207: public void setOpenFolderIcon(Icon icon) {
208: this .openFolderIcon = icon;
209: }
210:
211: public void setOpenFolderIcon(String url) {
212: try {
213: setOpenFolderIcon(new ImageIcon(new URL(url)));
214: } catch (MalformedURLException x) {
215: /* ignore and use default */
216: SwingMLLogger.getInstance().log(
217: "Unable to use URL: '" + url + "'.");
218: SwingMLLogger.getInstance().log(x);
219: }
220: }
221:
222: /**
223: * @param originalChildren The originalChildren to set.
224: */
225: public void setOriginalChildren(List originalChildren) {
226: this .originalChildren = originalChildren;
227: }
228:
229: public void setTreeTableDatas(List aTreeTableDatas) {
230: this .treeTableDatas = aTreeTableDatas;
231: }
232:
233: /*
234: * Sort data
235: */
236: public void sort() {
237: Iterator itr = getChildren().iterator();
238: ArrayList nodes = new ArrayList();
239: while (itr.hasNext()) {
240: Object child = itr.next();
241: if (child instanceof TreeTableBrowserTreeModel) {
242: TreeTableBrowserTreeModel childModel = (TreeTableBrowserTreeModel) child;
243: childModel.setModel(getModel());
244: childModel.sort();
245: } else if (child instanceof TreeTableBrowserNodeModel) {
246: nodes.add(child);
247: }
248: }
249: // remove nodes
250: getChildren().removeAll(nodes);
251: // sort nodes
252: Object[][] objects = convertToArray(nodes);
253: // sort nodes
254: getModel().getBrowser().sort.sort(objects);
255: // get sorted nodes
256: objects = getModel().getBrowser().sort.getSubject();
257: // Create a new list of Nodes from the sorted object[][]. and add to tree. The old
258: // nodes have been deleted.
259: ArrayList newOrder = new ArrayList();
260: for (int row = 0; row < objects.length; row++) {
261: // NOTE: THis should be refactored to use a common utility to clone nodes
262: TreeTableBrowserNodeModel newNode = new TreeTableBrowserNodeModel(
263: getContainer());
264: newNode.setParent(this );
265:
266: TreeTableBrowserNodeModel originalNode = (TreeTableBrowserNodeModel) ((TreeTableBrowserDataModel) objects[row][0])
267: .getParent();
268: List listeners = originalNode.getListeners();
269: if (listeners != null) {
270: Iterator schmiterator = listeners.iterator();
271: while (schmiterator.hasNext()) {
272: newNode.addListener(schmiterator.next());
273: }
274: }
275:
276: int numColumns = objects[row].length;
277: for (int column = 0; column < numColumns - 1; column++) {
278: newNode.addChild(objects[row][column]);
279: }
280: // set the node name, this is obtained from last column of array
281: newNode.setName((String) objects[row][numColumns - 1]);
282: newOrder.add(newNode);
283: }
284: // replace nodes with new list
285: getChildren().addAll(0, newOrder);
286: }
287:
288: public String toString() {
289: TreeTableBrowserDataModel theTreeTableDataModel = (TreeTableBrowserDataModel) getTreeTableDatas()
290: .get(0);
291: return theTreeTableDataModel.getText();
292: }
293: }
|