gwt实现模板

gwt实现模板的几个步骤:

 

1:定义一个继承ITemplateSource的接口,ITemplateSource的代码如下:

public interface ITemplateSource {

}

继承ITemplateSource的接口的代码如下:

public interface TemplateInterface extends ITemplateSource {

    /**
     * @gwt.templateUrl template_MyTemplate.html
     * @gwt.elementId MyTemplate
     */
    public String getMyTemplate();
   
}

 

2:写一个模块html实现页面,代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<div id="MyTemplate">//注:MyTemplate与1中的 @gwt.elementId MyTemplate 对应
<table width="300" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="100"><div align="right">姓名:</div></td>
    <td width="200"><span gwtid="name">name</span></td>//这里的gwtid与3中的String gwtid = getElementAttribute(el, "gwtid");里的gwtid对应
  </tr>
  <tr>
    <td><div align="right">密码:</div></td>
    <td><span gwtid="password">password</span></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><span gwtid="btnOK">btnOK</span></td>
  </tr>
</table>
</div>
</body>
</html>

 

3:实现一个继承了HTMLTemplatePanelX的类,HTMLTemplatePanelX代码如下:

public class HTMLTemplatePanelX extends HTMLPanel {

    public HTMLTemplatePanelX() {
        super("Loading...");
    }

    protected void setHtml(String html) {
        //System.out.println("getElement()="+getElement());
        //System.out.println("html="+html);
        DOM.setInnerHTML(getElement(), html);
        setTemplateContent();
    }

    private void setTemplateContent() {
        flushChildren(getElement());
        afterTemplateLoaded();
    }

    private void flushChildren(Element el) {
        //System.out.println("e1="+el);
        String gwtid = getElementAttribute(el, "gwtid");
        //System.out.println("gwtid="+gwtid);
        if (gwtid != null) {
            removeAttribute(el, "gwtid");
            Widget widget = getNamedWidget(gwtid);
            //System.out.println("widget="+widget);
            if (widget == null) { // no gwt widget defined for it
                // Element promptDiv = DOM.createDiv();
                Element promptDiv = DOM.createSpan();
                //System.out.println("promptDiv="+promptDiv);
                DOM.setInnerHTML(promptDiv, "!not defined!");
                replaceChild(DOM.getParent(el), promptDiv, el);
                return;
            }
            if (widget instanceof HTMLTemplatePanelX) {
                HTMLTemplatePanelX htmlTemplatePanel = (HTMLTemplatePanelX) widget;
                htmlTemplatePanel.flushChildren(el);
                //Element container = DOM.createDiv();
                Element container = DOM.createSpan();
                replaceChild(DOM.getParent(el), container, el);
                super.add(htmlTemplatePanel, container);
            } else {
                // Element container = DOM.createDiv();
                Element container = DOM.createSpan();
                replaceChild(DOM.getParent(el), container, el);
                super.add(widget, container);
            }
        } // dont process children under gwtid
        else {
            int count = DOM.getChildCount(el);
            for (int i = 0; i < count; i++) {
                flushChildren(DOM.getChild(el, i));
            }
        }
    }

    protected HashMap widgets = new HashMap();

    public void putWidget(String gwtid, Widget widget) {
        if (widget == null)
            widget = new HTML("The widgets is not allowed null....");
        widgets.put(gwtid, widget);
    }

    protected Widget getNamedWidget(String gwtid) {
        return (Widget) widgets.get(gwtid);
    }

    private static native void removeAttribute(Element element, String name) /*-{
        element.removeAttribute(name);
    }-*/;

    private static native String getElementAttribute(Element element,
            String attr) /*-{
        return element.getAttribute(attr);
    }-*/;

    private static native void replaceChild(Element parent, Element newChild,
            Element oldChild) /*-{
        parent.replaceChild(newChild, oldChild);
    }-*/;

    /**
     * the child class may override this method to do post loaded process
     */
    protected void afterTemplateLoaded() {

    }
}

 

实现了HTMLTemplatePanelX类的代码如下:

ublic class MyTemplate extends HTMLTemplatePanelX {

    private static TemplateInterface ti;

    /**
     * 得到模板接口实例
     *
     * @return TemplateInterface
     */
    public static TemplateInterface getTemplateInterface() {
        if (ti == null)
            ti = (TemplateInterface) GWT.create(TemplateInterface.class);
        return ti;
    }

    private final TextBox txtName = new TextBox();
    private final PasswordTextBox txtPassword = new PasswordTextBox();
    private Button btnOK = new Button("OK", new ClickListener() {

        public void onClick(Widget sender) {
            Window.alert(txtName.getText() + ":" + txtPassword.getText());
        }

    });

    public MyTemplate() {
        initWidgets();
        setHtml(MyTemplate.getTemplateInterface().getMyTemplate());
    }

    private void initWidgets() {
        putWidget("name", txtName);
        putWidget("password", txtPassword);
        putWidget("btnOK", btnOK);
    }

}

该页面定义html页面组件和事件,然后在入口类中通过panel.add(new MyTemplate());即克实现模板页面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值