Example usage for org.apache.wicket.ajax.markup.html AjaxLink getMarkupId

List of usage examples for org.apache.wicket.ajax.markup.html AjaxLink getMarkupId

Introduction

In this page you can find the example usage for org.apache.wicket.ajax.markup.html AjaxLink getMarkupId.

Prototype

public String getMarkupId() 

Source Link

Document

Retrieves id by which this component is represented within the markup.

Usage

From source file:org.hippoecm.frontend.plugins.console.menu.property.PropertyDialog.java

License:Apache License

public PropertyDialog(IModelReference<Node> modelReference) {
    this.modelReference = modelReference;
    final IModel<Node> model = modelReference.getModel();

    getParent().add(CssClass.append("property-dialog"));

    // list defined properties for automatic completion
    choiceModel = new LoadableDetachableModel<Map<String, List<PropertyDefinition>>>() {

        protected Map<String, List<PropertyDefinition>> load() {
            Map<String, List<PropertyDefinition>> choices = new HashMap<>();
            Node node = model.getObject();
            try {
                NodeType pnt = node.getPrimaryNodeType();
                for (PropertyDefinition pd : pnt.getPropertyDefinitions()) {
                    List<PropertyDefinition> list = choices.get(pd.getName());
                    if (list == null) {
                        list = new ArrayList<>(5);
                    }//w w w  .j av a2 s  .  c o m
                    list.add(pd);
                    choices.put(pd.getName(), list);
                }
                for (NodeType nt : node.getMixinNodeTypes()) {
                    for (PropertyDefinition pd : nt.getPropertyDefinitions()) {
                        List<PropertyDefinition> list = choices.get(pd.getName());
                        if (list == null) {
                            list = new ArrayList<>(5);
                        }
                        list.add(pd);
                        choices.put(pd.getName(), list);
                    }
                }
                // remove already set properties from suggestions:
                final Set<String> properties = new HashSet<>(choices.keySet());
                for (String property : properties) {
                    if (!isResidual(property) && node.hasProperty(property)) {
                        choices.remove(property);
                    }
                }
            } catch (RepositoryException e) {
                log.warn("Unable to populate autocomplete list for property names", e);
            }
            return choices;
        }
    };

    // checkbox for property ismultiple
    final CheckBox checkBox = new CheckBox("isMultiple", new Model<Boolean>() {

        @Override
        public void setObject(Boolean multiple) {
            PropertyDialog.this.isMultiple = multiple;
        }

        @Override
        public Boolean getObject() {
            if (PropertyDialog.this.name != null) {
                List<PropertyDefinition> propdefs = choiceModel.getObject().get(PropertyDialog.this.name);
                if (propdefs != null) {
                    for (PropertyDefinition pd : propdefs) {
                        if (PropertyType.nameFromValue(pd.getRequiredType()).equals(type)) {
                            // somehow need to set isMultiple here, otherwise it doesn't get picked up...
                            PropertyDialog.this.isMultiple = pd.isMultiple();
                            return pd.isMultiple();
                        }
                    }
                }
            }
            return PropertyDialog.this.isMultiple;
        }
    });
    checkBox.setOutputMarkupId(true);
    add(checkBox);

    // dropdown for property type
    final DropDownChoice<String> ddChoice = new DropDownChoice<String>("types") {
        @Override
        public List<? extends String> getChoices() {
            if (PropertyDialog.this.name != null) {
                List<PropertyDefinition> propdefs = choiceModel.getObject().get(PropertyDialog.this.name);
                if (propdefs != null) {
                    List<String> result = new ArrayList<>(propdefs.size());
                    for (PropertyDefinition pd : propdefs) {
                        result.add(PropertyType.nameFromValue(pd.getRequiredType()));
                    }
                    return result;
                }
            }
            return ALL_TYPES;

        }
    };
    ddChoice.setModel(new Model<String>() {
        @Override
        public void setObject(String object) {
            type = object;
        }

        @Override
        public String getObject() {
            List<? extends String> choices = ddChoice.getChoices();
            if (choices.size() == 1) {
                type = choices.iterator().next();
            }
            return type;
        }
    });

    ddChoice.setRequired(true);
    ddChoice.setOutputMarkupId(true);
    ddChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
        @Override
        protected void onUpdate(AjaxRequestTarget target) {
        }
    });
    add(ddChoice);

    values = new LinkedList<>();
    values.add("");

    final WebMarkupContainer valuesContainer = new WebMarkupContainer("valuesContainer");
    valuesContainer.setOutputMarkupId(true);
    add(valuesContainer);

    valuesContainer.add(new ListView<String>("values", values) {

        @Override
        protected void populateItem(final ListItem<String> item) {
            final TextField textField = new TextField<>("val", item.getModel());
            textField.add(new OnChangeAjaxBehavior() {

                @Override
                protected void onUpdate(final AjaxRequestTarget target) {
                }

            });
            item.add(textField);

            if (focusOnLatestValue && item.getIndex() == (values.size() - 1)) {
                AjaxRequestTarget ajax = RequestCycle.get().find(AjaxRequestTarget.class);
                if (ajax != null) {
                    ajax.focusComponent(textField);
                }
                focusOnLatestValue = false;
            }

            final AjaxLink deleteLink = new AjaxLink("removeLink") {
                @Override
                public void onClick(final AjaxRequestTarget target) {
                    values.remove(item.getIndex());
                    target.add(valuesContainer);
                }

                @Override
                public boolean isVisible() {
                    return super.isVisible() && item.getIndex() > 0;
                }
            };

            deleteLink.add(TitleAttribute.set(getString("property.value.remove")));
            deleteLink.add(new InputBehavior(new KeyType[] { KeyType.Enter }, EventType.click) {
                @Override
                protected String getTarget() {
                    return "'" + deleteLink.getMarkupId() + "'";
                }
            });
            item.add(deleteLink);
        }
    });

    final AjaxLink addLink = new AjaxLink("addLink") {
        @Override
        public void onClick(final AjaxRequestTarget target) {
            values.add("");
            target.add(valuesContainer);
            focusOnLatestValue = true;
        }

        @Override
        public boolean isVisible() {
            return isMultiple;
        }
    };
    addLink.add(TitleAttribute.set(getString("property.value.add")));
    addLink.add(new InputBehavior(new KeyType[] { KeyType.Enter }, EventType.click) {
        @Override
        protected String getTarget() {
            return "'" + addLink.getMarkupId() + "'";
        }
    });
    valuesContainer.add(addLink);

    checkBox.add(new AjaxFormComponentUpdatingBehavior("onchange") {
        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            target.add(valuesContainer);
            if (!isMultiple && values.size() > 1) {
                String first = values.get(0);
                values.clear();
                values.add(first);
            }
        }
    });

    // text field for property name
    AutoCompleteSettings settings = new AutoCompleteSettings();
    settings.setAdjustInputWidth(false);
    settings.setUseSmartPositioning(true);
    settings.setShowCompleteListOnFocusGain(true);
    settings.setShowListOnEmptyInput(true);
    // Setting a max height will trigger a correct recalculation of the height when the list of items is filtered
    settings.setMaxHeightInPx(400);

    final TextField<String> nameField = new AutoCompleteTextFieldWidget<String>("name",
            PropertyModel.of(this, "name"), settings) {

        @Override
        protected Iterator<String> getChoices(String input) {
            List<String> result = new ArrayList<>();
            for (String propName : choiceModel.getObject().keySet()) {
                if (propName.contains(input)) {
                    result.add(propName);
                }
            }
            return result.iterator();
        }

        @Override
        protected void onUpdate(final AjaxRequestTarget target) {
            super.onUpdate(target);

            target.add(ddChoice);
            target.add(checkBox);
            target.add(valuesContainer);
            focusOnLatestValue = true;
        }
    };

    nameField.setRequired(true);
    add(nameField);
    setFocus(nameField);
}