KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > databinding > datagrid > AbstractDataGridHtmlTag


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.databinding.datagrid;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.jsp.JspException JavaDoc;
22
23 import org.apache.beehive.netui.tags.AbstractSimpleTag;
24 import org.apache.beehive.netui.tags.TagConfig;
25 import org.apache.beehive.netui.tags.javascript.ScriptRequestState;
26 import org.apache.beehive.netui.tags.html.Form;
27 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
28 import org.apache.beehive.netui.tags.rendering.AbstractHtmlControlState;
29 import org.apache.beehive.netui.databinding.datagrid.api.rendering.DataGridTagModel;
30
31 /**
32  * <p>
33  * This class is a base class for all data grid tags that render HTML output for the grid. This base
34  * class provides services to its subclasses including lookup of the {@link DataGridTagModel} for the
35  * current data grid. In addition, this class exposes tagId generation functionality. These methods
36  * are used to create strings that will be written onto HTML tags for the "id" or "name" attributes.
37  * Such identifiers can be created in one of two ways -- indexed or un-indexed. An un-indexed tag
38  * identifier is one that the page author is responsible for making unique within a given
39  * scope in the rendered output. An indexed tag identifier is one that the data grid will suffix
40  * with the index for the current row in the grid. This is used to help create unique identifiers
41  * from JSP tags that are rendered repeatedly. For example, a {@link SpanCell} renders an HTML
42  * &lt;span&gt; tag; with its <code>tagId</code> attribute set, it will render a tag identifier for
43  * every row in the page. The indexed is added as a suffix in order to help page authors create
44  * more unique identifiers. Ultimately, the page author is responsible for ensuring that their
45  * tag identifiers are unique within a scope.
46  * </p>
47  */

