Example usage for org.apache.wicket Component getBehaviors

List of usage examples for org.apache.wicket Component getBehaviors

Introduction

In this page you can find the example usage for org.apache.wicket Component getBehaviors.

Prototype

public <M extends Behavior> List<M> getBehaviors(Class<M> type) 

Source Link

Document

Gets the subset of the currently coupled Behavior s that are of the provided type as an unmodifiable list.

Usage

From source file:org.apache.isis.viewer.wicket.ui.components.scalars.ScalarPanelAbstract2.java

License:Apache License

private void addFormComponentBehaviourToUpdateSubscribers() {
    Component scalarValueComponent = getScalarValueComponent();
    if (scalarValueComponent == null) {
        return;//w  ww.  j  a  va  2 s  .  co m
    }
    for (Behavior b : scalarValueComponent.getBehaviors(ScalarUpdatingBehavior.class)) {
        scalarValueComponent.remove(b);
    }
    scalarValueComponent.add(new ScalarUpdatingBehavior(this));
}

From source file:org.devgateway.eudevfin.ui.common.components.TextInputFieldTest.java

License:Open Source License

@Test
public void testTextRender() {
    TextInputField<String> component = new TextInputField<>("text", new Model<String>());
    component.typeString(10);/*from  w w w .j  av  a2 s .co m*/
    tester.startComponentInPage(component);
    tester.assertComponent("text", TextInputField.class);

    TagTester label = TagTester.createTagByAttribute(tester.getLastResponse().getDocument(), "class",
            "control-label");
    assertNotNull(label);
    assertEquals("label", label.getName());
    assertEquals("Label", label.getValue());

    TagTester controls = TagTester.createTagByAttribute(tester.getLastResponse().getDocument(), "class",
            "controls");
    assertNotNull(controls);
    assertEquals("div", controls.getName());

    List<TagTester> help = TagTester.createTagsByAttribute(tester.getLastResponse().getDocument(), "class",
            "help-block", false);
    assertNotNull(help);
    assertEquals(1L, help.size());
    assertEquals("Help Text", help.get(0).getValue());

    Component icon = tester.getComponentFromLastRenderedPage("text:control-group:detailedHelpIcon");
    //click on the expand help button and see if the extra help arrives
    tester.executeBehavior(icon.getBehaviors(AjaxEventBehavior.class).get(0));

    help = TagTester.createTagsByAttribute(tester.getLastResponse().getDocument(), "class", "help-block",
            false);
    assertNotNull(help);
    assertEquals(2L, help.size());
    assertEquals("Help Text", help.get(0).getValue());
    assertEquals("Help Detail", help.get(1).getValue());

    TagTester inputField = TagTester.createTagByAttribute(tester.getLastResponse().getDocument(), "type",
            "text");
    assertNotNull(inputField);
    assertEquals("input", inputField.getName());
    assertEquals("Placeholder", inputField.getAttribute("placeholder"));

}

From source file:org.efaps.ui.wicket.behaviors.dojo.AutoCompleteBehavior.java

License:Apache License

