KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > HtmlGroupBaseTag


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.html;
19
20 import org.apache.beehive.netui.script.common.IDataAccessProvider;
21 import org.apache.beehive.netui.tags.AbstractClassicTag;
22 import org.apache.beehive.netui.tags.ExpressionHandling;
23 import org.apache.beehive.netui.tags.IAttributeConsumer;
24 import org.apache.beehive.netui.tags.rendering.*;
25 import org.apache.beehive.netui.util.Bundle;
26 import org.apache.beehive.netui.util.iterator.IteratorFactory;
27 import org.apache.beehive.netui.util.logging.Logger;
28
29 import javax.servlet.ServletRequest JavaDoc;
30 import javax.servlet.jsp.JspException JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * Abstract base class that provides the <code>dataSource</code>, <code>defaultValue</code>, and
38  * <code>optionsDataSource</code> attributes.
39  */

40 abstract public class HtmlGroupBaseTag
41         extends AbstractClassicTag implements IDataAccessProvider, HtmlConstants, IAttributeConsumer
42 {
43     private static final Logger logger = Logger.getInstance(HtmlGroupBaseTag.class);
44
45     /**
46      * Constant defining a horizontal layout of the options.
47      */

48     public final String JavaDoc HORIZONTAL_VALUE = "horizontal";
49
50     /**
51      * Constant defining a vertical layout of the options.
52      */

53     public final String JavaDoc VERTICAL_VALUE = "vertical";
54
55     private InputBooleanTag.State _inputState = new InputBooleanTag.State();
56     private SpanTag.State _spanState = new SpanTag.State();
57     protected ConstantRendering _cr;
58
59     private HashMap JavaDoc _attrs;
60
61     protected String JavaDoc _dataSource; // The attribute value for the dataSource
62
protected Object JavaDoc _defaultValue; // A String that contains or references the data to render for the default value of this tag.
63
protected Object JavaDoc _optionsDataSource; // The value of the options data source.
64
protected boolean _disabled;
65
66     private String JavaDoc _orientation; // legal values "horizontal, virtical"
67
private Boolean JavaDoc _orientVal; // Three state boolean if we are doing virtical layout
68

69     private String JavaDoc _realName; // The name that is the result of do naming
70

71     private String JavaDoc _style; // The style attribute
72
private String JavaDoc _class; // The class attribute
73

74     protected String JavaDoc _labelStyle = null;
75     protected String JavaDoc _labelStyleClass = null;
76
77     protected boolean _repeater; // Boolean flag indicating if this is a repeater or not
78

79     // These variables are protected explicitly so they can be accessed by subclasses
80
protected int _repIdx = 0; // The current index for repeating over the optionsDataSource
81
protected Object JavaDoc _repCurItem; // The current item access by the IDataAccessProvider
82

83     public HtmlGroupBaseTag()
84     {
85         super();
86     }
87
88     /**
89      * @param value
90      * @return boolean
91      */

92     public abstract boolean isMatched(String JavaDoc value, Boolean JavaDoc defaultValue);
93
94     /**
95      * Base support for the attribute tag. This is overridden to prevent setting the <code>href</code>
96      * attribute. The <code>checkBoxGroup</code> and <code>radioButtonGroup</code> support two facets,
97      * <code>input</code> and <code>span</code>. The <code>input</code> is the default and will attach
98      * attributes to the &lt;input> element. The <code>span</code> facet will attach attributes to the
99      * &lt;span> elements which represents the label.
100      * @param name The name of the attribute. This value may not be null or the empty string.
101      * @param value The value of the attribute. This may contain an expression.
102      * @param facet The name of a facet to which the attribute will be applied. This is optional.
103      * @throws JspException A JspException may be thrown if there is an error setting the attribute.
104      */

105     public void setAttribute(String JavaDoc name, String JavaDoc value, String JavaDoc facet)
106             throws JspException JavaDoc
107     {
108         // validate the name attribute, in the case of an error simply return.
109
if (name == null || name.length() <= 0) {
110             String JavaDoc s = Bundle.getString("Tags_AttributeNameNotSet");
111             registerTagError(s, null);
112             return;
113         }
114
115         // it's not legal to set the id or name attributes this way
116
if (name.equals(ID) || name.equals(NAME)) {
117             String JavaDoc s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object JavaDoc[]{name});
118             registerTagError(s, null);
119             return;
120         }
121
122         // handle the facet. Span will place stuff into the spanState input is the default
123
// so we don't need to do anything.
124
if (facet != null) {
125             if (facet.equals("span")) {
126                 _spanState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, name, value);
127                 return;
128             }
129             else if (facet.equals("input")) {
130                 // do nothing...
131
}
132             else {
133                 String JavaDoc s = Bundle.getString("Tags_AttributeFacetNotSupported", new Object JavaDoc[]{facet});
134                 registerTagError(s, null);
135                 return;
136             }
137         }
138
139         // don't set the value on the input
140
if (name.equals(VALUE)) {
141             String JavaDoc s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object JavaDoc[]{name});
142             registerTagError(s, null);
143             return;
144         }
145
146         // we place the state into the special attribute map.
147
if (_attrs == null) {
148             _attrs = new HashMap JavaDoc();
149         }
150         _attrs.put(name, value);
151     }
152
153     /**
154      * Return the qualified name of the checkBoxGroup. The qualified name is the created
155      * by calling <code>doNaming()</code>.
156      * @return the qualified name based upon the <code>dataSource</code> name.
157      * @throws JspException
158      */

