package blue.lip.tags;
/**
* Author:李鹏
* CreateDate:2006-11-15
* Modifier:李鹏
* ModifyDate:2006-11-15
* Version:1.0
* Copyright(c)2006 李鹏
* All right reserved.
*/
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.JspException;
import java.io.IOException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import java.util.List;
/**
* 树标签处理类
*/
public class BlueTreeTag
implements Tag {
private PageContext pageContext;
private Tag parent;
public static String rootId; //根节点ID
public static String blueClass; //根据节点ID获得所有该节点子节点的实现类
public static String blueURL; //点击节点超连接---isUseDataURL为false时有用
public static boolean isUseDataURL; //是否使用节点封装对象的节点超连接
public static String rootName; //根节点名称
public static String imageURL; //所有图片目录路径
public static String rootImage; //根节点图片
public static String folderImg; //有子节点关闭图片
public static String folderOpenImg; //有子节点展开图片
public static String fileImg; //无子节点图片
public static String openImg; //节点展开图片
public static String closeImg; //节点关闭图片
public static String elementLineImg; //带有节点的线图片
public static String lineImg; //无节点的线图片
public static String endLineImg; //最后一个节点线图片
public static String endFolderOpenImg; //最后一个有子节点展开图片
public static String endFolderCloseImg; //最后一个有子节点关闭图片
public static boolean isFolderURL; //是否给有子节点的节点加超连接---isUseDataURL为false时有用
public static String rootURL; //根节点连接
public static String target; //所有连接的target属性
public static String linkClass; //超连接样式
public static String styleClass; //总样式
public static String imgClass; //图片样式
public static String useDataImg; //节点图片是否使用数据类图片,no为不使用,first为仅第一级目录使用,all全部使用
public static String js; //设置JS文件位置
public static String onMouseOver; //鼠标经过每个节点时的样式
public static String onMouseOut; //鼠标离开每个节点时的样式
public BlueTreeTag() {
super();
this.rootId = "0";
this.blueClass = null; //必须设置
this.blueURL = "#";
this.isUseDataURL = false;
this.rootName = "根节点";
this.rootImage = "rootImage.gif";
this.imageURL = null; //必须设置
this.folderImg = "folder.gif";
this.folderOpenImg = "folderOpen.gif";
this.fileImg = "file.gif";
this.openImg = "open.gif";
this.closeImg = "close.gif";
this.elementLineImg = "elementLine.gif";
this.lineImg = "line.gif";
this.endLineImg = "endLine.gif";
this.endFolderOpenImg = "endFolderOpen.gif";
this.endFolderCloseImg = "endFolderClose.gif";
this.isFolderURL = true;
this.rootURL = "#";
this.target = "_blank";
this.linkClass="";
this.imgClass="";
this.styleClass="";
this.useDataImg="no";
this.js=null; //必须设置
this.onMouseOver="onMouseOver";
this.onMouseOut="onMouseOut";
}
/**
* 设置标签的页面上下文
* @param pageContext
*/
public void setPageContext(PageContext pageContext) {
this.pageContext = pageContext;
}
/**
* 设置上一级标签
* @param t
*/
public void setParent(Tag parent) {
this.parent = parent;
}
public Tag getParent() {
return this.parent;
}
/**
* 开始标签时的操作
* @return
* @throws JspException
*/
public int doStartTag() throws JspException {
if(this.blueClass==null){
throw new JspException("树标签blueClass属性未设置!");
}
if(this.imageURL==null){
throw new JspException("树标签imageURL属性未设置!");
}
if(this.js==null){
throw new JspException("树标签js属性未设置!");
}
return this.SKIP_BODY; //返回SKIP_BODY,表示不计算标签体
}
/**
* 结束标签时的操作
* @return
* @throws JspException
*/
public int doEndTag() throws JspException {
JspWriter out = this.pageContext.getOut();
try {
String tree = this.createTree();
out.print(tree);
}
catch (Exception ex) {
throw new JspException("创建树错误:" + ex.getMessage());
}
return this.EVAL_PAGE;
}
/**
* Release 用于释放标签程序占用的资源,比如使用了数据库连接,那么应该关闭这个连接
*/
public void release() {
}
/**
* 构建树
* @return 返回树HTML代码字符串
*/
public String createTree() throws Exception {
StringBuffer result = new StringBuffer();
BlueTreeFactory treeFactory = new BlueTreeFactory();
BlueTreeInterFace tree = (BlueTreeInterFace) treeFactory.create(this.
blueClass);
List elements = null;
elements = tree.getTreeElements(this.rootId);
//样式
result.append("<style>");
result.append(".onMouseOver{");
result.append("background-color:#CCCCCC;");
result.append("}");
result.append(".onMouseOut{");
result.append("}");
result.append("</style>");
//js
result.append("<script src=\"");
result.append(this.js);
result.append("\">");
result.append("</script>");
//开始创建树
result.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"");
result.append(this.styleClass);
result.append("\">");
//创建跟节点
result.append("<tr onMouseOver=\"this.className='");
result.append(this.onMouseOver);
result.append("'\" onMouseOut=\"this.className='");
result.append(this.onMouseOut);
result.append("'\">");
result.append("<td>");
result.append("<a class=\"");
result.append(this.linkClass);
result.append("\" href=\"");
result.append(this.rootURL);
result.append("\" target=\"");
result.append(this.target);
result.append("\">");
result.append("<img align=\"absmiddle\" class=\"");
result.append(this.imgClass);
result.append("\" src=\"");
result.append(this.imageURL);
result.append(this.rootImage);
result.append("\">");
result.append(this.rootName);
result.append("</a>");
result.append("</td>");
result.append("</tr>");
//创建第一级节点
for (int i = 0; i < elements.size(); i++) {
BlueTreeData treeData = new BlueTreeData();
treeData = (BlueTreeData) elements.get(i);
//加超连接头
//===================================================
result.append("<tr onMouseOver=\"this.className='");
result.append(this.onMouseOver);
result.append("'\" onMouseOut=\"this.className='");
result.append(this.onMouseOut);
result.append("'\">");
result.append("<td>");
result.append("<a class=\"");
result.append(this.linkClass);
result.append("\" href=\"");
if((treeData.isIschild() && this.isIsFolderURL()) || !treeData.isIschild()){
if(this.isIsUseDataURL()){
result.append(treeData.getUrl());
}else{
result.append(this.blueURL);
}
//添加节点连接参数
result.append("?");
result.append("node=");
result.append(treeData.getId());
}else{
result.append("javascript:void(null);");
}
result.append("\" target=\"");
result.append(this.target);
//添加节点单击事件
//===================================================
if(treeData.isIschild()){
result.append("\" onClick=\"clickTreeNode('");
result.append(treeData.getId());
result.append("',1,'");
result.append(this.styleClass);
result.append("','");
result.append(this.imageURL);
result.append("','");
result.append(this.imgClass);
result.append("','");
result.append(this.lineImg);
result.append("','");
result.append(this.end
- 1
- 2
- 3
前往页