001 /*
002 * Apache License
003 * Version 2.0, January 2004
004 * http://www.apache.org/licenses/
005 *
006 * Copyright 2008-2010 by chenillekit.org
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 */
014
015 package org.chenillekit.tapestry.core.components;
016
017 import org.apache.tapestry5.BindingConstants;
018 import org.apache.tapestry5.MarkupWriter;
019 import org.apache.tapestry5.annotations.Environmental;
020 import org.apache.tapestry5.annotations.Import;
021 import org.apache.tapestry5.annotations.Parameter;
022 import org.apache.tapestry5.corelib.base.AbstractTextField;
023 import org.apache.tapestry5.json.JSONObject;
024 import org.apache.tapestry5.services.javascript.JavaScriptSupport;
025
026 /**
027 * A simple color picker component. Supports saving custom colors to the palette via cookies. To open the palette,
028 * click the color box at the right side of the input box.
029 *
030 * @version $Id: ColorPicker.java 674 2010-07-29 12:47:25Z homburgs $
031 */
032 @Import(stylesheet = "colorpicker/colorpicker.css", library = {"colorpicker/colorpicker.js", "../prototype-base-extensions.js"})
033 public class ColorPicker extends AbstractTextField
034 {
035 /**
036 * The value parameter of a ColorPicker must be a {@link java.lang.String}.
037 */
038 @Parameter(required = true, principal = true)
039 private String value;
040
041 /**
042 * an array of 40 colors (in hex format) to display in the swatch grid
043 */
044 @Parameter(required = false, defaultPrefix = BindingConstants.PROP)
045 private String[] colors;
046
047 /**
048 * the text to display in the "add custom color" button, defaults to "Add"
049 */
050 @Parameter(required = false, defaultPrefix = BindingConstants.LITERAL)
051 private String label;
052
053 /**
054 * RenderSupport to get unique client side id
055 */
056 @Environmental
057 private JavaScriptSupport javascriptSupport;
058
059
060 /**
061 * Invoked from {@link #begin(org.apache.tapestry5.MarkupWriter)} to write out the element and attributes (typically, <input>). The
062 * {@linkplain org.apache.tapestry5.corelib.base.AbstractField#getControlName() controlName} and {@linkplain org.apache.tapestry5.corelib.base.AbstractField#getClientId() clientId}
063 * properties will already have been set or updated.
064 * <p/>
065 * Generally, the subclass will invoke {@link org.apache.tapestry5.MarkupWriter#element(String, Object[])}, and will be responsible for
066 * including an {@link org.apache.tapestry5.annotations.AfterRender} phase method to invoke {@link org.apache.tapestry5.MarkupWriter#end()}.
067 *
068 * @param writer markup write to send output to
069 * @param value the value (either obtained and translated from the value parameter, or obtained from the tracker)
070 */
071 protected void writeFieldTag(MarkupWriter writer, String value)
072 {
073 String clientId = getClientId();
074
075 writer.element("input",
076
077 "type", "text",
078
079 "class", "colorpicker",
080
081 "name", getControlName(),
082
083 "id", clientId,
084
085 "value", value);
086 }
087
088 /**
089 * Tapestry render phase method. End a tag here.
090 */
091 void afterRender(MarkupWriter writer)
092 {
093 writer.end(); // input
094
095 JSONObject setup = new JSONObject();
096
097 if (colors != null)
098 setup.put("colors", colors);
099 if (label != null)
100 setup.put("addLabel", label);
101
102 javascriptSupport.addScript("new Control.ColorPicker('%s', %s);", getClientId(), setup);
103 }
104 }