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.yui;
016
017 import org.apache.tapestry5.BindingConstants;
018 import org.apache.tapestry5.ComponentResources;
019 import org.apache.tapestry5.MarkupWriter;
020 import org.apache.tapestry5.ValidationTracker;
021 import org.apache.tapestry5.annotations.Environmental;
022 import org.apache.tapestry5.annotations.Import;
023 import org.apache.tapestry5.annotations.Mixin;
024 import org.apache.tapestry5.annotations.Parameter;
025 import org.apache.tapestry5.corelib.mixins.RenderDisabled;
026 import org.apache.tapestry5.ioc.annotations.Inject;
027 import org.apache.tapestry5.json.JSONObject;
028 import org.apache.tapestry5.services.Request;
029 import org.apache.tapestry5.services.javascript.JavaScriptSupport;
030 import org.chenillekit.tapestry.core.base.AbstractYuiField;
031
032 /**
033 * @version $Id: StateButton.java 674 2010-07-29 12:47:25Z homburgs $
034 */
035 @Import(stylesheet = {"${yahoo.yui}/button/assets/skins/sam/button.css"},
036 library = {"${yahoo.yui}/button/button${yahoo.yui.mode}.js",
037 "../../Chenillekit.js", "StateButton.js"})
038 public class StateButton extends AbstractYuiField
039 {
040 /**
041 * The value to be read or updated. If not bound, the Checkbox will attempt to edit a property of its container
042 * whose name matches the component's id.
043 */
044 @Parameter(required = true, autoconnect = true)
045 private boolean value;
046
047 @Parameter(required = false, defaultPrefix = BindingConstants.LITERAL)
048 private String label;
049
050 @Inject
051 private Request request;
052
053 @SuppressWarnings("unused")
054 @Mixin
055 private RenderDisabled renderDisabled;
056
057 @Inject
058 private ComponentResources resources;
059
060 @Environmental
061 private ValidationTracker tracker;
062
063 /**
064 * RenderSupport to get unique client side id.
065 */
066 @Environmental
067 private JavaScriptSupport javascriptSupport;
068
069 /**
070 * Tapestry render phase method.
071 * Start a tag here, end it in afterRender
072 *
073 * @param writer the markup writer
074 */
075 void beginRender(MarkupWriter writer)
076 {
077 writer.element("span", "class", "yui-button", "id", getClientId());
078 writer.element("span", "class", "first-child");
079 writer.element("button", "id", getClientId() + "Button");
080 writer.write(label);
081 writer.end();
082 writer.end();
083 writer.end();
084 }
085
086 /**
087 * Tapestry render phase method. End a tag here.
088 *
089 * @param writer the markup writer
090 */
091 void afterRender(MarkupWriter writer)
092 {
093 JSONObject options = new JSONObject();
094
095 options.put("name", getControlName());
096 options.put("type", "checkbox");
097 options.put("disabled", isDisabled());
098 options.put("checked", value);
099
100 configure(options);
101
102 javascriptSupport.addScript("new Ck.YuiStateButton('%s', %s);", getClientId(), options);
103 }
104
105 /**
106 * Invoked to allow subclasses to further configure the parameters passed to this mixin's javascript
107 * options. Subclasses may override this method to configure additional features of this mixin.
108 * <p/>
109 * This implementation does nothing.
110 *
111 * @param options option object
112 */
113 protected void configure(JSONObject options)
114 {
115 }
116
117 /**
118 * Method implemented by subclasses to actually do the work of processing the submission of the form. The element's
119 * elementName property will already have been set. This method is only invoked if the field is <strong>not {@link
120 * #isDisabled() disabled}</strong>.
121 *
122 * @param elementName the name of the element (used to find the correct parameter in the request)
123 */
124 protected void processSubmission(String elementName)
125 {
126 String rawValue = request.getParameter(elementName);
127
128 if (rawValue == null)
129 value = false;
130 else if (rawValue.equalsIgnoreCase("undefined"))
131 value = true;
132 }
133 }