Search in sources :

Example 6 with TreeNode

use of com.github.bordertech.wcomponents.util.TreeNode in project wcomponents by BorderTech.

the class WDataTableRowRendererRenderer method doRender.

/**
 * Paints the given WDataTableRowRenderer.
 *
 * @param component the WDataTableRowRenderer to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WDataTableRowRenderer renderer = (WDataTableRowRenderer) component;
    XmlStringBuilder xml = renderContext.getWriter();
    WDataTable table = renderer.getTable();
    TableDataModel dataModel = table.getDataModel();
    UIContext uic = UIContextHolder.getCurrent();
    final int numCols = table.getColumnCount();
    int[] columnOrder = table.getColumnOrder();
    int rowIndex = getRowIndex(table, (SubUIContext) uic);
    boolean unselectable = table.getSelectMode() != SelectMode.NONE && !dataModel.isSelectable(rowIndex);
    xml.appendTagOpen("ui:tr");
    xml.appendAttribute("rowIndex", rowIndex);
    xml.appendOptionalAttribute("unselectable", unselectable, "true");
    xml.appendOptionalAttribute("selected", table.getSelectedRows().contains(rowIndex), "true");
    xml.appendOptionalAttribute("filterValues", getFilterValues(dataModel, rowIndex));
    if (table.getExpandMode() != WDataTable.ExpandMode.NONE && dataModel instanceof TreeTableDataModel) {
        TableTreeNode node = ((TreeTableDataModel) dataModel).getNodeAtLine(rowIndex);
        boolean expandable = !node.isLeaf() && !node.isExpanded();
        xml.appendOptionalAttribute("expandable", expandable, "true");
    }
    xml.appendClose();
    if (table.isShowRowHeaders()) {
        xml.appendTag("ui:th");
        renderer.getRowHeader().paint(renderContext);
        xml.appendEndTag("ui:th");
    }
    for (int i = 0; i < numCols; i++) {
        int colIndex = columnOrder == null ? i : columnOrder[i];
        WTableColumn col = table.getColumn(colIndex);
        if (col.isVisible()) {
            xml.appendTag("ui:td");
            renderer.getRenderer(colIndex).paint(renderContext);
            xml.appendEndTag("ui:td");
        }
    }
    if (table.getExpandMode() != WDataTable.ExpandMode.NONE && dataModel instanceof TreeTableDataModel) {
        TreeTableDataModel treeModel = (TreeTableDataModel) dataModel;
        TableTreeNode node = treeModel.getNodeAtLine(rowIndex);
        if (!node.isLeaf()) {
            xml.appendTagOpen("ui:subtr");
            xml.appendOptionalAttribute("open", node.isExpanded(), "true");
            xml.appendClose();
            if (node.isExpanded() || table.getExpandMode() == ExpandMode.CLIENT) {
                WRepeater repeater = table.getRepeater();
                // If there a renderer specified by any child, we only paint content that has a specified renderer
                boolean rendererPresent = false;
                for (Iterator<TreeNode> i = node.children(); !rendererPresent && i.hasNext(); ) {
                    TableTreeNode child = (TableTreeNode) i.next();
                    if (child.getRendererClass() != null) {
                        rendererPresent = true;
                    }
                }
                // Paint immediate children only.
                if (rendererPresent) {
                    xml.appendTagOpen("ui:content");
                    xml.appendOptionalAttribute("spanAllCols", node.isRendererSpansAllCols(), "true");
                    xml.appendClose();
                    for (Iterator<TreeNode> i = node.children(); i.hasNext(); ) {
                        TableTreeNode child = (TableTreeNode) i.next();
                        Integer rowId = child.getRowIndex() - 1;
                        UIContext nodeContext = repeater.getRowContext(rowId);
                        WComponent expandedRenderer = renderer.getExpandedTreeNodeRenderer(child.getRendererClass());
                        if (expandedRenderer != null) {
                            UIContextHolder.pushContext(nodeContext);
                            try {
                                expandedRenderer.paint(renderContext);
                            } finally {
                                UIContextHolder.popContext();
                            }
                        }
                    }
                    xml.appendEndTag("ui:content");
                } else {
                    for (Iterator<TreeNode> i = node.children(); i.hasNext(); ) {
                        TableTreeNode child = (TableTreeNode) i.next();
                        Integer rowId = child.getRowIndex() - 1;
                        UIContext nodeContext = repeater.getRowContext(rowId);
                        UIContextHolder.pushContext(nodeContext);
                        try {
                            render(component, renderContext);
                        } finally {
                            UIContextHolder.popContext();
                        }
                    }
                }
            }
            xml.appendEndTag("ui:subtr");
        }
    }
    xml.appendEndTag("ui:tr");
}
Also used : TableTreeNode(com.github.bordertech.wcomponents.TableTreeNode) TreeTableDataModel(com.github.bordertech.wcomponents.TreeTableDataModel) UIContext(com.github.bordertech.wcomponents.UIContext) SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) WTableColumn(com.github.bordertech.wcomponents.WTableColumn) WRepeater(com.github.bordertech.wcomponents.WRepeater) WComponent(com.github.bordertech.wcomponents.WComponent) WDataTable(com.github.bordertech.wcomponents.WDataTable) TableTreeNode(com.github.bordertech.wcomponents.TableTreeNode) TreeNode(com.github.bordertech.wcomponents.util.TreeNode) WDataTableRowRenderer(com.github.bordertech.wcomponents.WDataTableRowRenderer) TreeTableDataModel(com.github.bordertech.wcomponents.TreeTableDataModel) TableDataModel(com.github.bordertech.wcomponents.TableDataModel) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 7 with TreeNode

use of com.github.bordertech.wcomponents.util.TreeNode in project wcomponents by BorderTech.

the class DevToolkit method getTree.

/**
 * Retrieves a tree representation of the WComponent UI being served by the LDE, in a format suitable for rendering
 * to the UI.
 *
 * @return a tree representation of the WComponent UI.
 */
