Author: dmaliarevich
Date: 2008-12-08 07:46:37 -0500 (Mon, 08 Dec 2008)
New Revision: 12467
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf.vpe.jsf/src/org/jboss/tools/jsf/vpe/jsf/template/JsfCommandLinkTemplate.java
trunk/jsf/plugins/org.jboss.tools.jsf.vpe.jsf/src/org/jboss/tools/jsf/vpe/jsf/template/JsfSubView.java
trunk/jsf/plugins/org.jboss.tools.jsf.vpe.jsf/src/org/jboss/tools/jsf/vpe/jsf/template/util/ComponentUtil.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-3066, checking if h:commandLink is placed inside
h:form or a4j:form was added.
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf.vpe.jsf/src/org/jboss/tools/jsf/vpe/jsf/template/JsfCommandLinkTemplate.java
===================================================================
---
trunk/jsf/plugins/org.jboss.tools.jsf.vpe.jsf/src/org/jboss/tools/jsf/vpe/jsf/template/JsfCommandLinkTemplate.java 2008-12-08
09:00:41 UTC (rev 12466)
+++
trunk/jsf/plugins/org.jboss.tools.jsf.vpe.jsf/src/org/jboss/tools/jsf/vpe/jsf/template/JsfCommandLinkTemplate.java 2008-12-08
12:46:37 UTC (rev 12467)
@@ -12,6 +12,7 @@
import org.jboss.tools.jsf.vpe.jsf.template.util.ComponentUtil;
import org.jboss.tools.vpe.editor.context.VpePageContext;
+import org.jboss.tools.vpe.editor.template.VpeChildrenInfo;
import org.jboss.tools.vpe.editor.template.VpeCreationData;
import org.jboss.tools.vpe.editor.util.HTML;
import org.mozilla.interfaces.nsIDOMDocument;
@@ -27,32 +28,69 @@
*/
public class JsfCommandLinkTemplate extends AbstractOutputJsfTemplate {
+ private static final String H_FORM = "h:form"; //$NON-NLS-1$
+ private static final String A4J_FORM = "a4j:form"; //$NON-NLS-1$
+ private static final String OUTSIDE_FORM_TEXT = ": This link is disabled as it
is not nested within a JSF form."; //$NON-NLS-1$
+
public VpeCreationData create(VpePageContext pageContext, Node sourceNode,
nsIDOMDocument visualDocument) {
Element element = (Element) sourceNode;
+ nsIDOMElement parentElement;
boolean disabled = ComponentUtil.string2boolean(ComponentUtil
.getAttribute(element, HTML.ATTR_DISABLED));
-
- nsIDOMElement parentElement;
- if (disabled)
+ String value = ComponentUtil.getAttribute(element, HTML.ATTR_VALUE);
+ boolean hasParentForm = hasParentForm(element);
+
+ if (!hasParentForm) {
parentElement = visualDocument.createElement(HTML.TAG_SPAN);
- else
+ } else if (disabled){
+ parentElement = visualDocument.createElement(HTML.TAG_SPAN);
+ } else {
parentElement = visualDocument.createElement(HTML.TAG_A);
+ }
- VpeCreationData creationData = new VpeCreationData(parentElement);
-
// copy attributes
copyOutputJsfAttributes(parentElement, element);
- processOutputAttribute(pageContext, visualDocument, element,
- parentElement, creationData);
-
+ VpeCreationData creationData;
+ if (!hasParentForm) {
+ nsIDOMElement topSpan = visualDocument.createElement(HTML.TAG_SPAN);
+ nsIDOMElement noteSpan = visualDocument.createElement(HTML.TAG_SPAN);
+ noteSpan.appendChild(visualDocument.createTextNode(OUTSIDE_FORM_TEXT));
+ topSpan.appendChild(parentElement);
+ topSpan.appendChild(noteSpan);
+ creationData = new VpeCreationData(topSpan);
+ } else {
+ creationData = new VpeCreationData(parentElement);
+ }
+
+ VpeChildrenInfo linkInfo = new VpeChildrenInfo(parentElement);
+ creationData.addChildrenInfo(linkInfo);
+
+ for (Node child : ComponentUtil.getChildren(element)) {
+ linkInfo.addSourceChild(child);
+ }
+ if (ComponentUtil.isNotBlank(value)) {
+ parentElement.appendChild(visualDocument.createTextNode(value));
+ }
return creationData;
}
+ private boolean hasParentForm(Element sourceElement) {
+ Node parent = sourceElement.getParentNode();
+ while (parent != null && parent instanceof Element &&
parent.getNodeName() != null) {
+ if (parent.getNodeName().indexOf(H_FORM) >= 0
+ || parent.getNodeName().indexOf(A4J_FORM) >= 0) {
+ return true;
+ }
+ parent = parent.getParentNode();
+ }
+ return false;
+ }
+
@Override
public boolean isRecreateAtAttrChange(VpePageContext pageContext,
Element sourceElement, nsIDOMDocument visualDocument,
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf.vpe.jsf/src/org/jboss/tools/jsf/vpe/jsf/template/JsfSubView.java
===================================================================
---
trunk/jsf/plugins/org.jboss.tools.jsf.vpe.jsf/src/org/jboss/tools/jsf/vpe/jsf/template/JsfSubView.java 2008-12-08
09:00:41 UTC (rev 12466)
+++
trunk/jsf/plugins/org.jboss.tools.jsf.vpe.jsf/src/org/jboss/tools/jsf/vpe/jsf/template/JsfSubView.java 2008-12-08
12:46:37 UTC (rev 12467)
@@ -13,6 +13,7 @@
import java.util.ArrayList;
import java.util.List;
+import org.jboss.tools.jsf.vpe.jsf.template.util.ComponentUtil;
import org.jboss.tools.vpe.editor.context.VpePageContext;
import org.jboss.tools.vpe.editor.template.VpeAbstractTemplate;
import org.jboss.tools.vpe.editor.template.VpeChildrenInfo;
@@ -43,30 +44,13 @@
VpeChildrenInfo divInfo = new VpeChildrenInfo(div);
creationData.addChildrenInfo(divInfo);
- for (Node child : getChildren(sourceElement)) {
+ for (Node child : ComponentUtil.getChildren(sourceElement)) {
divInfo.addSourceChild(child);
}
return creationData;
}
- /**
- * Gets the children.
- *
- * @param sourceElement the source element
- *
- * @return the children
- */
- public static List<Node> getChildren(Element sourceElement) {
- ArrayList<Node> children = new ArrayList<Node>();
- NodeList nodeList = sourceElement.getChildNodes();
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node child = nodeList.item(i);
- children.add(child);
- }
- return children;
- }
-
/* (non-Javadoc)
* @see
org.jboss.tools.vpe.editor.template.VpeAbstractTemplate#isRecreateAtAttrChange(org.jboss.tools.vpe.editor.context.VpePageContext,
org.w3c.dom.Element, org.mozilla.interfaces.nsIDOMDocument,
org.mozilla.interfaces.nsIDOMElement, java.lang.Object, java.lang.String,
java.lang.String)
*/
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf.vpe.jsf/src/org/jboss/tools/jsf/vpe/jsf/template/util/ComponentUtil.java
===================================================================
---
trunk/jsf/plugins/org.jboss.tools.jsf.vpe.jsf/src/org/jboss/tools/jsf/vpe/jsf/template/util/ComponentUtil.java 2008-12-08
09:00:41 UTC (rev 12466)
+++
trunk/jsf/plugins/org.jboss.tools.jsf.vpe.jsf/src/org/jboss/tools/jsf/vpe/jsf/template/util/ComponentUtil.java 2008-12-08
12:46:37 UTC (rev 12467)
@@ -10,12 +10,17 @@
******************************************************************************/
package org.jboss.tools.jsf.vpe.jsf.template.util;
+import java.util.ArrayList;
+import java.util.List;
+
import org.jboss.tools.vpe.editor.bundle.BundleMap;
import org.jboss.tools.vpe.editor.context.VpePageContext;
import org.jboss.tools.vpe.editor.util.HTML;
import org.mozilla.interfaces.nsIDOMElement;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
/**
* Utilities for jsf templates
@@ -25,115 +30,156 @@
*/
public class ComponentUtil {
- /**
- * Returns value of attribute.
- *
- * @param sourceElement
- * @param attributeName
- * @return
- */
- public static String getAttribute(Element sourceElement,
- String attributeName) {
- String attribute = sourceElement.getAttribute(attributeName);
- if (attribute == null) {
- attribute = "";
- }
- return attribute;
+ /**
+ * Returns value of attribute.
+ *
+ * @param sourceElement
+ * @param attributeName
+ * @return
+ */
+ public static String getAttribute(Element sourceElement,
+ String attributeName) {
+ String attribute = sourceElement.getAttribute(attributeName);
+ if (attribute == null) {
+ attribute = "";
}
+ return attribute;
+ }
- /**
- * Returns value of attribute.
- *
- * @param sourceElement
- * @param attributeName
- * @return
- */
- public static String getAttribute(nsIDOMElement sourceElement,
- String attributeName) {
- String attribute = sourceElement.getAttribute(attributeName);
- if (attribute == null) {
- attribute = "";
- }
- return attribute;
+ /**
+ * Returns value of attribute.
+ *
+ * @param sourceElement
+ * @param attributeName
+ * @return
+ */
+ public static String getAttribute(nsIDOMElement sourceElement,
+ String attributeName) {
+ String attribute = sourceElement.getAttribute(attributeName);
+ if (attribute == null) {
+ attribute = "";
}
-
- /**
- * Returns {@code true} if the {@code element} has attribute
- * {@code disabled} and its value is {@link #string2boolean(String) true}
- */
- public static boolean isDisabled(Element element) {
- return ComponentUtil.string2boolean(
- ComponentUtil.getAttribute(element, JSF.ATTR_DISABLED));
- }
-
- /**
- * Sets attribute {@code "disabled"} of the {@code element} to {@code
"disabled"}
- * if the parameter {@code disabled} is {@code true},
- * <br/>
- * otherwise removes attribute {@code "disabled"} from the {@code element}
- * if it is present.
- */
- public static void setDisabled(nsIDOMElement element, boolean disabled) {
- if (disabled) {
- element.setAttribute(HTML.ATTR_DISABLED, HTML.ATTR_DISABLED);
- } else {
- element.removeAttribute(HTML.ATTR_DISABLED);
- }
- }
-
- /**
- * Copies {@code "disabled"} attribute from JSF {@code sourceElement} to
- * HTML {@code targetElement}.
- *
- * @see #isDisabled(Element)
- * @see #setDisabled(nsIDOMElement, boolean)
- */
- public static void copyDisabled(Element sourceElement, nsIDOMElement targetElement) {
- boolean disabled = ComponentUtil.isDisabled(sourceElement);
- ComponentUtil.setDisabled(targetElement, disabled);
- }
+ return attribute;
+ }
- /**
- * Parses string value retrieved from sourceElement.getAttribure(..) method
- * to its boolean value.
- * <p>
- * <code>true</code> is returned only if it specified explicitly,
- * otherwise <code>false</code> is returned.
- *
- * @param str
- * the string to parse
- * @return boolean value from string
- */
- public static boolean string2boolean(String str) {
- return Boolean.parseBoolean(str);
+ /**
+ * Returns {@code true} if the {@code element} has attribute {@code
+ * disabled} and its value is {@link #string2boolean(String) true}
+ */
+ public static boolean isDisabled(Element element) {
+ return ComponentUtil.string2boolean(ComponentUtil.getAttribute(element,
+ JSF.ATTR_DISABLED));
+ }
+
+ /**
+ * Sets attribute {@code "disabled"} of the {@code element} to {@code
+ * "disabled"} if the parameter {@code disabled} is {@code true},
<br/>
+ * otherwise removes attribute {@code "disabled"} from the {@code element}
+ * if it is present.
+ */
+ public static void setDisabled(nsIDOMElement element, boolean disabled) {
+ if (disabled) {
+ element.setAttribute(HTML.ATTR_DISABLED, HTML.ATTR_DISABLED);
+ } else {
+ element.removeAttribute(HTML.ATTR_DISABLED);
}
+ }
- /**
- * get bundle
- *
- * @param pageContext
- * @param attr
- * @return
- */
- public static String getBundleValue(VpePageContext pageContext, Attr attr) {
+ /**
+ * Copies {@code "disabled"} attribute from JSF {@code sourceElement} to
+ * HTML {@code targetElement}.
+ *
+ * @see #isDisabled(Element)
+ * @see #setDisabled(nsIDOMElement, boolean)
+ */
+ public static void copyDisabled(Element sourceElement,
+ nsIDOMElement targetElement) {
+ boolean disabled = ComponentUtil.isDisabled(sourceElement);
+ ComponentUtil.setDisabled(targetElement, disabled);
+ }
- return getBundleValue(pageContext, attr.getNodeValue());
+ /**
+ * Parses string value retrieved from sourceElement.getAttribure(..) method
+ * to its boolean value.
+ * <p>
+ * <code>true</code> is returned only if it specified explicitly,
otherwise
+ * <code>false</code> is returned.
+ *
+ * @param str
+ * the string to parse
+ * @return boolean value from string
+ */
+ public static boolean string2boolean(String str) {
+ return Boolean.parseBoolean(str);
+ }
- }
+ /**
+ * get bundle
+ *
+ * @param pageContext
+ * @param attr
+ * @return
+ */
+ public static String getBundleValue(VpePageContext pageContext, Attr attr) {
- /**
- *
- * @param pageContext
- * @param value
- * @param offfset
- * @return
- */
- public static String getBundleValue(VpePageContext pageContext,
- String value) {
+ return getBundleValue(pageContext, attr.getNodeValue());
- BundleMap bundle = pageContext.getBundle();
+ }
- return bundle.getBundleValue(value);
+ /**
+ *
+ * @param pageContext
+ * @param value
+ * @param offfset
+ * @return
+ */
+ public static String getBundleValue(VpePageContext pageContext, String value) {
+ BundleMap bundle = pageContext.getBundle();
+
+ return bundle.getBundleValue(value);
+
+ }
+
+ /**
+ * Gets the children.
+ *
+ * @param sourceElement
+ * the source element
+ *
+ * @return the children
+ */
+ public static List<Node> getChildren(Element sourceElement) {
+ ArrayList<Node> children = new ArrayList<Node>();
+ NodeList nodeList = sourceElement.getChildNodes();
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ Node child = nodeList.item(i);
+ children.add(child);
}
+ return children;
+ }
+
+ /**
+ * Checks if is blank.
+ *
+ * @param value
+ * the value
+ *
+ * @return true, if is blank
+ */
+ public static boolean isBlank(String value) {
+ return ((value == null) || (value.trim().length() == 0));
+ }
+
+ /**
+ * Checks if is not blank.
+ *
+ * @param value
+ * the value
+ *
+ * @return true, if is not blank
+ */
+ public static boolean isNotBlank(String value) {
+ return !isBlank(value);
+ }
}