KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > tree > DjenericFolder


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.specifier.tree;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.HashMap JavaDoc;
34
35 import javax.swing.ImageIcon JavaDoc;
36 import javax.swing.tree.TreePath JavaDoc;
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 JavaDoc _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 JavaDoc
76   {
77     // delete for all contents considered too dangerous.. do nothing
78
}
79
80   /**
81    * Returns a list of objects that is contained by this folder
82    * @return the list
83    * @throws DjenericException
84    */

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 JavaDoc toString()
154   {
155     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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   /**
165    * Displays the filter dialog, performs the query and displays the result in the tree
166    */

167   public void filter() throws DjenericException
168   {
169     // Overridden for folder nodes
170

171     ArrayList JavaDoc excludes = new ArrayList JavaDoc();
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 JavaDoc(this.getPath()));
188
189     String JavaDoc 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   /**
203    * Returns the object that this folder belongs to
204    */

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   /**
216    * Returns a descriptor for the relation that defines the contents of this folder.
217    * If there is no relation (because this folder is customized for example) then
218    * null is returned
219    * @return
220    */

221   public RelationUsage getVia()
222   {
223     return _via;
224   }
225
226   /**
227    * Sets the descriptor for the relation that defines the contents of this folder.
228    * The extentusage of this folder is also set to the usage of the detail side of
229    * the relation.
230    * @return
231    */

232   public void setVia(RelationUsage via)
233   {
234     _via = via;
235     setExtentUsage(via.getDetail());
236   }
237
238   public ImageIcon JavaDoc 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 JavaDoc
253   {
254     expandNode();
255     ArrayList JavaDoc result = new ArrayList JavaDoc();
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 JavaDoc(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 JavaDoc filter = getExtentUsage().getOqlExpression();
294     return filter != null && filter.trim().length() != 0;
295   }
296
297   /**
298    * Initializes the OQL object if there is a OQL expression defined for the extentusage
299    * @throws DjenericException
300    *
301    */

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 JavaDoc[] parameters;
315     try
316     {
317       parameters = oql.getParameterNames();
318     }
319     catch (ParseException e)
320     {
321       throw new DjenericException(e);
322     }
323     HashMap JavaDoc parentIds = new HashMap JavaDoc();
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   /**
344    * Displays (refreshes) the objects displayed in the tree
345    * @throws DjenericException
346    */

347   protected void showObjects() throws DjenericException
348   {
349     boolean isCollapsed = _tree.isCollapsed(new TreePath JavaDoc(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 JavaDoc(this.getPath()));
383       }
384     }
385   }
386
387 }
Popular Tags