001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/tags/sakai_2-4-1/samigo-app/src/java/org/sakaiproject/tool/assessment/jsf/renderer/PagerButtonRenderer.java $
003: * $Id: PagerButtonRenderer.java 15083 2006-09-20 20:03:55Z lydial@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.jsf.renderer;
021:
022: import java.io.IOException;
023: import javax.faces.component.UIComponent;
024: import javax.faces.component.UIOutput;
025: import javax.faces.context.FacesContext;
026: import javax.faces.context.ResponseWriter;
027: import javax.faces.render.Renderer;
028:
029: /**
030: * <p>Description: </p>
031: * <p>Render a next/previous control for a pager attached to a dataTable.</p>
032: * <p>Copyright: Copyright (c) 2004</p>
033: * <p>Organization: Sakai Project</p>
034: * @author Ed Smiley
035: * @version $Id: PagerButtonRenderer.java 15083 2006-09-20 20:03:55Z lydial@stanford.edu $
036: */
037:
038: public class PagerButtonRenderer extends Renderer {
039: // some IO stuff
040: private static final String numberValues[] = { "10", "20", "30",
041: "50", "100" };
042: private static final String DISABLED_ATTRIB = " disabled=\"disabled\"";
043: // this needs to be internationalized, tricky because of fragments
044: private static final String OF = "of";
045: private static final String VIEWING = "Viewing";
046: private static final String SHOW = "Show";
047: private static final String ITEMS = "items";
048: private static final String ITEMS_PER = "items per page";
049:
050: public boolean supportsComponentType(UIComponent component) {
051: return (component instanceof UIOutput);
052: }
053:
054: public void decode(FacesContext context, UIComponent component) {
055: }
056:
057: public void encodeChildren(FacesContext context,
058: UIComponent component) throws IOException {
059: ;
060: }
061:
062: public void encodeBegin(FacesContext context, UIComponent component)
063: throws IOException {
064: ;
065: }
066:
067: /**
068: * <p>Faces render output method .</p>
069: * <p>Method Generator: org.sakaiproject.tool.assessment.devtoolsRenderMaker</p>
070: *
071: * @param context <code>FacesContext</code> for the current request
072: * @param component <code>UIComponent</code> being rendered
073: *
074: * @throws IOException if an input/output error occurs
075: */
076: public void encodeEnd(FacesContext context, UIComponent component)
077: throws IOException {
078:
079: ResponseWriter writer = context.getResponseWriter();
080: String formId = (String) component.getAttributes()
081: .get("formId");
082: String firstItem = (String) component.getAttributes().get(
083: "firstItem");
084: String lastItem = (String) component.getAttributes().get(
085: "lastItem");
086: String dataTableId = (String) component.getAttributes().get(
087: "dataTableId");
088: String prevText = (String) component.getAttributes().get(
089: "prevText");
090: String nextText = (String) component.getAttributes().get(
091: "nextText");
092: String numItems = (String) component.getAttributes().get(
093: "numItems");
094: String totalItems = (String) component.getAttributes().get(
095: "totalItems");
096: String prevDisabled = (String) component.getAttributes().get(
097: "prevDisabled");
098: String nextDisabled = (String) component.getAttributes().get(
099: "nextDisabled");
100: String prevDisabledAttr = "";
101: String nextDisabledAttr = "";
102: if ("true".equals(prevDisabled)) {
103: prevDisabledAttr = DISABLED_ATTRIB;
104: }
105: if ("true".equals(nextDisabled)) {
106: nextDisabledAttr = DISABLED_ATTRIB;
107: }
108:
109: writer.write(" <span class=\"instruction\">" + VIEWING + " "
110: + firstItem + " - " + lastItem + " " + OF + " "
111: + totalItems + " " + ITEMS + "</span>");
112: writer.write(" <br />");
113: writer.write(" <input type=\"submit\"");
114: writer.write(" name=\"" + dataTableId + "_" + formId
115: + "__pager_button_control_prev_btn\"");
116: writer.write(" onclick=\"javascript:document.forms['"
117: + formId + "'].submit(); return false;\"");
118: writer.write(" value=\"< " + prevText + " " + numItems
119: + "\"");
120: writer.write(" " + prevDisabledAttr + "/>");
121:
122: String select = dataTableId + "_" + formId
123: + "__pager_button_control_select";
124:
125: writeSelectList(writer, numItems, select, formId);
126:
127: writer.write(" <input type=\"submit\"");
128: writer.write(" name=\"" + dataTableId + "_" + formId
129: + "__pager_button_control_next_btn\"");
130: writer.write(" onclick=\"javascript:document.forms['"
131: + formId + "'].submit(); return false;\"");
132: writer.write(" value=\"" + nextText + " " + numItems
133: + " >\"");
134: writer.write(" " + nextDisabledAttr + "/>");
135: writer.write(" <br />");
136: }
137:
138: /**
139: *
140: * @param writer for output
141: * @param numItems number of items to show string
142: * @param selectId the name to give the HTML select control
143: * @param formId the form to post onchange events to
144: * @throws IOException
145: */
146: private void writeSelectList(ResponseWriter writer,
147: String numItems, String selectId, String formId)
148: throws IOException {
149: writer.write(" <select ");
150: writer.write(" onchange=\"javascript:document.forms['"
151: + formId + "'].submit(); return false;\"");
152: writer.write(" name=\"" + selectId + "\">");
153:
154: for (int i = 0; i < numberValues.length; i++) {
155: String currentVal = numberValues[i];
156: writer.write(" <option ");
157: writer.write(" value=\"" + currentVal + "\"");
158: if (currentVal.equals(numItems)) {
159: writer.write(" selected=\"selected\" ");
160: }
161: writer.write(">" + SHOW + " " + currentVal + " "
162: + ITEMS_PER + "</option>");
163: }
164:
165: writer.write(" </select>");
166: }
167:
168: }
|