org.apache.click.extras.control
Class ColorPicker

java.lang.Object
  extended by org.apache.click.control.AbstractControl
      extended by org.apache.click.control.Field
          extended by org.apache.click.extras.control.ColorPicker
All Implemented Interfaces:
Serializable, Control, Stateful

public class ColorPicker
extends Field

Provides a ColorPicker control:   <input type='text'><img>.

Color Field
The ColorPicker control provides a popup DHTML color picker and a text input where users can enter the color in hex format. The text input field can be turned off by setting setShowTextField(boolean) to false (default is true).

The ColorPicker control will validate whether the entered color is present (if required) and that it is a valid hex color either in 3-digit presentation (ie #EEE) or 6-digit presentation (ie #EEEEEE). If the color is not required the color-picker popup will show a button for 'no-color' on the top-left.

CSS and JavaScript resources

ColorPicker depends on the Prototype JavaScript library.

The ColorPicker control makes use of the following resources (which Click automatically deploys to the application directories, /click/colorpicker and /click/prototype):

To import these ColorPicker files simply reference the variables $headElements and $jsElements in the page template. For example:
 <html>
 <head>
 $headElements
 </head>
 <body>

 $form

 $jsElements
 </body>
 </html> 

Credits

The color picker popup is based on JS script code from www.dhtmlgoodies.com.

See Also:
Serialized Form

Field Summary
protected  boolean showTextField
          The show text field option for entering a color hex value.
protected  int size
          The text field size attribute.
 
Fields inherited from class org.apache.click.control.Field
disabled, error, focus, form, help, label, labelStyle, labelStyleClass, parentStyleClassHint, parentStyleHint, readonly, required, tabindex, title, trim, validate, value
 
Fields inherited from class org.apache.click.control.AbstractControl
actionListener, attributes, behaviors, headElements, listener, listenerMethod, messages, name, parent, styles
 
Fields inherited from interface org.apache.click.Control
CONTROL_MESSAGES
 
Constructor Summary
ColorPicker()
          Create a ColorPicker with no name defined.
ColorPicker(String name)
          Construct a ColorPicker with the given name.
ColorPicker(String name, boolean required)
          Construct a ColorPicker with the given name and required status.
ColorPicker(String name, boolean required, boolean showTextField)
          Constructs a ColorPicker with the given name, required status and display text field option.
ColorPicker(String name, String label)
          Construct the ColorPicker with the given name and label.
 
Method Summary
 int getControlSizeEst()
           
 List<Element> getHeadElements()
          Return the ColorPicker HTML HEAD elements for the following resources:

click/colorpicker/colorpicker.css click/prototype/prototype.js click/colorpicker/colorpicker.js

 boolean getShowTextField()
          Whether the TextField to enter the color hex number should be shown or not.
 int getSize()
          Return the field size.
 String getValidationJavaScript()
          Return the field JavaScript client side validation function.
 void render(HtmlStringBuffer buffer)
          Render the HTML representation of the ColorPicker.
protected  void renderTemplate(HtmlStringBuffer buffer, Map<String,?> model)
          Render a Velocity template for the given data model.
 void setShowTextField(boolean showTextField)
          Whether the TextField to enter the color hex number should be shown or not.
 void setSize(int size)
          Set the field size.
 String toString()
          Returns the HTML for the color-picker.
 void validate()
          Validates the input to check whether is required or not and that the input contains a valid color hex value.
 
Methods inherited from class org.apache.click.control.Field
bindRequestValue, getError, getErrorLabel, getFocus, getFocusJavaScript, getForm, getHelp, getId, getLabel, getLabelStyle, getLabelStyleClass, getParentStyleClassHint, getParentStyleHint, getRequestValue, getState, getTabIndex, getTextAlign, getTitle, getValidate, getValue, getValueObject, getWidth, isDisabled, isHidden, isReadonly, isRequired, isTrim, isValid, onProcess, removeState, renderTagBegin, restoreState, saveState, setDisabled, setError, setErrorMessage, setErrorMessage, setFocus, setForm, setHelp, setLabel, setLabelStyle, setLabelStyleClass, setListener, setParent, setParentStyleClassHint, setParentStyleHint, setReadonly, setRequired, setState, setTabIndex, setTextAlign, setTitle, setTrim, setValidate, setValue, setValueObject, setWidth
 
Methods inherited from class org.apache.click.control.AbstractControl
addBehavior, addStyleClass, appendAttributes, dispatchActionEvent, getActionListener, getAttribute, getAttributes, getBehaviors, getContext, getHtmlImports, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, getTag, hasAttribute, hasAttributes, hasBehaviors, hasStyles, isAjaxTarget, onDeploy, onDestroy, onInit, onRender, removeBehavior, removeStyleClass, renderTagEnd, setActionListener, setAttribute, setId, setName, setStyle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

showTextField

protected boolean showTextField
The show text field option for entering a color hex value. The default value is true.


size

protected int size
The text field size attribute. The default size is 7.

Constructor Detail

ColorPicker

public ColorPicker(String name)
Construct a ColorPicker with the given name. The color picker will show the text input field.

Parameters:
name - the name of the field

ColorPicker

public ColorPicker(String name,
                   String label)
Construct the ColorPicker with the given name and label.

Parameters:
name - the name of the field
label - the label of the field

ColorPicker

public ColorPicker(String name,
                   boolean required)
Construct a ColorPicker with the given name and required status.

Parameters:
name - the name of the field
required - the field required status

ColorPicker

public ColorPicker(String name,
                   boolean required,
                   boolean showTextField)
Constructs a ColorPicker with the given name, required status and display text field option.

Parameters:
name - the name of field
required - the field required status
showTextField - flag to show the text input field

ColorPicker

public ColorPicker()
Create a ColorPicker with no name defined.

Please note the control's name must be defined before it is valid.

Method Detail

getHeadElements

public List<Element> getHeadElements()
Return the ColorPicker HTML HEAD elements for the following resources:

Specified by:
getHeadElements in interface Control
Overrides:
getHeadElements in class AbstractControl
Returns:
the HTML HEAD elements for the control
See Also:
Control.getHeadElements()

getShowTextField

public boolean getShowTextField()
Whether the TextField to enter the color hex number should be shown or not. Default is true

Returns:
Returns the showTextField.

setShowTextField

public void setShowTextField(boolean showTextField)
Whether the TextField to enter the color hex number should be shown or not. Default is true.

Parameters:
showTextField - the showTextField to set

getSize

public int getSize()
Return the field size. By default is 7. Only used when getShowTextField() true.

Returns:
the field size

setSize

public void setSize(int size)
Set the field size.

Parameters:
size - the field size

getValidationJavaScript

public String getValidationJavaScript()
Return the field JavaScript client side validation function.

The function name must follow the format validate_[id], where the id is the DOM element id of the fields focusable HTML element, to ensure the function has a unique name.

Overrides:
getValidationJavaScript in class Field
Returns:
the field JavaScript client side validation function

getControlSizeEst

public int getControlSizeEst()
Overrides:
getControlSizeEst in class AbstractControl
Returns:
the estimated rendered control size in characters
See Also:
AbstractControl.getControlSizeEst()

render

public void render(HtmlStringBuffer buffer)
Render the HTML representation of the ColorPicker.

Specified by:
render in interface Control
Overrides:
render in class AbstractControl
Parameters:
buffer - the specified buffer to render the control's output to
See Also:
toString()

toString

public String toString()
Returns the HTML for the color-picker. This is the content of the ColorPicker.htm template.

Overrides:
toString in class AbstractControl
Returns:
a HTML rendered ColorPicker string

validate

public void validate()
Validates the input to check whether is required or not and that the input contains a valid color hex value.

Overrides:
validate in class Field
See Also:
TextField.validate()

renderTemplate

protected void renderTemplate(HtmlStringBuffer buffer,
                              Map<String,?> model)
Render a Velocity template for the given data model.

Parameters:
buffer - the specified buffer to render the template output to
model - the model data to merge with the template