001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.example.common.tags.example.component;
016:
017: import java.io.IOException;
018: import java.io.Writer;
019: import java.util.List;
020: import javax.servlet.jsp.JspException;
021: import org.araneaframework.jsp.UiEvent;
022: import org.araneaframework.jsp.UiUpdateEvent;
023: import org.araneaframework.jsp.tag.PresentationTag;
024: import org.araneaframework.jsp.tag.uilib.list.ListTag;
025: import org.araneaframework.jsp.util.JspScriptUtil;
026: import org.araneaframework.jsp.util.JspUpdateRegionUtil;
027: import org.araneaframework.jsp.util.JspUtil;
028: import org.araneaframework.jsp.util.JspWidgetCallUtil;
029: import org.araneaframework.uilib.list.ListWidget;
030: import org.araneaframework.uilib.list.SequenceHelper;
031: import org.araneaframework.uilib.util.MessageUtil;
032:
033: /**
034: * List widget sequence footer tag.
035: *
036: * @author Oleg Mürk
037: * @author Taimo Peelo (taimo@araneaframework.org)
038: *
039: * @jsp.tag
040: * name = "componentListFooter"
041: * body-content = "empty"
042: */
043: public class ComponentListFooterTag extends PresentationTag {
044: public final static String PREVIOUS_PAGE_EVENT_ID = "previousPage";
045: public final static String NEXT_PAGE_EVENT_ID = "nextPage";
046: public final static String PREVIOUS_BLOCK_EVENT_ID = "previousBlock";
047: public final static String NEXT_BLOCK_EVENT_ID = "nextBlock";
048: public final static String FIRST_PAGE_EVENT_ID = "firstPage";
049: public final static String LAST_PAGE_EVENT_ID = "lastPage";
050: public final static String JUMP_TO_PAGE_EVENT_ID = "jumpToPage";
051:
052: public final static String PREVIOUS_PAGE_LABEL_ID = "list.sequence.previous_page";
053: public final static String NEXT_PAGE_LABEL_ID = "list.sequence.next_page";
054:
055: public final static String LEFT_CELL_STYLE = "left";
056: public final static String RIGHT_CELL_STYLE = "right";
057: public final static String SEQUENCE_CELL_STYLE = "sequence";
058:
059: public final static String SHOW_SLICE_EVENT_ID = "showSlice";
060: public final static String SHOW_ALL_EVENT_ID = "showAll";
061:
062: public final static String DEFAULT_NO_DATA_STRING_ID = "list.info.noData";
063:
064: protected String listId;
065:
066: protected String numberStyleClass = "nr";
067: protected String infoStyleClass = "info";
068: protected String firstClass = "first";
069: protected String prevClass = "prev";
070: protected String nextClass = "next";
071: protected String lastClass = "last";
072: protected String showAll = "demo.showAll";
073: protected String showPartial = "demo.showPartial";
074:
075: protected String noDataStringId = ComponentListFooterTag.DEFAULT_NO_DATA_STRING_ID;
076:
077: // update regions
078: private String updateRegions;
079: private String globalUpdateRegions;
080:
081: private List updateRegionNames;
082:
083: public ComponentListFooterTag() {
084: styleClass = "pages";
085: }
086:
087: public int doStartTag(Writer out) throws Exception {
088: super .doStartTag(out);
089:
090: this .updateRegionNames = JspUpdateRegionUtil
091: .getUpdateRegionNames(pageContext, updateRegions,
092: globalUpdateRegions);
093:
094: // Get list data
095: listId = (String) requireContextEntry(ListTag.LIST_FULL_ID_KEY);
096: ListWidget.ViewModel viewModel = (ListWidget.ViewModel) requireContextEntry(ListTag.LIST_VIEW_MODEL_KEY);
097:
098: // Get sequnce data
099: SequenceHelper.ViewModel sequenceViewModel = viewModel
100: .getSequence();
101:
102: long firstPage = sequenceViewModel.getFirstPage().longValue();
103: long lastPage = sequenceViewModel.getLastPage().longValue();
104:
105: long blockFirstPage = sequenceViewModel.getBlockFirstPage()
106: .longValue();
107: long blockLastPage = sequenceViewModel.getBlockLastPage()
108: .longValue();
109:
110: long currentPage = sequenceViewModel.getCurrentPage()
111: .longValue();
112: long totalItemCount = sequenceViewModel.getTotalItemCount()
113: .longValue();
114: boolean allItemsShown = sequenceViewModel.getAllItemsShown()
115: .booleanValue();
116:
117: long firstShown = sequenceViewModel.getPageFirstItem()
118: .longValue();
119: long lastShown = sequenceViewModel.getPageLastItem()
120: .longValue();
121:
122: JspUtil.writeOpenStartTag(out, "div");
123: JspUtil.writeAttribute(out, "class", getStyleClass());
124: JspUtil.writeAttribute(out, "style", getStyle());
125: JspUtil.writeCloseStartTag(out);
126:
127: if (totalItemCount > 0 && !allItemsShown) {
128: /* FIRST, PREV */
129: JspUtil.writeOpenStartTag(out, "div");
130: JspUtil.writeAttribute(out, "class", numberStyleClass);
131: JspUtil.writeCloseStartTag(out);
132:
133: writeOpenEventLink(out, FIRST_PAGE_EVENT_ID, null,
134: firstPage != currentPage, firstClass);
135: out.write(" ");
136: JspUtil.writeEndTag_SS(out, "a");
137:
138: writeOpenEventLink(out, PREVIOUS_PAGE_EVENT_ID, null,
139: firstPage < currentPage, prevClass);
140: out.write(" ");
141: JspUtil.writeEndTag_SS(out, "a");
142:
143: /* END FIRST, PREV */
144:
145: for (long page = blockFirstPage; page <= blockLastPage; page++) {
146: // Jump to page
147: writeOpenEventLink(out, JUMP_TO_PAGE_EVENT_ID,
148: new Long(page).toString(), page != currentPage,
149: page == currentPage ? "active" : null);
150: JspUtil.writeEscaped(out, new Long(
151: (page - firstPage) + 1).toString());
152: JspUtil.writeEndTag_SS(out, "a");
153: }
154:
155: writeOpenEventLink(out, NEXT_PAGE_EVENT_ID, null,
156: currentPage < lastPage, nextClass);
157: out.write(" ");
158: JspUtil.writeEndTag_SS(out, "a");
159:
160: writeOpenEventLink(out, LAST_PAGE_EVENT_ID, null,
161: lastPage != currentPage, lastClass);
162: out.write(" ");
163: JspUtil.writeEndTag_SS(out, "a");
164:
165: JspUtil.writeEndTag(out, "div"); // numbers
166:
167: writeInfo(out, totalItemCount, allItemsShown, firstShown,
168: lastShown);
169: } else {
170: if (totalItemCount == 0)
171: JspUtil.writeEscaped(out, JspUtil.getResourceString(
172: pageContext, noDataStringId));
173: else if (totalItemCount != 0 && allItemsShown) {
174: writeInfo(out, totalItemCount, allItemsShown,
175: firstShown, lastShown);
176: }
177: }
178:
179: JspUtil.writeEndTag(out, "div");
180:
181: return EVAL_BODY_INCLUDE;
182: }
183:
184: /* ***********************************************************************************
185: * Tag attributes
186: * ***********************************************************************************/
187:
188: /**
189: * @jsp.attribute
190: * type = "java.lang.String"
191: * required = "false"
192: * description = "Data string id for empty list message."
193: */
194: public void setNoDataStringId(String noDataStringId)
195: throws JspException {
196: this .noDataStringId = (String) evaluateNotNull(
197: "noDataStringId", noDataStringId, String.class);
198: }
199:
200: /**
201: * @jsp.attribute
202: * type = "java.lang.String"
203: * required = "false"
204: * description = "Style for list footer info."
205: */
206: public void setInfoStyleClass(String infoStyleClass) {
207: this .infoStyleClass = infoStyleClass;
208: }
209:
210: /**
211: * @jsp.attribute
212: * type = "java.lang.String"
213: * required = "false"
214: * description = "Style for list footer numbers."
215: */
216: public void setNumberStyleClass(String numberStyleClass) {
217: this .numberStyleClass = numberStyleClass;
218: }
219:
220: /* ***********************************************************************************
221: * Helper functions
222: * ***********************************************************************************/
223:
224: protected void writeInfo(Writer out, long totalItemCount,
225: boolean allItemsShown, long firstShown, long lastShown)
226: throws IOException, JspException {
227: JspUtil.writeOpenStartTag(out, "div");
228: JspUtil.writeAttribute(out, "class", infoStyleClass);
229: JspUtil.writeCloseStartTag(out);
230:
231: out.write(MessageUtil.localize("common.Showing",
232: getEnvironment())
233: + " [");
234: out.write(new Long(firstShown).toString());
235: out.write("-");
236: out.write(new Long(lastShown).toString());
237: out.write("]. "
238: + MessageUtil
239: .localize("common.Total", getEnvironment())
240: + " ");
241: JspUtil.writeEscaped(out, new Long(totalItemCount).toString());
242: out.write(". ");
243:
244: JspUtil.writeOpenStartTag(out, "a");
245: JspUtil.writeAttribute(out, "class", "aranea-link-button");
246: JspUtil.writeAttribute(out, "href", "#");
247:
248: JspUtil
249: .writeEventAttributes(out,
250: allItemsShown ? getShowSliceEvent()
251: : getShowAllEvent());
252: JspWidgetCallUtil.writeSubmitScriptForEvent(out, "onclick");
253:
254: JspUtil.writeCloseStartTag_SS(out);
255: JspUtil.writeEscaped(out, JspUtil.getResourceString(
256: pageContext, allItemsShown ? showPartial : showAll));
257: JspUtil.writeEndTag_SS(out, "a");
258:
259: JspUtil.writeEndTag(out, "div"); //info
260: }
261:
262: protected void writeOpenEventLink(Writer out, String eventId,
263: String eventParam, boolean enabled, String styleClass)
264: throws Exception {
265: UiEvent event = createListEvent(eventId, eventParam);
266:
267: JspUtil.writeOpenStartTag(out, "a");
268: if (enabled) {
269: JspUtil.writeAttribute(out, "class", "aranea-link-button "
270: + styleClass);
271: JspUtil.writeAttribute(out, "href", "#");
272: } else {
273: JspUtil.writeAttribute(out, "class", styleClass);
274: JspUtil.writeAttribute(out, "href",
275: "javascript:return false;");
276: }
277:
278: JspUtil.writeEventAttributes(out, event);
279:
280: if (enabled)
281: JspWidgetCallUtil.writeSubmitScriptForEvent(out, "onclick");
282: else
283: JspScriptUtil.writeEmptyEventAttribute(out, "onclick");
284: JspUtil.writeCloseStartTag_SS(out);
285: }
286:
287: protected UiEvent createListEvent(String eventId, String eventParam) {
288: UiUpdateEvent event = new UiUpdateEvent();
289: event.setId(eventId);
290: event.setParam(eventParam);
291: event.setTarget(listId);
292: event.setUpdateRegionNames(updateRegionNames);
293: return event;
294: }
295:
296: protected UiEvent getShowSliceEvent() {
297: UiUpdateEvent result = new UiUpdateEvent();
298: result.setId(SHOW_SLICE_EVENT_ID);
299: result.setUpdateRegionNames(updateRegionNames);
300: result.setTarget(listId);
301: return result;
302: }
303:
304: protected UiEvent getShowAllEvent() {
305: UiUpdateEvent result = new UiUpdateEvent();
306: result.setUpdateRegionNames(updateRegionNames);
307: result.setId(SHOW_ALL_EVENT_ID);
308: result.setTarget(listId);
309: return result;
310: }
311:
312: /**
313: * @jsp.attribute
314: * type = "java.lang.String"
315: * required = "false"
316: * description = "Enumerates the regions of markup to be updated in this widget scope. Please see <code><ui:updateRegion></code> for details."
317: */
318: public void setUpdateRegions(String updateRegions)
319: throws JspException {
320: this .updateRegions = (String) evaluate("updateRegions",
321: updateRegions, String.class);
322: }
323:
324: /**
325: * @jsp.attribute
326: * type = "java.lang.String"
327: * required = "false"
328: * description = "Enumerates the regions of markup to be updated globally. Please see <code><ui:updateRegion></code> for details."
329: */
330: public void setGlobalUpdateRegions(String globalUpdateRegions)
331: throws JspException {
332: this .globalUpdateRegions = (String) evaluate(
333: "globalUpdateRegions", globalUpdateRegions,
334: String.class);
335: }
336: }
|