Search in sources :

Example 46 with UIContext

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

the class SubordinateControlHelper method registerSubordinateControl.

/**
 * Register the Subordinate Control so that it can be applied by the {@link SubordinateControlInterceptor}.
 *
 * @param controlId the subordinate id
 */
public static void registerSubordinateControl(final String controlId) {
    UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
    if (uic == null) {
        throw new SystemException("No User Context available to register Subordinate Control.");
    }
    Set<String> controls = (Set<String>) uic.getFwkAttribute(SUBORDINATE_CONTROL_SESSION_KEY);
    if (controls == null) {
        controls = new HashSet<>();
        uic.setFwkAttribute(SUBORDINATE_CONTROL_SESSION_KEY, controls);
    }
    controls.add(controlId);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext)

Example 47 with UIContext

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

the class VelocityRendererImpl method renderTemplate.

/**
 * {@inheritDoc}
 */
@Override
public void renderTemplate(final String templateName, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) {
    LOG.debug("Rendering velocity template [" + templateName + "].");
    // Velocity uses a ClassLoader so dont use an absolute path.
    String name = templateName.startsWith("/") ? templateName.substring(1) : templateName;
    try {
        // Load template
        Template template = getVelocityEngine().getTemplate(name);
        // Map the tagged components to be used in the replace writer
        Map<String, WComponent> componentsByKey = TemplateUtil.mapTaggedComponents(context, taggedComponents);
        // Setup context
        VelocityContext velocityContext = new VelocityContext();
        for (Map.Entry<String, Object> entry : context.entrySet()) {
            velocityContext.put(entry.getKey(), entry.getValue());
        }
        // Write template
        UIContext uic = UIContextHolder.getCurrent();
        try (TemplateWriter velocityWriter = new TemplateWriter(writer, componentsByKey, uic)) {
            template.merge(velocityContext, velocityWriter);
        }
    } catch (ResourceNotFoundException e) {
        throw new SystemException("Could not find velocity template [" + templateName + "]. " + e.getMessage(), e);
    } catch (Exception e) {
        throw new SystemException("Problems with velocity template [" + templateName + "]. " + e.getMessage(), e);
    }
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) VelocityContext(org.apache.velocity.VelocityContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template) WComponent(com.github.bordertech.wcomponents.WComponent) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Map(java.util.Map)

Example 48 with UIContext

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

the class TreeUtil method findWComponents.

/**
 * Retrieves WComponents by their path in the WComponent tree.
 * <p>
 * Paths are specified using class names, starting from the furthest ancestor. To reduce the path lengths, class
 * names do not need to be fully-qualified. The path does not need to explicitly state intermediate components
 * between components, and may include an index suffix to select a particular instance of a component in e.g. a
 * repeater or a set of fields. Some example paths are shown below.
 * </p>
 * Example paths.
 * <dl>
 * <dt><code>{ "MyComponent" }</code></dt>
 * <dd>Matches the first instance of MyComponent.</dd>
 * <dt><code>{ "MyComponent[0]" }</code></dt>
 * <dd>Also matches the first instance of MyComponent.</dd>
 * <dt><code>{ "MyComponent[1]" }</code></dt>
 * <dd>Matches the second instance of MyComponent.</dd>
 * <dt><code>{ "MyPanel", "MyComponent" }</code></dt>
 * <dd>Matches the first instance of MyComponent which is nested anywhere under a MyPanel.</dd>
 * <dt><code>{ "MyApp", "MyPanel", "MyComponent" }</code></dt>
 * <dd>Matches the first instance of MyComponent, nested within a MyPanel, which is in turn nested somewhere within
 * a MyApp.</dd>
 * </dl>
 *
 * @param component the component to search from.
 * @param path the path to the WComponent.
 * @param visibleOnly visible only
 * @return the component matching the given path, or null if not found.
 */
public static ComponentWithContext[] findWComponents(final WComponent component, final String[] path, final boolean visibleOnly) {
    UIContext uic = UIContextHolder.getCurrent();
    if (uic == null) {
        throw new IllegalStateException("No user context available.");
    }
    List<ComponentWithContext> matchAtLevel = new ArrayList<>();
    matchAtLevel.add(new ComponentWithContext(component, uic));
    for (int i = 0; i < path.length; i++) {
        List<ComponentWithContext> matchAtLastLevel = matchAtLevel;
        matchAtLevel = new ArrayList<>();
        for (ComponentWithContext comp : matchAtLastLevel) {
            String[] parts = path[i].trim().split("[\\[\\]]");
            String className = parts[0].trim();
            int index = parts.length == 2 ? Integer.parseInt(parts[1]) : -1;
            FindComponentsByClassVisitor visitor = new FindComponentsByClassVisitor(comp.getComponent(), className, i == 0);
            UIContextHolder.pushContext(comp.getContext());
            try {
                if (visibleOnly) {
                    TreeUtil.traverseVisible(comp.getComponent(), visitor);
                } else {
                    TreeUtil.traverse(comp.getComponent(), visitor);
                }
            } finally {
                UIContextHolder.popContext();
            }
            if (index >= 0) {
                if (index < visitor.getResult().size()) {
                    matchAtLevel.add(visitor.getResult().get(index));
                }
            } else {
                matchAtLevel.addAll(visitor.getResult());
            }
        }
    }
    return matchAtLevel.toArray(new ComponentWithContext[matchAtLevel.size()]);
}
Also used : FindComponentsByClassVisitor(com.github.bordertech.wcomponents.util.visitor.FindComponentsByClassVisitor) UIContext(com.github.bordertech.wcomponents.UIContext) ArrayList(java.util.ArrayList) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 49 with UIContext

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

