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

📄 reporthandler.java

📁 jsf example about book manager
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            }
        };

    private static final Comparator ASC_OWNER_COMPARATOR = new Comparator() {
            public int compare(Object o1, Object o2) {
                String s1 = ((Report) o1).getOwner();
                String s2 = ((Report) o2).getOwner();
                return s1.compareTo(s2);
            }
        };

    private static final Comparator DESC_OWNER_COMPARATOR = new Comparator() {
            public int compare(Object o1, Object o2) {
                String s1 = ((Report) o1).getOwner();
                String s2 = ((Report) o2).getOwner();
                return s2.compareTo(s1);
            }
        };

    private static final Comparator ASC_DATE_COMPARATOR = new Comparator() {
            public int compare(Object o1, Object o2) {
                Date d1 = ((Report) o1).getStartDate();
                Date d2 = ((Report) o2).getStartDate();
                return d1.compareTo(d2);
            }
        };

    private static final Comparator DESC_DATE_COMPARATOR = new Comparator() {
            public int compare(Object o1, Object o2) {
                Date d1 = ((Report) o1).getStartDate();
                Date d2 = ((Report) o2).getStartDate();
                return d2.compareTo(d1);
            }
        };

    private static final Comparator ASC_TOTAL_COMPARATOR = new Comparator() {
            public int compare(Object o1, Object o2) {
                Double d1 = new Double(((Report) o1).getTotal());
                Double d2 = new Double(((Report) o2).getTotal());

                return d1.compareTo(d2);
            }
        };

    private static final Comparator DESC_TOTAL_COMPARATOR = new Comparator() {
            public int compare(Object o1, Object o2) {
                Double d1 = new Double(((Report) o1).getTotal());
                Double d2 = new Double(((Report) o2).getTotal());
                return d2.compareTo(d1);
            }
        };

    private static final Comparator ASC_STATUS_COMPARATOR = new Comparator() {
            public int compare(Object o1, Object o2) {
                Integer i1 = new Integer(((Report) o1).getStatus());
                Integer i2 = new Integer(((Report) o2).getStatus());
                return i1.compareTo(i2);
            }
        };

    private static final Comparator DESC_STATUS_COMPARATOR = new Comparator() {
            public int compare(Object o1, Object o2) {
                Integer i1 = new Integer(((Report) o1).getStatus());
                Integer i2 = new Integer(((Report) o2).getStatus());
                return i2.compareTo(i1);
            }
        };

    /*
     * Methods related to the display of the reports table.
     */

    /**
     * Returns the number of rows to show in the reports table.
     */
    public int getNoOfRows() {
        return noOfRows;
    }

    /**
     * Sets the number of rows to show in the reports table.
     */
    public void setNoOfRows(int noOfRows) {
        this.noOfRows = noOfRows;
    }

    /**
     * Returns the index for the first row in the reports table.
     */
    public int getFirstRowIndex() {
        return firstRowIndex;
    }

    /**
     * Returns "true" if the first page of reports is displayed.
     */
    public boolean isScrollFirstDisabled() {
        return firstRowIndex == 0;
    }

    /**
     * Returns "true" if the last page of reports is displayed.
     */
    public boolean isScrollLastDisabled() {
        return firstRowIndex >= reportsModel.getRowCount() - noOfRows;
    }

    /**
     * Returns "true" if there aren't enough rows to scroll forward
     * one page.
     */
    public boolean isScrollNextDisabled() {
        return firstRowIndex >= reportsModel.getRowCount() - noOfRows;
    }

    /**
     * Returns "true" if the first page is displayed.
     */
    public boolean isScrollPreviousDisabled() {
        return firstRowIndex == 0;
    }

    /**
     * Sets the index for the first row to display in the reports
     * list to zero.
     */
    public String scrollFirst() {
        firstRowIndex = 0;
        return "success";
    }

    /**
     * Sets the index for the first row to display in the reports
     * list to the index of the top row for the last page.
     */
    public String scrollLast() {
        firstRowIndex = reportsModel.getRowCount() - noOfRows;
        if (firstRowIndex < 0) {
            firstRowIndex = 0;
        }
        return "success";
    }

    /**
     * Sets the index for the first row to display in the reports
     * table to the index of the top row for the next page, or
     * to zero if there is no more page.
     */
    public String scrollNext() {
        firstRowIndex += noOfRows;
        if (firstRowIndex >= reportsModel.getRowCount()) {
            firstRowIndex = reportsModel.getRowCount() - noOfRows;
            if (firstRowIndex < 0) {
                firstRowIndex = 0;
            }
        }
        
        return "success";
    }

    /**
     * Sets the index for the first row to display in the reports
     * table to the index of the top row for the previou page, or
     * to zero if there is no more page.
     */
    public String scrollPrevious() {
        firstRowIndex -= noOfRows;
        if (firstRowIndex < 0) {
            firstRowIndex = 0;
        }
        return "success";
    }

    /*
     * Method releated to the page navigation bar.
     */

    /**
     * Returns a List with Page instances for each page of reports.
     */
    public List getPages() {
	int totalNoOfRows = getSortedReportsModel().getRowCount();
        int noOfPages =  totalNoOfRows / noOfRows;
	if (totalNoOfRows % noOfRows > 0) {
	    noOfPages += 1;
	}
	
        List pages = new ArrayList(noOfPages);
        for (int i = 0; i < noOfPages; i++) {
	    pages.add(new Page(i + 1, this));
        }
	return pages;
    }

    /**
     * Returns the number of links to render in the navigation bar.
     */
    public int getNoOfPageLinks() {
	return noOfPageLinks;
    }

    /**
     * Returns the index for the first page link to render.
     */
    public int getFirstPageIndex() {
        int noOfPages = getPages().size();
	if (noOfPages <= noOfPageLinks) {
	    return 0;
	}
	
	int firstPageIndex = (firstRowIndex / noOfRows) - 1;
	if (firstPageIndex < 0) {
	    firstPageIndex = 0;
	}
	else if (noOfPages - firstPageIndex < noOfPageLinks) {
	    firstPageIndex = noOfPages - noOfPageLinks;
	}
	return firstPageIndex;
    }

    /**
     * Returns the index for the page matching the currently
     * rendered set of reports in the reports list.
     */
    public int getCurrentPage() {
	return (firstRowIndex / noOfRows) + 1;
    }

    /**
     * This class represents a page in the page navigation bar model.
     */
    public static class Page {
	private int number;
	private ReportHandler handler;

	public Page(int number, ReportHandler handler) {
	    this.number = number;
	    this.handler = handler;
	}

	public int getNumber() {
	    return number;
	}

	public String select() {
	    handler.setCurrentPage(number); 
	    return null;
	}
    }

    /**
     * Sets the index for the first row in the reports table to match
     * the specified page number.
     */
    private void setCurrentPage(int currentPage) {
	firstRowIndex = (currentPage - 1) * noOfRows;
    }

    /*
     * Private methods.
     */

    /**
     * If the current report is new, changes its status to "open"
     * and add it to the registry; otherwise, updates the report
     * in the registry. Resets the cached entries DataModel.
     */
    private void saveReport() throws RegistryException {
        if (isReportNew()) {
            currentReport.setStatus(Report.STATUS_OPEN);
            registry.addReport(currentReport);
        }
        else {
            registry.updateReport(currentReport);
        }
	entriesModel = null;
    }

    /**
     * If the current report isn't new (i.e., not yet stored),
     * refreshes the current report with a copy from the registry
     * to ensure the local copy is the latest version.
     */
    private void refreshCache() throws RegistryException {
        if (!isReportNew()) {
            setCurrentReport(registry.getReport(currentReport.getId()));
        }
    }

    /**
     * Creates a new Report instance initialized with the current
     * user as the owner, and resets the cached entries DataModel.
     */
    private Report createNewReport() {
        Report report = new Report();
        report.setOwner(getCurrentUser());
	entriesModel = null;
        return report;
    }

    /**
     * Makes the provided Report the current report, and resets the
     * cached entries DataModel.
     */
    private void setCurrentReport(Report report) {
	currentReport = report;
	entriesModel = null;
    }

    /**
     * Returns a Date one month prior to the provided Date.
     */
    private Date getPreviousMonth(Date current) {
        GregorianCalendar c = new GregorianCalendar();
        c.setTime(current);
        c.add(Calendar.MONTH, -1);
        return c.getTime();
    }

    /**
     * Returns the username of the current, authenticated user.
     */
    private String getCurrentUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext ec = context.getExternalContext();
        return ec.getRemoteUser();
    }

    /**
     * Adds the message matching the key in the application's
     * resource bundle, formatted with the parameters (if any),
     * the the JSF message queue as a global message.
     */
    private void addMessage(String messageKey, Object param) {
        FacesContext context = FacesContext.getCurrentInstance();
        Application application = context.getApplication();
        String messageBundleName = application.getMessageBundle();
        Locale locale = context.getViewRoot().getLocale();
        ResourceBundle rb = 
            ResourceBundle.getBundle(messageBundleName, locale);
        String msgPattern = rb.getString(messageKey);
        String msg = msgPattern;
        if (param != null) {
            Object[] params = {param};
            msg = MessageFormat.format(msgPattern, params);
        }
        FacesMessage facesMsg = 
            new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
        context.addMessage(null, facesMsg);
    }

    /**
     * Returns "true" if the current report has status "new".
     */
    private boolean isReportNew() {
        return currentReport.getStatus() == Report.STATUS_NEW;
    }
}

⌨️ 快捷键说明

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