159     public final String JavaDoc getQualifiedDataSourceName()
160             throws JspException JavaDoc
161     {
162         if (_realName == null)
163             _realName = doNaming();
164         return _realName;
165     }
166
167     /**
168      * Returns the boolean value or expression indicating the
169      * disable state of the RadioButtonGroup.
170      * @return the disabled state (true or false) or an expression
171      */

172     public boolean isDisabled()
173     {
174         return _disabled;
175     }
176
177     /**
178      * Set the disable state either with the literal "true" or "false"
179      * or with an expression.
180      * @param disabled true or false or an expression
181      * @jsptagref.attributedescription Set the disable state either with the literal "true"
182      * or "false" or with an expression.
183      * @jsptagref.databindable false
184      * @jsptagref.attributesyntaxvalue <i>boolean_disabled</i>
185      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
186      * description="Set the disable state either with the literal "true" or "false"
187      * or with an expression."
188      */

189     public void setDisabled(boolean disabled)
190     {
191         _disabled = disabled;
192     }
193
194     /**
195      * Set the orientation of the resulting options group. The layout will be either <code>horizontal</code>
196      * or <code>vertical</code>. The default is <code>horizontal</code>.
197      * @param orientation true or false or an expression
198      * @jsptagref.attributedescription Set the orientation of the resulting options group.
199      * @jsptagref.databindable false
200      * @jsptagref.attributesyntaxvalue <i>string_orientation</i>
201      * @netui:attribute required="false" rtexprvalue="true"
202      * description="Set the orientation of the resulting options group."
203      */

204     public void setOrientation(String JavaDoc orientation)
205     {
206         _orientation = setNonEmptyValueAttribute(orientation);
207     }
208
209     /**
210      * Returns <code>true</code> if vertical layout is set.
211      * @return <code>true</code> if vertical layout is set
212      */

213     public boolean isVertical()
214     {
215         if (_orientVal == null) {
216             boolean val = VERTICAL_VALUE.equalsIgnoreCase(_orientation);
217             _orientVal = new Boolean JavaDoc(val);
218         }
219         return _orientVal.booleanValue();
220     }
221
222     /**
223      * Set whether repeating of contained options is on.
224      * @param repeater the repeater value ("true" or "false")
225      * @jsptagref.attributedescription Set whether repeating of contained options is on.
226      * @jsptagref.databindable false
227      * @jsptagref.attributesyntaxvalue <i>boolean_repeater</i>
228      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
229      * description="Set whether repeating of contained options is on."
230      */

231     public void setRepeater(boolean repeater)
232     {
233         _repeater = repeater;
234     }
235
236     /**
237      * Gets whether a repeating contained options is on.
238      * @return the repeater value
239      */

