001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.component.tree;
035:
036: import javax.faces.model.DataModel;
037: import javax.faces.model.DataModelEvent;
038: import javax.faces.model.DataModelListener;
039: import javax.swing.tree.DefaultMutableTreeNode;
040: import javax.swing.tree.TreeModel;
041: import java.util.HashMap;
042: import java.util.Map;
043:
044: /**
045: * TreeDataModel is an implementation of DataModel that wraps a
046: * DefaultTreeModel.
047: */
048: public class TreeDataModel extends DataModel {
049:
050: private int rowIndex = -1;
051: private TreeModel treeModel;
052: private Map rowIndexMap;
053:
054: /**
055: * Default no args contructor
056: */
057: public TreeDataModel() {
058: this (null);
059: }
060:
061: /**
062: * @param treeModel
063: */
064: public TreeDataModel(TreeModel treeModel) {
065: super ();
066: setWrappedData(treeModel);
067: rowIndexMap = new HashMap();
068: setChildCount();
069:
070: }
071:
072: /* (non-Javadoc)
073: * @see javax.faces.model.DataModel#isRowAvailable()
074: */
075: public boolean isRowAvailable() {
076: if (treeModel == null) {
077: return (false);
078: } else if ((rowIndex >= 0) && (rowIndex < childCount)) {
079: return (true);
080: } else {
081: return (false);
082: }
083: }
084:
085: /* (non-Javadoc)
086: * @see javax.faces.model.DataModel#getRowCount()
087: */
088: public int getRowCount() {
089: return childCount;
090: }
091:
092: private int childCount = -1;
093:
094: private int setChildCount(DefaultMutableTreeNode treeNode) {
095:
096: int count = treeNode.getChildCount();
097: for (int i = 0; i < count; i++) {
098: DefaultMutableTreeNode child = (DefaultMutableTreeNode) treeNode
099: .getChildAt(i);
100: ((IceUserObject) child.getUserObject())
101: .setRowIndex(treeNodeRowIndex++);
102: addNodeToMap(child, ((IceUserObject) child.getUserObject())
103: .getRowIndex());
104:
105: if (((IceUserObject) child.getUserObject()).isExpanded()) {
106: childCount += treeNode.getChildCount();
107: setChildCount(child);
108: }
109: }
110: return childCount;
111: }
112:
113: private int treeNodeRowIndex = -1;
114:
115: private void setChildCount() {
116: DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel
117: .getRoot();
118: rowIndexMap.clear();
119: treeNodeRowIndex = 0;
120: //There will be a root always
121: childCount = 1;
122: ((IceUserObject) root.getUserObject())
123: .setRowIndex(treeNodeRowIndex++);
124: addNodeToMap(root, ((IceUserObject) root.getUserObject())
125: .getRowIndex());
126: if (((IceUserObject) root.getUserObject()).isExpanded()) {
127: childCount += root.getChildCount();
128:
129: }
130: }
131:
132: private void addNodeToMap(DefaultMutableTreeNode node,
133: int treeNodeRowIndex) {
134:
135: rowIndexMap.put(new Integer(treeNodeRowIndex), node);
136: }
137:
138: /* (non-Javadoc)
139: * @see javax.faces.model.DataModel#getRowData()
140: */
141: public Object getRowData() {
142: if (treeModel == null) {
143: return (null);
144: } else if (!isRowAvailable()) {
145: throw new IllegalArgumentException();
146: } else {
147: return (rowIndexMap.get(new Integer(rowIndex)));
148: }
149: }
150:
151: /* (non-Javadoc)
152: * @see javax.faces.model.DataModel#getRowIndex()
153: */
154: public int getRowIndex() {
155: return rowIndex;
156: }
157:
158: /* (non-Javadoc)
159: * @see javax.faces.model.DataModel#setRowIndex(int)
160: */
161: public void setRowIndex(int rowIndex) {
162: if (rowIndex < -1) {
163: throw new IllegalArgumentException();
164: }
165: int old = this .rowIndex;
166: this .rowIndex = rowIndex;
167: if (treeModel == null) {
168: return;
169: }
170: DataModelListener[] listeners = getDataModelListeners();
171: if ((old != this .rowIndex) && (listeners != null)) {
172: Object rowData = null;
173: if (isRowAvailable()) {
174: rowData = getRowData();
175: }
176: DataModelEvent event = new DataModelEvent(this ,
177: this .rowIndex, rowData);
178: int n = listeners.length;
179: for (int i = 0; i < n; i++) {
180: if (null != listeners[i]) {
181: listeners[i].rowSelected(event);
182: }
183: }
184: }
185: }
186:
187: /* (non-Javadoc)
188: * @see javax.faces.model.DataModel#getWrappedData()
189: */
190: public Object getWrappedData() {
191: return treeModel;
192: }
193:
194: /* (non-Javadoc)
195: * @see javax.faces.model.DataModel#setWrappedData(java.lang.Object)
196: */
197: public void setWrappedData(Object data) {
198: if (data == null) {
199: treeModel = null;
200: setRowIndex(-1);
201: } else {
202: treeModel = (TreeModel) data;
203: rowIndex = -1;
204: setRowIndex(0);
205: }
206:
207: }
208:
209: }
|