Example usage for org.apache.wicket.model IChainingModel IChainingModel

List of usage examples for org.apache.wicket.model IChainingModel IChainingModel

Introduction

In this page you can find the example usage for org.apache.wicket.model IChainingModel IChainingModel.

Prototype

IChainingModel

Source Link

Usage

From source file:org.hippoecm.frontend.editor.plugins.linkpicker.FacetSelectTemplatePlugin.java

License:Apache License

public FacetSelectTemplatePlugin(final IPluginContext context, IPluginConfig config) {
    super(context, config);

    Node node = getModelObject();
    try {/*from  w ww  .  j ava  2  s.co  m*/
        if (!node.hasProperty("hippo:docbase")) {
            node.setProperty("hippo:docbase", node.getSession().getRootNode().getUUID());
        }
        if (!node.hasProperty("hippo:facets")) {
            node.setProperty("hippo:facets", new String[0]);
        }
        if (!node.hasProperty("hippo:values")) {
            node.setProperty("hippo:values", new String[0]);
        }
        if (!node.hasProperty("hippo:modes")) {
            node.setProperty("hippo:modes", new String[0]);
        }
    } catch (ValueFormatException e) {
        log.error(e.getMessage());
    } catch (PathNotFoundException e) {
        log.error(e.getMessage());
    } catch (RepositoryException e) {
        log.error(e.getMessage());
    }

    final IModel<String> displayModel = new LoadableDetachableModel<String>() {
        private static final long serialVersionUID = 1L;

        @Override
        protected String load() {
            Node node = FacetSelectTemplatePlugin.this.getModelObject();
            try {
                if (node != null && node.hasProperty("hippo:docbase")) {
                    String docbaseUUID = node.getProperty("hippo:docbase").getString();
                    if (docbaseUUID == null || docbaseUUID.equals("") || docbaseUUID.startsWith("cafebabe-")) {
                        return EMPTY_LINK_TEXT;
                    }
                    return node.getSession().getNodeByUUID(docbaseUUID).getPath();
                }
            } catch (ValueFormatException e) {
                log.warn("Invalid value format for docbase " + e.getMessage());
                log.debug("Invalid value format for docbase ", e);
            } catch (PathNotFoundException e) {
                log.warn("Docbase not found " + e.getMessage());
                log.debug("Docbase not found ", e);
            } catch (RepositoryException e) {
                log.error("Invalid docbase" + e.getMessage(), e);
            }
            return EMPTY_LINK_TEXT;
        }
    };

    mode = IEditor.Mode.fromString(config.getString("mode"), IEditor.Mode.VIEW);
    try {
        IDataProvider<Integer> provider = new IDataProvider<Integer>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Iterator<Integer> iterator(long first, long count) {
                return new Iterator<Integer>() {
                    int current = 0;

                    public boolean hasNext() {
                        try {
                            Node node = ((JcrNodeModel) FacetSelectTemplatePlugin.this.getDefaultModel())
                                    .getNode();
                            return current < node.getProperty("hippo:facets").getValues().length;
                        } catch (RepositoryException ex) {
                            return false;
                        }
                    }

                    public Integer next() {
                        if (hasNext()) {
                            return Integer.valueOf(current++);
                        } else {
                            throw new NoSuchElementException();
                        }
                    }

                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }

            @Override
            public long size() {
                try {
                    Node node = ((JcrNodeModel) FacetSelectTemplatePlugin.this.getDefaultModel()).getNode();
                    return node.getProperty("hippo:facets").getValues().length;
                } catch (RepositoryException ex) {
                    return 0;
                }
            }

            @Override
            public IModel<Integer> model(Integer object) {
                return new Model<Integer>(object);
            }

            @Override
            public void detach() {
            }
        };
        if (IEditor.Mode.EDIT == mode) {
            final JcrPropertyValueModel<String> docbaseModel = new JcrPropertyValueModel<String>(
                    new JcrPropertyModel<String>(node.getProperty("hippo:docbase")));

            IDialogFactory dialogFactory = new IDialogFactory() {
                private static final long serialVersionUID = 1L;

                public AbstractDialog<String> createDialog() {
                    final IPluginConfig dialogConfig = LinkPickerDialogConfig
                            .fromPluginConfig(getPluginConfig(), docbaseModel);
                    return new LinkPickerDialog(context, dialogConfig, new IChainingModel<String>() {
                        private static final long serialVersionUID = 1L;

                        public String getObject() {
                            return docbaseModel.getObject();
                        }

                        public void setObject(String object) {
                            docbaseModel.setObject(object);
                            redraw();
                        }

                        public IModel<String> getChainedModel() {
                            return docbaseModel;
                        }

                        public void setChainedModel(IModel<?> model) {
                            throw new UnsupportedOperationException("Value model cannot be changed");
                        }

                        public void detach() {
                            docbaseModel.detach();
                        }
                    });
                }
            };

            add(new ClearableDialogLink("docbase", displayModel, dialogFactory, getDialogService()) {
                private static final long serialVersionUID = 1L;

                @Override
                public void onClear() {
                    Node node = ((JcrNodeModel) FacetSelectTemplatePlugin.this.getDefaultModel()).getNode();
                    try {
                        node.setProperty("hippo:docbase", node.getSession().getRootNode().getUUID());
                    } catch (RepositoryException e) {
                        log.error("Unable to reset docbase to rootnode uuid", e);
                    }
                    redraw();
                }

                @Override
                public boolean isClearVisible() {
                    // Checking for string literals ain't pretty. It's probably better to create a better display model.
                    return !EMPTY_LINK_TEXT.equals((String) displayModel.getObject());
                }
            });

            add(new DataView<Integer>("arguments", provider) {
                private static final long serialVersionUID = 1L;

                @Override
                public void populateItem(final Item<Integer> item) {
                    Node node = FacetSelectTemplatePlugin.this.getModelObject();
                    final int index = item.getModelObject().intValue();
                    try {
                        item.add(new TextFieldWidget("facet", new JcrPropertyValueModel<String>(index,
                                new JcrPropertyModel<String>(node.getProperty("hippo:facets")))));
                        item.add(new TextFieldWidget("mode", new JcrPropertyValueModel<String>(index,
                                new JcrPropertyModel<String>(node.getProperty("hippo:modes")))));
                        item.add(new TextFieldWidget("value", new JcrPropertyValueModel<String>(index,
                                new JcrPropertyModel<String>(node.getProperty("hippo:values")))));
                        AjaxLink<Void> removeButton;
                        item.add(removeButton = new AjaxLink<Void>("remove") {
                            private static final long serialVersionUID = 1L;

                            @Override
                            public void onClick(AjaxRequestTarget target) {
                                Node node = ((JcrNodeModel) FacetSelectTemplatePlugin.this.getDefaultModel())
                                        .getNode();
                                for (String property : new String[] { "hippo:facets", "hippo:modes",
                                        "hippo:values" }) {
                                    try {
                                        Value[] oldValues = node.getProperty(property).getValues();
                                        Value[] newValues = new Value[oldValues.length - 1];
                                        System.arraycopy(oldValues, 0, newValues, 0, index);
                                        System.arraycopy(oldValues, index + 1, newValues, 0,
                                                oldValues.length - index - 1);
                                        node.setProperty(property, newValues);
                                    } catch (RepositoryException ex) {
                                        log.error("cannot add new facet select line", ex);
                                    }
                                }
                                FacetSelectTemplatePlugin.this.redraw();
                            }
                        });
                        removeButton.setOutputMarkupId(true);
                    } catch (RepositoryException ex) {
                        log.error("cannot read facet select line", ex);
                    }
                }
            });
            AjaxLink<Void> addButton;
            add(addButton = new AjaxLink<Void>("add") {
                private static final long serialVersionUID = 1L;

                @Override
                public void onClick(AjaxRequestTarget target) {
                    Node node = ((JcrNodeModel) FacetSelectTemplatePlugin.this.getDefaultModel()).getNode();
                    for (String property : new String[] { "hippo:facets", "hippo:modes", "hippo:values" }) {
                        try {
                            Value[] oldValues = node.getProperty(property).getValues();
                            Value[] newValues = new Value[oldValues.length + 1];
                            System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
                            newValues[newValues.length - 1] = node.getSession().getValueFactory()
                                    .createValue("");
                            node.setProperty(property, newValues);
                        } catch (RepositoryException ex) {
                            log.error("cannot add new facet select line", ex);
                        }
                    }
                    FacetSelectTemplatePlugin.this.redraw();
                }
            });
            addButton.setOutputMarkupId(true);
        } else {
            add(new Label("docbase", displayModel));
            add(new DataView<Integer>("arguments", provider) {
                private static final long serialVersionUID = 1L;

                @Override
                public void populateItem(final Item<Integer> item) {
                    try {
                        Node node = FacetSelectTemplatePlugin.this.getModelObject();
                        int index = item.getModelObject().intValue();
                        item.add(new Label("facet",
                                node.getProperty("hippo:facets").getValues()[index].getString()));
                        item.add(new Label("mode",
                                node.getProperty("hippo:modes").getValues()[index].getString()));
                        item.add(new Label("value",
                                node.getProperty("hippo:values").getValues()[index].getString()));
                        Label removeButton;
                        item.add(removeButton = new Label("remove"));
                        removeButton.setVisible(false);
                    } catch (RepositoryException ex) {
                        log.error("cannot read facet select line", ex);
                    }
                }
            });
            Label addButton;
            add(addButton = new Label("add"));
            addButton.setVisible(false);
        }
    } catch (PathNotFoundException ex) {
        log.error("failed to read existing facet select", ex);
    } catch (ValueFormatException ex) {
        log.error("failed to read existing facet select", ex);
    } catch (RepositoryException ex) {
        log.error("failed to read existing facet select", ex);
    }

    setOutputMarkupId(true);
}