240     public boolean isRepeater()
241     {
242         return _repeater;
243     }
244
245     /**
246      * Sets the style of the rendered html tag.
247      * @param style the html style.
248      * @jsptagref.attributedescription Specifies style information for the current element.
249      * @jsptagref.databindable false
250      * @jsptagref.attributesyntaxvalue <i>string_style</i>
251      * @netui:attribute required="false" rtexprvalue="true"
252      * description="Sets the style of the rendered html tag."
253      */

254     public void setStyle(String JavaDoc style)
255     {
256         _style = setNonEmptyValueAttribute(style);
257     }
258
259     /**
260      * Sets the style class of the rendered html tag.
261      * @param styleClass the html style class.
262      * @jsptagref.attributedescription The style class (a style sheet selector).
263      * @jsptagref.databindable false
264      * @jsptagref.attributesyntaxvalue <i>string_styleClass</i>
265      * @netui:attribute required="false" rtexprvalue="true"
266      * description="Sets the style class of the rendered html tag."
267      */

268     public void setStyleClass(String JavaDoc styleClass)
269     {
270         _class = setNonEmptyValueAttribute(styleClass);
271     }
272
273     /**
274      * Return the label style for each contained CheckBoxOption..
275      * @return the label style
276      */

277     public String JavaDoc getLabelStyle()
278     {
279         return _labelStyle;
280     }
281
282     /**
283      * Set the label style for each contained CheckBoxOption.
284      * @param labelStyle the label style
285      * @jsptagref.attributedescription Set the label style for each contained CheckBoxOption.
286      * @jsptagref.databindable false
287      * @jsptagref.attributesyntaxvalue <i>string_labelStyle</i>
288      * @netui:attribute required="false" rtexprvalue="true"
289      * description="Set the label style for each contained CheckBoxOption."
290      */

291     public void setLabelStyle(String JavaDoc labelStyle)
292     {
293         _labelStyle = setNonEmptyValueAttribute(labelStyle);
294     }
295
296     /**
297      * Return the label style class for each contained CheckBoxOption..
298      * @return the label style
299      */

300     public String JavaDoc getLabelStyleClass()
301     {
302         return _labelStyleClass;
303     }
304
305     /**
306      * Set the label style class for each contained CheckBoxOption.
307      * @param labelStyleClass the label style
308      * @jsptagref.attributedescription Set the label style class for each contained CheckBoxOption.
309      * @jsptagref.databindable false
310      * @jsptagref.attributesyntaxvalue <i>string_labelStyleClass</i>
311      * @netui:attribute required="false" rtexprvalue="true"
312      * description="Set the label style class for each contained CheckBoxOption."
313      */

314     public void setLabelStyleClass(String JavaDoc labelStyleClass)
315     {
316         _labelStyleClass = setNonEmptyValueAttribute(labelStyleClass);
317     }
318
319
320     /**
321      * Sets the tag's data source (can be an expression).
322      * @param dataSource the data source
323      * @jsptagref.attributedescription An expression to be evaluated. It determines
324      * the source of populating data for the tag
325      * @jsptagref.databindable false
326      * @jsptagref.attributesyntaxvalue <i>string_dataSource</i>
327      * @netui:attribute required="true"
328      * description="Sets the tag's data source."
329      */

330     public void setDataSource(String JavaDoc dataSource)
331             throws JspException JavaDoc
332     {
333         _dataSource = dataSource;
334     }
335
336     /**
337      * Gets the tag's data source (can be an expression).
338      * @return the data source
339      */

340     public String JavaDoc getDataSource()
341     {
342         return _dataSource;
343     }
344
345     //********************************** IDataAccessProvider Interface ******************************
346
// getDataSource is implemented above
347

348     /**
349      * Get the current index in this iteration. This should be a
350      * zero based integer that increments after each iteration.
351      * @return the current index of iteration or 0
352      */

353     public int getCurrentIndex()
354     {
355         return _repIdx;
356     }
357
358     /**
359      * Get the current data item in this IDataAccessProvider.
360      * @return the current data item or <code>null</code>
361      */

362     public Object JavaDoc getCurrentItem()
363     {
364         return _repCurItem;
365     }
366
367     /**
368      * Get a metadata object for the current item. This interface
369      * is optional, and implementations of this interface are
370      * provided by the IDataAccessProvider interface. See these
371      * implementations for information about their support for
372      * current item metadata.
373      * @return the current metadata or <code>null</code> if no metadata can be
374      * found or metadata is not supported by a IDataAccessProvider implementation
375      */

