|
楼主 |
发表于 2011-12-27 10:56:55
|
显示全部楼层
3、建立Java文件/UI/x5demo/components/resizeTextarea/dsrc/ResizeTextarea.java
在/UI/x5demo/components/resizeTextarea目录下新建dsrc目录,新建ResizeTextarea.java文件。在java文件中,通过实现com.justep.xbl.JavaTemplate的execute方法,获取传入的参数,生成界面组件代码。
完整代码如下
- import java.util.Map;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import org.dom4j.QName;
- import com.justep.components.Utils;
- import com.justep.xbl.JavaTemplate;
- import com.justep.xbl.runtime.XBLException;
- public class ResizeTextarea implements JavaTemplate{
- private String id;
- private String ref;
- private String textareaId;
- private String onAfterChange;
- public Element execute(Element bound, Map context) throws XBLException {
- /* 在这里实现从传入的参数bound中获取组件的信息,生成一个span、一个文本框和两个按钮 */
- this.init(bound);
- Element root = DocumentHelper.createElement(new QName("root"));
- root.add(createXBLAttribute());
- root.add(createTextarea(this.textareaId,this.ref));
- root.add(createTrigger("tr1","变高","float:top","higher"));
- root.add(createTrigger("tr2","变矮","float:left","lower"));
- /* 可以在这里使用控制台输出看到返回的结果
- System.out.println(root.asXML()). */
- return root;
- }
-
- private void init(Element bound) throws XBLException {
- this.id = Utils.getAttributeOfReuired(bound, "id");
- this.ref = Utils.getAttributeOfReuired(bound, "ref");
- this.textareaId = "_textarea_" + this.id;
- this.onAfterChange = bound.attributeValue("onAfterChange");
- }
- private Element createXBLAttribute() {
- Element e = Utils.createXBLAttribute();
- e.addAttribute("id", this.id);
- e.addAttribute("ref", this.ref);
- e.addAttribute("textarea-id",this.textareaId);
- e.addAttribute("onAfterChange", this.onAfterChange);
- return e;
- }
-
- private Element createTextarea(String id,String ref) {
- Element e = DocumentHelper.createElement(new QName("textarea", Utils.XFORMS_NAMESPACE));
- e.addAttribute("id", id);
- e.addAttribute("style", "float:left");
- if (!Utils.isStringEmpty(ref))
- e.addAttribute("ref", ref);
- return e;
- }
- private Element createTrigger(String id,String label,String style,String funcName) {
- Element trigger = DocumentHelper.createElement(new QName("trigger", Utils.XFORMS_NAMESPACE));
- trigger.addAttribute("id", id);
- trigger.addAttribute("style", style);
- Element labelElement = trigger.addElement(new QName("label", Utils.XFORMS_NAMESPACE));
- if (!Utils.isStringEmpty(label))
- labelElement.setText(label);
- Element action = Utils.createScriptAction(
- "DOMActivate",
- null,
- "justep.XBLObject.getXBLObject(event.target)."+funcName+"();");
- trigger.add(action);
- return trigger;
- }
- }
复制代码 |
|