the class WSuggestionsRenderer_Test method testDoPaintAjaxOptions.

@Test
public void testDoPaintAjaxOptions() throws IOException, SAXException, XpathException {
    List<String> options = Arrays.asList("A", "B", "C");
    WSuggestions field = new WSuggestions(options);
    // Set action for AJAX refresh
    field.setRefreshAction(new Action() {

        @Override
        public void execute(final ActionEvent event) {
        // Do nothing
        }
    });
    assertSchemaMatch(field);
    assertXpathEvaluatesTo(field.getId(), "//ui:suggestions/@id", field);
    assertXpathNotExists("//ui:suggestions/@min", field);
    assertXpathNotExists("//ui:suggestions/@data", field);
    // AJAX flag should be true
    assertXpathEvaluatesTo("true", "//ui:suggestions/@ajax", field);
    // Suggestions should only be rendered when refreshed via AJAX
    assertXpathNotExists("//ui:suggestions/ui:suggestion", field);
    // Setup suggestions as the current AJAX trigger
    UIContext uic = createUIContext();
    uic.setUI(new DefaultWComponent());
    setActiveContext(uic);
    try {
        AjaxOperation operation = new AjaxOperation(field.getId(), field.getId());
        AjaxHelper.setCurrentOperationDetails(operation, null);
        assertSchemaMatch(field);
        assertXpathExists("//ui:suggestions/ui:suggestion", field);
        assertXpathEvaluatesTo(options.get(0), "//ui:suggestions/ui:suggestion[1]/@value", field);
        assertXpathEvaluatesTo(options.get(1), "//ui:suggestions/ui:suggestion[2]/@value", field);
        assertXpathEvaluatesTo(options.get(2), "//ui:suggestions/ui:suggestion[3]/@value", field);
    } finally {
        AjaxHelper.clearCurrentOperationDetails();
    }
}
Also used : Action(com.github.bordertech.wcomponents.Action) AjaxOperation(com.github.bordertech.wcomponents.AjaxOperation) UIContext(com.github.bordertech.wcomponents.UIContext) WSuggestions(com.github.bordertech.wcomponents.WSuggestions) ActionEvent(com.github.bordertech.wcomponents.ActionEvent) DefaultWComponent(com.github.bordertech.wcomponents.DefaultWComponent) Test(org.junit.Test)

Example 50 with UIContext

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

the class WTableRenderer_Test method testXssEscaping.

@Test
public void testXssEscaping() throws IOException, SAXException, XpathException {
    WTable table = new WTable();
    table.addColumn(new WTableColumn(getMaliciousContent(), WText.class));
    table.addColumn(new WTableColumn(getMaliciousContent(), WText.class));
    table.addColumn(new WTableColumn(getMaliciousContent(), WText.class));
    table.setNoDataMessage(getMaliciousAttribute("ui:table"));
    UIContext uic = createUIContext();
    assertSafeContent(table);
    WButton button = new WButton("dummy");
    table.addAction(button);
    table.addActionConstraint(button, new ActionConstraint(0, 1, false, getMaliciousAttribute("ui:action")));
    assertSafeContent(table);
    TableModel tableModel = createTableModel();
    table.setTableModel(tableModel);
    // clear out cached data from previous renders
    uic.clearScratchMap();
    assertSafeContent(table);
    table.setCaption(getMaliciousAttribute("ui:table"));
    assertSafeContent(table);
}
Also used : WTable(com.github.bordertech.wcomponents.WTable) WTableColumn(com.github.bordertech.wcomponents.WTableColumn) WText(com.github.bordertech.wcomponents.WText) UIContext(com.github.bordertech.wcomponents.UIContext) ActionConstraint(com.github.bordertech.wcomponents.WTable.ActionConstraint) WButton(com.github.bordertech.wcomponents.WButton) TableModel(com.github.bordertech.wcomponents.WTable.TableModel) AdapterBasicTableModel(com.github.bordertech.wcomponents.AdapterBasicTableModel) SimpleTableModel(com.github.bordertech.wcomponents.SimpleTableModel) Test(org.junit.Test)

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