You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2010/03/14 01:35:01 UTC
svn commit: r922696 - in /click/trunk/click: build/ documentation/docs/
framework/src/org/apache/click/control/
framework/test/org/apache/click/control/
Author: sabob
Date: Sun Mar 14 00:35:00 2010
New Revision: 922696
URL: http://svn.apache.org/viewvc?rev=922696&view=rev
Log:
Added methods to Field for styling labels and parent elements. CLK-595
Added:
click/trunk/click/framework/test/org/apache/click/control/FieldTest.java
Modified:
click/trunk/click/build/checkstyle-suppressions.xml
click/trunk/click/documentation/docs/roadmap-changes.html
click/trunk/click/framework/src/org/apache/click/control/Field.java
click/trunk/click/framework/src/org/apache/click/control/FieldSet.java
click/trunk/click/framework/src/org/apache/click/control/Form.java
Modified: click/trunk/click/build/checkstyle-suppressions.xml
URL: http://svn.apache.org/viewvc/click/trunk/click/build/checkstyle-suppressions.xml?rev=922696&r1=922695&r2=922696&view=diff
==============================================================================
--- click/trunk/click/build/checkstyle-suppressions.xml (original)
+++ click/trunk/click/build/checkstyle-suppressions.xml Sun Mar 14 00:35:00 2010
@@ -114,11 +114,11 @@
<suppress checks="MethodLength"
files="Form.java"
- lines="2250, 2342"/>
+ lines="2313"/>
<suppress checks="MethodLength"
files="FieldSet.java"
- lines="856"/>
+ lines="915"/>
<!-- Static Variable Name -->
Modified: click/trunk/click/documentation/docs/roadmap-changes.html
URL: http://svn.apache.org/viewvc/click/trunk/click/documentation/docs/roadmap-changes.html?rev=922696&r1=922695&r2=922696&view=diff
==============================================================================
--- click/trunk/click/documentation/docs/roadmap-changes.html (original)
+++ click/trunk/click/documentation/docs/roadmap-changes.html Sun Mar 14 00:35:00 2010
@@ -127,6 +127,19 @@ includes improved Ajax support and @Bind
[<a target="_blank" href="https://issues.apache.org/jira/browse/CLK-638">CLK-638</a>].
</li>
<li class="change">
+ Added methods to Fields for styling their labels. See new methods
+ <a href="click-api/org/apache/click/control/Field.html#setLabelStyle(java.lang.String)">Field.setLabelStyle(String)</a>
+ and <a href="click-api/org/apache/click/control/Field.html#setLabelStyleClass(java.lang.String)">Field.setLabelStyleClass(String)</a>
+ [<a target="_blank" href="https://issues.apache.org/jira/browse/CLK-595">CLK-595</a>].
+ </li>
+ <li class="change">
+ Added methods to Fields for providing styling hints to their
+ containing elements. See new methods
+ <a href="click-api/org/apache/click/control/Field.html#setParentStyleHint(java.lang.String)">Field.setParentStyleHint(String)</a>
+ and <a href="click-api/org/apache/click/control/Field.html#setParentStyleClassHint(java.lang.String)">Field.setParentStyleClassHint(String)</a>
+ [<a target="_blank" href="https://issues.apache.org/jira/browse/CLK-595">CLK-595</a>].
+ </li>
+ <li class="change">
Added new Context methods
<a href="click-api/org/apache/click/Context.html#hasRequestParameter(java.lang.String)">Context.hasRequestParameter(String)</a>
and <a href="click-api/org/apache/click/Context.html#hasRequestAttribute(java.lang.String)">Context.hasRequestAttribute(String)</a>.
Modified: click/trunk/click/framework/src/org/apache/click/control/Field.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/control/Field.java?rev=922696&r1=922695&r2=922696&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/control/Field.java (original)
+++ click/trunk/click/framework/src/org/apache/click/control/Field.java Sun Mar 14 00:35:00 2010
@@ -205,6 +205,18 @@ public abstract class Field extends Abst
/** The Field label. */
protected String label;
+ /** The field label "style" attribute value. */
+ protected String labelStyle;
+
+ /** The field label "class" attribute value. */
+ protected String labelStyleClass;
+
+ /** The field's parent element "style" attribute hint. */
+ protected String parentStyleHint;
+
+ /** The field's parent element "class" attribute hint. */
+ protected String parentStyleClassHint;
+
/** The Field is readonly flag. */
protected boolean readonly;
@@ -580,6 +592,112 @@ public abstract class Field extends Abst
}
/**
+ * Return the field label "style" attribute value.
+ *
+ * @see #setLabelStyle(java.lang.String)
+ *
+ * @return the field label "style" attribute value
+ */
+ public String getLabelStyle() {
+ return labelStyle;
+ }
+
+ /**
+ * Set the field label "style" attribute value.
+ * <p/>
+ * <b>Please note</b>: the label is rendered by the containing Form
+ * or container, not the field itself. It is up to the parent Form
+ * (or container) on how to apply the label style.
+ * <p/>
+ * <pre class="prettyprint">
+ * nameField.setLabelStyle("color: green; font-weight: bold");</pre>
+ *
+ * @param value the field label "style" attribute value
+ */
+ public void setLabelStyle(String value) {
+ this.labelStyle = value;
+ }
+
+ /**
+ * Return the field label "class" attribute value.
+ *
+ * @see #setLabelStyleClass(java.lang.String)
+ *
+ * @return the field label "class" attribute value
+ */
+ public String getLabelStyleClass() {
+ return labelStyleClass;
+ }
+
+ /**
+ * Set the field label "class" attribute value.
+ * <p/>
+ * <b>Please note</b>: the label is rendered by the containing Form
+ * or container, not the field itself. It is up to the parent Form
+ * (or container) on how to apply the label style class.
+ *
+ * @param value the field label "class" attribute value
+ */
+ public void setLabelStyleClass(String value) {
+ this.labelStyleClass = value;
+ }
+
+ /**
+ * Return the field's parent "style" attribute hint.
+ *
+ * @see #setParentStyleHint(java.lang.String)
+ *
+ * @return the field's parent "style" attribute hint
+ */
+ public String getParentStyleHint() {
+ return parentStyleHint;
+ }
+
+ /**
+ * Set the field's parent "style" attribute hint.
+ * <p/>
+ * <pre class="prettyprint">
+ * nameField.setParentStyleHint("margin-bottom; 10px");</pre>
+ *
+ * <b>Please note:</b>The field's parent style provides a hint to Forms
+ * (or other containers) what style to render, but it is up to the
+ * Form (or container) implementation how the style will be applied.
+ * For example, Form will render the parent style on the table cells
+ * containing the field and label.
+ *
+ * @param styleHint the field's parent "style" attribute hint
+ */
+ public void setParentStyleHint(String styleHint) {
+ this.parentStyleHint = styleHint;
+ }
+
+ /**
+ * Return the field's parent "class" attribute hint.
+ *
+ * @see #setParentStyleClassHint(java.lang.String)
+ *
+ * @return the field's parent "class" attribute hint
+ */
+ public String getParentStyleClassHint() {
+ return parentStyleClassHint;
+ }
+
+ /**
+ * Set the field's parent "class" attribute hint.
+ * <p/>
+ * <b>Please note:</b>The parent style class provides a hint to Forms
+ * (or other containers) which CSS class to render, but it is up to the
+ * Form (or container) implementation how the style class will be applied.
+ * For example, Form will render the parent style class on the table cells
+ * containing the field and label.
+ *
+ * @param styleClassHint the field parent "class" attribute hint
+ */
+ public void setParentStyleClassHint(String styleClassHint) {
+ this.parentStyleClassHint = styleClassHint;
+ }
+
+ /**
* The callback listener will only be called during processing if the Field
* value is valid. If the field has validation errors the listener will not
* be called.
Modified: click/trunk/click/framework/src/org/apache/click/control/FieldSet.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/control/FieldSet.java?rev=922696&r1=922695&r2=922696&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/control/FieldSet.java (original)
+++ click/trunk/click/framework/src/org/apache/click/control/FieldSet.java Sun Mar 14 00:35:00 2010
@@ -952,10 +952,19 @@ public class FieldSet extends Field impl
if (control instanceof Label) {
Label label = (Label) control;
- buffer.append("<td class=\"fields\" align=\"");
+ buffer.append("<td align=\"");
buffer.append(getForm().getLabelAlign());
+ buffer.append("\" class=\"fields");
+
+ String cellStyleClass = label.getParentStyleClassHint();
+ if (cellStyleClass != null) {
+ buffer.append(" ");
+ buffer.append(cellStyleClass);
+ }
buffer.append("\"");
+ buffer.appendAttribute("style", label.getParentStyleHint());
+
if (width != null) {
int colspan = (width.intValue() * 2);
buffer.appendAttribute("colspan", colspan);
@@ -986,42 +995,84 @@ public class FieldSet extends Field impl
Form form = getForm();
// Write out label
if (Form.POSITION_LEFT.equals(form.getLabelsPosition())) {
- buffer.append("<td class=\"fields\"");
+ buffer.append("<td class=\"fields");
+ String cellStyleClass = field.getParentStyleClassHint();
+ if (cellStyleClass != null) {
+ buffer.append(" ");
+ buffer.append(cellStyleClass);
+ }
+ buffer.append("\"");
buffer.appendAttribute("align", form.getLabelAlign());
- buffer.appendAttribute("style", form.getLabelStyle());
+ String cellStyle = field.getParentStyleHint();
+ if (cellStyle == null) {
+ cellStyle = form.getLabelStyle();
+ }
+ buffer.appendAttribute("style", cellStyle);
buffer.append(">");
} else {
- buffer.append("<td class=\"fields\" valign=\"top\"");
- buffer.appendAttribute("style", form.getLabelStyle());
+ buffer.append("<td valign=\"top\" class=\"fields");
+ String cellStyleClass = field.getParentStyleClassHint();
+ if (cellStyleClass != null) {
+ buffer.append(" ");
+ buffer.append(cellStyleClass);
+ }
+ buffer.append("\"");
+ String cellStyle = field.getParentStyleHint();
+ if (cellStyle == null) {
+ cellStyle = form.getLabelStyle();
+ }
+ buffer.appendAttribute("style", cellStyle);
buffer.append(">");
}
- if (field.isRequired()) {
- buffer.append(form.getMessage("label-required-prefix"));
- } else {
- buffer.append(form.getMessage("label-not-required-prefix"));
- }
- buffer.elementStart("label");
- buffer.appendAttribute("for", field.getId());
- if (field.isDisabled()) {
- buffer.appendAttributeDisabled();
- }
- if (field.getError() != null) {
- buffer.appendAttribute("class", "error");
- }
- buffer.closeTag();
- buffer.append(field.getLabel());
- buffer.elementEnd("label");
- if (field.isRequired()) {
- buffer.append(form.getMessage("label-required-suffix"));
- } else {
- buffer.append(form.getMessage("label-not-required-suffix"));
+ // Store the field id and label (the values could be null)
+ String fieldId = field.getId();
+ String fieldLabel = field.getLabel();
+
+ // Only render a label if the fieldId and fieldLabel is set
+ if (fieldId != null && fieldLabel != null) {
+ if (field.isRequired()) {
+ buffer.append(form.getMessage("label-required-prefix"));
+ } else {
+ buffer.append(form.getMessage("label-not-required-prefix"));
+ }
+ buffer.elementStart("label");
+ buffer.appendAttribute("for", field.getId());
+ buffer.appendAttribute("style", field.getLabelStyle());
+ if (field.isDisabled()) {
+ buffer.appendAttributeDisabled();
+ }
+ String cellClass = field.getLabelStyleClass();
+ if (field.getError() == null) {
+ buffer.appendAttribute("class", cellClass);
+ } else {
+ buffer.append(" class=\"error");
+ if (cellClass != null) {
+ buffer.append(" ");
+ buffer.append(cellClass);
+ }
+ buffer.append("\"");
+ }
+ buffer.closeTag();
+ buffer.append(field.getLabel());
+ buffer.elementEnd("label");
+ if (field.isRequired()) {
+ buffer.append(form.getMessage("label-required-suffix"));
+ } else {
+ buffer.append(form.getMessage("label-not-required-suffix"));
+ }
}
if (Form.POSITION_LEFT.equals(form.getLabelsPosition())) {
buffer.append("</td>\n");
- buffer.append("<td align=\"left\"");
- buffer.appendAttribute("style", form.getFieldStyle());
+ buffer.append("<td");
+ buffer.appendAttribute("class", field.getParentStyleClassHint());
+ buffer.appendAttribute("align", "left");
+ String cellStyle = field.getParentStyleHint();
+ if (cellStyle == null) {
+ cellStyle = form.getFieldStyle();
+ }
+ buffer.appendAttribute("style", cellStyle);
if (width != null) {
int colspan = (width.intValue() * 2) - 1;
Modified: click/trunk/click/framework/src/org/apache/click/control/Form.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/control/Form.java?rev=922696&r1=922695&r2=922696&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/control/Form.java (original)
+++ click/trunk/click/framework/src/org/apache/click/control/Form.java Sun Mar 14 00:35:00 2010
@@ -1504,7 +1504,11 @@ public class Form extends AbstractContai
}
/**
- * Set the field <td> "style" attribute value.
+ * Set the field <td> "style" attribute value. Fields can override
+ * this value by providing a {@link Field#setParentStyleHint(java.lang.String)}.
+ *
+ * @see Field#setParentStyleHint(java.lang.String)
+ * @see Field#setParentStyleClassHint(java.lang.String)
*
* @param value the field <td> "style" attribute value
*/
@@ -2337,7 +2341,16 @@ public class Form extends AbstractContai
}
if (control instanceof FieldSet) {
- buffer.append("<td class=\"fields\"");
+ FieldSet fieldSet = (FieldSet) control;
+ buffer.append("<td class=\"fields");
+ String cellStyleClass = fieldSet.getParentStyleClassHint();
+ if (cellStyleClass != null) {
+ buffer.append(" ");
+ buffer.append(cellStyleClass);
+ }
+ buffer.append("\"");
+
+ buffer.appendAttribute("style", fieldSet.getParentStyleHint());
if (width != null) {
int colspan = (width.intValue() * 2);
@@ -2352,10 +2365,19 @@ public class Form extends AbstractContai
} else if (control instanceof Label) {
Label label = (Label) control;
- buffer.append("<td class=\"fields\" align=\"");
+ buffer.append("<td align=\"");
buffer.append(getLabelAlign());
+ buffer.append("\" class=\"fields");
+
+ String cellStyleClass = label.getParentStyleClassHint();
+ if (cellStyleClass != null) {
+ buffer.append(" ");
+ buffer.append(cellStyleClass);
+ }
buffer.append("\"");
+ buffer.appendAttribute("style", label.getParentStyleHint());
+
if (width != null) {
int colspan = (width.intValue() * 2);
buffer.appendAttribute("colspan", colspan);
@@ -2385,13 +2407,33 @@ public class Form extends AbstractContai
Field field = (Field) control;
// Write out label
if (POSITION_LEFT.equals(getLabelsPosition())) {
- buffer.append("<td class=\"fields\"");
+ buffer.append("<td class=\"fields");
+ String cellStyleClass = field.getParentStyleClassHint();
+ if (cellStyleClass != null) {
+ buffer.append(" ");
+ buffer.append(cellStyleClass);
+ }
+ buffer.append("\"");
buffer.appendAttribute("align", getLabelAlign());
- buffer.appendAttribute("style", getLabelStyle());
+ String cellStyle = field.getParentStyleHint();
+ if (cellStyle == null) {
+ cellStyle = getLabelStyle();
+ }
+ buffer.appendAttribute("style", cellStyle);
buffer.append(">");
} else {
- buffer.append("<td class=\"fields\" valign=\"top\"");
- buffer.appendAttribute("style", getLabelStyle());
+ buffer.append("<td valign=\"top\" class=\"fields");
+ String cellStyleClass = field.getParentStyleClassHint();
+ if (cellStyleClass != null) {
+ buffer.append(" ");
+ buffer.append(cellStyleClass);
+ }
+ buffer.append("\"");
+ String cellStyle = field.getParentStyleHint();
+ if (cellStyle == null) {
+ cellStyle = getLabelStyle();
+ }
+ buffer.appendAttribute("style", cellStyle);
buffer.append(">");
}
@@ -2408,11 +2450,20 @@ public class Form extends AbstractContai
}
buffer.elementStart("label");
buffer.appendAttribute("for", fieldId);
+ buffer.appendAttribute("style", field.getLabelStyle());
if (field.isDisabled()) {
buffer.appendAttributeDisabled();
}
- if (field.getError() != null) {
- buffer.appendAttribute("class", "error");
+ String cellClass = field.getLabelStyleClass();
+ if (field.getError() == null) {
+ buffer.appendAttribute("class", cellClass);
+ } else {
+ buffer.append(" class=\"error");
+ if (cellClass != null) {
+ buffer.append(" ");
+ buffer.append(cellClass);
+ }
+ buffer.append("\"");
}
buffer.closeTag();
buffer.append(fieldLabel);
@@ -2426,8 +2477,15 @@ public class Form extends AbstractContai
if (POSITION_LEFT.equals(getLabelsPosition())) {
buffer.append("</td>\n");
- buffer.append("<td align=\"left\"");
- buffer.appendAttribute("style", getFieldStyle());
+ buffer.append("<td");
+ buffer.appendAttribute("class", field.getParentStyleClassHint());
+ buffer.appendAttribute("align", "left");
+
+ String cellStyle = field.getParentStyleHint();
+ if (cellStyle == null) {
+ cellStyle = getFieldStyle();
+ }
+ buffer.appendAttribute("style", cellStyle);
if (width != null) {
int colspan = (width.intValue() * 2) - 1;
Added: click/trunk/click/framework/test/org/apache/click/control/FieldTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/control/FieldTest.java?rev=922696&view=auto
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/control/FieldTest.java (added)
+++ click/trunk/click/framework/test/org/apache/click/control/FieldTest.java Sun Mar 14 00:35:00 2010
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.click.control;
+
+import junit.framework.TestCase;
+import org.apache.click.MockContext;
+
+/**
+ * Test Field behavior.
+ */
+public class FieldTest extends TestCase {
+
+ /**
+ * Check that Field label style is rendered by Form and FieldSet.
+ *
+ * CLK-595
+ */
+ public void testLabelStyle() {
+ MockContext.initContext();
+
+ // Check that Form renders the field label style
+ Form form = new Form("form");
+ Field field = new TextField("field");
+ form.add(field);
+
+ field.setLabelStyle("color: green");
+ assertTrue(form.toString().contains("<label for=\"form_field\" style=\"color: green\">"));
+
+ // Check that FieldSet renders the field label style
+ form = new Form("form");
+ FieldSet fs = new FieldSet("fs");
+ form.add(fs);
+ field = new TextField("field");
+ fs.add(field);
+
+ field.setLabelStyle("color: green");
+ assertTrue(fs.toString().contains("<label for=\"form_field\" style=\"color: green\">"));
+ }
+
+ /**
+ * Check that Field label style class is rendered by Form and FieldSet.
+ *
+ * CLK-595
+ */
+ public void testLabelStyleClass() {
+ MockContext.initContext();
+
+ // Check that Form renders the field label style
+ Form form = new Form("form");
+ Field field = new TextField("field");
+ form.add(field);
+
+ field.setLabelStyleClass("autumn");
+ assertTrue(form.toString().contains("<label for=\"form_field\" class=\"autumn\">"));
+
+ // Check that FieldSet renders the field label style
+ form = new Form("form");
+ // FieldStyle value should be overridden by the parentStyleHint below
+ form.setFieldStyle("font-weight:bold");
+ FieldSet fs = new FieldSet("fs");
+ form.add(fs);
+ field = new TextField("field");
+ fs.add(field);
+
+ field.setLabelStyleClass("autumn");
+ assertTrue(fs.toString().contains("<label for=\"form_field\" class=\"autumn\">"));
+ }
+
+ /**
+ * Check that Field parent style hint is rendered by Form and FieldSet.
+ *
+ * CLK-595
+ */
+ public void testParentStyleHint() {
+ MockContext.initContext();
+
+ // Check that Form renders the field label style
+ Form form = new Form("form");
+ Field field = new TextField("field");
+ form.add(field);
+
+ field.setParentStyleHint("color: green");
+ // Check that style hint is rendered on the label and field cells
+ assertTrue(form.toString().contains("<td class=\"fields\" align=\"left\" style=\"color: green\"><label"));
+ assertTrue(form.toString().contains("<td align=\"left\" style=\"color: green\"><input"));
+
+ // Check that FieldSet renders the field label style
+ form = new Form("form");
+ // FieldStyle value should be overridden by the parentStyleHint below
+ form.setFieldStyle("font-weight:bold");
+ FieldSet fs = new FieldSet("fs");
+ form.add(fs);
+ field = new TextField("field");
+ fs.add(field);
+
+ field.setParentStyleHint("color: green");
+ // Check that style hint is rendered on the label and field cells
+ assertTrue(fs.toString().contains("<td class=\"fields\" align=\"left\" style=\"color: green\"><label"));
+ assertTrue(fs.toString().contains("<td align=\"left\" style=\"color: green\"><input"));
+ }
+
+ /**
+ * Check that Field parent style class hint is rendered by Form and FieldSet.
+ *
+ * CLK-595
+ */
+ public void testParentStyleClassHint() {
+ MockContext.initContext();
+
+ // Check that Form renders the field label style
+ Form form = new Form("form");
+ Field field = new TextField("field");
+ form.add(field);
+
+ field.setParentStyleClassHint("autumn");
+ // Check that style class hint is rendered on the label and field cells
+ assertTrue(form.toString().contains("<td class=\"fields autumn\" align=\"left\"><label"));
+ assertTrue(form.toString().contains("<td class=\"autumn\" align=\"left\"><input"));
+
+ // Check that FieldSet renders the field label style
+ form = new Form("form");
+ FieldSet fs = new FieldSet("fs");
+ form.add(fs);
+ field = new TextField("field");
+ fs.add(field);
+
+ field.setParentStyleClassHint("autumn");
+ // Check that style class hint is rendered on the label and field cells
+ assertTrue(fs.toString().contains("<td class=\"fields autumn\" align=\"left\"><label"));
+ assertTrue(fs.toString().contains("<td class=\"autumn\" align=\"left\"><input"));
+ }
+}