KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > tree > TreeDataModel


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.component.tree;
35
36 import javax.faces.model.DataModel;
37 import javax.faces.model.DataModelEvent;
38 import javax.faces.model.DataModelListener;
39 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
40 import javax.swing.tree.TreeModel JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.Map JavaDoc;
43
44 /**
45  * TreeDataModel is an implementation of DataModel that wraps a
46  * DefaultTreeModel.
47  */

48 public class TreeDataModel extends DataModel {
49
50     private int rowIndex = -1;
51     private TreeModel JavaDoc treeModel;
52     private Map JavaDoc rowIndexMap;
53
54     /**
55      * Default no args contructor
56      */

57     public TreeDataModel() {
58         this(null);
59     }
60
61     /**
62      * @param treeModel
63      */

64     public TreeDataModel(TreeModel JavaDoc treeModel) {
65         super();
66         setWrappedData(treeModel);
67         rowIndexMap = new HashMap JavaDoc();
68         setChildCount();
69
70     }
71
72     /* (non-Javadoc)
73     * @see javax.faces.model.DataModel#isRowAvailable()
74     */

75     public boolean isRowAvailable() {
76         if (treeModel == null) {
77             return (false);
78         } else if ((rowIndex >= 0) && (rowIndex < childCount)) {
79             return (true);
80         } else {
81             return (false);
82         }
83     }
84
85     /* (non-Javadoc)
86      * @see javax.faces.model.DataModel#getRowCount()
87      */

88     public int getRowCount() {
89         return childCount;
90     }
91
92     private int childCount = -1;
93
94     private int setChildCount(DefaultMutableTreeNode JavaDoc treeNode) {
95
96         int count = treeNode.getChildCount();
97         for (int i = 0; i < count; i++) {
98             DefaultMutableTreeNode JavaDoc child =
99                     (DefaultMutableTreeNode JavaDoc) treeNode.getChildAt(i);
100             ((IceUserObject) child.getUserObject())
101                     .setRowIndex(treeNodeRowIndex++);
102             addNodeToMap(child,
103                          ((IceUserObject) child.getUserObject()).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 JavaDoc root =
117                 (DefaultMutableTreeNode JavaDoc) treeModel.getRoot();
118         rowIndexMap.clear();
119         treeNodeRowIndex = 0;
120         //There will be a root always
121
childCount = 1;
122         ((IceUserObject) root.getUserObject()).setRowIndex(treeNodeRowIndex++);
123         addNodeToMap(root,
124                      ((IceUserObject) root.getUserObject()).getRowIndex());
125         if (((IceUserObject) root.getUserObject()).isExpanded()) {
126             childCount += root.getChildCount();
127
128         }
129     }
130
131     private void addNodeToMap(DefaultMutableTreeNode JavaDoc node,
132                               int treeNodeRowIndex) {
133
134
135         rowIndexMap.put(new Integer JavaDoc(treeNodeRowIndex), node);
136     }
137
138     /* (non-Javadoc)
139     * @see javax.faces.model.DataModel#getRowData()
140     */

141     public Object JavaDoc getRowData() {
142         if (treeModel == null) {
143             return (null);
144         } else if (!isRowAvailable()) {
145             throw new IllegalArgumentException JavaDoc();
146         } else {
147             return (rowIndexMap.get(new Integer JavaDoc(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 JavaDoc();
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 JavaDoc rowData = null;
173             if (isRowAvailable()) {
174                 rowData = getRowData();
175             }
176             DataModelEvent event =
177                     new DataModelEvent(this, 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 JavaDoc 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 JavaDoc data) {
198         if (data == null) {
199             treeModel = null;
200             setRowIndex(-1);
201         } else {
202             treeModel = (TreeModel JavaDoc) data;
203             rowIndex = -1;
204             setRowIndex(0);
205         }
206
207     }
208
209 }
210
Popular Tags