KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > web > renderers > tree > AbstractWebNode


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
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * 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, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.web.renderers.tree;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35
36 import org.w3c.dom.DOMException JavaDoc;
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39
40 import com.genimen.djeneric.language.Messages;
41 import com.genimen.djeneric.repository.DjSession;
42 import com.genimen.djeneric.repository.exceptions.DjenericException;
43 import com.genimen.djeneric.structure.ExtentUsage;
44 import com.genimen.djeneric.util.DjStatusDisplayer;
45
46 public abstract class AbstractWebNode
47 {
48   public static final int MAX_NESTED_LEVEL = 25;
49   private static int _uidCounter = 1;
50
51   protected boolean _alreadyLoaded = false;
52   protected ExtentUsage _extentUsageBasedOn;
53   protected DjStatusDisplayer _statusDisplayer = null;
54   AbstractWebNode _parent = null;
55   ArrayList JavaDoc _children = new ArrayList JavaDoc();
56   boolean _expanded = false;
57   private String JavaDoc _uid;
58   private String JavaDoc _path;
59   DjSession _session;
60
61   public AbstractWebNode(DjSession session)
62   {
63     _session = session;
64     _uid = String.valueOf(_uidCounter++);
65     if (_uidCounter > 999999999) _uidCounter = 1;
66   }
67
68   public abstract Element JavaDoc asXml(Document JavaDoc doc) throws DOMException JavaDoc, DjenericException;
69
70   public abstract void reload() throws DjenericException;
71
72   public abstract void delete() throws Exception JavaDoc;
73
74   public abstract void setExpanded(boolean expand) throws DjenericException;
75   
76   public void setExtentUsage(ExtentUsage usage)
77   {
78     _extentUsageBasedOn = usage;
79   }
80
81   public void setStatusMessage(String JavaDoc msg, boolean informative)
82   {
83     if (_statusDisplayer != null)
84     {
85       _statusDisplayer.setStatusMessage(msg, informative);
86     }
87   }
88
89   public ExtentUsage getExtentUsage()
90   {
91     return _extentUsageBasedOn;
92   }
93
94   public String JavaDoc toString()
95   {
96     if (_extentUsageBasedOn != null) return _extentUsageBasedOn.getTitle();
97     return "Model";
98   }
99
100   public AbstractWebNode getParent()
101   {
102     return _parent;
103   }
104
105   public void insertAsChild(AbstractWebNode node)
106   {
107     _children.add(node);
108     node.setParent(this);
109   }
110
111   public void setParent(AbstractWebNode node)
112   {
113     _parent = node;
114     // Reset the uidpath; it might have changed
115
_path = null;
116   }
117
118   public void reloadParent() throws Exception JavaDoc
119   {
120     AbstractWebNode p = getParent();
121     if (p != null) p.reload();
122   }
123
124   protected void expandAll(int level) throws DjenericException
125   {
126     if (level >= MAX_NESTED_LEVEL)
127     {
128       setStatusMessage(Messages.getString("DjenericTreeNode.MaximumNested", String.valueOf(MAX_NESTED_LEVEL)), true);
129     }
130     else
131     {
132
133       setExpanded(true);
134
135       for (int i = 0; i < getChildCount(); i++)
136       {
137         getChildAt(i).expandAll(level + 1);
138       }
139     }
140   }
141
142   public AbstractWebNode getChildAt(int i)
143   {
144     return (AbstractWebNode) _children.get(i);
145   }
146
147   public int getChildCount()
148   {
149     return _children.size();
150   }
151
152   public void collapseAll() throws DjenericException
153   {
154     setExpanded(false);
155
156     for (int i = 0; i < getChildCount(); i++)
157     {
158       getChildAt(i).collapseAll();
159     }
160   }
161
162   public boolean canDelete() throws DjenericException
163   {
164     return getExtentUsage() != null && getExtentUsage().isDeleteAllowed() && getSession().canDelete();
165   }
166
167   public boolean canCreate() throws DjenericException
168   {
169     if (getExtentUsage() == null) return false;
170
171     return getExtentUsage() != null && getExtentUsage().isInsertAllowed() && canEdit() && getSession().canCreate();
172   }
173
174   public boolean canFilter() throws DjenericException
175   {
176     return false;
177   }
178
179   public boolean canEdit() throws DjenericException
180   {
181     return getExtentUsage() != null && getExtentUsage().getEditor() != null && getSession().canModify();
182   }
183
184   public void filter() throws DjenericException
185   {
186     // Overridden for folder nodes
187
throw new DjenericException(Messages.getString("DjenericTreeNode.CanNotFilter"));
188   }
189
190   public AbstractWebNode getNode(String JavaDoc path)
191   {
192     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, "/");
193     return selectNode(st);
194   }
195
196   protected AbstractWebNode selectNode(StringTokenizer JavaDoc st)
197   {
198     if (!st.hasMoreElements())
199     {
200       return this;
201     }
202
203     while (st.hasMoreElements())
204     {
205       String JavaDoc currentNodeUid = st.nextToken();
206
207       for (int i = 0; i < getChildCount(); i++)
208       {
209         if (currentNodeUid.equals(getChildAt(i).getUid()))
210         {
211           return getChildAt(i).selectNode(st);
212         }
213       }
214     }
215     return null;
216   }
217
218   public boolean canCopy() throws DjenericException
219   {
220     return false;
221   }
222
223   public boolean canExport() throws DjenericException
224   {
225     return false;
226   }
227
228   protected void collectParameters(HashMap JavaDoc parentIds)
229   {
230     if (getParent() != null) getParent().collectParameters(parentIds);
231   }
232
233   public HashMap JavaDoc getParameters()
234   {
235     HashMap JavaDoc result = new HashMap JavaDoc();
236     collectParameters(result);
237     return result;
238   }
239
240   public DjStatusDisplayer getStatusDisplayer()
241   {
242     return _statusDisplayer;
243   }
244
245   public void setStatusDisplayer(DjStatusDisplayer statusDisplayer)
246   {
247     _statusDisplayer = statusDisplayer;
248   }
249
250   public boolean isExpanded()
251   {
252     return _expanded;
253   }
254
255   public void expandPath() throws DjenericException
256   {
257     setExpanded(true);
258     if (getParent() != null) getParent().expandPath();
259   }
260
261   public void removeAllChildren()
262   {
263     _children.clear();
264
265   }
266
267   public void removeChild(AbstractWebNode node)
268   {
269     _children.remove(node);
270
271   }
272
273   protected String JavaDoc getUid()
274   {
275     return _uid;
276   }
277
278   public String JavaDoc getPath()
279   {
280     if (_path != null) return _path;
281     if (getParent() != null)
282     {
283       _path = getParent().getPath() + "/" + _uid;
284     }
285     else
286     {
287       // this is the dummy root node; not part of the path
288
_path = "";
289     }
290
291     return _path;
292   }
293
294   public boolean isExpandable()
295   {
296     return (_alreadyLoaded && getChildCount() > 0) || getExtentUsage().getDetailRelationCount() > 0 || !_alreadyLoaded;
297   }
298
299   public DjSession getSession()
300   {
301     return _session;
302   }
303
304   public void expandAll() throws DjenericException
305   {
306     expandAll(0);
307   }
308
309 }
Popular Tags