001: /*
002: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
003: * @version $Id: TableBean.java 6406 2008-01-16 13:25:58Z wehrens $
004: */
005: package org.gridsphere.provider.portletui.beans;
006:
007: import org.gridsphere.provider.portletui.model.DefaultTableModel;
008: import org.gridsphere.services.core.persistence.QueryFilter;
009:
010: /**
011: * A <code>TableBean</code> provides a table element
012: */
013: public class TableBean extends BaseComponentBean implements TagBean {
014:
015: public static final String CURRENT_PAGE = "tablepage";
016: public static final String SHOW_ALL = "showall";
017: public static final String SHOW_PAGES = "showpages";
018:
019: public static final String NEXT_PAGE = "nextpage";
020: public static final String PREV_PAGE = "prevpage";
021:
022: protected DefaultTableModel defaultModel = null;
023: protected String cellSpacing = null;
024: protected String cellPadding = null;
025: protected String border = null;
026: protected String background = null;
027: protected String width = null;
028: protected String align = null;
029: protected String valign = null;
030: protected int currentPage = 0;
031: protected boolean isSortable = false;
032: protected boolean isZebra = false;
033: protected String sortableId = "t1";
034: private int rowCount = 0;
035: private int maxRows = -1;
036: private boolean showall = false;
037: protected String uris = "";
038: protected String uriString = "";
039: protected String title = null;
040: protected int numEntries = 0;
041: protected boolean filter = false;
042:
043: /**
044: * Constructs a default table bean
045: */
046: public TableBean() {
047: super ();
048: }
049:
050: /**
051: * Constructs a table bean with a supplied CSS style
052: *
053: * @param cssStyle the CSS style
054: */
055: public TableBean(String cssStyle) {
056: this .cssClass = cssStyle;
057: }
058:
059: public void setQueryFilter(QueryFilter filter) {
060: this .numEntries = filter.getTotalItems();
061: this .maxRows = filter.getMaxResults();
062: this .filter = true;
063: }
064:
065: /**
066: * Sets the default table model for this table
067: *
068: * @param defaultModel the table model
069: */
070: public void setTableModel(DefaultTableModel defaultModel) {
071: this .defaultModel = defaultModel;
072: }
073:
074: /**
075: * Returns the default table model
076: *
077: * @return the default table model
078: */
079: public DefaultTableModel getTableModel() {
080: return defaultModel;
081: }
082:
083: /**
084: * Returns true if a query filter is associated with this table
085: *
086: * @return true if a query filter is associated with this table
087: */
088: public boolean getFilter() {
089: return filter;
090: }
091:
092: public void setFilter(boolean filter) {
093: this .filter = filter;
094: }
095:
096: public int getNumEntries() {
097: return numEntries;
098: }
099:
100: public void setNumEntries(int numEntries) {
101: this .numEntries = numEntries;
102: }
103:
104: /**
105: * Sets the table width
106: *
107: * @param width the table width
108: */
109: public void setWidth(String width) {
110: this .width = width;
111: }
112:
113: /**
114: * Returns the table width
115: *
116: * @return the table width
117: */
118: public String getWidth() {
119: return width;
120: }
121:
122: /**
123: * Sets the table alignment e.g. "left", "center" or "right"
124: *
125: * @param align the table alignment
126: */
127: public void setAlign(String align) {
128: this .align = align;
129: }
130:
131: /**
132: * Returns the table alignment e.g. "left", "center" or "right"
133: *
134: * @return the table alignment
135: */
136: public String getAlign() {
137: return align;
138: }
139:
140: /**
141: * Returns the table vertical alignment e.g. 'top', 'bottom'
142: *
143: * @return the table vertical alignment
144: */
145: public String getValign() {
146: return valign;
147: }
148:
149: /**
150: * Sets the table horizontal alignment, e.g. 'top', 'bottom'
151: *
152: * @param valign the tables horizontal alignment
153: */
154: public void setValign(String valign) {
155: this .valign = valign;
156: }
157:
158: /**
159: * Sets the table cell spacing
160: *
161: * @param cellSpacing the table cell spacing
162: */
163: public void setCellSpacing(String cellSpacing) {
164: this .cellSpacing = cellSpacing;
165: }
166:
167: /**
168: * Returns the table cell spacing
169: *
170: * @return the table cell spacing
171: */
172: public String getCellSpacing() {
173: return cellSpacing;
174: }
175:
176: /**
177: * Sets the table cell spacing
178: *
179: * @param cellPadding the table cell padding
180: */
181: public void setCellPadding(String cellPadding) {
182: this .cellPadding = cellPadding;
183: }
184:
185: /**
186: * Returns the table cell padding
187: *
188: * @return the table cell padding
189: */
190: public String getCellPadding() {
191: return cellPadding;
192: }
193:
194: /**
195: * Sets the table background
196: *
197: * @param background the table background
198: */
199: public void setBackground(String background) {
200: this .background = background;
201: }
202:
203: /**
204: * Returns the table background
205: *
206: * @return the table background
207: */
208: public String getBackground() {
209: return background;
210: }
211:
212: /**
213: * Sets the table border
214: *
215: * @param border the table border
216: */
217: public void setBorder(String border) {
218: this .border = border;
219: }
220:
221: /**
222: * Returns the tableborder
223: *
224: * @return the table border
225: */
226: public String getBorder() {
227: return border;
228: }
229:
230: public void setZebra(boolean isZebra) {
231: this .isZebra = isZebra;
232: }
233:
234: public boolean getZebra() {
235: return isZebra;
236: }
237:
238: public void setSortable(boolean isSortable) {
239: this .isSortable = isSortable;
240: }
241:
242: public boolean getSortable() {
243: return isSortable;
244: }
245:
246: public void setSortableID(String sortableId) {
247: this .sortableId = sortableId;
248: }
249:
250: public String getSortableID() {
251: return sortableId;
252: }
253:
254: public void setRowCount(int rowCount) {
255: this .rowCount = rowCount;
256: }
257:
258: public int getRowCount() {
259: return rowCount;
260: }
261:
262: public void setCurrentPage(int currentPage) {
263: this .currentPage = currentPage;
264: }
265:
266: public int getCurrentPage() {
267: return currentPage;
268: }
269:
270: public void setMaxRows(int maxRows) {
271: this .maxRows = maxRows;
272: }
273:
274: public int getMaxRows() {
275: return maxRows;
276: }
277:
278: public String getTitle() {
279: return title;
280: }
281:
282: public void setTitle(String title) {
283: this .title = title;
284: }
285:
286: public boolean isShowall() {
287: return showall;
288: }
289:
290: public void setShowall(boolean showall) {
291: this .showall = showall;
292: }
293:
294: public void setURIString(String uriString) {
295: this .uriString = uriString;
296: }
297:
298: public String toStartString() {
299: StringBuffer sb = new StringBuffer();
300: if (isSortable) {
301: sb.append("<table class=\"sortable\" id=\"" + sortableId
302: + "\" ");
303: } else {
304: sb.append("<table" + getFormattedCss());
305: }
306: if (cellSpacing != null)
307: sb.append(" cellspacing=\"" + cellSpacing + "\" ");
308: if (cellPadding != null)
309: sb.append(" cellpadding=\"" + cellPadding + "\" ");
310: if (border != null)
311: sb.append(" border=\"" + border + "\" ");
312: if (width != null)
313: sb.append(" width=\"" + width + "\" ");
314: if (align != null)
315: sb.append(" align=\"" + align + "\" ");
316: /// Removed for XHTML 1.0 Strict compliance
317: /*
318: if (valign != null) {
319: sb.append(" valign=\"" + valign + "\" ");
320: }
321: */
322: sb.append(">");
323: if (title != null)
324: sb.append("<caption>" + title + "</caption>");
325: if (defaultModel != null)
326: sb.append(defaultModel.toStartString());
327: return sb.toString();
328: }
329:
330: public String toEndString() {
331: StringBuffer sb = new StringBuffer();
332: sb.append("</table>");
333: String uri = "";
334: if (showall) {
335: uri = uriString;
336: sb.append("<p>"); // added for XHTML 1.0 Strict compliance
337: String showPages = TableBean.SHOW_PAGES;
338: showPages = "rp_" + showPages;
339: sb.append("<a href=\"" + uri + "?" + showPages + "\">"
340: + this .getLocalizedText("SHOW_PAGES") + "</a>");
341: sb.append("</p>"); // added for XHTML 1.0 Strict compliance
342: }
343: if (maxRows > 0) {
344: int numpages = 0;
345: // formula to figure out how many pages there should be-- if it divides evenly then
346: // use that, otherwise need to add 1
347: if (numEntries != 0) {
348: numpages = numEntries / maxRows;
349: numpages += numEntries % maxRows > 0 ? 1 : 0;
350: } else {
351: numpages = rowCount / maxRows;
352: numpages += rowCount % maxRows > 0 ? 1 : 0;
353: }
354:
355: //System.err.println("numpages = " + numpages);
356: int dispPage = currentPage + 1;
357: if ((dispPage == numpages) && (numpages == 1))
358: return sb.toString();
359: int c = 0;
360: //System.err.println("maxrows=" + maxRows + " numEntries=" + numEntries + " rowCount=" + rowCount + " numpages=" + numpages);
361:
362: sb.append("<p>"); // added for XHTML 1.0 Strict compliance
363: sb.append(this .getLocalizedText("PAGE") + dispPage
364: + this .getLocalizedText("OUT_OF_PAGES") + numpages);
365:
366: for (int i = 0; i < numpages; i++) {
367: c = i + 1;
368: if (c == dispPage) {
369: sb.append(" | <b>" + c + "</b>");
370: } else {
371: // create an actionlink
372: uris = uriString;
373: //System.err.println("uri = " + uris);
374: String curPage = TableBean.CURRENT_PAGE;
375: curPage = "rp_" + curPage;
376: uri = uris + "?" + curPage + "=" + i;
377: sb.append(" | " + "<a href=\"" + uri + "\">" + c
378: + "</a>");
379: }
380: }
381: if (!filter) {
382: uri = uriString;
383: sb.append(" | ");
384: String showall = TableBean.SHOW_ALL;
385: showall = "rp_" + TableBean.SHOW_ALL;
386: sb.append("<a href=\"" + uri + "?" + showall + "\">"
387: + this .getLocalizedText("SHOW_ALL") + "</a>");
388: }
389: sb.append("</p>"); // added for XHTML 1.0 Strict compliance
390: rowCount = 0;
391: }
392:
393: return sb.toString();
394: }
395:
396: }
|