001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.databinding.datagrid.api.rendering;
020:
021: import org.apache.beehive.netui.util.internal.InternalStringBuilder;
022:
023: import java.util.List;
024:
025: /**
026: * <p>
027: * JavaBean base class that creates HTML style class names used to render various HTML elements in the data grid.
028: * </p>
029: */
030: public abstract class StyleModel {
031:
032: private static final String DELIM = "-";
033: private static final String SPACE = " ";
034: private static final String EMPTY = "";
035:
036: private String _stylePrefix = null;
037:
038: /**
039: * Default constructor.
040: */
041: public StyleModel() {
042: }
043:
044: /**
045: * Constructor that takes a style prefix string. If a style prefix is provided, StyleModel subclasses
046: * should use this as a prefix to any style names that are produced.
047: *
048: * @param stylePrefix the prefix to use for styles
049: */
050: public StyleModel(String stylePrefix) {
051: _stylePrefix = stylePrefix;
052: }
053:
054: /**
055: * Set the style prefix.
056: * @param stylePrefix the style prefix
057: */
058: public void setStylePrefix(String stylePrefix) {
059: _stylePrefix = stylePrefix;
060: }
061:
062: /**
063: * Get the style prefix
064: * @return the style prefix
065: */
066: public String getStylePrefix() {
067: return _stylePrefix;
068: }
069:
070: /**
071: * Get the style class for an HTML table tag.
072: * @return the style class
073: */
074: public abstract String getTableClass();
075:
076: /**
077: * Get the style class for an HTML caption tag.
078: * @return the style class
079: */
080: public abstract String getCaptionClass();
081:
082: /**
083: * Get the style class for an HTML thead tag.
084: * @return the style class
085: */
086: public abstract String getTableHeadClass();
087:
088: /**
089: * Get the style class for an HTML tfoot tag.
090: * @return the style class
091: */
092: public abstract String getTableFootClass();
093:
094: /**
095: * Get the style class for an HTML tr tag rendered in the grid's header.
096: * @return the style class
097: */
098: public abstract String getHeaderRowClass();
099:
100: /**
101: * Get the style class for an HTML tr tag rendered in the grid's footer.
102: * @return the style class
103: */
104: public abstract String getFooterRowClass();
105:
106: /**
107: * Get the style class for an HTML tr tag rendered in a grid row. This style class will be used on
108: * even numbered rows.
109: * @return the style class
110: */
111: public abstract String getRowClass();
112:
113: /**
114: * Get the style class for an HTML tr tag rendered in a grid row. This style class will be used on
115: * odd numbered rows.
116: * @return the style class
117: */
118: public abstract String getAltRowClass();
119:
120: /**
121: * Get the style class for an HTML td tag rendered for a grid cell.
122: * @return the style class
123: */
124: public abstract String getDataCellClass();
125:
126: /**
127: * Get the style class for an HTML th tag rendered for a grid's header cell.
128: * @return the style class
129: */
130: public abstract String getHeaderCellClass();
131:
132: /**
133: * Get the style class for an HTML th tag for a grid's header cell whose data is sorted.
134: * @return the style class
135: */
136: public abstract String getHeaderCellSortedClass();
137:
138: /**
139: * Get the style class for an HTML th tag for a grid's header cell that is sortable.
140: * @return the style class
141: */
142: public abstract String getHeaderCellSortableClass();
143:
144: /**
145: * Get the style class for an HTML th tag for a grid's header cell that is filtered.
146: * @return the style class
147: */
148: public abstract String getHeaderCellFilteredClass();
149:
150: /**
151: * Get the style class for an HTML td tag for a grid cell that is sorted.
152: * @return the style class
153: */
154: public abstract String getDataCellSortedClass();
155:
156: /**
157: * Get the style class for an HTML td tag for a grid cell that is filtered.
158: * @return the style class
159: */
160: public abstract String getDataCellFilteredClass();
161:
162: /**
163: * Build the value of the HTML style class attribute from the {@link List} of style classes. The style classes
164: * are converted into a string in their list order. For example, a list with contents:
165: * <pre>
166: * foo,bar,baz
167: * </pre>
168: * will be convereted into a style class whose value is <code>foo,bar,baz</code>
169: *
170: * @param styleClasses the classes to render into a style class value
171: * @return the string style class or an empty string if no style classes are provided
172: */
173: public String buildStyleClassValue(List/*<String>*/styleClasses) {
174: if (styleClasses == null)
175: return EMPTY;
176:
177: boolean styleWritten = false;
178: InternalStringBuilder buf = new InternalStringBuilder();
179: for (int i = 0; i < styleClasses.size(); i++) {
180: if (styleWritten)
181: buf.append(SPACE);
182:
183: if (styleClasses.get(i) != null) {
184: buf.append(styleClasses.get(i));
185: styleWritten = true;
186: }
187: }
188:
189: if (!styleWritten)
190: return null;
191: else
192: return buf.toString();
193: }
194:
195: /**
196: * Given a base style class name, this method adds a style prefix to produce a complete style class.
197: * @param baseStyle the core style class name
198: * @return the style class
199: */
200: protected String buildStyleClass(String baseStyle) {
201: if (_stylePrefix != null)
202: return prefix(baseStyle);
203: else
204: return baseStyle;
205: }
206:
207: /* @todo: perf - could cache the style names once they've been produced */
208: /**
209: * Utility method to concatenate the given style class name and the style prefix.
210: * @param style the core style name
211: * @return the style class
212: */
213: private final String prefix(String style) {
214: InternalStringBuilder sb = new InternalStringBuilder(16);
215: sb.append(_stylePrefix);
216: if (style != null) {
217: sb.append(DELIM);
218: sb.append(style);
219: }
220: return sb.toString();
221: }
222: }
|