48 public abstract class AbstractDataGridHtmlTag
49     extends AbstractSimpleTag {
50
51     /**
52      * Get the {@link DataGridTagModel} for the data grid.
53      * @return the data grid tag model
54      */

55     protected final DataGridTagModel lookupDataGridTagModel() {
56         return DataGridUtil.getDataGridTagModel(getJspContext());
57     }
58
59     /**
60      * Create an indexed tag identifier given a state object and a base tag identifier. The <code>tagId</code>
61      * will have the index of the current item in the data grid attached as a suffix to the
62      * the given base identifier.
63      * @param state the {@link AbstractHtmlState} upon which the tag identifier will be set once created
64      * @param tagId the base tag identifier name
65      * @throws JspException
66      */

67     protected final void applyIndexedTagId(AbstractHtmlState state, String JavaDoc tagId)
68         throws JspException JavaDoc {
69         state.id = indexTagId(generateTagId(tagId));
70     }
71
72     /**
73      * Create an un-indexed tag identifier for the given state object.
74      * @param state the {@link AbstractHtmlState} upon which the tag identifier will be set once created
75      * @param tagId the base tag identifier
76      * @throws JspException
77      */

78     protected final void applyTagId(AbstractHtmlState state, String JavaDoc tagId)
79         throws JspException JavaDoc {
80         state.id = generateTagId(tagId);
81     }
82
83     /**
84      * Generate a tag ID.
85      * @param tagId the tag id
86      * @return the generated tag id
87      * @throws JspException if an error occurs creating the value of the tag id attribute
88      */

89     private final String JavaDoc generateTagId(String JavaDoc tagId)
90         throws JspException JavaDoc {
91         return setRequiredValueAttribute(tagId, "tagId");
92     }
93
94     /**
95      * Generate an indexed tag ID. This method will use the
96      * {@link org.apache.beehive.netui.databinding.datagrid.api.rendering.DataGridTagModel#getCurrentIndex()} method
97      * to scope a given name to a particular row in the data grid.
98      * @param tagId the base tag id
99      * @return an index-scoped tag id
100      */

101     private final String JavaDoc indexTagId(String JavaDoc tagId) {
102         DataGridTagModel dataGridTagModel = lookupDataGridTagModel();
103         assert dataGridTagModel != null;
104
105         int index = dataGridTagModel.getCurrentIndex();
106         assert index >= 0;
107
108         return tagId + index;
109     }
110
111     /**
112      * <p>
113      * Generate a name and id given a {@link AbstractHtmlState} object. Data grid callers may invoke this
114      * method with subclasses rendering markup containing tags that must set HTML tag IDs for use
115      * via JavaScript on the client.
116      * <br/>
117      * Assumptions:
118      * <ul>
119      * <li>The state.name must be fully formed or the "real name" of the form.</li>
120      * <li>The state.id is the tagId value set on the tag and <b>has not</b> be rewritten yet to form the "real id"</li>
121      * </ul>
122      * </p>
123      * @param state the HTML state whose tag id to set
124      * @param parentForm a {@link Form} tag if one contains this tag
125      * @return String a block of JavaScript if script must e rendered to the page in order to support
126      * lookups of HTML elements using a tag id. If returned, the String <b>must</b> be rendered
127      * to the output stream. <code>null</code> if no script must be rendered.
128      */

129     protected final String JavaDoc renderNameAndId(HttpServletRequest JavaDoc request, AbstractHtmlState state, Form parentForm) {
130         // if id is not set then we need to exit
131
if (state.id == null)
132             return null;
133
134         // check to see if this is an instance of a HTML Control
135
boolean ctrlState = (state instanceof AbstractHtmlControlState);
136
137         // form keeps track of this so that it can add this control to it's focus map
138
if (parentForm != null && ctrlState) {
139             AbstractHtmlControlState hcs = (AbstractHtmlControlState) state;
140             if (hcs.name == null && parentForm.isFocusSet())
141                 hcs.name = state.id;
142             parentForm.addTagID(state.id, ((AbstractHtmlControlState) state).name);
143         }
144
145         // rewrite the id, save the original value so it can be used in maps
146
String JavaDoc id = state.id;
147         state.id = getIdForTagId(id);
148
149         // Legacy Java Script support -- This writes out a single table with both the id and names
150
// mixed. This is legacy support to match the pre beehive behavior.
151
String JavaDoc idScript = null;
152         if (TagConfig.isLegacyJavaScript()) {
153             ScriptRequestState srs = ScriptRequestState.getScriptRequestState(request);
154             if (!ctrlState) {
155                 idScript = srs.mapLegacyTagId(getScriptReporter(), id, state.id);
156             }
157             else {
158                 AbstractHtmlControlState cState = (AbstractHtmlControlState) state;
159                 if (cState.name != null)
160                     idScript = srs.mapLegacyTagId(getScriptReporter(), id, cState.name);
161                 else
162                     idScript = srs.mapLegacyTagId(getScriptReporter(), id, state.id);
163             }
164         }
165
166         // map the tagId to the real id
167
String JavaDoc name = null;
168         if (ctrlState) {
169             AbstractHtmlControlState cState = (AbstractHtmlControlState) state;
170             name = cState.name;
171         }
172
173         String JavaDoc script = renderDefaultNameAndId((HttpServletRequest JavaDoc)request, state, id, name);
174         if (script != null) {
175             if (idScript != null)
176                 idScript = idScript + script;
177             else idScript = script;
178         }
179         return idScript;
180     }
181
182
183     protected String JavaDoc renderDefaultNameAndId(HttpServletRequest JavaDoc request, AbstractHtmlState state, String JavaDoc id, String JavaDoc name) {
184         // map the tagId to the real id
185
String JavaDoc script = null;
186         if (TagConfig.isDefaultJavaScript()) {
187             ScriptRequestState srs = ScriptRequestState.getScriptRequestState(request);
188             script = srs.mapTagId(getScriptReporter(), id, state.id, name);
189         }
190         return script;
191     }
192 }
193
Popular Tags