From source file:org.hippoecm.frontend.editor.plugins.linkpicker.LinkPickerPlugin.java

License:Apache License

public LinkPickerPlugin(final IPluginContext context, IPluginConfig config) {
    super(context, config);

    valueModel = getModel();/*from  ww  w .j a va 2 s  .  c  om*/

    final IModel<String> displayModel = new LoadableDetachableModel<String>() {
        private static final long serialVersionUID = 1L;

        @Override
        protected String load() {
            String docbaseUUID = valueModel.getObject();
            if (docbaseUUID == null || docbaseUUID.equals("") || docbaseUUID.startsWith("cafebabe-")) {
                return EMPTY_LINK_TEXT;
            }
            try {
                return UserSession.get().getJcrSession().getNodeByUUID(docbaseUUID).getPath();
            } catch (ValueFormatException e) {
                log.warn("Invalid value format for docbase " + e.getMessage());
                log.debug("Invalid value format for docbase ", e);
            } catch (PathNotFoundException e) {
                log.warn("Docbase not found " + e.getMessage());
                log.debug("Docbase not found ", e);
            } catch (ItemNotFoundException e) {
                log.info("Docbase not found " + e.getMessage());
                log.debug("Docbase not found ", e);
            } catch (RepositoryException e) {
                log.error("Invalid docbase " + e.getMessage(), e);
            }
            return EMPTY_LINK_TEXT;
        }
    };

    final IEditor.Mode mode = IEditor.Mode.fromString(config.getString("mode"), IEditor.Mode.VIEW);
    if (mode == IEditor.Mode.EDIT) {
        IDialogFactory dialogFactory = new IDialogFactory() {
            private static final long serialVersionUID = 1L;

            public AbstractDialog<String> createDialog() {
                final IPluginConfig dialogConfig = LinkPickerDialogConfig.fromPluginConfig(getPluginConfig(),
                        (JcrPropertyValueModel) valueModel);
                return new LinkPickerDialog(context, dialogConfig, new IChainingModel<String>() {
                    private static final long serialVersionUID = 1L;

                    public String getObject() {
                        return valueModel.getObject();
                    }

                    public void setObject(String object) {
                        valueModel.setObject(object);
                        redraw();
                    }

                    public IModel<String> getChainedModel() {
                        return valueModel;
                    }

                    public void setChainedModel(IModel<?> model) {
                        throw new UnsupportedOperationException("Value model cannot be changed");
                    }

                    public void detach() {
                        valueModel.detach();
                    }

                });
            }
        };

        add(new ClearableDialogLink("value", displayModel, dialogFactory, getDialogService()) {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClear() {
                try {
                    valueModel.setObject(UserSession.get().getJcrSession().getRootNode().getUUID());
                } catch (RepositoryException e) {
                    log.error("Unable to reset docbase to rootnode uuid", e);
                }
                redraw();
            }

            @Override
            public boolean isClearVisible() {
                // Checking for string literals ain't pretty. It's probably better to create a better display model.
                return !EMPTY_LINK_TEXT.equals((String) displayModel.getObject());
            }
        });

    } else {
        add(new Label("value", displayModel));
    }
    setOutputMarkupId(true);
}

From source file:org.hippoecm.frontend.editor.plugins.linkpicker.MirrorTemplatePlugin.java

License:Apache License

/**
 * Create a link picker dialog// w  w w.  j a v a 2  s  .  co  m
 */
private AbstractDialog<String> createLinkPickerDialog(final IPluginContext context) {
    final JcrPropertyValueModel<String> docbaseModel = getDocBaseModel();
    final IPluginConfig dialogConfig = LinkPickerDialogConfig.fromPluginConfig(getPluginConfig(), docbaseModel);
    final IChainingModel<String> linkPickerModel = new IChainingModel<String>() {
        public String getObject() {
            return docbaseModel.getObject();
        }

        public void setObject(String uuid) {
            getDocBaseModel().setObject(uuid);
            redraw();
        }

        public IModel<String> getChainedModel() {
            return docbaseModel;
        }

        public void setChainedModel(IModel<?> model) {
            throw new UnsupportedOperationException("Value model cannot be changed");
        }

        public void detach() {
            docbaseModel.detach();
        }
    };
    return new LinkPickerDialog(context, dialogConfig, linkPickerModel);
}