Search in sources :

Example 41 with UIContext

use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.

the class WrongStepAjaxInterceptor method handleRenderRedirect.

/**
 * Redirect the user via the AJAX response.
 *
 * @param writer the print writer for the response
 */
private void handleRenderRedirect(final PrintWriter writer) {
    UIContext uic = UIContextHolder.getCurrent();
    // Redirect user to error page
    LOG.warn("User will be redirected to " + redirectUrl);
    // Setup response with redirect
    getResponse().setContentType(WebUtilities.CONTENT_TYPE_XML);
    writer.write(XMLUtil.getXMLDeclarationWithThemeXslt(uic));
    writer.print("<ui:ajaxresponse ");
    writer.print(XMLUtil.UI_NAMESPACE);
    writer.print(">");
    writer.print("<ui:ajaxtarget id=\"" + triggerId + "\" action=\"replace\">");
    // Redirect URL
    writer.print("<ui:redirect url=\"" + redirectUrl + "\" />");
    writer.print("</ui:ajaxtarget>");
    writer.print("</ui:ajaxresponse>");
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext)

Example 42 with UIContext

use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.

the class WrongStepServerInterceptor method preparePaint.

/**
 * {@inheritDoc}
 */
@Override
public void preparePaint(final Request request) {
    // Increment the step counter
    UIContext uic = UIContextHolder.getCurrent();
    StepCountUtil.incrementSessionStep(uic);
    super.preparePaint(request);
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext)

Example 43 with UIContext

use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.

the class VelocityRenderer method fillContext.

/**
 * Fills the given velocity context with data from the component which is being rendered. A map of components is
 * also built up, in order to support deferred rendering.
 *
 * @param component the current component being rendered.
 * @param context the velocity context to modify.
 * @param componentsByKey a map to store components for deferred rendering.
 */
private void fillContext(final WComponent component, final VelocityContext context, final Map<String, WComponent> componentsByKey) {
    // Also make the component available under the "this" key.
    context.put("this", component);
    // Make the UIContext available under the "uicontext" key.
    UIContext uic = UIContextHolder.getCurrent();
    context.put("uicontext", uic);
    context.put("uic", uic);
    if (component instanceof VelocityProperties) {
        Map<?, ?> map = ((VelocityProperties) component).getVelocityMap();
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            String key = (String) entry.getKey();
            Object value = entry.getValue();
            context.put(key, value);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Handling children");
    }
    // As well as going into their own named slots, visible children are also
    // placed into a list called children
    ArrayList<String> children = new ArrayList<>();
    if (component instanceof Container) {
        Container container = (Container) component;
        for (int i = 0; i < container.getChildCount(); i++) {
            WComponent child = container.getChildAt(i);
            String tag = child.getTag();
            if (tag != null || child.isVisible()) {
                // The key needs to be something which would never be output by a Velocity template.
                String key = "<VelocityLayout" + child.getId() + "/>";
                componentsByKey.put(key, child);
                if (tag != null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Adding child " + tag + " to context");
                    }
                    addToContext(context, tag, key);
                }
                if (child.isVisible()) {
                    children.add(key);
                }
            }
        }
        context.put("children", children);
    }
    // Put the context in the context
    context.put("context", context);
}
Also used : AbstractWComponent(com.github.bordertech.wcomponents.AbstractWComponent) WComponent(com.github.bordertech.wcomponents.WComponent) Container(com.github.bordertech.wcomponents.Container) UIContext(com.github.bordertech.wcomponents.UIContext) ArrayList(java.util.ArrayList) VelocityProperties(com.github.bordertech.wcomponents.velocity.VelocityProperties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 44 with UIContext

use of com.github.bordertech.wcomponents.UIContext 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 45 with UIContext

use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.

the class SubordinateControlHelper method applyRegisteredControls.

/**
 * Apply the registered Subordinate Controls.
 *
 * @param request the request being processed.
 * @param useRequestValues the flag to indicate the controls should use values from the request.
 */
public static void applyRegisteredControls(final Request request, final boolean useRequestValues) {
    Set<String> controls = getRegisteredSubordinateControls();
    if (controls == null) {
        return;
    }
    // Process Controls
    for (String controlId : controls) {
        // Find the Component for this ID
        ComponentWithContext controlWithContext = WebUtilities.getComponentById(controlId, true);
        if (controlWithContext == null) {
            LOG.warn("Subordinate control for id " + controlId + " is no longer in the tree.");
            continue;
        }
        if (!(controlWithContext.getComponent() instanceof WSubordinateControl)) {
            LOG.warn("Component for id " + controlId + " is not a subordinate control.");
            continue;
        }
        WSubordinateControl control = (WSubordinateControl) controlWithContext.getComponent();
        UIContext uic = controlWithContext.getContext();
        UIContextHolder.pushContext(uic);
        try {
            if (useRequestValues) {
                control.applyTheControls(request);
            } else {
                control.applyTheControls();
            }
        } finally {
            UIContextHolder.popContext();
        }
    }
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Aggregations

UIContext (com.github.bordertech.wcomponents.UIContext)114 Test (org.junit.Test)47 WComponent (com.github.bordertech.wcomponents.WComponent)18 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)15 SystemException (com.github.bordertech.wcomponents.util.SystemException)14 WApplication (com.github.bordertech.wcomponents.WApplication)13 UIContextImpl (com.github.bordertech.wcomponents.UIContextImpl)11 WText (com.github.bordertech.wcomponents.WText)11 PrintWriter (java.io.PrintWriter)11 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)10 SeleniumWComponentsWebDriver (com.github.bordertech.wcomponents.test.selenium.driver.SeleniumWComponentsWebDriver)9 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)7 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)7 WDropdown (com.github.bordertech.wcomponents.WDropdown)7 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)7 StringWriter (java.io.StringWriter)7 AjaxOperation (com.github.bordertech.wcomponents.AjaxOperation)6 Environment (com.github.bordertech.wcomponents.Environment)6 MockWEnvironment (com.github.bordertech.wcomponents.MockWEnvironment)6 WButton (com.github.bordertech.wcomponents.WButton)6