⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsanttaskselectiontreedialog.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        // TODO query the selected paths for all subnodes.
        TreePath[] pathArr = m_tree.getSelectionPaths();
        // avoid NPE but skip loop:
        if (pathArr == null) {
            pathArr = new TreePath[0];
        }
        TreePath path;
        StringBuffer pathString;
        DefaultMutableTreeNode node;

        // pathString is the path string of the selected TreePath from the tree.
        // it may be a leaf (single selection) or not. In the latter case iteration
        // continues to all subnodes to add all reachable leafs as module names.
        // Furthermore every non-leaf-node may be a module name like: org.opencms.workplace and
        // org.opencms.workplace.tools.content...
        for (int i = 0; i < pathArr.length; i++) {
            pathString = new StringBuffer();
            path = pathArr[i];
            // build the path string to the selected path:
            Object[] entries = path.getPath();
            // skip "root"
            for (int j = 1; j < entries.length; j++) {
                pathString.append(entries[j]);
                if (j < entries.length - 1) {
                    pathString.append(".");
                }
            }
            node = (DefaultMutableTreeNode)path.getLastPathComponent();
            if (node.isLeaf()) {
                ret.append(pathString.toString());
                ret.append(CmsAntTaskSelectionTreePrompt.LIST_SEPARATOR);
            } else {
                // first look, wether this is already a module, even if not leaf, (e.g.
                // org.opencms.workplace <-> org.opencms.workplace.tools.accounts...)
                if (m_allModuleList.contains(pathString.toString())) {
                    ret.append(pathString.toString());
                    ret.append(CmsAntTaskSelectionTreePrompt.LIST_SEPARATOR);
                } else {
                    // nop
                }
                // search all leaf nodes and append subpaths:

                ret.append(getSubpaths(node, pathString.toString()));
            }

        }
        dispose();
        if (m_aborted || ret.toString().trim().length() < CmsAntTaskSelectionTreePrompt.LIST_SEPARATOR.length()) {
            return null;
        } else {
            return ret.toString();
        }
    }

    /**
     * Centers the dialog on the screen.
     * <p>
     * 
     * If the size of the dialog exceeds that of the screen, then the size of the dialog is reset to
     * the size of the screen.
     * <p>
     */
    private void center() {

        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension window = getSize();
        // ensure that no parts of the dialog will be off-screen
        int height = window.height;
        int width = window.width;
        if (window.height > screen.height) {
            window.height = screen.height;
            height = screen.height - 50;
            width = width + 50;
        }
        if (window.width > screen.width) {
            window.width = screen.width;
        }
        int xCoord = (screen.width / 2 - window.width / 2);
        int yCoord = (screen.height / 2 - window.height / 2);
        setLocation(xCoord, yCoord);
        setSize(width, height);
    }

    private TreeModel createTree() {

        // for every entry: cut into paths, build a tree path that collates equal paths.
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
        TreeModel tm = new DefaultTreeModel(root);
        Iterator itModules = m_allModuleList.iterator();
        StringTokenizer itPath;
        Enumeration childEnum;
        String pathElement;
        DefaultMutableTreeNode node, child;
        boolean found = false;
        while (itModules.hasNext()) {
            itPath = new StringTokenizer((String)itModules.next(), ".");
            node = root;
            while (itPath.hasMoreTokens()) {
                // is this node already there?
                pathElement = itPath.nextToken();
                childEnum = node.children();
                found = false;
                while (childEnum.hasMoreElements()) {
                    child = (DefaultMutableTreeNode)childEnum.nextElement();
                    if (pathElement.equals(child.getUserObject())) {

                        // found node for path String
                        // reuse old path, descend and continue with next path element.
                        node = child;
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    // did not break, node was not found
                    child = new DefaultMutableTreeNode();
                    child.setUserObject(pathElement);
                    node.add(child);
                    node = child;
                }
            }
        }
        return tm;

    }

    /**
     * Recursive depth first traversal that stops at expansion level and expands those paths.
     * <p>
     * 
     * @param treePath the current path in recursion
     */
    private void expandTree(TreePath treePath) {

        if (treePath.getPathCount() == m_promptTask.getExpansionLevels()) {
            m_tree.expandPath(treePath);

        } else {
            DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)treePath.getLastPathComponent();
            Enumeration children = treeNode.children();
            while (children.hasMoreElements()) {
                expandTree(treePath.pathByAddingChild(children.nextElement()));
            }
        }

    }

    private String getSubpaths(TreeNode node, String parentPath) {

        Enumeration children = node.children();
        TreeNode child;
        String path = parentPath;
        StringBuffer ret = new StringBuffer();
        while (children.hasMoreElements()) {
            child = (TreeNode)children.nextElement();
            if (parentPath.length() == 0) {
                path = child.toString();
            } else {
                path = parentPath + "." + child.toString();
            }
            if (child.isLeaf()) {
                ret.append(path);
                ret.append(CmsAntTaskSelectionTreePrompt.LIST_SEPARATOR);
            } else {
                ret.append(getSubpaths(child, path));
            }
        }
        return ret.toString();
    }

    /**
     * Parses the string of comma separated module names obtained from the ant script into the
     * internal list of module names for better access in several locations.
     * <p>
     * 
     */
    private void parseModuleList() {

        m_allModuleList = new LinkedList();
        StringTokenizer itPaths = new StringTokenizer(
            m_promptTask.getAllValues(),
            CmsAntTaskSelectionPrompt.LIST_SEPARATOR);

        String token;
        while (itPaths.hasMoreElements()) {
            token = itPaths.nextToken().trim();
            m_allModuleList.add(token);
        }
    }

    /**
     * Recursivley selects the nodes that are qualified by the default selections.
     * 
     * @param node the current Node
     * @param path the node path
     * @param treePath the tree path
     */
    private void selectDefaultNodes(DefaultMutableTreeNode node, String path, TreePath treePath) {

        // allow root property to be set:
        String defaultString = m_promptTask.getDefaultValue();
        if ("root".equalsIgnoreCase(defaultString.trim())) {
            if (node == m_tree.getModel().getRoot()) {
                m_tree.setSelectionPath(treePath);
            }
        } else {
            StringTokenizer tokenizer = new StringTokenizer(defaultString, CmsAntTaskSelectionTreePrompt.LIST_SEPARATOR);
            String defaultEntry;
            while (tokenizer.hasMoreTokens()) {
                defaultEntry = tokenizer.nextToken();
                // don't print in recursions for path
                if (node.getLevel() == 0) {
                    System.out.println("Preselection: " + defaultEntry);
                }
                if (defaultEntry.equals(path)) {
                    m_tree.addSelectionPath(treePath);
                    return;
                }
            }
            Enumeration children = node.children();
            DefaultMutableTreeNode subNode;
            String subPath;
            while (children.hasMoreElements()) {
                subPath = path;
                if (subPath.length() != 0) {
                    subPath += ".";
                }
                subNode = (DefaultMutableTreeNode)children.nextElement();
                subPath += subNode.toString();
                selectDefaultNodes(subNode, subPath, treePath.pathByAddingChild(subNode));
            }

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -