1 30 package com.genimen.djeneric.tools.specifier.tree; 31 32 import java.util.ArrayList ; 33 import java.util.HashMap ; 34 35 import javax.swing.ImageIcon ; 36 import javax.swing.tree.TreePath ; 37 38 import com.genimen.djeneric.language.Messages; 39 import com.genimen.djeneric.repository.DjAssociation; 40 import com.genimen.djeneric.repository.DjCursor; 41 import com.genimen.djeneric.repository.DjList; 42 import com.genimen.djeneric.repository.DjObject; 43 import com.genimen.djeneric.repository.DjOql; 44 import com.genimen.djeneric.repository.DjProperty; 45 import com.genimen.djeneric.repository.exceptions.DjenericException; 46 import com.genimen.djeneric.repository.oql.core.ParseException; 47 import com.genimen.djeneric.structure.ExtentUsage; 48 import com.genimen.djeneric.structure.RelationUsage; 49 import com.genimen.djeneric.tools.specifier.Specifier; 50 import com.genimen.djeneric.tools.specifier.dialogs.FilterDialog; 51 import com.genimen.djeneric.tools.specifier.interfaces.DjenericObjectContainer; 52 53 public class DjenericFolder extends DjenericTreeNode implements DjenericObjectContainer 54 { 55 private static final long serialVersionUID = 1L; 56 public final static int MAX_OBJECTS_IN_FOLDER = 300; 57 protected boolean _allDisplayed = true; 58 protected boolean _filtered = false; 59 protected int[] _sortIndices = null; 60 protected DjList _objects = null; 61 protected DjOql _oql = null; 62 protected RelationUsage _via = null; 63 private Boolean _canFilter = null; 64 65 public DjenericFolder() 66 { 67 } 68 69 public void reload() throws DjenericException 70 { 71 clearLoadedObjects(); 72 expandNode(); 73 } 74 75 public void delete() throws Exception 76 { 77 } 79 80 85 public DjList getObjects() throws DjenericException 86 { 87 if (_objects != null) return _objects; 88 89 try 90 { 91 setStatusMessage(Messages.getString("global.Loading"), true); 92 93 initOql(); 94 95 _allDisplayed = true; 96 _objects = new DjList(); 97 98 ExtentUsage usage = getExtentUsage(); 99 _objects.setStoredTypeName(usage.getExtent()); 100 101 DjCursor cursor; 102 if (isRootFolder() || !isContentsLoadedViaRelation()) 103 { 104 if (_oql != null) cursor = getSession().getObjectsCursor(_oql, true); 105 else cursor = getSession().getObjectsCursor(usage.getExtent(), true); 106 } 107 else 108 { 109 DjAssociation assoc = getObject().getDetailAssociationByName(getVia().getRelation().getName(), 110 usage.getExtent()); 111 if (_oql != null) cursor = assoc.getObjectsCursor(_oql, true); 112 else cursor = assoc.getObjectsCursor(true); 113 } 114 115 try 116 { 117 int maxObjects = MAX_OBJECTS_IN_FOLDER; 118 DjObject obj; 119 while (((obj = cursor.getNext()) != null) && maxObjects-- > 0) 120 { 121 _objects.add(obj); 122 } 123 if (maxObjects < 0) 124 { 125 _allDisplayed = false; 126 } 127 } 128 finally 129 { 130 cursor.close(); 131 } 132 133 if (_sortIndices == null) _sortIndices = getExtentUsage().getExtent().getPropertySortIndices(); 134 _objects.sort(_sortIndices); 135 return _objects; 136 } 137 finally 138 { 139 setStatusMessage(Specifier.OK_MSG, true); 140 } 141 } 142 143 protected boolean isContentsLoadedViaRelation() 144 { 145 return getVia() != null && getVia().getRelation() != null; 146 } 147 148 protected boolean isRootFolder() 149 { 150 return getVia() == null; 151 } 152 153 public String toString() 154 { 155 StringBuffer sb = new StringBuffer (50); 156 157 sb.append(super.toString()); 158 if (_filtered) sb.append(Messages.getString("DjenericFolder.Filtered")); 159 if (!_allDisplayed) sb.append(Messages.getString("DjenericFolder.Partially")); 160 161 return sb.toString(); 162 } 163 164 167 public void filter() throws DjenericException 168 { 169 171 ArrayList excludes = new ArrayList (); 172 if (isContentsLoadedViaRelation()) excludes.add(getVia().getDetailPropertyName()); 173 174 FilterDialog fd = new FilterDialog(getFrame(), getSession(), getExtentUsage(), excludes); 175 if (hasOqlFilter()) fd.setAdditionalFilter(getExtentUsage().getOqlExpression()); 176 177 if (fd.wasCancelled()) return; 178 179 clearLoadedObjects(); 180 181 _oql = fd.getOql(); 182 setOqlParameters(_oql, true); 183 184 _filtered = true; 185 DjList lst = getObjects(); 186 showObjects(); 187 _tree.expandPath(new TreePath (this.getPath())); 188 189 String msg; 190 if (!_allDisplayed) msg = Messages.getString("DjenericFolder.FoundTruncated", String.valueOf(lst.size()), 191 getExtentUsage().getExtent().getNamePlural()); 192 else 193 { 194 if (lst.size() == 1) msg = Messages.getString("DjenericFolder.Found1", getExtentUsage().getExtent() 195 .getNameSingular()); 196 else msg = Messages.getString("DjenericFolder.Found", String.valueOf(lst.size()), getExtentUsage().getExtent() 197 .getNamePlural()); 198 } 199 setStatusMessage(msg, true); 200 } 201 202 205 public DjObject getObject() 206 { 207 DjenericTreeNode parent = getParentTreeNode(); 208 if (parent != null && parent instanceof DjenericNode) 209 { 210 return ((DjenericNode) parent).getObject(); 211 } 212 return null; 213 } 214 215 221 public RelationUsage getVia() 222 { 223 return _via; 224 } 225 226 232 public void setVia(RelationUsage via) 233 { 234 _via = via; 235 setExtentUsage(via.getDetail()); 236 } 237 238 public ImageIcon getImageIcon() 239 { 240 return Specifier.getImageIcon("folder.gif"); 241 } 242 243 public void expandNode() throws DjenericException 244 { 245 if (_alreadyLoaded) 246 { 247 return; 248 } 249 showObjects(); 250 } 251 252 public DjenericNode[] getChildNodes() throws Exception 253 { 254 expandNode(); 255 ArrayList result = new ArrayList (); 256 for (int i = 0; i < getChildCount(); i++) 257 result.add(getChildAt(i)); 258 return (DjenericNode[]) result.toArray(new DjenericNode[0]); 259 } 260 261 public boolean canDelete() 262 { 263 return false; 264 } 265 266 public boolean canFilter() 267 { 268 if (getExtentUsage() == null) return false; 269 270 if (_canFilter != null) return _canFilter.booleanValue(); 271 272 boolean qry = false; 273 274 DjProperty[] props = getExtentUsage().getExtent().getProperties(); 275 for (int i = 0; i < props.length && !qry; i++) 276 { 277 qry = props[i].isQueryable(); 278 } 279 280 _canFilter = new Boolean (qry); 281 return qry; 282 } 283 284 public boolean canExport() 285 { 286 return getExtentUsage() != null && getExtentUsage().getEditor() != null; 287 } 288 289 public boolean hasOqlFilter() 290 { 291 if (getExtentUsage() == null) return false; 292 293 String filter = getExtentUsage().getOqlExpression(); 294 return filter != null && filter.trim().length() != 0; 295 } 296 297 302 protected void initOql() throws DjenericException 303 { 304 if (hasOqlFilter() && _oql == null) 305 { 306 _oql = getExtentUsage().createDefaultOql(getSession()); 307 308 setOqlParameters(_oql, false); 309 } 310 } 311 312 private void setOqlParameters(DjOql oql, boolean ignoreNonExistingParameters) throws DjenericException 313 { 314 String [] parameters; 315 try 316 { 317 parameters = oql.getParameterNames(); 318 } 319 catch (ParseException e) 320 { 321 throw new DjenericException(e); 322 } 323 HashMap parentIds = new HashMap (); 324 collectParameters(parentIds); 325 326 for (int i = 0; i < parameters.length; i++) 327 { 328 DjObject obj = (DjObject) parentIds.get(parameters[i]); 329 if (obj == null && !ignoreNonExistingParameters) throw new DjenericException("Parameter with ID " + parameters[i] 330 + " not defined in tree"); 331 if (obj != null) oql.setParameter(parameters[i], obj.getObjectId()); 332 } 333 } 334 335 protected void clearLoadedObjects() 336 { 337 _alreadyLoaded = false; 338 _objects = null; 339 _oql = null; 340 _filtered = false; 341 } 342 343 347 protected void showObjects() throws DjenericException 348 { 349 boolean isCollapsed = _tree.isCollapsed(new TreePath (this.getPath())); 350 removeAllChildren(); 351 352 try 353 { 354 ExtentUsage usage = getExtentUsage(); 355 boolean detailHasKids = (usage.getDetailRelationCount() != 0); 356 getObjects(); 357 358 for (int j = 0; j < _objects.size(); j++) 359 { 360 DjObject obj = _objects.getDjenericObjectAt(j); 361 DjenericNode node = new DjenericNode(); 362 node.setExtentUsage(usage); 363 node.setSpecifierPanelContainer(getSpecifierPanelContainer()); 364 node.setObject(obj); 365 if (detailHasKids) 366 { 367 insertAsFolder(node); 368 } 369 else 370 { 371 insertAsChild(node); 372 } 373 } 374 375 _alreadyLoaded = true; 376 } 377 finally 378 { 379 getModel().nodeStructureChanged(this); 380 if (!isCollapsed) 381 { 382 _tree.expandPath(new TreePath (this.getPath())); 383 } 384 } 385 } 386 387 } | Popular Tags |