1 package com.openedit.store.links; 2 3 import java.util.ArrayList ; 4 import java.util.Collections ; 5 import java.util.HashSet ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Set ; 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 fieldHiddenCatalogs; 20 protected Set fieldLimitToCatalogs; 21 protected CatalogArchive fieldCatalogArchive; 22 23 public CatalogWebTreeModel() 24 { 25 } 26 27 public CatalogWebTreeModel( Category inRootCatalog ) 28 { 29 fieldRootCatalog = inRootCatalog; 30 } 31 34 public Object getChild( Object inParent, int index ) 35 { 36 return listChildren(inParent).get( index ); 37 } 38 39 public List listChildren(Object inParent) 40 { 41 if( inParent == null) 42 { 43 return Collections.EMPTY_LIST; 44 } 45 Category parent = (Category) inParent; 46 List ok = new ArrayList (parent.getChildren().size()); 47 for (Iterator iter = parent.getChildren().iterator(); iter.hasNext();) 48 { 49 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 for (Iterator 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 for (Iterator 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 for (Iterator 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 return true; 99 } 100 101 return true; 102 } 103 public Set getHiddenCatalogs() 104 { 105 if ( fieldHiddenCatalogs == null) 106 { 107 limitList(); 108 } 109 return fieldHiddenCatalogs; 110 } 111 public Set getLimitToCatalogs() 112 { 113 if ( fieldLimitToCatalogs == null) 114 { 115 limitList(); 116 } 117 return fieldLimitToCatalogs; 118 } 119 120 protected void limitList() 121 { 122 fieldHiddenCatalogs = new HashSet (); 124 fieldLimitToCatalogs = new HashSet (); 125 for (Iterator iterator = getUser().listGroupPermissions().iterator(); iterator.hasNext();) 126 { 127 String perm = (String ) iterator.next(); 128 if( perm.startsWith("limittocategory:")) 129 { 130 String catid = perm.substring("limittocategory:".length()); 131 Category cat = getCatalogArchive().getCatalog(catid); 132 if( cat != null) 133 { 134 fieldLimitToCatalogs.add(cat); 135 } 136 } 143 else if ( perm.startsWith("hidecatalog:" ) ) { 145 String catid = perm.substring("hidecatalog:".length()); 146 fieldHiddenCatalogs.add(catid); 147 } 148 else if ( perm.startsWith("backgroundcatalog:") ) { 150 String catid = perm.substring("backgroundcatalog:".length()); 151 fieldHiddenCatalogs.add(catid); 152 } 153 } 154 } 155 156 public List getChildren(Object inParent) 157 { 158 return listChildren(inParent); 159 } 160 161 public List getChildrenInRows(Object inParent, int inColCount) 162 { 163 List children = getChildren(inParent); 165 double rowscount = (double)children.size() / (double)inColCount; 166 167 List rows = new ArrayList (); 168 for (int i = 0; i < rowscount; i++) 169 { 170 int start = i*inColCount; 171 int end = i*inColCount + inColCount; 172 List 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 inParent ) 180 { 181 return listChildren(inParent).size(); 182 } 183 184 public int getIndexOfChild( Object inParent, Object inChild ) 185 { 186 return listChildren(inParent).indexOf( inChild ); 187 } 188 189 public boolean isLeaf( Object inNode ) 190 { 191 return !( (Category) inNode ).hasChildren(); 192 } 193 194 public Object getRoot() 195 { 196 return fieldRootCatalog; 197 } 198 199 public String getId( Object inNode ) 200 { 201 return ( (Category) inNode ).getId(); 202 } 203 204 public Object getParent(Object 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 getChildById(String inId) 230 { 231 return findNodeById(getRoot(), inId); 232 } 233 234 public Object findNodeById(Object inRoot, String inId) 235 { 236 String test = getId(inRoot); 237 if ( test.equals(inId) ) 238 { 239 return inRoot; 240 } 241 for (Iterator iterator = getChildren(inRoot).iterator(); iterator.hasNext();) 242 { 243 Object 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 |