⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scrollercomponent.java

📁 以前做的一个j2ee的项目
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package gov.gdlt.ssgly.taxweb.tag.components;

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;
import java.util.StringTokenizer;

/**
 * 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 int ACTION_JUMP = -4;

    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;
        int jumpPage = 1;
        boolean isJump = false;
        String clientId = getClientId(context);
        Map requestParameterMap = (Map) context.getExternalContext().
                                  getRequestParameterMap();

        action = (String) requestParameterMap.get(clientId + "_action");
        String scollAction = (String)requestParameterMap.get("scollAction");
        String reqPage=(String)requestParameterMap.get("pageNo")==null || "".equals((String)requestParameterMap.get("pageNo"))?"1":(String)requestParameterMap.get("pageNo");
        if (action == null || action.length() == 0) {
            if(scollAction!=null && !"".equals(scollAction)){
            this.getAttributes().put("currentPage", new Integer(scollAction));
            }else{
            this.getAttributes().put("currentPage", new Integer(reqPage));
            }
            if((requestParameterMap.get("gjz"))!=null){
            this.getAttributes().put("currentPage", new Integer(1));
            }
            // nothing to decode
            return;
        }
        //MethodBinding mb = Util.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;
        case ACTION_JUMP:
            String jumppage = (String) requestParameterMap.get("jump");
            Integer cpage = new Integer(1);
            try {
                cpage = new Integer(jumppage);
            } catch (Exception e) {
                currentPage = 1;
            }
            int totalPages = getTotalPages();
            if (totalPages <= (cpage.intValue())) {
                currentPage = totalPages;
            } else if ((cpage.intValue()) <= 0) {
                currentPage = 1;
            } else {
                currentPage = cpage.intValue();
            }
            break;
        default:
            currentPage = actionInt;
            break;
        }
        // from the currentPage, calculate the current row to scroll to.
        currentRow = (currentPage - 1) * getRowsPerPage();
        this.getAttributes().put("currentPage", new Integer(currentPage));
        this.getAttributes().put("currentRow", new Integer(currentRow));
       // requestParameterMap.put("currpage",((Integer)this.getAttributes().get("currentPage")).toString());
        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();

        int totalCount = getTotalCount();

        String style = getStyle();
        String styleClass = getStyleClass();

        writer.write("<table border=\"0\" cellpadding=\"0\" align=\"right\">");
        writer.write("<tr align=\"center\" valign=\"top\">");
        //writer.write("<td><font size=\"-1\">Result&nbsp;Page:&nbsp;</font></td>");

        // write the Previous link if necessary
        if (styleClass != null) {
            writer.write("<td class=\"" + styleClass + "\">");
        } else {
            if (style != null) {
                writer.write("<td style=\"" + style + "\">");
            } else {
                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;

        int countPage = getCountPage();
        //countPage=countPage>=totalPages?totalPages:countPage;
        if ((currentPage + 3) <= countPage) {
            if (countPage <= totalPages) {
                last = countPage;
            }
        } else {
            if ((currentPage + 3) <= totalPages) {
                last = currentPage + 3;
            }
        }

        first = (last - (countPage - 1)) <= 0 ? 1 : (last - (countPage - 1));

//        if (countPage < currentPage) {
//            if(currentPage % countPage >0){
//                first = (currentPage / countPage) * countPage + 1;
//            }else{
//               first = ((currentPage-(countPage-1)) / countPage) * countPage + 1;
//            }
//            //first = currentPage - 10;
//        }
//        if ((first + (countPage-1)) < totalPages) {
//            last = first + (countPage-1);
//        }
//        if (styleClass != null) {
//                writer.write("<td class=\"" + styleClass + "\">");
//            } else {
//                if (style != null) {
//                    writer.write("<td style=\"" + style + "\">");
//                } else {
//                    writer.write("<td>");
//                }
//            }
        for (i = first; i <= last; i++) {
            writeNavWidgetMarkup(context, clientId, i, (i != currentPage));
            //writer.write("</td>");
        }

        // write the Next link if necessary
//        if (styleClass != null) {
//            writer.write("<td class=\"" + styleClass + "\">");
//        } else {
//            if (style != null) {
//                writer.write("<td style=\"" + style + "\">");
//            } else {
//                writer.write("<td>");
//            }
//        }
        writeNavWidgetMarkup(context, clientId, ACTION_NEXT,
                             (currentPage < totalPages));
        //显示总页数
        String formClientId = getForm(context).getClientId(context);
        writer.write("   共" + totalCount + "条");
        writer.write("," + totalPages + "页");
        if (getIsJump()) {
            writer.write(",跳转到<input type=\"text\" name=\"jump\" size=\"2\" class=\"inputText\" onkeypress=\"javascript:if(event.keyCode=='13'){document.forms['" +
                         formClientId + "']['" + clientId +
                         "_curPage'].value='" + currentPage +
                         "';document.forms['" + formClientId + "']['" +
                         clientId + "_action'].value='-4';document.forms['" +
                         formClientId + "'].submit();}\">页");
        }
        writer.write("<input type=\"hidden\" name=\"scollAction\">");
        writer.write("</td></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) {
        case ACTION_NEXT:
            facetName = "next";
            linkText = "下一页";

            break;
        case ACTION_PREVIOUS:
            facetName = "previous";
            linkText = "上一页";

            break;
        default:
            facetName = "number";
            linkText = "[" + navActionType + "]";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -