KickJava   Java API By Example, From Geeks To Geeks.

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


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.tools.specifier.tree;
31
32 import java.awt.Container JavaDoc;
33 import java.awt.Frame JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36
37 import javax.swing.ImageIcon JavaDoc;
38 import javax.swing.RepaintManager JavaDoc;
39 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
40 import javax.swing.tree.DefaultTreeModel JavaDoc;
41 import javax.swing.tree.TreeNode JavaDoc;
42 import javax.swing.tree.TreePath JavaDoc;
43
44 import com.genimen.djeneric.language.Messages;
45 import com.genimen.djeneric.repository.DjPersistenceManager;
46 import com.genimen.djeneric.repository.DjSession;
47 import com.genimen.djeneric.repository.exceptions.DjenericException;
48 import com.genimen.djeneric.structure.ExtentUsage;
49 import com.genimen.djeneric.tools.specifier.interfaces.SpecifierPanelContainer;
50 import com.genimen.djeneric.util.DjStatusDisplayer;
51
52 public abstract class DjenericTreeNode extends DefaultMutableTreeNode JavaDoc
53 {
54   protected boolean _alreadyLoaded = false;
55   protected DjenericTree _tree = null;
56   protected ExtentUsage _extentUsageBasedOn;
57   protected DjStatusDisplayer _statusDisplayer = null;
58   public static final int MAX_NESTED_LEVEL = 25;
59   SpecifierPanelContainer _specifier;
60
61   public DjenericTreeNode()
62   {
63   }
64
65   public void setExtentUsage(ExtentUsage usage)
66   {
67     _extentUsageBasedOn = usage;
68   }
69
70   public void setTree(DjenericTree tree)
71   {
72     _tree = tree;
73   }
74
75   public void setStatusMessage(String JavaDoc msg, boolean informative)
76   {
77     if (_statusDisplayer != null)
78     {
79       _statusDisplayer.setStatusMessage(msg, informative);
80     }
81     else if (_tree == null)
82     {
83       System.out.println(msg);
84     }
85     else
86     {
87       Container JavaDoc cont = _tree.getParent();
88       while (cont != null && !(cont instanceof DjStatusDisplayer))
89         cont = cont.getParent();
90       _statusDisplayer = (DjStatusDisplayer) cont;
91     }
92
93     if (_statusDisplayer != null) _statusDisplayer.setStatusMessageNow(msg, informative);
94   }
95
96   public Frame JavaDoc getFrame()
97   {
98     if (_tree == null)
99     {
100       return null;
101     }
102     else
103     {
104       Container JavaDoc cont = _tree.getParent();
105       while (cont != null && !(cont instanceof Frame JavaDoc))
106         cont = cont.getParent();
107       return (Frame JavaDoc) cont;
108     }
109   }
110
111   public ExtentUsage getExtentUsage()
112   {
113     return _extentUsageBasedOn;
114   }
115
116   public String JavaDoc toString()
117   {
118     if (_extentUsageBasedOn != null) return _extentUsageBasedOn.getTitle();
119     return "Model";
120   }
121
122   public DefaultTreeModel JavaDoc getModel()
123   {
124     return (DefaultTreeModel JavaDoc) _tree.getModel();
125   }
126
127   public DjPersistenceManager getManager()
128   {
129     return _tree.getManager();
130   }
131
132   public DjSession getSession()
133   {
134     return _tree.getSession();
135   }
136
137   public DjenericTreeNode getParentTreeNode()
138   {
139     return (DjenericTreeNode) getParent();
140   }
141
142   protected void copyInternalReferences(DjenericTreeNode node)
143   {
144     node.setTree(_tree);
145     node.setSpecifierPanelContainer(_specifier);
146   }
147
148   public void insertAsChild(DjenericTreeNode node)
149   {
150     getModel().insertNodeInto(node, this, getChildCount());
151     copyInternalReferences(node);
152   }
153
154   public void insertAsFolder(DjenericTreeNode node)
155   {
156     getModel().insertNodeInto(node, this, getChildCount());
157     getModel().insertNodeInto(new DefaultMutableTreeNode JavaDoc(), node, 0);
158     copyInternalReferences(node);
159   }
160
161   public void insertAsSibling(DjenericTreeNode node)
162   {
163     getModel().insertNodeInto(node, parent, parent.getChildCount());
164     copyInternalReferences(node);
165   }
166
167   public void removeFromTree()
168   {
169     getModel().removeNodeFromParent(this);
170   }
171
172   public void repaintTree()
173   {
174     RepaintManager JavaDoc rm = RepaintManager.currentManager(_tree);
175     if (rm != null)
176     {
177       rm.validateInvalidComponents();
178       rm.paintDirtyRegions();
179     }
180   }
181
182   public void reloadParent() throws Exception JavaDoc
183   {
184     DjenericTreeNode p = getParentTreeNode();
185     if (p != null) p.reload();
186   }
187
188   public void expandAll(int level)
189   {
190     if (level >= MAX_NESTED_LEVEL)
191     {
192       setStatusMessage(Messages.getString("DjenericTreeNode.MaximumNested", String.valueOf(MAX_NESTED_LEVEL)), true);
193       return;
194     }
195
196     _tree.expandPath(new TreePath JavaDoc(this.getPath()));
197     int cc = getChildCount();
198     for (int i = 0; i < cc; i++)
199     {
200       DjenericTreeNode child = (DjenericTreeNode) getChildAt(i);
201       child.expandAll(level + 1);
202     }
203   }
204
205   public void collapseAll()
206   {
207     int cc = getChildCount();
208     for (int i = 0; i < cc; i++)
209     {
210       // Check for dummy node of folders not expanded yet:
211
if (!(getChildAt(i) instanceof DjenericTreeNode)) continue;
212       DjenericTreeNode child = (DjenericTreeNode) getChildAt(i);
213       child.collapseAll();
214     }
215     _tree.collapsePath(new TreePath JavaDoc(this.getPath()));
216   }
217
218   public boolean canDelete()
219   {
220     return _extentUsageBasedOn.isDeleteAllowed();
221   }
222
223   public boolean canCreate()
224   {
225     if (getExtentUsage() == null) return false;
226
227     return getExtentUsage().isInsertAllowed() && canEdit();
228   }
229
230   public boolean canFilter()
231   {
232     return false;
233   }
234
235   public boolean canEdit()
236   {
237     if (getExtentUsage() == null) return false;
238
239     return getExtentUsage().getEditor() != null;
240   }
241
242   public void filter() throws DjenericException
243   {
244     // Overridden for folder nodes
245
throw new DjenericException(Messages.getString("DjenericTreeNode.CanNotFilter"));
246   }
247
248   public abstract void reload() throws DjenericException;
249
250   public abstract void expandNode() throws Exception JavaDoc;
251
252   public abstract ImageIcon JavaDoc getImageIcon();
253
254   public abstract void delete() throws Exception JavaDoc;
255
256   public String JavaDoc getPathString()
257
258   {
259     TreeNode JavaDoc[] nodePath = getPath();
260     StringBuffer JavaDoc result = new StringBuffer JavaDoc(100);
261     // Skip the root node; which is not part of the path
262
for (int i = 1; i < nodePath.length; i++)
263     {
264       result.append(nodePath[i].toString());
265       if (i < nodePath.length - 1) result.append("/");
266     }
267     return result.toString();
268   }
269
270   public TreePath JavaDoc selectNode(String JavaDoc path)
271   {
272     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, "/");
273     return selectNode(this, st);
274   }
275
276   protected TreePath JavaDoc selectNode(DefaultMutableTreeNode JavaDoc parent, StringTokenizer JavaDoc st)
277   {
278     if (!st.hasMoreElements())
279     {
280       TreePath JavaDoc tp = new TreePath JavaDoc(parent.getPath());
281       _tree.setSelectionPath(tp);
282       return tp;
283     }
284
285     while (st.hasMoreElements())
286     {
287       String JavaDoc currentNodeName = st.nextToken();
288
289       for (int i = 0; i < parent.getChildCount(); i++)
290       {
291         if (currentNodeName.equals(parent.getChildAt(i).toString()))
292         {
293           _tree.expandPath(new TreePath JavaDoc(((DefaultMutableTreeNode JavaDoc) (parent.getChildAt(i))).getPath()));
294           return selectNode((DefaultMutableTreeNode JavaDoc) parent.getChildAt(i), st);
295         }
296       }
297     }
298     return null;
299   }
300
301   public boolean canCopy()
302   {
303     return false;
304   }
305
306   public boolean canExport()
307   {
308     return false;
309   }
310
311   public SpecifierPanelContainer getSpecifierPanelContainer()
312   {
313     return _specifier;
314   }
315
316   public void setSpecifierPanelContainer(SpecifierPanelContainer specifier)
317   {
318     _specifier = specifier;
319   }
320
321   protected void collectParameters(HashMap JavaDoc parentIds)
322   {
323     if (getParent() != null) getParentTreeNode().collectParameters(parentIds);
324   }
325
326   public HashMap JavaDoc getParameters()
327   {
328     HashMap JavaDoc result = new HashMap JavaDoc();
329     collectParameters(result);
330     return result;
331   }
332
333 }
Popular Tags