KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
33
34 import javax.swing.ImageIcon JavaDoc;
35 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
36 import javax.swing.tree.DefaultTreeModel JavaDoc;
37 import javax.swing.tree.TreePath JavaDoc;
38
39 import com.genimen.djeneric.language.Messages;
40 import com.genimen.djeneric.repository.DjObject;
41 import com.genimen.djeneric.repository.exceptions.DjenericException;
42 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
43 import com.genimen.djeneric.structure.ExtentUsage;
44 import com.genimen.djeneric.structure.RelationUsage;
45 import com.genimen.djeneric.structure.ResourceDefinition;
46 import com.genimen.djeneric.tools.specifier.Specifier;
47 import com.genimen.djeneric.tools.specifier.interfaces.DjenericObjectContainer;
48
49 public class DjenericNode extends DjenericTreeNode implements DjenericObjectContainer
50 {
51   private static final long serialVersionUID = 1L;
52   DjObject _obj;
53   ImageIcon JavaDoc _imageIcon = null;
54
55   public DjenericNode()
56   {
57   }
58
59   public ImageIcon JavaDoc getImageIcon()
60   {
61     if (_imageIcon != null) return _imageIcon;
62
63     ExtentUsage usg = getExtentUsage();
64
65     if (usg != null)
66     {
67       ResourceDefinition res = usg.getImageIconResource();
68       if (res != null)
69       {
70         _imageIcon = new ImageIcon JavaDoc(res.getBytes());
71       }
72     }
73     if (_imageIcon == null) _imageIcon = Specifier.getImageIcon("document.gif");
74     return _imageIcon;
75   }
76
77   public DjObject getObject()
78   {
79     return _obj;
80   }
81
82   public void setObject(DjObject obj)
83   {
84     _obj = obj;
85   }
86
87   public String JavaDoc toString()
88   {
89     if (_obj.isMarkedForDelete()) return Messages.getString("global.Deleted");
90     return _obj.getDescriptor(getExtentUsage().getDescriptorExpression());
91   }
92
93   public boolean notInTreeAnymore()
94   {
95     return getParent() == null;
96   }
97
98   public void delegateReload(Object JavaDoc someNode) throws DjenericException
99   {
100     if (!(someNode instanceof DefaultMutableTreeNode JavaDoc)) return;
101     DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) someNode;
102
103     DefaultTreeModel JavaDoc mdl = getModel();
104     for (int i = 0; i < mdl.getChildCount(node); i++)
105     {
106       Object JavaDoc n = mdl.getChild(node, i);
107       if (n instanceof DjenericNode)
108       {
109         DjenericNode dn = (DjenericNode) n;
110         if (dn._obj.getObjectId() == _obj.getObjectId()) dn.reload();
111       }
112       else delegateReload(n);
113     }
114   }
115
116   public void reload() throws DjenericException
117   {
118     if (notInTreeAnymore())
119     {
120       delegateReload(getModel().getRoot());
121       return;
122     }
123     _alreadyLoaded = false;
124     getObject().reload();
125     expandNode();
126   }
127
128   public void delegateExpandNode(Object JavaDoc someNode) throws DjenericException
129   {
130     if (!(someNode instanceof DefaultMutableTreeNode JavaDoc)) return;
131     DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) someNode;
132
133     DefaultTreeModel JavaDoc mdl = getModel();
134     for (int i = 0; i < mdl.getChildCount(node); i++)
135     {
136       Object JavaDoc n = mdl.getChild(node, i);
137       if (n instanceof DjenericNode)
138       {
139         DjenericNode dn = (DjenericNode) n;
140         if (dn._obj.getObjectId() == _obj.getObjectId()) dn.expandNode();
141       }
142       else delegateReload(n);
143     }
144   }
145
146   public void expandNode() throws DjenericException
147   {
148     if (_alreadyLoaded) return;
149
150     if (notInTreeAnymore())
151     {
152       delegateExpandNode(getModel().getRoot());
153       return;
154     }
155
156     boolean isCollapsed = _tree.isCollapsed(new TreePath JavaDoc(this.getPath()));
157     removeAllChildren();
158
159     try
160     {
161       ExtentUsage taus = getExtentUsage();
162       for (int i = 0; i < taus.getDetailRelationCount(); i++)
163       {
164         RelationUsage usage = taus.getDetailRelation(i);
165         String JavaDoc className = usage.getDetail().getCustomNodeClass();
166
167         DjenericFolder folder;
168
169         if (className == null) folder = new DjenericFolder();
170         else folder = (DjenericFolder) getSpecifierPanelContainer().loadClass(className).newInstance();
171
172         folder.setVia(usage);
173         folder.setSpecifierPanelContainer(getSpecifierPanelContainer());
174
175         insertAsFolder(folder);
176       }
177       _alreadyLoaded = true;
178     }
179     catch (InstantiationException JavaDoc e)
180     {
181       throw new DjenericException(e);
182     }
183     catch (IllegalAccessException JavaDoc e)
184     {
185       throw new DjenericException(e);
186     }
187     catch (ClassNotFoundException JavaDoc e)
188     {
189       throw new DjenericException(e);
190     }
191     finally
192     {
193       getModel().nodeStructureChanged(this);
194       if (!isCollapsed) _tree.expandPath(new TreePath JavaDoc(this.getPath()));
195     }
196   }
197
198   public void delete() throws Exception JavaDoc
199   {
200     getObject().markForDelete();
201     getSession().commit();
202     DjenericTreeNode parent = getParentTreeNode();
203     removeFromParent();
204     getModel().nodeStructureChanged(parent);
205   }
206
207   public boolean canCopy()
208   {
209     return canEdit() && canCreate() && getExtentUsage().getEditorTarget() == null;
210   }
211
212   public boolean canExport()
213   {
214     return getExtentUsage().getEditor() != null;
215   }
216
217   protected void collectParameters(HashMap JavaDoc parentIds)
218   {
219     if (getObject() != null && !parentIds.containsKey(getExtentUsage().getId()))
220     {
221       parentIds.put(getExtentUsage().getId(), getObject());
222     }
223     super.collectParameters(parentIds);
224   }
225
226   public DjObject getTargetObject() throws ObjectNotDefinedException, DjenericException
227   {
228     String JavaDoc editorTarget = getExtentUsage().getEditorTarget();
229     DjObject obj = getObject();
230
231     if (editorTarget != null)
232     {
233       obj = (DjObject) obj.get(editorTarget);
234     }
235     return obj;
236   }
237
238 }
Popular Tags