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