001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.ui;
018:
019: import java.io.Writer;
020: import java.util.ArrayList;
021: import java.util.Enumeration;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.swing.tree.DefaultMutableTreeNode;
027:
028: import org.ofbiz.entity.GenericDelegator;
029: import org.ofbiz.entity.GenericEntityException;
030: import org.ofbiz.entity.GenericValue;
031:
032: /**
033: * DOCUMENT ME!
034: *
035: */
036: public class BasicTreeNode extends DefaultMutableTreeNode implements
037: UITreeNode {
038: private static String defaultClassName = "com.sourcetap.sfa.ui.BasicTreeNode";
039:
040: public BasicTreeNode() {
041: super ();
042: }
043:
044: public BasicTreeNode(Object userObject) {
045: super (userObject);
046: }
047:
048: /**
049: * DOCUMENT ME!
050: *
051: * @param delegator
052: * @param entityName
053: * @param keyAttributeName
054: * @param parentAttributeName
055: * @param nodeClassName
056: *
057: * @return
058: *
059: * @throws GenericEntityException
060: * @throws ClassNotFoundException
061: * @throws LinkageError
062: * @throws ExceptionInInitializerError
063: * @throws InstantiationException
064: * @throws IllegalAccessException
065: */
066: public static BasicTreeNode createTree(GenericDelegator delegator,
067: String entityName, String keyAttributeName,
068: String parentAttributeName, String nodeClassName)
069: throws GenericEntityException, ClassNotFoundException,
070: LinkageError, ExceptionInInitializerError,
071: InstantiationException, IllegalAccessException {
072: ArrayList orderBy = new ArrayList();
073: orderBy.add(parentAttributeName);
074:
075: List rows = delegator.findAll(entityName, orderBy);
076: GenericValue[] genericValue = (GenericValue[]) rows
077: .toArray(new GenericValue[0]);
078:
079: return createTree(genericValue, keyAttributeName,
080: parentAttributeName, nodeClassName);
081: }
082:
083: /**
084: * DOCUMENT ME!
085: *
086: * @param delegator
087: * @param entityName
088: * @param keyAttributeName
089: * @param parentAttributeName
090: *
091: * @return
092: *
093: * @throws GenericEntityException
094: * @throws ClassNotFoundException
095: * @throws LinkageError
096: * @throws ExceptionInInitializerError
097: * @throws InstantiationException
098: * @throws IllegalAccessException
099: */
100: public static BasicTreeNode createTree(GenericDelegator delegator,
101: String entityName, String keyAttributeName,
102: String parentAttributeName) throws GenericEntityException,
103: ClassNotFoundException, LinkageError,
104: ExceptionInInitializerError, InstantiationException,
105: IllegalAccessException {
106: return createTree(delegator, entityName, keyAttributeName,
107: parentAttributeName, defaultClassName);
108: }
109:
110: /**
111: * DOCUMENT ME!
112: *
113: * @param gv
114: * @param keyAttributeName
115: * @param parentAttributeName
116: * @param nodeClassName
117: *
118: * @return
119: *
120: * @throws ClassNotFoundException
121: * @throws LinkageError
122: * @throws ExceptionInInitializerError
123: * @throws InstantiationException
124: * @throws IllegalAccessException
125: */
126: public static BasicTreeNode createTree(GenericValue[] gv,
127: String keyAttributeName, String parentAttributeName,
128: String nodeClassName) throws ClassNotFoundException,
129: LinkageError, ExceptionInInitializerError,
130: InstantiationException, IllegalAccessException {
131: Class nodeClass = Class.forName(nodeClassName);
132:
133: HashMap nodes = new HashMap();
134:
135: BasicTreeNode root = null;
136:
137: boolean multiRootNodes = false;
138: int numNodes = gv.length;
139: int i;
140:
141: for (i = 0; i < numNodes; i++) {
142: String parentId = (String) gv[i].get(parentAttributeName);
143: String nodeId = (String) gv[i].get(keyAttributeName);
144:
145: if (parentId == null) {
146: parentId = "";
147: }
148:
149: BasicTreeNode parent = (BasicTreeNode) nodes.get(parentId);
150:
151: if (parent == null) {
152: parent = (BasicTreeNode) nodeClass.newInstance();
153: parent.setUserObject("PLACEHOLDER");
154: nodes.put(parentId, parent);
155: }
156:
157: parent = (BasicTreeNode) nodes.get(parentId);
158:
159: BasicTreeNode this Node = (BasicTreeNode) nodes.get(nodeId);
160:
161: if (this Node == null) {
162: this Node = (BasicTreeNode) nodeClass.newInstance();
163: this Node.setUserObject(gv[i]);
164: nodes.put(nodeId, this Node);
165: } else {
166: this Node.setUserObject(gv[i]);
167: }
168:
169: parent.add(this Node);
170: }
171:
172: root = (BasicTreeNode) nodes.get("");
173:
174: for (Iterator i1 = nodes.values().iterator(); i1.hasNext();) {
175: BasicTreeNode node = (BasicTreeNode) i1.next();
176: Object userObject = node.getUserObject();
177:
178: if (userObject instanceof String) {
179: if (root == null) {
180: root = node;
181: } else if (!root.equals(node)) {
182: root.add(node);
183: }
184: }
185: }
186:
187: if ((root != null) && (root.getUserObject() instanceof String)
188: && (root.getChildCount() == 1)) {
189: root = (BasicTreeNode) root.getChildAt(0);
190: root.setParent(null);
191: }
192:
193: return root;
194: }
195:
196: /* outputs the HTML necessary to render the node contents */
197: public void displayHtml(Writer out) throws java.io.IOException {
198: if (isRoot()) {
199: out.write("<table border=0 cellspacing=0 cellpadding=0>\n");
200: out.write("<tr>\n");
201: out.write("<td valign=top>\n");
202: } else {
203: BasicTreeNode parent = (BasicTreeNode) getParent();
204: int numSiblings = parent.getChildCount();
205: int curPos = parent.getIndex(this );
206:
207: out
208: .write("<table width=100% border=0 cellspacing=0 cellpadding=0>\n");
209:
210: if (curPos == 0) {
211: out.write("<td width=50% height=0></td>\n");
212: } else {
213: out
214: .write("<td width=50% ><img src=/sfaimages/horz.gif width=100% height=1></td>\n");
215: }
216:
217: if (curPos == (numSiblings - 1)) {
218: out.write("<td width=50% height=0></td>\n");
219: } else {
220: out
221: .write("<td width=50% ><img src=/sfaimages/horz.gif width=100% height=1></td>\n");
222: }
223:
224: out
225: .write("</tr><tr><td colspan=2 align=center><img src=/sfaimages/vert.gif></td></tr>\n");
226:
227: out.write("</table>\n");
228: }
229:
230: out
231: .write("<table align=center width=100% border=0 cellspacing=0 cellpassing=0>\n");
232: out
233: .write("<tr align=center><td width=5></td><td align=center>\n");
234: out
235: .write("<table width=100 border=1 cellspacing=0 cellpadding=2 align=center bordercolordark=#000000 bordercolorlight=#000000 bordercolor=#000000>\n");
236: out.write("<tr></td><td height=80>\n");
237:
238: String displayText = getDisplayText();
239: out.write("" + displayText + "");
240:
241: out.write("</td></tr></table>\n");
242: out.write("</td><td width=5></td></tr></table>\n");
243:
244: int numChildren = getChildCount();
245:
246: if (numChildren > 0) {
247: out
248: .write("<table width=100% border=0 cellspacing=0 cellpadding=0 align=center>\n");
249: out.write("<tr><td align=center colspan=" + numChildren
250: + "><img src='/sfaimages/vert.gif'></td></tr>\n");
251: out.write("</table>\n");
252: }
253:
254: out.write("<table border=0 cellspacing=0 cellpadding=0>\n");
255: out.write("<tr>\n");
256:
257: for (Enumeration e = children(); e.hasMoreElements();) {
258: out.write("<td valign=top align=center>\n");
259:
260: BasicTreeNode child = (BasicTreeNode) e.nextElement();
261: child.displayHtml(out);
262: out.write("</td>\n");
263: }
264:
265: out.write("</tr></table>\n");
266:
267: if (isRoot()) {
268: out.write("</tr>\n");
269: out.write("</table>\n");
270: }
271: }
272:
273: /* can be overridden to display the contents of a node */
274: public String getDisplayText() {
275: Object o = getUserObject();
276:
277: String name = "";
278:
279: if (o instanceof String) {
280: name = (String) o;
281:
282: if (name.equals("PLACEHOLDER")) {
283: name = "";
284: }
285: } else if (o instanceof GenericValue) {
286: GenericValue gv = (GenericValue) o;
287: name = (String) gv.toString();
288:
289: // name = (String) gv.get("roleName");
290: }
291:
292: String html = "<font color=#003399><b><center>" + name
293: + "</center></b>\n";
294:
295: // html += "<br><div align=right><a href=\"viewdeptinfo.php?deptid=$deptid1\"><img src=\"/sfaimages/info.gif\" alt=\"View Department Information\" width=\"17\" height=\"17\" border=0></a></div>\n";
296: return html;
297: }
298: }
|