KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > links > CatalogWebTreeModel


1 package com.openedit.store.links;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Set JavaDoc;
9
10 import com.openedit.store.CatalogArchive;
11 import com.openedit.store.Category;
12 import com.openedit.users.User;
13 import com.openedit.webui.tree.WebTreeModel;
14
15 public class CatalogWebTreeModel implements WebTreeModel
16 {
17     protected Category fieldRootCatalog;
18     protected User fieldUser;
19     protected Set JavaDoc fieldHiddenCatalogs;
20     protected Set JavaDoc fieldLimitToCatalogs;
21     protected CatalogArchive fieldCatalogArchive;
22     
23     public CatalogWebTreeModel()
24     {
25     }
26     
27     public CatalogWebTreeModel( Category inRootCatalog )
28     {
29         fieldRootCatalog = inRootCatalog;
30     }
31     /**
32      * @deprecated Use the list children method since it is faster
33      */

34     public Object JavaDoc getChild( Object JavaDoc inParent, int index )
35     {
36         return listChildren(inParent).get( index );
37     }
38
39     public List JavaDoc listChildren(Object JavaDoc inParent)
40     {
41         if( inParent == null)
42         {
43             return Collections.EMPTY_LIST;
44         }
45         Category parent = (Category) inParent;
46         List JavaDoc ok = new ArrayList JavaDoc(parent.getChildren().size());
47         for (Iterator JavaDoc iter = parent.getChildren().iterator(); iter.hasNext();)
48         {
49             //If this is slow then we might consider only checking the top level catalogs
50
Category cat = (Category) iter.next();
51             if ( okToAdd(cat) ) {
52                 ok.add(cat);
53             }
54         }
55         return ok;
56     }
57     protected boolean okToAdd(Category inCat)
58     {
59         if( inCat.getParentCatalog() == null )
60         {
61             return true;
62         }
63         if( getHiddenCatalogs().contains(inCat.getId()) )
64         {
65             return false;
66         }
67         if( getLimitToCatalogs().size() > 0 )
68         {
69             //Only worry about including these catalogs
70
for (Iterator JavaDoc iterator = getLimitToCatalogs().iterator(); iterator.hasNext();)
71             {
72                 Category okid = (Category)iterator.next();
73                 if( inCat.getId().equals(okid.getId()) || okid.hasParent(inCat.getId() ) )
74                 {
75                     return true;
76                 }
77             }
78             //This could be slow
79
for (Iterator JavaDoc iterator = getLimitToCatalogs().iterator(); iterator.hasNext();)
80             {
81                 Category okid = (Category)iterator.next();
82                 if( inCat.hasParent(okid.getId() ) )
83                 {
84                     return true;
85                 }
86             }
87             
88             //None found so cancel if at same level as included one
89
for (Iterator JavaDoc iterator = getLimitToCatalogs().iterator(); iterator.hasNext();)
90             {
91                 Category okid = (Category)iterator.next();
92                 if( inCat.getLevel() == okid.getLevel() )
93                 {
94                     return false;
95                 }
96             }
97             //index/photo2/stuff1 nostuff
98
return true;
99         }
100         
101         return true;
102     }
103     public Set JavaDoc getHiddenCatalogs()
104     {
105         if ( fieldHiddenCatalogs == null)
106         {
107             limitList();
108         }
109         return fieldHiddenCatalogs;
110     }
111     public Set JavaDoc getLimitToCatalogs()
112     {
113         if ( fieldLimitToCatalogs == null)
114         {
115             limitList();
116         }
117         return fieldLimitToCatalogs;
118     }
119
120     protected void limitList()
121     {
122         //look over this users permissions and see if there is a limit
123
fieldHiddenCatalogs = new HashSet JavaDoc();
124         fieldLimitToCatalogs = new HashSet JavaDoc();
125         for (Iterator JavaDoc iterator = getUser().listGroupPermissions().iterator(); iterator.hasNext();)
126         {
127             String JavaDoc perm = (String JavaDoc) iterator.next();
128             if( perm.startsWith("limittocategory:"))
129             {
130                 String JavaDoc catid = perm.substring("limittocategory:".length());
131                 Category cat = getCatalogArchive().getCatalog(catid);
132                 if( cat != null)
133                 {
134                     fieldLimitToCatalogs.add(cat);
135                 }
136 // Category cat = getCatalogArchive().getCatalog(catid);
137
// while( cat != null )
138
// {
139
// fieldExclusiveCatalogs.add(cat.getId());
140
// cat = cat.getParentCatalog();
141
// }
142
}
143             else if ( perm.startsWith("hidecatalog:" ) ) //Aways exclude it
144
{
145                 String JavaDoc catid = perm.substring("hidecatalog:".length());
146                 fieldHiddenCatalogs.add(catid);
147             }
148             else if ( perm.startsWith("backgroundcatalog:") ) //Aways exclude it
149
{
150                 String JavaDoc catid = perm.substring("backgroundcatalog:".length());
151                 fieldHiddenCatalogs.add(catid);
152             }
153         }
154     }
155
156     public List JavaDoc getChildren(Object JavaDoc inParent)
157     {
158         return listChildren(inParent);
159     }
160
161     public List JavaDoc getChildrenInRows(Object JavaDoc inParent, int inColCount)
162     {
163         //Now break up the page into rows by dividing the count they wanted
164
List JavaDoc children = getChildren(inParent);
165         double rowscount = (double)children.size() / (double)inColCount;
166         
167         List JavaDoc rows = new ArrayList JavaDoc();
168         for (int i = 0; i < rowscount; i++)
169         {
170             int start = i*inColCount;
171             int end = i*inColCount + inColCount;
172             List JavaDoc sublist = children.subList(start,Math.min( children.size(),end ));
173             rows.add(sublist);
174         }
175         return rows;
176     }
177
178     
179     public int getChildCount( Object JavaDoc inParent )
180     {
181         return listChildren(inParent).size();
182     }
183
184     public int getIndexOfChild( Object JavaDoc inParent, Object JavaDoc inChild )
185     {
186         return listChildren(inParent).indexOf( inChild );
187     }
188
189     public boolean isLeaf( Object JavaDoc inNode )
190     {
191         return !( (Category) inNode ).hasChildren();
192     }
193
194     public Object JavaDoc getRoot()
195     {
196         return fieldRootCatalog;
197     }
198     
199     public String JavaDoc getId( Object JavaDoc inNode )
200     {
201         return ( (Category) inNode ).getId();
202     }
203
204     public Object JavaDoc getParent(Object JavaDoc inNode)
205     {
206         Category child = (Category)inNode;
207         return child.getParentCatalog();
208     }
209
210     public User getUser()
211     {
212         return fieldUser;
213     }
214
215     public void setUser(User inUser)
216     {
217         fieldUser = inUser;
218     }
219     public Category getRootCatalog()
220     {
221         return fieldRootCatalog;
222     }
223
224     public void setRootCatalog(Category inRootCatalog)
225     {
226         fieldRootCatalog = inRootCatalog;
227     }
228
229     public Object JavaDoc getChildById(String JavaDoc inId)
230     {
231         return findNodeById(getRoot(), inId);
232     }
233
234     public Object JavaDoc findNodeById(Object JavaDoc inRoot, String JavaDoc inId)
235     {
236         String JavaDoc test = getId(inRoot);
237         if ( test.equals(inId) )
238         {
239             return inRoot;
240         }
241         for (Iterator JavaDoc iterator = getChildren(inRoot).iterator(); iterator.hasNext();)
242         {
243             Object JavaDoc child = iterator.next();
244             child = findNodeById(child,inId);
245             if ( child != null)
246             {
247                 return child;
248             }
249         }
250         return null;
251     }
252
253     public CatalogArchive getCatalogArchive()
254     {
255         return fieldCatalogArchive;
256     }
257
258     public void setCatalogArchive(CatalogArchive inCatalogArchive)
259     {
260         fieldCatalogArchive = inCatalogArchive;
261     }
262
263 }
264
Popular Tags