📄 basictreenode.java
字号:
/*
*
* Copyright (c) 2004 SourceTap - www.sourcetap.com
*
* The contents of this file are subject to the SourceTap Public License
* ("License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
*/
package com.sourcetap.sfa.ui;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
/**
* DOCUMENT ME!
*
*/
public class BasicTreeNode extends DefaultMutableTreeNode implements UITreeNode {
private static String defaultClassName = "com.sourcetap.sfa.ui.BasicTreeNode";
public BasicTreeNode() {
super();
}
public BasicTreeNode(Object userObject) {
super(userObject);
}
/**
* DOCUMENT ME!
*
* @param delegator
* @param entityName
* @param keyAttributeName
* @param parentAttributeName
* @param nodeClassName
*
* @return
*
* @throws GenericEntityException
* @throws ClassNotFoundException
* @throws LinkageError
* @throws ExceptionInInitializerError
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static BasicTreeNode createTree(GenericDelegator delegator,
String entityName, String keyAttributeName, String parentAttributeName,
String nodeClassName)
throws GenericEntityException, ClassNotFoundException, LinkageError,
ExceptionInInitializerError, InstantiationException,
IllegalAccessException {
ArrayList orderBy = new ArrayList();
orderBy.add(parentAttributeName);
List rows = delegator.findAll(entityName, orderBy);
GenericValue[] genericValue = (GenericValue[]) rows.toArray(new GenericValue[0]);
return createTree(genericValue, keyAttributeName, parentAttributeName,
nodeClassName);
}
/**
* DOCUMENT ME!
*
* @param delegator
* @param entityName
* @param keyAttributeName
* @param parentAttributeName
*
* @return
*
* @throws GenericEntityException
* @throws ClassNotFoundException
* @throws LinkageError
* @throws ExceptionInInitializerError
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static BasicTreeNode createTree(GenericDelegator delegator,
String entityName, String keyAttributeName, String parentAttributeName)
throws GenericEntityException, ClassNotFoundException, LinkageError,
ExceptionInInitializerError, InstantiationException,
IllegalAccessException {
return createTree(delegator, entityName, keyAttributeName,
parentAttributeName, defaultClassName);
}
/**
* DOCUMENT ME!
*
* @param gv
* @param keyAttributeName
* @param parentAttributeName
* @param nodeClassName
*
* @return
*
* @throws ClassNotFoundException
* @throws LinkageError
* @throws ExceptionInInitializerError
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static BasicTreeNode createTree(GenericValue[] gv,
String keyAttributeName, String parentAttributeName,
String nodeClassName)
throws ClassNotFoundException, LinkageError,
ExceptionInInitializerError, InstantiationException,
IllegalAccessException {
Class nodeClass = Class.forName(nodeClassName);
HashMap nodes = new HashMap();
BasicTreeNode root = null;
boolean multiRootNodes = false;
int numNodes = gv.length;
int i;
for (i = 0; i < numNodes; i++) {
String parentId = (String) gv[i].get(parentAttributeName);
String nodeId = (String) gv[i].get(keyAttributeName);
if (parentId == null) {
parentId = "";
}
BasicTreeNode parent = (BasicTreeNode) nodes.get(parentId);
if (parent == null) {
parent = (BasicTreeNode) nodeClass.newInstance();
parent.setUserObject("PLACEHOLDER");
nodes.put(parentId, parent);
}
parent = (BasicTreeNode) nodes.get(parentId);
BasicTreeNode thisNode = (BasicTreeNode) nodes.get(nodeId);
if (thisNode == null) {
thisNode = (BasicTreeNode) nodeClass.newInstance();
thisNode.setUserObject(gv[i]);
nodes.put(nodeId, thisNode);
} else {
thisNode.setUserObject(gv[i]);
}
parent.add(thisNode);
}
root = (BasicTreeNode) nodes.get("");
for (Iterator i1 = nodes.values().iterator(); i1.hasNext();) {
BasicTreeNode node = (BasicTreeNode) i1.next();
Object userObject = node.getUserObject();
if (userObject instanceof String) {
if (root == null) {
root = node;
} else if (!root.equals(node)) {
root.add(node);
}
}
}
if ((root != null) && (root.getUserObject() instanceof String) &&
(root.getChildCount() == 1)) {
root = (BasicTreeNode) root.getChildAt(0);
root.setParent(null);
}
return root;
}
/* outputs the HTML necessary to render the node contents */
public void displayHtml(Writer out) throws java.io.IOException {
if (isRoot()) {
out.write("<table border=0 cellspacing=0 cellpadding=0>\n");
out.write("<tr>\n");
out.write("<td valign=top>\n");
} else {
BasicTreeNode parent = (BasicTreeNode) getParent();
int numSiblings = parent.getChildCount();
int curPos = parent.getIndex(this);
out.write(
"<table width=100% border=0 cellspacing=0 cellpadding=0>\n");
if (curPos == 0) {
out.write("<td width=50% height=0></td>\n");
} else {
out.write(
"<td width=50% ><img src=/sfaimages/horz.gif width=100% height=1></td>\n");
}
if (curPos == (numSiblings - 1)) {
out.write("<td width=50% height=0></td>\n");
} else {
out.write(
"<td width=50% ><img src=/sfaimages/horz.gif width=100% height=1></td>\n");
}
out.write(
"</tr><tr><td colspan=2 align=center><img src=/sfaimages/vert.gif></td></tr>\n");
out.write("</table>\n");
}
out.write(
"<table align=center width=100% border=0 cellspacing=0 cellpassing=0>\n");
out.write("<tr align=center><td width=5></td><td align=center>\n");
out.write(
"<table width=100 border=1 cellspacing=0 cellpadding=2 align=center bordercolordark=#000000 bordercolorlight=#000000 bordercolor=#000000>\n");
out.write("<tr></td><td height=80>\n");
String displayText = getDisplayText();
out.write("" + displayText + "");
out.write("</td></tr></table>\n");
out.write("</td><td width=5></td></tr></table>\n");
int numChildren = getChildCount();
if (numChildren > 0) {
out.write(
"<table width=100% border=0 cellspacing=0 cellpadding=0 align=center>\n");
out.write("<tr><td align=center colspan=" + numChildren +
"><img src='/sfaimages/vert.gif'></td></tr>\n");
out.write("</table>\n");
}
out.write("<table border=0 cellspacing=0 cellpadding=0>\n");
out.write("<tr>\n");
for (Enumeration e = children(); e.hasMoreElements();) {
out.write("<td valign=top align=center>\n");
BasicTreeNode child = (BasicTreeNode) e.nextElement();
child.displayHtml(out);
out.write("</td>\n");
}
out.write("</tr></table>\n");
if (isRoot()) {
out.write("</tr>\n");
out.write("</table>\n");
}
}
/* can be overridden to display the contents of a node */
public String getDisplayText() {
Object o = getUserObject();
String name = "";
if (o instanceof String) {
name = (String) o;
if (name.equals("PLACEHOLDER")) {
name = "";
}
} else if (o instanceof GenericValue) {
GenericValue gv = (GenericValue) o;
name = (String) gv.toString();
// name = (String) gv.get("roleName");
}
String html = "<font color=#003399><b><center>" + name +
"</center></b>\n";
// 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";
return html;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -