Search in sources :

Example 1 with SubUIContext

use of com.github.bordertech.wcomponents.WRepeater.SubUIContext 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 2 with SubUIContext

use of com.github.bordertech.wcomponents.WRepeater.SubUIContext in project wcomponents by BorderTech.

the class UIContextImpl_Test method testFocusableInWRepeaterNested.

@Test
public void testFocusableInWRepeaterNested() {
    // Beans list
    final String optionX = "X";
    final String optionY = "Y";
    final List<String> beans = Arrays.asList(new String[] { optionX, optionY });
    // Another beans list for nested repeater
    final String optionA = "A";
    final String optionB = "B";
    final String optionC = "C";
    final List<String> beans2 = Arrays.asList(new String[] { optionA, optionB, optionC });
    // Setup the nested repeater
    WRepeater repeater2 = new WRepeater();
    WComponent content2 = new WBeanComponent() {

        @Override
        public void handleRequest(final Request request) {
            super.handleRequest(request);
            UIContext uic = UIContextHolder.getCurrent();
            if (uic instanceof SubUIContext) {
                SubUIContext suic = (SubUIContext) uic;
                // Set the component on the third row to have focus
                if (suic.getRowIndex() == 2) {
                    uic.setFocussed(this, uic);
                }
            }
        }
    };
    repeater2.setRepeatedComponent(content2);
    // Setup the top level repeater
    WRepeater repeater = new WRepeater();
    WBeanContainer content = new WBeanContainer() {

        @Override
        public void handleRequest(final Request request) {
            super.handleRequest(request);
            UIContext uic = UIContextHolder.getCurrent();
            if (uic instanceof SubUIContext) {
                SubUIContext suic = (SubUIContext) uic;
                if (suic.getRowIndex() == 0) {
                    // Set the bean list on the nested repeater, only for the first row
                    WRepeater nested = (WRepeater) getChildAt(0);
                    nested.setBeanList(beans2);
                }
            }
        }
    };
    content.add(repeater2);
    repeater.setRepeatedComponent(content);
    // Set the repeater as the "UI" for context
    UIContext uic = new UIContextImpl();
    uic.setUI(repeater);
    setActiveContext(uic);
    // Setup the bean list on the top level repeater
    repeater.setBeanList(beans);
    // ServiceRequest on the repeater (to create the SubUiContexts)
    MockRequest request = new MockRequest();
    repeater.serviceRequest(request);
    // Simulate clearing of scratch map
    uic.clearScratchMap();
    // Get the focused component
    String focusId = uic.getFocussedId();
    // Get the ID of the component on the second row
    String rowComponentId = null;
    try {
        UIContext suic = repeater.getRowContext(optionX);
        UIContextHolder.pushContext(suic);
        UIContext suic2 = repeater2.getRowContext(optionC);
        UIContextHolder.pushContext(suic2);
        rowComponentId = content2.getId();
    } finally {
        UIContextHolder.reset();
    }
    Assert.assertNotNull("Focus ID should not be null", focusId);
    Assert.assertEquals("Focus ID should be the ID for the component on the 2nd row", rowComponentId, focusId);
}
Also used : SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 3 with SubUIContext

use of com.github.bordertech.wcomponents.WRepeater.SubUIContext in project wcomponents by BorderTech.

the class UIContextImpl_Test method testFocusableInWRepeater.

@Test
public void testFocusableInWRepeater() {
    WContainer root = new WContainer();
    // Setup the repeater
    WRepeater repeater = new WRepeater();
    WComponent content = new WBeanComponent() {

        @Override
        public void handleRequest(final Request request) {
            super.handleRequest(request);
            UIContext uic = UIContextHolder.getCurrent();
            if (uic instanceof SubUIContext) {
                SubUIContext suic = (SubUIContext) uic;
                // Set the component on the second row to have focus
                if (suic.getRowIndex() == 1) {
                    uic.setFocussed(this, uic);
                }
            }
        }
    };
    repeater.setRepeatedComponent(content);
    root.add(repeater);
    // Set the repeater as the "UI" for context
    UIContext uic = new UIContextImpl();
    uic.setUI(root);
    setActiveContext(uic);
    // Setup the bean list on the repeater
    final String optionA = "A";
    final String optionB = "B";
    final String optionC = "C";
    List<String> beans = Arrays.asList(new String[] { optionA, optionB, optionC });
    repeater.setBeanList(beans);
    // ServiceRequest on the repeater (to create the SubUiContexts)
    MockRequest request = new MockRequest();
    repeater.serviceRequest(request);
    // Simulate clearing of scratch map
    uic.clearScratchMap();
    // Get the focused component
    String focusId = uic.getFocussedId();
    // Get the ID of the component on the second row
    UIContext suic = repeater.getRowContext(optionB);
    UIContextHolder.pushContext(suic);
    String rowComponentId = null;
    try {
        rowComponentId = content.getId();
    } finally {
        UIContextHolder.popContext();
    }
    Assert.assertNotNull("Focus ID should not be null", focusId);
    Assert.assertEquals("Focus ID should be the ID for the component on the 2nd row", rowComponentId, focusId);
    // Make root hidden
    root.setHidden(true);
    Assert.assertNull("Focus ID should be null with hidden parent", uic.getFocussedId());
}
Also used : SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 4 with SubUIContext

use of com.github.bordertech.wcomponents.WRepeater.SubUIContext in project wcomponents by BorderTech.

the class WDataTable_Test method testTableDefaultIds.

@Test
public void testTableDefaultIds() {
    WNamingContext context = new WNamingContext("TEST");
    WDataTable table = new WDataTable();
    table.setDataModel(new SimpleTableDataModel(new String[][] { { "1" }, { "3" }, { "2" } }));
    WComponent repeated = new WBeanComponent();
    table.addColumn(new WTableColumn("dummy", repeated));
    context.add(table);
    context.setLocked(true);
    setActiveContext(createUIContext());
    String prefix = "TEST" + WComponent.ID_CONTEXT_SEPERATOR + WComponent.ID_FRAMEWORK_ASSIGNED_SEPERATOR;
    // Table ID
    Assert.assertEquals("Incorrect default id for table", prefix + "0", table.getId());
    String tableId = table.getId();
    // Repeater ID
    Assert.assertEquals("Incorrect default id for table repeater", tableId + "-row", table.getRepeater().getId());
    // Repeater repeat root ID
    Assert.assertEquals("Incorrect default id for table repeater root", tableId + "-row-r", table.getRepeater().getRepeatRoot().getId());
    String rowPrefix = table.getRepeater().getRepeatRoot().getId();
    // Allow for WTableColumn between repeated component
    String rowSuffix = "_0b0";
    // Row IDs
    for (UIContext uic : table.getRepeater().getRowContexts()) {
        // Id has uic row render id in it
        int row = ((SubUIContext) uic).getContextId();
        String idPrefix = rowPrefix + row + WComponent.ID_CONTEXT_SEPERATOR + rowSuffix;
        try {
            UIContextHolder.pushContext(uic);
            Assert.assertEquals("Incorrect default id for repeated component", idPrefix, repeated.getId());
        } finally {
            UIContextHolder.popContext();
        }
    }
}
Also used : SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) Test(org.junit.Test)

Example 5 with SubUIContext

use of com.github.bordertech.wcomponents.WRepeater.SubUIContext in project wcomponents by BorderTech.

the class WDataTableRowRenderer method getRowRendererClass.

/**
 * Retrieves the renderer class for a given row.
 *
 * @return the renderer class for a given row (if it's an expanded row), otherwise null.
 */
private Class<? extends WComponent> getRowRendererClass() {
    UIContext uic = UIContextHolder.getCurrent();
    if (table.getDataModel() instanceof TreeTableDataModel && uic instanceof SubUIContext) {
        int row = getRowIndex((SubUIContext) uic);
        TreeTableDataModel dataModel = (TreeTableDataModel) table.getDataModel();
        TableTreeNode node = dataModel.getNodeAtLine(row);
        return node.getRendererClass();
    }
    return null;
}
Also used : SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext)

Aggregations

SubUIContext (com.github.bordertech.wcomponents.WRepeater.SubUIContext)19 Test (org.junit.Test)14 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)4 UIContext (com.github.bordertech.wcomponents.UIContext)2 RowIdWrapper (com.github.bordertech.wcomponents.WTable.RowIdWrapper)2 TableDataModel (com.github.bordertech.wcomponents.TableDataModel)1 TableTreeNode (com.github.bordertech.wcomponents.TableTreeNode)1 TreeTableDataModel (com.github.bordertech.wcomponents.TreeTableDataModel)1 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 WTable (com.github.bordertech.wcomponents.WTable)1 WTableColumn (com.github.bordertech.wcomponents.WTableColumn)1 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)1 TreeNode (com.github.bordertech.wcomponents.util.TreeNode)1 ArrayList (java.util.ArrayList)1