Index: console/frontend/src/main/java/org/hippoecm/frontend/plugins/console/editor/HstReferenceEditor.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- console/frontend/src/main/java/org/hippoecm/frontend/plugins/console/editor/HstReferenceEditor.java (revision 45500) +++ console/frontend/src/main/java/org/hippoecm/frontend/plugins/console/editor/HstReferenceEditor.java (revision ) @@ -27,17 +27,22 @@ import org.apache.wicket.behavior.AttributeAppender; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.panel.Panel; +import org.apache.wicket.model.AbstractReadOnlyModel; import org.apache.wicket.model.IModel; -import org.apache.wicket.model.Model; +import org.apache.wicket.model.PropertyModel; import org.hippoecm.frontend.model.JcrNodeModel; import org.hippoecm.frontend.model.properties.JcrPropertyModel; import org.hippoecm.frontend.model.properties.JcrPropertyValueModel; import org.hippoecm.frontend.session.UserSession; import org.hippoecm.frontend.widgets.TextFieldWidget; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; class HstReferenceEditor extends Panel { private static final long serialVersionUID = 1L; + public static final Logger log = LoggerFactory.getLogger(HstReferenceEditor.class); + protected static final String PROPERTY_HST_TEMPLATE = "hst:template"; protected static final String PROPERTY_HST_COMPONENTCONFIGURATIONID = "hst:componentconfigurationid"; protected static final String PROPERTY_HST_REFERENCECOMPONENT = "hst:referencecomponent"; @@ -55,46 +60,92 @@ private static final String PATH_PREFIX_WORKSPACE_CONTAINERS = "hst:workspace/hst:containers/"; private static final String PATH_HST_DEFAULT = "/hst:hst/hst:configurations/hst:default"; - HstReferenceEditor(String id, JcrPropertyModel propertyModel, JcrPropertyValueModel valueModel) { + HstReferenceEditor(String id, JcrPropertyModel propertyModel, JcrPropertyValueModel valueModel) { super(id); - try { - Node targetNode = getHstReferencedNode(propertyModel, valueModel); + setOutputMarkupId(true); - // link to referenced node - AjaxLink link = new AjaxLink("reference-link", new JcrNodeModel(targetNode)) { - private static final long serialVersionUID = 1L; + final ReferenceLink referenceLink = new ReferenceLink("reference-link", propertyModel, valueModel); + add(referenceLink); + // input field + TextFieldWidget editor = new TextFieldWidget("reference-edit", valueModel){ - @Override + @Override - public void onClick(AjaxRequestTarget requestTarget) { - EditorPlugin plugin = (EditorPlugin) findParent(EditorPlugin.class); - plugin.setDefaultModel((JcrNodeModel) getModel()); + protected void onUpdate(final AjaxRequestTarget target) { + referenceLink.load(); + target.add(HstReferenceEditor.this); - } - }; + } + }; - add(link); - addLinkTitle(link, targetNode, propertyModel.getProperty().getName()); - link.add(new Label("reference-link-text", new Model(targetNode.getPath()))); - - // input field - TextFieldWidget editor = new TextFieldWidget("reference-edit", valueModel); - editor.setSize("40"); - add(editor); + editor.setSize("40"); + add(editor); + } - } catch (PathNotFoundException e) { - TextFieldWidget editor = new TextFieldWidget("reference-edit", valueModel); - editor.setSize("40"); - add(editor); + private static class ReferenceLink extends AjaxLink { + private static final long serialVersionUID = 1L; - DisabledLink link = new DisabledLink("reference-link", new Model("(Reference might be inherited)")); - link.add(new AttributeAppender("style", new Model("color:blue"), " ")); - add(link); + @SuppressWarnings("unused") + private String linkText = null; + private JcrNodeModel linkModel = null; + private JcrPropertyModel propertyModel; + public ReferenceLink(final String id, final JcrPropertyModel propertyModel, final IModel valueModel) { + super(id, valueModel); + this.propertyModel = propertyModel; + + load(); + add(new Label("reference-link-text", new PropertyModel(this, "linkText"))); + add(new AttributeAppender("style", new AbstractReadOnlyModel() { + @Override + public Object getObject() { + return !isValidLink() ? "color:blue" : ""; + } + }, " ")); + } + + private void load() { + linkText = null; + linkModel = null; + + String propertyValue = getModelObject(); + try { + Node targetNode = getHstReferencedNode(propertyModel, propertyValue); + linkModel = new JcrNodeModel(targetNode); + linkText = targetNode.getPath(); + addLinkTitle(this, targetNode, propertyModel.getProperty().getName()); + } catch (PathNotFoundException e) { + linkText = "(Reference not found. Might be used in inheriting structure though.)"; - } catch (RepositoryException e) { + } catch (RepositoryException e) { - add(new Label("reference-edit", e.getClass().getName())); - add(new DisabledLink("reference-link", new Model(e.getMessage()))); + linkText = "Repository Exception: " + e.getMessage(); + log.error("Error loading target node by reference " + propertyValue); - } - } + } + } + @Override + protected boolean isLinkEnabled() { + return isValidLink(); + } + + private boolean isValidLink() { + return linkModel != null; + } + + @Override + public void onClick(final AjaxRequestTarget target) { + if (linkModel != null) { + findParent(EditorPlugin.class).setDefaultModel(linkModel); + } + } + + @Override + protected void onDetach() { + if (linkModel != null) { + linkModel.detach(); + } + super.onDetach(); + } + + } + - /** + /** * Inspect the target node to see if some info from that node can be added as link title * * @param link the link to add the title to @@ -102,7 +153,7 @@ * @param propertyName name of the property shown * @throws RepositoryException for any unexpected repository problem */ - private void addLinkTitle(final AjaxLink link, final Node targetNode, final String propertyName) throws RepositoryException { + private static void addLinkTitle(final AjaxLink link, final Node targetNode, final String propertyName) throws RepositoryException { String title = null; if (propertyName.equals(PROPERTY_HST_TEMPLATE)) { if(targetNode.hasProperty(PROPERTY_HST_RENDERPATH)) { @@ -127,12 +178,12 @@ * Get the hst configuration node that a hst property refers to * * @param propertyModel model representing the property - * @param valueModel model representing the value of the property - * @return the requested node or null + * @param propertyValue the value of the property + * @return the requested node + * @throws javax.jcr.PathNotFoundException when the referenced node cannot be found * @throws javax.jcr.RepositoryException for any unexpected repository problem */ - private Node getHstReferencedNode(final JcrPropertyModel propertyModel, final JcrPropertyValueModel valueModel) throws RepositoryException { - String propertyValue = valueModel.getValue().getString(); + private static Node getHstReferencedNode(final JcrPropertyModel propertyModel, final String propertyValue) throws RepositoryException { // first try: hst configuration nodes in the current hst:workspace or hst:configuration group Node currentHstConfiguration = propertyModel.getProperty().getParent(); @@ -187,7 +238,7 @@ * @return the requested node or null * @throws javax.jcr.RepositoryException for any unexpected repository problem */ - private Node getConfigurationNode(final Node hstConfiguration, String nodeName, final JcrPropertyModel propertyModel) throws RepositoryException { + private static Node getConfigurationNode(final Node hstConfiguration, String nodeName, final JcrPropertyModel propertyModel) throws RepositoryException { StringBuilder relPath = new StringBuilder(); addPathPrefix(relPath, propertyModel); relPath.append(nodeName); @@ -204,7 +255,7 @@ * @param propertyModel model representing the property * @throws RepositoryException for any unexpected repository problem */ - private void addPathPrefix(final StringBuilder relPath, final JcrPropertyModel propertyModel) throws RepositoryException { + private static void addPathPrefix(final StringBuilder relPath, final JcrPropertyModel propertyModel) throws RepositoryException { // references from hst:template properties are always to hst:templates nodes final boolean isHstTemplate = propertyModel.getProperty().getName().equals(PROPERTY_HST_TEMPLATE); if(isHstTemplate) { @@ -219,19 +270,4 @@ relPath.append(PATH_PREFIX_WORKSPACE_CONTAINERS); } } - - private class DisabledLink extends AjaxLink { - private static final long serialVersionUID = 1L; - - public DisabledLink(String id, IModel linktext) { - super(id); - setEnabled(false); - add(new Label("reference-link-text", linktext)); - } - - @Override - public void onClick(AjaxRequestTarget target) { - } - } - }