动态生成界面

自动从xml页面生成java swing界面的功能,不过比较简单:
xslt可能生成web页面,也就是html或者其它脚本型的界面比较方便。

配置文件:


<config>
<ConfigDialogTitle>BMC Patrol 采集配置ConfigDialogTitle>
<ConfigGroup>
<title>基本配置title>
<configItem name="采集点名称">configItem>
<configItem name="采集点描述">configItem>
<configItem name="采集点ID">configItem>
ConfigGroup>
<ConfigGroup>
<title>Patrol Agent连接配置title>
<configItem name="Patrol Agent IP">configItem>    
<configItem name="Patrol Agent 端口">configItem>
<configItem name="Patrol Agent 用户">configItem>
<configItem name="Patrol Agent 密码">configItem>
ConfigGroup>
config>

代码:

Digester digest=new Digester();
        digest.addObjectCreate("config","com.ubi.config.demo.ConfigDialog");
        digest.addCallMethod("config/ConfigDialogTitle","setTitle", 0);
        digest.addObjectCreate("config/ConfigGroup", "com.ubi.config.demo.ConfigTab");
        digest.addCallMethod("config/ConfigGroup/title","setName",0);
        digest.addCallMethod( "config/ConfigGroup/configItem", "addConfigItem", 1 );
        digest.addCallParam( "config/ConfigGroup/configItem", 0,"name" );
        digest.addSetNext( "config/ConfigGroup", "addConfigTab" );

        try {
            BufferedReader r=new BufferedReader(new StringReader(cfgTemplate));
            Object obj=digest.parse(r);
            ConfigDialog dlg=(ConfigDialog)obj;
            dlg.setReaderTypeID(this.readerTypeIntID);
            dlg.setSize(new Dimension(600,400));
            dlg.show();
            System.out.println("parsed object is "+obj);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

相关类:

/*
 * Created on 2005-3-2
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ubi.config.demo;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import com.ubi.config.demo.listener.CheckBoxTreeCellRenderer;

/**
 * @author ibm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ConfigDialog extends JDialog implements ActionListener{
    JTabbedPane configTabs;
    int readerTypeID;
    public void setReaderTypeID(int id){
        readerTypeID=id;
    }
    JPanel contentP=new JPanel();
    public ConfigDialog(){
        super();
        contentP.setLayout(new BorderLayout());
        this.setContentPane(contentP);
        this.setSize(new Dimension(200,150));
        configTabs=new JTabbedPane();
        contentP.add(configTabs, BorderLayout.CENTER);
        JPanel btnPane=new JPanel();
        contentP.add(btnPane, BorderLayout.SOUTH);
        btnPane.setLayout(new FlowLayout());
        JButton btnAdd=new JButton("确定");
        JButton btnCacel=new JButton("取消");
        btnPane.add(btnAdd);
        btnPane.add(btnCacel);
        btnAdd.addActionListener(this);
        btnCacel.addActionListener(this);
    }

    public void addConfigTab(ConfigTab newTab){
        configTabs.addTab(newTab.getName(), newTab);
    }
    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent arg0) {
        if (arg0.getActionCommand().equals("取消")){
            this.hide();
        }
        if (arg0.getActionCommand().equals("确定")){
            buildEventFilterPage();
        }
    }
    void buildEventFilterPage(){
        APMJTree events=new APMJTree("事件过滤");
        events.setCellRenderer(new CheckBoxTreeCellRenderer());
        ResultSet rs=null;
        try {
            rs=ConfigMain.db.executeQuery("select * from coll_pnt_inst_event where coll_pnt_inst_uid="+this.readerTypeID);
            while (rs.next()){
                events.buildPath(rs.getString("coll_pnt_inst_event_id"));
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                rs.getStatement().close();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        JScrollPane scroll=new JScrollPane(events);
        events.expandRow(0);
        JDialog dlg=new JDialog();
        dlg.setTitle("采集事件过滤");
        dlg.getContentPane().setLayout(new BorderLayout());
        dlg.getContentPane().add(scroll,BorderLayout.CENTER);
        dlg.setSize(400,300);
        dlg.setModal(true);
        dlg.show();
    }
}
/*
 * Created on 2005-3-2
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ubi.config.demo;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author ibm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ConfigTab extends JPanel{
    public ConfigTab(){
        super();
        this.setLayout(null);
    }
    int  nextPointX=20;
    int positionY=20;
    int space=50;

    public void addConfigItem(String itemName){
        JLabel lb=new JLabel(itemName);
        lb.setBounds(positionY,nextPointX,150,20);        
        this.add(lb);
        JTextField txt=new JTextField();
        txt.setBounds(positionY+155,nextPointX,150,20);
        nextPointX+=space;
        this.add(txt);           
    }


}

你可能感兴趣的:(java)