net.sf.click.extras.control
Class ColorPicker

java.lang.Object
  extended bynet.sf.click.control.AbstractControl
      extended bynet.sf.click.control.Field
          extended bynet.sf.click.extras.control.ColorPicker
All Implemented Interfaces:
Control, Serializable

public class ColorPicker
extends Field

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

Color Field
The ColorPicker control provides a popup DHTML color picker <div> 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 wheter 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 #eeeee). If the color is not required the color-picker popup will show a button for 'no-color' on the top-left.

The color picker popup is based on JS script code from liferay.com which in turn is based on code from http://typetester.maratz.com/. To enable the color popup, reference the $cssImports and $jsImports in the page template. For example:

 <html>
 <head>
 $cssImports
 </head>
 <body>
 $form
 </body>
 </html>
 $jsImports 

Author:
Christian Essl
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 net.sf.click.control.Field
disabled, error, focus, form, help, label, readonly, required, tabindex, title, validate, value
 
Fields inherited from class net.sf.click.control.AbstractControl
actionListener, attributes, listener, listenerMethod, messages, name, parent, styles
 
Fields inherited from interface net.sf.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()
           
 String getHtmlImports()
          Return the HTML head import statements for the JavaScript files click/prototype.js) and click/colorpicker.js.
 boolean getShowTextField()
          Wheter 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 onDeploy(ServletContext servletContext)
          Deploy the static resource files in the colorpicker package.
 void render(HtmlStringBuffer buffer)
          Render the HTML representation of the ColorPicker.
protected  void renderTemplate(HtmlStringBuffer buffer, Map model)
          Render a Velocity template for the given data model.
 void setShowTextField(boolean showTextField)
          Wheter 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 wheter is required or not and that the input contains a valid color hex value.
 
Methods inherited from class net.sf.click.control.Field
bindRequestValue, getError, getErrorLabel, getFocus, getFocusJavaScript, getForm, getHelp, getId, getLabel, getRequestValue, getTabIndex, getTextAlign, getTitle, getValidate, getValue, getValueObject, getWidth, isDisabled, isHidden, isReadonly, isRequired, isValid, onInit, onProcess, setDisabled, setError, setErrorMessage, setErrorMessage, setErrorMessage, setErrorMessage, setFocus, setForm, setHelp, setLabel, setListener, setParent, setReadonly, setRequired, setTabIndex, setTextAlign, setTitle, setValidate, setValue, setValueObject, setWidth
 
Methods inherited from class net.sf.click.control.AbstractControl
addStyleClass, appendAttributes, getActionListener, getAttribute, getAttributes, getContext, getMessage, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, getTag, hasAttribute, hasAttributes, hasStyles, onDestroy, onRender, registerActionEvent, removeStyleClass, renderTagBegin, 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

getHtmlImports

public String getHtmlImports()
Return the HTML head import statements for the JavaScript files click/prototype.js) and click/colorpicker.js.

Returns:
the HTML head import statements for prototype.js and colorpicker.js
See Also:
AbstractControl.getHtmlImports()

getShowTextField

public boolean getShowTextField()
Wheter 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)
Wheter 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.

Returns:
the field JavaScript client side validation function

onDeploy

public void onDeploy(ServletContext servletContext)
Deploy the static resource files in the colorpicker package.

Parameters:
servletContext - the ServletContext
See Also:
AbstractControl.onDeploy(javax.servlet.ServletContext)

getControlSizeEst

public int getControlSizeEst()
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.

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.

Returns:
a HTML rendered ColorPicker string

validate

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

See Also:
TextField.validate()

renderTemplate

protected void renderTemplate(HtmlStringBuffer buffer,
                              Map 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