KickJava   Java API By Example, From Geeks To Geeks.

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


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.web.renderers.tree;
31
32 import java.util.HashMap JavaDoc;
33
34 import org.w3c.dom.DOMException JavaDoc;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37
38 import com.genimen.djeneric.language.Messages;
39 import com.genimen.djeneric.repository.DjObject;
40 import com.genimen.djeneric.repository.DjSession;
41 import com.genimen.djeneric.repository.exceptions.DjenericException;
42 import com.genimen.djeneric.repository.exceptions.NotPersistedException;
43 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
44 import com.genimen.djeneric.structure.ExtentUsage;
45 import com.genimen.djeneric.structure.RelationUsage;
46 import com.genimen.djeneric.tools.specifier.interfaces.DjenericObjectContainer;
47
48 public class WebNode extends AbstractWebNode implements DjenericObjectContainer
49 {
50   DjObject _obj;
51   boolean _deleted = false;
52
53   public WebNode(DjSession session)
54   {
55     super(session);
56   }
57
58   public DjObject getObject()
59   {
60     return _obj;
61   }
62
63   public void setObject(DjObject obj)
64   {
65     _obj = obj;
66   }
67
68   public String JavaDoc toString()
69   {
70     if (_deleted) return Messages.getString("global.Deleted");
71     if (_obj == null) return "null";
72     if (_obj.isMarkedForDelete()) return Messages.getString("global.Deleted");
73     return _obj.getDescriptor(getExtentUsage().getDescriptorExpression());
74   }
75
76   public boolean notInTreeAnymore()
77   {
78     return getParent() == null;
79   }
80
81   public void reload() throws DjenericException
82   {
83     _alreadyLoaded = false;
84     try
85     {
86       getObject().reload();
87     }
88     catch (NotPersistedException npe)
89     {
90       _deleted = true;
91       removeAllChildren();
92     }
93     setExpanded(true);
94   }
95
96   public void load() throws DjenericException
97   {
98     if (_alreadyLoaded) return;
99
100     ExtentUsage taus = getExtentUsage();
101     if (taus != null)
102     {
103       removeAllChildren();
104
105       for (int i = 0; i < taus.getDetailRelationCount(); i++)
106       {
107         RelationUsage usage = taus.getDetailRelation(i);
108         String JavaDoc className = usage.getDetail().getCustomNodeClass();
109
110         WebFolder folder = new WebFolder(getSession());
111
112         folder.setVia(usage);
113         insertAsChild(folder);
114       }
115     }
116     _alreadyLoaded = true;
117   }
118
119   public void delete() throws DjenericException
120   {
121     getObject().markForDelete();
122     getSession().commit();
123     AbstractWebNode parent = getParent();
124     if (parent != null) parent.removeChild(this);
125   }
126
127   public boolean canCopy() throws DjenericException
128   {
129     return canEdit() && canCreate() && getExtentUsage().getEditorTarget() == null;
130   }
131
132   public boolean canExport()
133   {
134     return getExtentUsage() != null && getExtentUsage().getEditor() != null;
135   }
136
137   protected void collectParameters(HashMap JavaDoc parentIds)
138   {
139     if (getObject() != null && !parentIds.containsKey(getExtentUsage().getId()))
140     {
141       parentIds.put(getExtentUsage().getId(), getObject());
142     }
143     super.collectParameters(parentIds);
144   }
145
146   public Element JavaDoc asXml(Document JavaDoc doc) throws DOMException JavaDoc, DjenericException
147   {
148     Element JavaDoc node = doc.createElement("node");
149
150     node.setAttribute("title", toString());
151     node.setAttribute("nodeid", String.valueOf(getPath()));
152     node.setAttribute("expandable", String.valueOf(isExpandable()));
153     node.setAttribute("expanded", String.valueOf(isExpanded()));
154     node.setAttribute("canedit", String.valueOf(canEdit()));
155     node.setAttribute("candelete", String.valueOf(canDelete()));
156     node.setAttribute("cancreate", String.valueOf(canCreate()));
157     node.setAttribute("canfilter", String.valueOf(canFilter()));
158     node.setAttribute("cancopy", String.valueOf(canCopy()));
159     node.setAttribute("canexport", String.valueOf(canExport()));
160     node.setAttribute("canimport", String.valueOf(getSession().canModify() && getSession().canCreate()));
161
162     if (getExtentUsage() != null && getExtentUsage().getImageIconResource() != null)
163     {
164       node.setAttribute("image", getExtentUsage().getImageIconResource().getAbsolutePath());
165     }
166     if (isExpanded())
167     {
168       for (int i = 0; i < getChildCount(); i++)
169       {
170         node.appendChild(getChildAt(i).asXml(doc));
171       }
172     }
173
174     return node;
175   }
176
177   public void setExpanded(boolean expand) throws DjenericException
178   {
179     if (expand && !_alreadyLoaded)
180     {
181       load();
182     }
183     _expanded = expand;
184   }
185
186   public boolean isExpandable()
187   {
188     return (_alreadyLoaded && getChildCount() > 0) || getExtentUsage().getDetailRelationCount() > 0;
189   }
190
191   public DjObject getTargetObject() throws ObjectNotDefinedException, DjenericException
192   {
193     String JavaDoc editorTarget = getExtentUsage().getEditorTarget();
194     DjObject obj = getObject();
195
196     if (editorTarget != null)
197     {
198       obj = (DjObject) obj.get(editorTarget);
199     }
200     return obj;
201   }
202
203 }
Popular Tags