KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > rendering > InputBooleanTag


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.rendering;
19
20 import org.apache.beehive.netui.tags.html.HtmlConstants;
21
22 import java.util.HashMap JavaDoc;
23
24 /**
25  * Render and HTML <input> element of either type checkbox or radio. In HTML 4.01 the start
26  * tag is required and the end tag is forbidden. There are no required attributes.
27  */

28 abstract public class InputBooleanTag extends TagHtmlBase implements HtmlConstants
29 {
30     public static void add(HashMap JavaDoc html, HashMap JavaDoc htmlQuirks, HashMap JavaDoc xhtml)
31     {
32         html.put(INPUT_BOOLEAN_TAG, new HtmlRendering());
33         htmlQuirks.put(INPUT_BOOLEAN_TAG, new HtmlRendering());
34         xhtml.put(INPUT_BOOLEAN_TAG, new XhtmlRendering());
35     }
36
37     public static class State extends AbstractHtmlControlState
38     {
39         public String JavaDoc type;
40         public boolean disabled;
41         public boolean checked;
42         public String JavaDoc value;
43
44         public void clear()
45         {
46             super.clear();
47
48             type = null;
49             disabled = false;
50             checked = false;
51             value = null;
52         }
53     }
54
55     public void doStartTag(AbstractRenderAppender sb, AbstractTagState renderState)
56     {
57         assert(sb != null) : "Parameter 'sb' must not be null";
58         assert(renderState != null) : "Parameter 'renderState' must not be null";
59         assert(renderState instanceof State) : "Paramater 'renderState' must be an instance of InputBooleanTag.State";
60
61         State state = (State) renderState;
62
63         // Generate an HTML input element
64
// Create an appropriate "input" element based on our parameters
65
renderTag(sb, INPUT);
66         renderAttribute(sb, TYPE, state.type);
67         renderAttribute(sb, NAME, state.name);
68         renderAttribute(sb, ID, state.id);
69         renderAttribute(sb, CLASS, state.styleClass);
70         renderAttribute(sb, VALUE, state.value);
71         renderDisabled(sb, state.disabled);
72         renderChecked(sb, state.checked);
73
74         renderAttributes(AbstractHtmlState.ATTR_GENERAL, sb, state);
75         renderAttribute(sb, STYLE, state.style);
76         renderAttributes(AbstractHtmlState.ATTR_JAVASCRIPT, sb, state);
77         writeEnd(sb);
78     }
79
80     public void doEndTag(AbstractRenderAppender sb)
81     {
82     }
83
84     abstract protected void writeEnd(AbstractRenderAppender sb);
85
86     abstract protected void renderDisabled(AbstractRenderAppender sb, boolean disabled);
87
88     abstract protected void renderChecked(AbstractRenderAppender sb, boolean checked);
89
90     private static class HtmlRendering extends InputBooleanTag
91     {
92         protected void writeEnd(AbstractRenderAppender sb)
93         {
94             sb.append(">");
95         }
96
97         protected void renderDisabled(AbstractRenderAppender sb, boolean disabled)
98         {
99             if (disabled)
100                 sb.append(" " + DISABLED);
101
102         }
103
104         protected void renderChecked(AbstractRenderAppender sb, boolean checked)
105         {
106             if (checked)
107                 sb.append(" " + CHECKED);
108
109         }
110     }
111
112     private static class XhtmlRendering extends InputBooleanTag
113     {
114         protected void writeEnd(AbstractRenderAppender sb)
115         {
116             sb.append(" />");
117         }
118
119         protected void renderDisabled(AbstractRenderAppender sb, boolean disabled)
120         {
121             if (disabled)
122                 renderAttribute(sb, DISABLED, DISABLED);
123         }
124
125         protected void renderChecked(AbstractRenderAppender sb, boolean checked)
126         {
127             if (checked)
128                 renderAttribute(sb, CHECKED, CHECKED);
129         }
130     }
131 }
132
Popular Tags