376     public Object JavaDoc getCurrentMetadata()
377     {
378         return null;
379     }
380
381     /**
382      * Get the parent IDataAccessProvider of a IDataAccessProvider. A IDataAccessProvider
383      * implementation may be able to nest IDataAccessProviders. In this case,
384      * it can be useful to be able to also nest access to data from parent
385      * providers. Implementations of this interface are left with having
386      * to discover and export parents. The return value from this call
387      * on an implementing Object can be <code>null</code>.
388      * @return the parent IDataAccessProvider or <code>null</code> if this method
389      * is not supported or the parent can not be found.
390      */

391     public IDataAccessProvider getProviderParent()
392     {
393         return (IDataAccessProvider) findAncestorWithClass(this, IDataAccessProvider.class);
394     }
395
396     /**
397      * Return an <code>ArrayList</code> which represents a chain of <code>INameInterceptor</code>
398      * objects. This method by default returns <code>null</code> and should be overridden
399      * by objects that support naming.
400      * @return an <code>ArrayList</code> that will contain <code>INameInterceptor</code> objects.
401      */

402     protected List JavaDoc getNamingChain()
403     {
404         return AbstractClassicTag.DefaultNamingChain;
405     }
406
407     /**
408      * Return the Object that is represented by the specified data source.
409      * @return Object
410      * @throws JspException
411      */

412     protected Object JavaDoc evaluateDataSource()
413             throws JspException JavaDoc
414     {
415         ExpressionHandling expr = new ExpressionHandling(this);
416         String JavaDoc dataSource = "{" + _dataSource + "}";
417         String JavaDoc ds = expr.ensureValidExpression(dataSource, "dataSource", "DataSourceError");
418         if (ds == null)
419             return null;
420
421         // have a valid expression
422
return expr.evaluateExpression(dataSource, "dataSource", pageContext);
423     }
424
425     protected String JavaDoc doNaming()
426             throws JspException JavaDoc
427     {
428         String JavaDoc dataSource = "{" + _dataSource + "}";
429         return applyNamingChain(dataSource);
430     }
431
432     /**
433      * Sets the default value (can be an expression).
434      * @param defaultValue the default value
435      * @jsptagref.attributedescription The String literal or expression to be used as the default value.
436      * @jsptagref.databindable false
437      * @jsptagref.attributesyntaxvalue <i>string_or_expression_default</i>
438      * @netui:attribute required="false" rtexprvalue="true"
439      * description="The String literal or expression to be used as the default value."
440      */

441     public void setDefaultValue(Object JavaDoc defaultValue)
442             throws JspException JavaDoc
443     {
444         if (defaultValue == null) {
445             String JavaDoc s = Bundle.getString("Tags_AttrValueRequired", new Object JavaDoc[]{"defaultValue"});
446             registerTagError(s, null);
447             return;
448         }
449         _defaultValue = defaultValue;
450     }
451
452     /**
453      * Gets the options datasource value (an expression).
454      * @return the options datasource
455      */

456     public Object JavaDoc getOptionsDataSource()
457     {
458         return _optionsDataSource;
459     }
460
461     /**
462      * Sets the options datasource value (an expression).
463      * @param optionsDataSource the options datasource
464      * @jsptagref.attributedescription Sets the options datasource value (an expression).
465      * @jsptagref.databindable true
466      * @jsptagref.attributesyntaxvalue <i>expression_optionsDataSource</i>
467      * @netui:attribute required="false" rtexprvalue="true" type="java.lang.Object"
468      * description="Sets the options datasource value."
469      */

470     public void setOptionsDataSource(Object JavaDoc optionsDataSource)
471             throws JspException JavaDoc
472     {
473         if (optionsDataSource == null) {
474             String JavaDoc s = Bundle.getString("Tags_AttrValueRequired", new Object JavaDoc[]{"optionsDataSource"});
475             registerTagError(s, null);
476             return;
477         }
478         _optionsDataSource = optionsDataSource;
479     }
480
481     /**
482      * Return the real value of the <code>optionDataSource</code> attribute. The value returned will
483      * always be an instance of <code>Iterator</code> This value reflects the
484      * result of expression evaluation on the options data source.
485      * @return the object that represents the options data source.
486      */