@Override
public void renderHead(final Component _component, final IHeaderResponse _response) {
    super.renderHead(_component, _response);
    // add wicket ajax to be sure that is is included
    CoreLibrariesContributor.contributeAjax(this.component.getApplication(), _response);

    final CharSequence ajaxBaseUrl = Strings
            .escapeMarkup(this.component.getRequestCycle().getUrlRenderer().getBaseUrl().toString());
    _response.render(JavaScriptHeaderItem.forScript("Wicket.Ajax.baseUrl=\"" + ajaxBaseUrl + "\";",
            "wicket-ajax-base-url"));

    _response.render(AbstractEFapsHeaderItem.forCss(AutoCompleteBehavior.CSS));

    final String comboBoxId = "cb" + _component.getMarkupId();
    final StringBuilder js = new StringBuilder().append("var ").append(comboBoxId);

    switch (this.settings.getAutoType()) {
    case SUGGESTION:
        js.append(" = new AutoSuggestion({");
        break;//from w  w w  .  j  a v a  2 s .  c o m
    case TOKEN:
        js.append(" = new AutoTokenInput({");
        break;
    default:
        js.append(" = new AutoComplete({");
        break;
    }

    js.append("id:\"").append(_component.getMarkupId()).append("\",").append("name:\"")
            .append(this.settings.getFieldName()).append("\",").append("placeHolder:ph,").append("store: as,")
            .append("value: \"\",").append("callbackUrl:\"" + getCallbackUrl() + "\",");

    switch (this.settings.getAutoType()) {
    case TOKEN:
        break;
    default:
        final String id = ((AutoCompleteField) _component).getItemValue();
        final String label = ((AutoCompleteField) _component).getItemLabel();
        // only if both value are valid it makes sence to add it
        if (StringUtils.isNotEmpty(id) && StringUtils.isNotEmpty(label)) {
            js.append("item: { id:\"").append(id).append("\", name:\"").append(label).append("\", label:\"")
                    .append(label).append("\"},");
        }
        break;
    }

    if (this.settings.getFieldConfiguration().hasProperty(UIFormFieldProperty.WIDTH)
            && !this.settings.getFieldConfiguration().isTableField()) {
        js.append("style:\"width:").append(this.settings.getFieldConfiguration().getWidth()).append("\",");
    }
    if (!this.settings.isHasDownArrow()) {
        js.append("hasDownArrow:").append(this.settings.isHasDownArrow()).append(",");
    }
    if (this.settings.getMinInputLength() > 1) {
        js.append("minInputLength:").append(this.settings.getMinInputLength()).append(",");
    }
    if (this.settings.getSearchDelay() != 500) {
        js.append("searchDelay:").append(this.settings.getSearchDelay()).append(",");
    }

    if (!"p".equals(this.settings.getParamName())) {
        js.append("paramName:\"").append(this.settings.getParamName()).append("\",");
    }

    if (!this.settings.getExtraParameters().isEmpty()) {
        js.append("extraParameters:[");
        boolean first = true;
        for (final String ep : this.settings.getExtraParameters()) {
            if (first) {
                first = false;
            } else {
                js.append(",");
            }
            js.append("\"").append(ep).append("\"");
        }
        js.append("],");
    }
    if (Type.SUGGESTION.equals(this.settings.getAutoType())) {
        js.append("labelAttr: \"label\",");
    }

    js.append("searchAttr: \"name\"}, \"").append(_component.getMarkupId()).append("\");\n");

    if (this.settings.isRequired() && !Type.TOKEN.equals(this.settings.getAutoType())) {
        js.append("on(").append(comboBoxId).append(", 'change', function() {").append("var label=")
                .append(comboBoxId).append(".item.label;")
                .append("if (!(label === undefined || label === null)) {").append(comboBoxId)
                .append(".item.name=label;").append(comboBoxId).append(".set(\"item\",").append(comboBoxId)
                .append(".item);").append("}");
        if (this.fieldUpdate != null) {
            js.append(this.fieldUpdate.getCallbackScript4Dojo());
        }
        js.append("});\n");
    } else if (this.fieldUpdate != null) {
        js.append("on(").append(comboBoxId).append(", 'change', function() {")
                .append(this.fieldUpdate.getCallbackScript4Dojo()).append("});\n");
    }

    if (!_component.getBehaviors(SetSelectedRowBehavior.class).isEmpty()) {
        js.append("on(").append(comboBoxId).append(", 'focus', function() {").append(
                _component.getBehaviors(SetSelectedRowBehavior.class).get(0).getJavaScript("this.valueNode"))
                .append("});\n");
    }
    if (Type.TOKEN.equals(this.settings.getAutoType())) {
        final List<IOption> tokens = ((AutoCompleteField) _component).getTokens();
        for (final IOption token : tokens) {
            js.append(comboBoxId).append(".addToken(\"")
                    .append(StringEscapeUtils.escapeEcmaScript(token.getValue().toString())).append("\",\"")
                    .append(StringEscapeUtils.escapeEcmaScript(token.getLabel())).append("\");\n");
        }
    }
    _response.render(AutoCompleteHeaderItem.forScript(js.toString(), EnumSet.of(this.settings.getAutoType())));
}

From source file:wicketdnd.test.DnDTester.java

License:Apache License

private <T extends Behavior> T getBehavior(Component component, Class<T> clazz) {
    List<T> behaviors = component.getBehaviors(clazz);
    if (behaviors.size() == 0) {
        throw new WicketRuntimeException("no behavior of type " + clazz.getName());
    } else if (behaviors.size() > 1) {
        throw new WicketRuntimeException("multiple behaviors of type " + clazz.getName());
    }//w w  w . j a va 2s. c o  m

    return behaviors.get(0);
}