001: /**
002: *
003: */package clime.messadmin.providers.spi;
004:
005: import java.util.ResourceBundle;
006:
007: import javax.servlet.ServletContext;
008: import javax.servlet.http.HttpSession;
009:
010: import clime.messadmin.i18n.I18NSupport;
011: import clime.messadmin.model.Server;
012:
013: /**
014: * Base implementation class for DataProvider displaying tabular data.
015: * @author Cédrik LIME
016: */
017: public abstract class BaseTabularDataProvider {
018:
019: /**
020: *
021: */
022: public BaseTabularDataProvider() {
023: super ();
024: }
025:
026: /**
027: * Convenience method for i18n
028: * @since 4.1
029: */
030: protected ClassLoader getClassLoader(final HttpSession session) {
031: return Server.getInstance().getApplication(
032: session.getServletContext()).getApplicationInfo()
033: .getClassLoader();
034: }
035:
036: /**
037: * Convenience method for i18n
038: * @since 4.1
039: */
040: protected ClassLoader getClassLoader(final ServletContext context) {
041: return Server.getInstance().getApplication(context)
042: .getApplicationInfo().getClassLoader();
043: }
044:
045: /**
046: * Convenience method for i18n
047: * @since 4.1
048: */
049: protected ResourceBundle getResourceBundle(String baseName,
050: ClassLoader cl) {
051: return I18NSupport.getResourceBundle(baseName, cl);
052: }
053:
054: /**
055: * Convenience method for i18n
056: * @since 4.1
057: */
058: protected String getLocalizedMessage(String baseName,
059: ClassLoader cl, String key) {
060: return I18NSupport.getLocalizedMessage(baseName, cl, key);
061: }
062:
063: /**
064: * Convenience method for i18n
065: * @since 4.1
066: */
067: protected String getLocalizedMessage(String baseName,
068: ClassLoader cl, String key, Object[] args) {
069: return I18NSupport.getLocalizedMessage(baseName, cl, key, args);
070: }
071:
072: // /**
073: // * @param object
074: // * @return specific data labels for given object, or null if it can be determined
075: // */
076: // public abstract String[] getTabularDataLabels(final Object object);
077: // /**
078: // * @param object
079: // * @return specific data values for given object, or null if it can be determined
080: // */
081: // public abstract Object[][] getTabularData(final Object object);
082: //
083: // protected abstract String getTableCaption(String[] labels, Object[][] data);
084: //
085: // protected String getXHTMLData(Object object) {
086: // try {
087: // String[] labels = getTabularDataLabels(object);
088: // Object[][] values = getTabularData(object);
089: // return buildXHTML(labels, values, tableId, getTableCaption(labels, values));
090: // } catch (RuntimeException rte) {
091: // return "Error in " + this.getClass().getName() + ": " + rte;
092: // }
093: // }
094:
095: protected String buildXHTML(String[] labels, Object[][] values,
096: String tableId, String tableCaption) {
097: if (labels == null || values == null || values.length == 0) {
098: return "";
099: }
100: StringBuffer buffer = new StringBuffer(128 + 16 * labels.length
101: + 16 * values.length * labels.length);
102: buffer
103: .append("<table id=\"")
104: .append(tableId)
105: .append(
106: "\" class=\"strippable\" style=\"text-align: left;\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n");
107: appendCaption(buffer, tableCaption);
108: appendHeader(buffer, labels);
109: appendFooter(buffer, labels);
110: appendBody(buffer, values);
111: buffer.append("</table>\n");
112: return buffer.toString();
113: }
114:
115: protected void appendCaption(StringBuffer buffer,
116: String tableCaption) {
117: buffer.append("<caption style=\"font-variant: small-caps;\">")
118: .append(tableCaption).append("</caption>\n");
119: }
120:
121: protected void appendHeader(StringBuffer buffer, String[] labels) {
122: buffer.append("<thead><tr>\n");
123: for (int i = 0; i < labels.length; ++i) {
124: String label = labels[i];
125: buffer.append("<th>");
126: appendHeaderLabel(buffer, label);
127: buffer.append("</th>");
128: }
129: buffer.append("\n</tr></thead>\n");
130: }
131:
132: protected void appendHeaderLabel(StringBuffer buffer, String label) {
133: if (label != null) {
134: buffer.append(label);
135: }
136: }
137:
138: protected void appendBody(StringBuffer buffer, Object[][] values) {
139: buffer.append("<tbody>\n");
140: for (int i = 0; i < values.length; ++i) {
141: Object[] row = values[i];
142: appendRow(buffer, row);
143: }
144: buffer.append("</tbody>\n");
145: }
146:
147: protected void appendRow(StringBuffer buffer, Object[] row) {
148: buffer.append("<tr>\n");
149: for (int j = 0; j < row.length; ++j) {
150: Object value = row[j];
151: buffer.append("<td>");
152: appendValue(buffer, value);
153: buffer.append("</td>");
154: }
155: buffer.append("</tr>\n");
156: }
157:
158: protected void appendValue(StringBuffer buffer, Object value) {
159: if (value != null) {
160: // buffer.append("<span title=\"").append(value.getClass()).append("\">");
161: buffer.append(value);
162: // buffer.append("</span>");
163: }
164: }
165:
166: protected void appendFooter(StringBuffer buffer, String[] labels) {
167: // buffer.append("<tfoot><tr>\n");
168: // for (int i = 0; i < labels.length; ++i) {
169: // String label = labels[i];
170: // buffer.append("<th>");
171: // appendFooterLabel(buffer, label);
172: // buffer.append("</th>");
173: // }
174: // buffer.append("\n</tr></tfoot>\n");
175: }
176:
177: protected void appendFooterLabel(StringBuffer buffer, String label) {
178: appendHeaderLabel(buffer, label);
179: }
180: }
|