首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

创建一种声明性 XML UI 语言 -5

创建一种声明性 XML UI 语言 -5

DOM 节点编组这个框架提供了一个 toString 方法(见 ),以便 XUINode 将自身编组为 XML。根节点可以包含多个子节点,子节点可以包含自己的子节点,依次类推。通过调用根级别节点的 toString 方法,这个框架可以轻松编组整个 XML 文档。名称空间被添加进来,以便每个元素知道自己在层级中的级别(通过 level 变量)。这样,当 toString 方法被调用时,它可以实现缩进,以使这些文档更易于阅读。
清单 5. XUINode toString 方法实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@Override
public String toString() {
    StringBuffer sb = new StringBuffer();
    String namespacePrefix = "";
    // insert indenting ... 2 spaces for now.
    if(isRoot) {
        sb.append(XMLPI + "\n");
        sb.append(API_COMMENT + "\n");
    } else  {
        sb.append("\n");
        for(int s = 0; s < level; s++) {
            sb.append("  ");
        }
    }
    sb.append("<");
    // get namespaces for this node
    Enumeration keys = nameSpaces.keys();

    String names = "";
    while(keys.hasMoreElements()) {
        String uri = (String)keys.nextElement();
        String prefix = (String)nameSpaces.get(uri);
        /* if its the xsi namespace (XML Schema Instance),
         * ignore it, this isn't part of that namespace but it is
         * needed for the XML Schema validator to work. */
        if(!(prefix.equals("xsi"))) {
            sb.append(prefix + ":");
            namespacePrefix = prefix;
        }
        names += (" " + "xmlns:" + prefix + "=\"" + uri + "\"");
    }
    if(beginOfNamespace) {
        sb.append(name + names);
    } else {
        sb.append(name);
    }

    // do attributes if there are any
    if(attributes.getLength() > 0) {
        int length = attributes.getLength();
        for(int i = 0; i < length; i++) {
            String attributeValue = attributes.getValue(i);
            String attributeQName = attributes.getQName(i);
            sb.append(" " + attributeQName + "=\"" + attributeValue + "\"");
        }
    }
    sb.append(">");
    sb.append(cdata);
    int size = childNodes.size();
    for(int i = 0; i < size; i++) {
        XUINode e = (XUINode)childNodes.get(i);
        sb.append(e.toString());
    }
    if(size > 0) {
        sb.append("\n");
        for(int s = 0; s < (level); s++)
            sb.append("  ");
    }
    if(namespacePrefix.length() > 0) {
        sb.append("</" + namespacePrefix + ":" + name + ">");
    } else {
        sb.append("</" + name + ">");
    }

    return sb.toString();
}




添加到一个容器组件另一个值得探讨的部分是容器类型 XUIWindow,它是 XUIComponent 的间接子类型。XUIWindow 实现表示一个 javax.swing.JFrame 组件,因此它必须允许将子元素添加到布局中。 展示了它的实现。首先必须确保只有某些类型的子元素可以添加到 XUIWindow 中。这样,XUIComponent 的 DOM 节点表示(XUINode)将被检索以便访问该元素的属性。注意,这要求所有 XUIComponent 的构造器初始化这些值。
下面进一步检查以确保以下两点:该组件是一个中级容器(比如 XUIPanel);这个中级容器能够包含在 XUIWindow 的行列网格中。最后,将组件添加到 XUIWindow 要确保以下两点:组件已启用并在布局网格中设置了正确的位置;XUIWindow 的 XUINode(win 变量)获得了一个引用 —— 引用新的子组件的 XUINode,即 addChildNode() 调用。
清单 6. XUIWindow addComponent 方法实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public void addComponent(XUIComponent component) throws XUITypeFormatException {
    if(component instanceof XUIBasicDialog
        || component instanceof XUIOpenFileDialog
        || component instanceof XUICustomDialog
        || component instanceof XUIMenuBar
        || component instanceof XUIPanel
        || component instanceof XUISplitPanel
        || component instanceof XUITabbedPanel
        || component instanceof XUISaveFileDialog) {
        // get the node
        XUINode node = component.getNodeRepresentation();

        if(!(component instanceof XUIMenuBar)) {
            int x = Integer.parseInt(node.getAttributeValue("x"));
            int y = Integer.parseInt(node.getAttributeValue("y"));
            int width = Integer.parseInt(node.getAttributeValue("width"));
            int height = Integer.parseInt(node.getAttributeValue("height"));

            // can't add dialogs so need to check for type here.
            if(component instanceof XUIBasicDialog
                || component instanceof XUIOpenFileDialog
                || component instanceof XUICustomDialog
                || component instanceof XUISaveFileDialog) ; // nothing
            else {
                // check to make sure it fits within the grid.
                Dimension localGrid = this.getGrid();
                if(width > localGrid.getWidth() || height >
                    localGrid.getHeight()) {
                    throw new XUITypeFormatException(node.getName()
                        + " (id: " + node.getAttributeID()
                        + ") must be within this window's grid width and"
                        + "height (w: " + localGrid.getWidth()
                        + " + h: " + localGrid.getHeight() + ")");
                }
                Rectangle rect = new Rectangle(y, x, width, height);

                component.getPeer().setEnabled(true);
                frame.getContentPane().add(component.getPeer(), rect);
                // for mapping components to the regions they occupy
                childComponentMappings.put(component, rect);
            }
            component.setComponentLocation(x, y);

        } else {
            // do specifics for a menubar
            frame.setJMenuBar((JMenuBar)component.getPeer());
        }

        frame.invalidate();
        frame.validate();

        // add the component's node
        int level = win.getLevel();
        node.setLevel(++level);

        if(win.getParent() == null)
            win.addChildNode(node);

    } else {
        StringBuffer sb = new StringBuffer();
        sb.append("Type not supported in XUIWindow. ");
        sb.appen("The following types are supported:\n");

        for(int i = 0; i < supportedComponents.size(); i++) {
            String s = (String)supportedComponents.get(i);
            sb.append("- " + s + "\n");
        }
        throw new XUITypeFormatException(sb.toString());
    }
}

返回列表