KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > databinding > datagrid > api > rendering > StyleModel


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.databinding.datagrid.api.rendering;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import java.util.List JavaDoc;
23
24 /**
25  * <p>
26  * JavaBean base class that creates HTML style class names used to render various HTML elements in the data grid.
27  * </p>
28  */

29 public abstract class StyleModel {
30
31     private static final String JavaDoc DELIM = "-";
32     private static final String JavaDoc SPACE = " ";
33     private static final String JavaDoc EMPTY = "";
34
35     private String JavaDoc _stylePrefix = null;
36
37     /**
38      * Default constructor.
39      */

40     public StyleModel() {
41     }
42
43     /**
44      * Constructor that takes a style prefix string. If a style prefix is provided, StyleModel subclasses
45      * should use this as a prefix to any style names that are produced.
46      *
47      * @param stylePrefix the prefix to use for styles
48      */

49     public StyleModel(String JavaDoc stylePrefix) {
50         _stylePrefix = stylePrefix;
51     }
52
53     /**
54      * Set the style prefix.
55      * @param stylePrefix the style prefix
56      */

57     public void setStylePrefix(String JavaDoc stylePrefix) {
58         _stylePrefix = stylePrefix;
59     }
60
61     /**
62      * Get the style prefix
63      * @return the style prefix
64      */

65     public String JavaDoc getStylePrefix() {
66         return _stylePrefix;
67     }
68
69     /**
70      * Get the style class for an HTML table tag.
71      * @return the style class
72      */

73     public abstract String JavaDoc getTableClass();
74
75     /**
76      * Get the style class for an HTML caption tag.
77      * @return the style class
78      */

79     public abstract String JavaDoc getCaptionClass();
80
81     /**
82      * Get the style class for an HTML thead tag.
83      * @return the style class
84      */

85     public abstract String JavaDoc getTableHeadClass();
86
87     /**
88      * Get the style class for an HTML tfoot tag.
89      * @return the style class
90      */

91     public abstract String JavaDoc getTableFootClass();
92
93     /**
94      * Get the style class for an HTML tr tag rendered in the grid's header.
95      * @return the style class
96      */

97     public abstract String JavaDoc getHeaderRowClass();
98
99     /**
100      * Get the style class for an HTML tr tag rendered in the grid's footer.
101      * @return the style class
102      */

103     public abstract String JavaDoc getFooterRowClass();
104
105     /**
106      * Get the style class for an HTML tr tag rendered in a grid row. This style class will be used on
107      * even numbered rows.
108      * @return the style class
109      */

110     public abstract String JavaDoc getRowClass();
111
112     /**
113      * Get the style class for an HTML tr tag rendered in a grid row. This style class will be used on
114      * odd numbered rows.
115      * @return the style class
116      */

117     public abstract String JavaDoc getAltRowClass();
118
119     /**
120      * Get the style class for an HTML td tag rendered for a grid cell.
121      * @return the style class
122      */

123     public abstract String JavaDoc getDataCellClass();
124
125     /**
126      * Get the style class for an HTML th tag rendered for a grid's header cell.
127      * @return the style class
128      */

129     public abstract String JavaDoc getHeaderCellClass();
130
131     /**
132      * Get the style class for an HTML th tag for a grid's header cell whose data is sorted.
133      * @return the style class
134      */

135     public abstract String JavaDoc getHeaderCellSortedClass();
136
137     /**
138      * Get the style class for an HTML th tag for a grid's header cell that is sortable.
139      * @return the style class
140      */

141     public abstract String JavaDoc getHeaderCellSortableClass();
142
143     /**
144      * Get the style class for an HTML th tag for a grid's header cell that is filtered.
145      * @return the style class
146      */

147     public abstract String JavaDoc getHeaderCellFilteredClass();
148
149     /**
150      * Get the style class for an HTML td tag for a grid cell that is sorted.
151      * @return the style class
152      */

153     public abstract String JavaDoc getDataCellSortedClass();
154
155     /**
156      * Get the style class for an HTML td tag for a grid cell that is filtered.
157      * @return the style class
158      */

159     public abstract String JavaDoc getDataCellFilteredClass();
160
161     /**
162      * Build the value of the HTML style class attribute from the {@link List} of style classes. The style classes
163      * are converted into a string in their list order. For example, a list with contents:
164      * <pre>
165      * foo,bar,baz
166      * </pre>
167      * will be convereted into a style class whose value is <code>foo,bar,baz</code>
168      *
169      * @param styleClasses the classes to render into a style class value
170      * @return the string style class or an empty string if no style classes are provided
171      */

172     public String JavaDoc buildStyleClassValue(List JavaDoc/*<String>*/ styleClasses) {
173         if(styleClasses == null)
174             return EMPTY;
175
176         boolean styleWritten = false;
177         InternalStringBuilder buf = new InternalStringBuilder();
178         for(int i = 0; i < styleClasses.size(); i++) {
179             if(styleWritten)
180                 buf.append(SPACE);
181
182             if(styleClasses.get(i) != null) {
183                 buf.append(styleClasses.get(i));
184                 styleWritten = true;
185             }
186         }
187
188         if(!styleWritten)
189             return null;
190         else return buf.toString();
191     }
192
193     /**
194      * Given a base style class name, this method adds a style prefix to produce a complete style class.
195      * @param baseStyle the core style class name
196      * @return the style class
197      */

198     protected String JavaDoc buildStyleClass(String JavaDoc baseStyle) {
199         if(_stylePrefix != null)
200             return prefix(baseStyle);
201         else return baseStyle;
202     }
203
204     /* @todo: perf - could cache the style names once they've been produced */
205     /**
206      * Utility method to concatenate the given style class name and the style prefix.
207      * @param style the core style name
208      * @return the style class
209      */

210     private final String JavaDoc prefix(String JavaDoc style) {
211         InternalStringBuilder sb = new InternalStringBuilder(16);
212         sb.append(_stylePrefix);
213         if(style != null) {
214             sb.append(DELIM);
215             sb.append(style);
216         }
217         return sb.toString();
218     }
219 }
220
Popular Tags