万能的树(HtmlTree)

yangchongwu 2004-07-23 10:36:38
看到置顶的两个贴子竟然是目录树,感慨良多,java这么强大的功能,单单只是为了目录树是不是有点太可惜了,我写了一段代码,可以在树上加入超连接、checkBox、select、甚至table,所有implements 了如下Component interface 的组件都可以。一下是代码,欢迎各位高手指教:

以下一个接口两个抽象类是组件包的基础,可以方便的以他们为基础编写自己的组件
//********************************
package htmlComponent;

/**
* @author brian.yang
*组件包的唯一接口
*/
public interface Component {
Object getKey();//用来定位组件的标识
String getHtmlSource();
}
//***********************************
package htmlComponent;

/**
* @author brian.yang
*为了方便编写组件而写的抽象类
*/
public abstract class AbstractComponent implements Component,Comparable {
protected Object key;
protected Attributes attrs;

public AbstractComponent(){
this(null,null);
}
public AbstractComponent(Object key){
this(key,null);
}
public AbstractComponent(Object key,Attributes attributes){
this.key=key;
this.attrs=new Attributes();
if (attributes!=null)
this.attrs.putAll(attributes);
}

public String getName() {

return key.toString();
}


public java.util.HashMap getAttributes() {
return attrs;
}


public String getTag() {
return "";
}

public String getContent(){
return "";
}

public String getEndTag() {
return "";
}

public Object getKey(){
return key;
}
public int compareTo(Object o){
if (o instanceof Component)
return ((Comparable)getKey()).compareTo(
((AbstractComponent)o).getKey() );
return ((Comparable)getKey()).compareTo(o);
}
public boolean equals(Object o){
if (o instanceof Component)
return getKey().equals(((Component)o).getKey());
return getKey().equals(o);
}
public String getHtmlSource(){
//根据html标签的特点进一步将getHtmlSource()分成了三个方法
return getTag()+getContent()+getEndTag();
}
public String toString(){
return getHtmlSource();
}
}
//*************************
package htmlComponent;

/**
* @author brian.yang
*含有多个元素的组件的基类,如:<div>块,<select>,<table>,<tr>等
*/
public abstract class MultiComponent extends AbstractComponent {
protected java.util.ArrayList elements=new java.util.ArrayList();
protected String seperator="";
public MultiComponent() {
super();
}

public MultiComponent(Object key) {
super(key);
}

public MultiComponent(Object key, Attributes attributes) {
super(key, attributes);
}

public java.util.List getElements(){
return elements;
}

/* *
*将包含的多个elements输出的代码,包含对实现了Component
*接口的对象的特殊处理
*/
public String getContent(){
String strContent="";
if (elements.size()>0)
if(elements.get(0) instanceof Component){
Component o=(Component)elements.get(0);
strContent+=o.getHtmlSource();
}else strContent=elements.get(0).toString();
for(int i=1 ;i<elements.size();i++)
if(elements.get(i) instanceof Component){
Component o=(Component)elements.get(i);
strContent+=seperator+o.getHtmlSource();
}else
strContent+=seperator+elements.get(i);
return strContent;
}


}
//******************************
package htmlComponent;

/**
* @author brian.yang
*标签的属性类,无关紧要,可以略过
*
*/
public class Attributes extends java.util.HashMap {

public static final String NAME="name";
public static final String VALUE="value";

public static final String SRC="src";
public static final String LINK="href";

public static final String BORDER="border";
public static final String HEIGHT="height";
public static final String ONCLICK="onclick";
public class Input{
public static final String TYPE="type";
}
public class Style{
public static final String STYLEID="Id";
public static final String STYLECLASS="Class";
public static final String STYLE="style";
}
public Attributes(){
super();
}
/*未完成
public Attributes(String strAttrs){
super();

}*/
public String toString(){
String strAttr="";
java.util.Iterator it=this.entrySet().iterator();
while(it.hasNext()){
java.util.Map.Entry entry=(java.util.Map.Entry)it.next();
if (entry.getValue()==null)
strAttr+= " "+entry.getKey();
else
strAttr+=" "+entry.getKey()+"=\""+entry.getValue()+"\" ";
}

return strAttr;
}
}
...全文
1224 66 打赏 收藏 转发到动态 举报
写回复
用AI写文章
66 条回复
切换为时间正序
请发表友善的回复…
发表回复
myy 2005-08-05
  • 打赏
  • 举报
回复
无话可说。

我只是觉得写 Java代码 比写 HTML 更麻烦。
都转行吧 2005-08-05
  • 打赏
  • 举报
回复
ding
tonee 2005-08-05
  • 打赏
  • 举报
回复
关注一下~
nitc 2005-08-05
  • 打赏
  • 举报
回复
up
nitc_007@hotmail.com
startjoy 2005-08-05
  • 打赏
  • 举报
回复
Up
dxj1234 2005-08-05
  • 打赏
  • 举报
回复
up 啊
vulcan23 2005-08-05
  • 打赏
  • 举报
回复
无限敬仰中,收藏,日后研究
lihongxing2002 2005-08-05
  • 打赏
  • 举报
回复
up
D_O_N_G 2004-08-02
  • 打赏
  • 举报
回复
分析!!!
gy11111113 2004-08-02
  • 打赏
  • 举报
回复
to yangchongwu

谢谢
dcren521 2004-07-29
  • 打赏
  • 举报
回复
打包给小妹我一份好吗?
mh5188@yahoo.com.cn
eidolon_warrior 2004-07-26
  • 打赏
  • 举报
回复
up
yangchongwu 2004-07-26
  • 打赏
  • 举报
回复
关于Attributes类,由于在一个项目中,界面的风格要求统一,所以每种类型的组件会有大部分相同的属性,这样的话,我们可以将这些标准属性统一管理,不必在开发每个页面的时候都新建立一组属性类,而只需要一个引用就可以了。
javahappy 2004-07-24
  • 打赏
  • 举报
回复
lihai
SugarRay 2004-07-24
  • 打赏
  • 举报
回复
不错。收藏
flydrago2001 2004-07-24
  • 打赏
  • 举报
回复
不错! 思维严密!不过有更好的,请看www.vb.com\43988gp
AHUA1001 2004-07-24
  • 打赏
  • 举报
回复
好东西。
programdolt 2004-07-24
  • 打赏
  • 举报
回复
mark
giftedjacky 2004-07-24
  • 打赏
  • 举报
回复
占一位置
leeshun 2004-07-24
  • 打赏
  • 举报
回复
UP UP UP
加载更多回复(45)

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