487     protected Object JavaDoc evaluateOptionsDataSource()
488             throws JspException JavaDoc
489     {
490         if (_optionsDataSource == null) {
491             // optionsDataSource is null so this is a warning
492
logger.warn(Bundle.getString("Tags_IteratorError",
493                     new Object JavaDoc[]{getTagName(), "optionsDataSource", _optionsDataSource}));
494             return IteratorFactory.EMPTY_ITERATOR;
495         }
496
497         if (_optionsDataSource instanceof Map JavaDoc)
498             return _optionsDataSource;
499
500         Iterator JavaDoc it;
501         it = IteratorFactory.createIterator(_optionsDataSource);
502         if (it == null) {
503             it = IteratorFactory.EMPTY_ITERATOR;
504         }
505
506         assert (it != null && it instanceof Iterator JavaDoc);
507         return it;
508     }
509
510     /**
511      * This will create a new option in the HTML.
512      */

513     protected void addOption(AbstractRenderAppender buffer, String JavaDoc type, String JavaDoc optionValue,
514                              String JavaDoc optionDisplay, int idx, String JavaDoc altText, char accessKey, boolean disabled)
515             throws JspException JavaDoc
516     {
517         ServletRequest JavaDoc req = pageContext.getRequest();
518         if (_cr == null)
519             _cr = TagRenderingBase.Factory.getConstantRendering(req);
520
521         assert(buffer != null);
522         assert(optionValue != null);
523         assert(optionDisplay != null);
524         assert(type != null);
525
526         if (_orientation != null && isVertical()) {
527             _cr.TR_TD(buffer);
528         }
529
530         _inputState.clear();
531         _inputState.type = type;
532         _inputState.name = getQualifiedDataSourceName();
533         _inputState.value = optionValue;
534         _inputState.style = _style;
535         _inputState.styleClass = _class;
536
537         if (isMatched(optionValue, null)) {
538             _inputState.checked = true;
539         }
540         _inputState.disabled = isDisabled();
541         _inputState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALT, altText);
542         if (accessKey != 0x00)
543             _inputState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ACCESSKEY, Character.toString(accessKey));
544
545         // if there are attributes defined push them to the options.
546
if (_attrs != null && _attrs.size() > 0) {
547             Iterator JavaDoc iterator = _attrs.keySet().iterator();
548             for (; iterator.hasNext();) {
549                 String JavaDoc key = (String JavaDoc) iterator.next();
550                 if (key == null)
551                     continue;
552
553                 String JavaDoc value = (String JavaDoc) _attrs.get(key);
554                 _inputState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, key, value);
555             }
556         }
557
558         TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.INPUT_BOOLEAN_TAG, req);
559         br.doStartTag(buffer, _inputState);
560         br.doEndTag(buffer);
561
562         String JavaDoc ls = _labelStyle;
563         String JavaDoc lsc = _labelStyleClass;
564
565         _spanState.style = ls;
566         _spanState.styleClass = lsc;
567
568         br = TagRenderingBase.Factory.getRendering(TagRenderingBase.SPAN_TAG, req);
569         br.doStartTag(buffer, _spanState);
570         buffer.append(optionDisplay);
571         br.doEndTag(buffer);
572
573         // backward compatibility this is now overridden by the _orientation
574
if (_orientation == null) {
575             _cr.BR(buffer);
576         }
577         else {
578             if (isVertical()) {
579                 _cr.TR_TD(buffer);
580             }
581             else {
582                 _cr.NBSP(buffer);
583             }
584         }
585     }
586
587     /**
588      * Release any acquired resources.
589      */

590     protected void localRelease()
591     {
592         super.localRelease();
593         if (_attrs != null)
594             _attrs.clear();
595         _cr = null;
596         _dataSource = null;
597         _defaultValue = null;
598         _optionsDataSource = null;
599         _disabled = false;
600         _orientation = null;
601         _orientVal = null;
602         _realName = null;
603         _style = null;
604         _class = null;
605         _labelStyle = null;
606         _labelStyleClass = null;
607
608         _repIdx = 0;
609         _repCurItem = null;
610     }
611
612 }
613
Popular Tags