001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.myfaces.tobago.component;
021
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024 import org.apache.myfaces.tobago.event.PageActionEvent;
025 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_FIRST;
026
027 import javax.faces.context.FacesContext;
028 import javax.faces.el.EvaluationException;
029 import javax.faces.el.MethodBinding;
030 import javax.faces.el.MethodNotFoundException;
031 import javax.faces.el.ValueBinding;
032
033 public class Pager extends MethodBinding {
034
035 private static final Log LOG = LogFactory.getLog(Pager.class);
036
037 public Class getType(FacesContext facescontext)
038 throws MethodNotFoundException {
039 return String.class;
040 }
041
042 public Object invoke(FacesContext facesContext, Object[] aobj)
043 throws EvaluationException {
044 if (aobj[0] instanceof PageActionEvent) {
045 PageActionEvent pageEvent = (PageActionEvent) aobj[0];
046
047 UIData sheet = pageEvent.getSheet();
048 int first = -1;
049
050 if (LOG.isDebugEnabled()) {
051 LOG.debug("action = '" + pageEvent.getAction().name() + "'");
052 }
053
054 int start;
055 switch (pageEvent.getAction()) {
056 case FIRST:
057 first = 0;
058 break;
059 case PREV:
060 start = sheet.getFirst() - sheet.getRows();
061 first = start < 0 ? 0 : start;
062 break;
063 case NEXT:
064 if (sheet.hasRowCount()) {
065 start = sheet.getFirst() + sheet.getRows();
066 first = start > sheet.getRowCount() ? sheet.getLastPageIndex() : start;
067 } else {
068 if (sheet.isAtEnd()) {
069 first = sheet.getFirst();
070 } else {
071 first = sheet.getFirst() + sheet.getRows();
072 }
073 }
074 break;
075 case LAST:
076 first = sheet.getLastPageIndex();
077 break;
078 case TO_ROW:
079 start = pageEvent.getValue() - 1;
080 if (start > sheet.getLastPageIndex()) {
081 start = sheet.getLastPageIndex();
082 } else if (start < 0) {
083 start = 0;
084 }
085 first = start;
086 break;
087 case TO_PAGE:
088 start = pageEvent.getValue() - 1;
089 if (LOG.isDebugEnabled()) {
090 LOG.debug("start = " + start + " sheet.getRows() = "
091 + sheet.getRows() + " => start = " + (start * sheet.getRows()));
092 }
093 start = start * sheet.getRows();
094 if (start > sheet.getLastPageIndex()) {
095 start = sheet.getLastPageIndex();
096 } else if (start < 0) {
097 start = 0;
098 }
099 first = start;
100 break;
101 default:
102 // can't happen
103 }
104 ValueBinding valueBinding = sheet.getValueBinding(ATTR_FIRST);
105 if (valueBinding != null) {
106 valueBinding.setValue(facesContext, first);
107 } else {
108 sheet.setFirst(first);
109 }
110
111 sheet.getSheetState(facesContext).setFirst(first);
112 // sheet.queueEvent(new SheetStateChangeEvent(sheet));
113 } else {
114 if (LOG.isDebugEnabled()) {
115 LOG.debug("aobj[0] instanceof '" + aobj[0] + "'");
116 }
117 }
118
119 return null;
120 }
121 }
122