public TreeNode getTree() {
    UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
    String[] debugTree = uic.getUI().toString().split("\n");
    TreeNode root = new UITreeNode(debugTree[0]);
    TreeNode parent = root;
    TreeNode last = root;
    for (int i = 1; i < debugTree.length; i++) {
        String line = debugTree[i].trim();
        if (line.charAt(0) == '[') {
            parent = last;
            TreeNode child = new UITreeNode(debugTree[++i]);
            parent.add(child);
            last = child;
        } else if (line.charAt(0) == ']') {
            parent = parent.getParent();
        } else {
            TreeNode child = new UITreeNode(debugTree[i]);
            parent.add(child);
            last = child;
        }
    }
    return root;
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) TreeNode(com.github.bordertech.wcomponents.util.TreeNode) AbstractTreeNode(com.github.bordertech.wcomponents.util.AbstractTreeNode)

Example 8 with TreeNode

use of com.github.bordertech.wcomponents.util.TreeNode in project wcomponents by BorderTech.

the class ColumnMenuExample method mapColumnHierarchy.

/**
 * Recursively maps a tree hierarchy to a column menu.
 *
 * @param currentComponent the current component in the menu.
 * @param currentNode the current node in the tree.
 * @param selectedMenuText the WText to display the selected menu item.
 */
private void mapColumnHierarchy(final WComponent currentComponent, final StringTreeNode currentNode, final WText selectedMenuText) {
    if (currentNode.isLeaf()) {
        WMenuItem menuItem = new WMenuItem(currentNode.getData(), new ExampleMenuAction(selectedMenuText));
        menuItem.setActionObject(currentNode.getData());
        if (currentComponent instanceof WMenu) {
            ((WMenu) currentComponent).add(menuItem);
        } else {
            ((WSubMenu) currentComponent).add(menuItem);
        }
    } else {
        WSubMenu subMenu = new WSubMenu(currentNode.getData());
        subMenu.setSelectMode(SelectMode.SINGLE);
        subMenu.setSelectable(false);
        subMenu.setAction(new ExampleMenuAction(selectedMenuText));
        subMenu.setActionObject(currentNode.getData());
        if (currentComponent instanceof WMenu) {
            ((WMenu) currentComponent).add(subMenu);
        } else {
            ((WSubMenu) currentComponent).add(subMenu);
        }
        // Expand the first level in the tree by default.
        if (currentNode.getLevel() == 0) {
            subMenu.setOpen(true);
        }
        for (Iterator<TreeNode> i = currentNode.children(); i.hasNext(); ) {
            mapColumnHierarchy(subMenu, (StringTreeNode) i.next(), selectedMenuText);
        }
    }
}
Also used : WMenuItem(com.github.bordertech.wcomponents.WMenuItem) WSubMenu(com.github.bordertech.wcomponents.WSubMenu) TreeNode(com.github.bordertech.wcomponents.util.TreeNode) WMenu(com.github.bordertech.wcomponents.WMenu)

Aggregations

TreeNode (com.github.bordertech.wcomponents.util.TreeNode)8 TableDataModel (com.github.bordertech.wcomponents.TableDataModel)2 TableTreeNode (com.github.bordertech.wcomponents.TableTreeNode)2 TreeTableDataModel (com.github.bordertech.wcomponents.TreeTableDataModel)2 UIContext (com.github.bordertech.wcomponents.UIContext)2 WMenu (com.github.bordertech.wcomponents.WMenu)2 WMenuItem (com.github.bordertech.wcomponents.WMenuItem)2 WSubMenu (com.github.bordertech.wcomponents.WSubMenu)2 AbstractTreeNode (com.github.bordertech.wcomponents.util.AbstractTreeNode)2 WComponent (com.github.bordertech.wcomponents.WComponent)1 WDataTable (com.github.bordertech.wcomponents.WDataTable)1 WDataTableRowRenderer (com.github.bordertech.wcomponents.WDataTableRowRenderer)1 WRepeater (com.github.bordertech.wcomponents.WRepeater)1 SubUIContext (com.github.bordertech.wcomponents.WRepeater.SubUIContext)1 WTableColumn (com.github.bordertech.wcomponents.WTableColumn)1 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)1 SystemException (com.github.bordertech.wcomponents.util.SystemException)1