001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.faces.component.scroller;
023:
024: import javax.faces.component.NamingContainer;
025: import javax.faces.component.UIComponent;
026: import javax.faces.component.UIData;
027: import javax.faces.component.UIForm;
028: import javax.faces.context.FacesContext;
029: import javax.faces.context.ResponseWriter;
030: import javax.faces.el.ValueBinding;
031: import javax.faces.render.Renderer;
032: import java.io.IOException;
033: import java.util.Iterator;
034: import java.util.Map;
035:
036: /**
037: * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
038: * @version $Revision: 8784 $
039: */
040: public class ScrollerRenderer extends Renderer {
041:
042: private final String PREVIOUS = "Previous";
043: private final String NEXT = "Next";
044: private final String FIRST = "First";
045: private final String LAST = "Last";
046:
047: public void encodeBegin(FacesContext facesContext,
048: UIComponent component) throws IOException {
049: // Get Attributes
050: String styleClass = (String) getAttribute(facesContext,
051: component, "styleClass");
052: String datatableId = (String) getAttribute(facesContext,
053: component, "for");
054:
055: // Get Form Component
056: UIComponent formComponent = component;
057: while (!(formComponent instanceof UIForm)) {
058: formComponent = formComponent.getParent();
059: }
060: String formComponentId = formComponent
061: .getClientId(facesContext);
062:
063: // Get Writer
064: ResponseWriter writer = facesContext.getResponseWriter();
065:
066: // Get UIData
067: String id = getId(datatableId, component
068: .getClientId(facesContext));
069: UIData data = (UIData) findComponent(
070: facesContext.getViewRoot(), facesContext, id);
071:
072: int first = data.getFirst();
073: int rowCount = data.getRowCount();
074: int pageSize = data.getRows();
075: if (pageSize <= 0) {
076: pageSize = rowCount;
077: }
078:
079: int pages = rowCount / pageSize;
080: if (rowCount % pageSize != 0) {
081: pages++;
082: }
083:
084: int currentPage = first / pageSize;
085: if (first >= rowCount - pageSize) {
086: currentPage = pages - 1;
087: }
088:
089: int startPage = 0;
090: int endPage = pages;
091:
092: if (currentPage > 0) {
093: writeLink(writer, component, formComponentId, component
094: .getClientId(facesContext), PREVIOUS, styleClass);
095: }
096: if (first < rowCount - pageSize) {
097: writeLink(writer, component, formComponentId, component
098: .getClientId(facesContext), NEXT, styleClass);
099: }
100:
101: writeHiddenField(writer, component, component
102: .getClientId(facesContext));
103: }
104:
105: public void decode(FacesContext facesContext, UIComponent component) {
106: String id = component.getClientId(facesContext);
107: Map parameters = facesContext.getExternalContext()
108: .getRequestParameterMap();
109: String response = (String) parameters.get(id);
110:
111: String datatableId = (String) getAttribute(facesContext,
112: component, "for");
113:
114: // Get UIData
115: String dataId = getId(datatableId, component
116: .getClientId(facesContext));
117: UIData data = (UIData) findComponent(
118: facesContext.getViewRoot(), facesContext, dataId);
119:
120: int first = data.getFirst();
121: int rowCount = data.getRowCount();
122: int pageSize = data.getRows();
123: if (pageSize <= 0) {
124: pageSize = rowCount;
125: }
126:
127: if (response.equals(PREVIOUS)) {
128: first -= pageSize;
129: } else if (response.equals(NEXT)) {
130: first += pageSize;
131: }
132:
133: if (first < 0) {
134: first = 0;
135: }
136: data.setFirst(first);
137: }
138:
139: private Object getAttribute(FacesContext facesContext,
140: UIComponent component, String name) {
141: ValueBinding vb = component.getValueBinding(name);
142: if (vb != null) {
143: return vb.getValue(facesContext);
144: } else {
145: return component.getAttributes().get(name);
146: }
147: }
148:
149: public String getId(String id, String base) {
150: String separator = "" + NamingContainer.SEPARATOR_CHAR;
151: String[] idSplitted = id.split(separator);
152: String[] baseSplitted = base.split(separator);
153: StringBuffer buffer = new StringBuffer();
154: for (int i = 0; i < (baseSplitted.length - idSplitted.length); i++) {
155: buffer.append(baseSplitted[i]);
156: buffer.append(separator);
157: }
158: buffer.append(id);
159: return buffer.toString();
160: }
161:
162: private UIComponent findComponent(UIComponent component,
163: FacesContext facesContext, String id) {
164: String componentId = component.getClientId(facesContext);
165: if (componentId.equals(id)) {
166: return component;
167: }
168: Iterator it = component.getChildren().iterator();
169: while (it.hasNext()) {
170: UIComponent comp = (UIComponent) it.next();
171: UIComponent found = findComponent(comp, facesContext, id);
172: if (found != null) {
173: return found;
174: }
175:
176: }
177: return null;
178: }
179:
180: private void writeHiddenField(ResponseWriter writer,
181: UIComponent component, String id) throws IOException {
182: writer.startElement("input", component);
183: writer.writeAttribute("type", "hidden", null);
184: writer.writeAttribute("name", id, null);
185: writer.endElement("input");
186: }
187:
188: private void writeLink(ResponseWriter writer,
189: UIComponent component, String formComponentId, String id,
190: String value, String styleClass) throws IOException {
191: writer.writeText(" ", null);
192: writer.startElement("input", component);
193: writer.writeAttribute("value", value, null);
194: writer.writeAttribute("type", "submit", null);
195: writer.writeAttribute("onclick", onclickCode(formComponentId,
196: id, value), null);
197: if (styleClass != null) {
198: writer.writeAttribute("class", styleClass, "styleClass");
199: }
200: writer.endElement("input");
201:
202: /*
203: writer.writeText(" ", null);
204: writer.startElement("a", component);
205: writer.writeAttribute("href", "#", null);
206: writer.writeAttribute("onclick", onclickCode(formComponentId, id, value), null);
207: if (styleClass != null)
208: {
209: writer.writeAttribute("class", styleClass, "styleClass");
210: }
211: writer.writeText(value, null);
212: writer.endElement("a");
213: */
214: }
215:
216: private String onclickCode(String formComponentId, String id,
217: String value) {
218: StringBuffer buffer = new StringBuffer();
219: buffer.append("document.forms[");
220: buffer.append("'");
221: buffer.append(formComponentId);
222: buffer.append("']");
223: buffer.append(".elements['");
224: buffer.append(id);
225: buffer.append("'].value='");
226: buffer.append(value);
227: buffer.append("';");
228: buffer.append(" document.forms[");
229: buffer.append("'");
230: buffer.append(formComponentId);
231: buffer.append("'");
232: buffer.append("].submit()");
233: buffer.append("; return false;");
234: return buffer.toString();
235: }
236:
237: }
|