📄 scrollercomponent.java
字号:
package com.jsfcompref.trainer.components;
import javax.faces.component.UICommand;
/*
* Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any
* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
* EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
* DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
* RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
* ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
* FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
* SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
* THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or
* intended for use in the design, construction, operation or
* maintenance of any nuclear facility.
*/
//import components.renderkit.Util;
//import javax.faces.component.UICommand;
import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.component.UIForm;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.el.MethodBinding;
import javax.faces.event.ActionEvent;
import java.io.IOException;
import java.util.Map;
/**
* This component produces a search engine style scroller that facilitates
* easy navigation over results that span across several pages. It
* demonstrates how a component can do decoding and encoding
* without delegating it to a renderer.
*/
public class ScrollerComponent extends UICommand {
public static final String NORTH = "NORTH";
public static final String SOUTH = "SOUTH";
public static final String EAST = "EAST";
public static final String WEST = "WEST";
public static final String BOTH = "BOTH";
public static final int ACTION_NEXT = -1;
public static final int ACTION_PREVIOUS = -2;
public static final int ACTION_NUMBER = -3;
public static final String FORM_NUMBER_ATTR = "com.sun.faces.FormNumber";
/**
* The component attribute that tells where to put the user supplied
* markup in relation to the "jump to the Nth page of results"
* widget.
*/
public static final String FACET_MARKUP_ORIENTATION_ATTR =
"navFacetOrientation";
public ScrollerComponent() {
super();
this.setRendererType(null);
}
public void decode(FacesContext context) {
String curPage = null;
String action = null;
int actionInt = 0;
int currentPage = 1;
int currentRow = 1;
String clientId = getClientId(context);
Map requestParameterMap = (Map) context.getExternalContext().
getRequestParameterMap();
action = (String) requestParameterMap.get(clientId + "_action");
if (action == null || action.length() == 0) {
// nothing to decode
return;
}
MethodBinding mb = createConstantMethodBinding(action);
this.getAttributes().put("action", mb);
curPage = (String) requestParameterMap.get(clientId + "_curPage");
currentPage = Integer.valueOf(curPage).intValue();
// Assert that action's length is 1.
switch (actionInt = Integer.valueOf(action).intValue()) {
case ACTION_NEXT:
currentPage++;
break;
case ACTION_PREVIOUS:
currentPage--;
// Assert 1 < currentPage
break;
default:
currentPage = actionInt;
break;
}
// from the currentPage, calculate the current row to scroll to.
currentRow = (currentPage - 1) * getRowsPerPage(context);
this.getAttributes().put("currentPage", new Integer(currentPage));
this.getAttributes().put("currentRow", new Integer(currentRow));
this.queueEvent(new ActionEvent(this));
}
public void encodeBegin(FacesContext context) throws IOException {
return;
}
public void encodeEnd(FacesContext context) throws IOException {
int currentPage = 1;
ResponseWriter writer = context.getResponseWriter();
String clientId = getClientId(context);
Integer curPage = (Integer) getAttributes().get("currentPage");
if (curPage != null) {
currentPage = curPage.intValue();
}
int totalPages = getTotalPages(context);
writer.write("<table border=\"0\" cellpadding=\"0\" align=\"center\">");
writer.write("<tr align=\"center\" valign=\"top\">");
// Removed "Result Page: output" - Cschalk
// writer.write( "<td><font size=\"-1\">Result Page: </font></td>");
// write the Previous link if necessary
writer.write("<td>");
writeNavWidgetMarkup(context, clientId, ACTION_PREVIOUS,
(1 < currentPage));
// last arg is true iff we're not the first page
writer.write("</td>");
// render the page navigation links
int i = 0;
int first = 1;
int last = totalPages;
if (10 < currentPage) {
first = currentPage - 10;
}
if ((currentPage + 9) < totalPages) {
last = currentPage + 9;
}
for (i = first; i <= last; i++) {
writer.write("<td>");
writeNavWidgetMarkup(context, clientId, i, (i != currentPage));
writer.write("</td>");
}
// write the Next link if necessary
writer.write("<td>");
writeNavWidgetMarkup(context, clientId, ACTION_NEXT,
(currentPage < totalPages));
writer.write("</td>");
writer.write("</tr>");
writer.write(getHiddenFields(clientId));
writer.write("</table>");
}
public boolean getRendersChildren() {
return true;
}
/**
* <p>Return the component family for this component.</p>
*/
public String getFamily() {
return ("Scroller");
}
//
// Helper methods
//
/**
* Write the markup to render a navigation widget. Override this to
* replace the default navigation widget of link with something
* else.
*/
protected void writeNavWidgetMarkup(FacesContext context,
String clientId,
int navActionType,
boolean enabled) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String facetOrientation = NORTH;
String facetName = null;
String linkText = null;
String localLinkText = null;
UIComponent facet = null;
boolean isCurrentPage = false;
boolean isPageNumber = false;
// Assign values for local variables based on the navActionType
switch (navActionType) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -