万能的树(HtmlTree)
看到置顶的两个贴子竟然是目录树,感慨良多,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;
}
}