01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/utils/mvc/impl/ListScrollIndexerImpl.java $
03: * $Id: ListScrollIndexerImpl.java 14230 2006-09-05 18:02:51Z chmaurer@iupui.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.metaobj.utils.mvc.impl;
21:
22: import java.util.List;
23: import java.util.Map;
24:
25: import org.apache.commons.logging.Log;
26: import org.apache.commons.logging.LogFactory;
27: import org.sakaiproject.metaobj.utils.mvc.intf.ListScroll;
28: import org.sakaiproject.metaobj.utils.mvc.intf.ListScrollIndexer;
29:
30: public class ListScrollIndexerImpl implements ListScrollIndexer {
31: protected final transient Log logger = LogFactory
32: .getLog(getClass());
33: private int perPage;
34:
35: public List indexList(Map request, Map model, List sourceList) {
36: int startingIndex = 0;
37: int total = sourceList.size();
38:
39: String ensureVisible = (String) request
40: .get(ListScroll.ENSURE_VISIBLE_TAG);
41:
42: if (ensureVisible != null) {
43: int visibleIndex = Integer.parseInt(ensureVisible);
44: int startingPage = (visibleIndex / perPage);
45: startingIndex = startingPage * perPage;
46: } else {
47: String newStart = (String) request
48: .get(ListScroll.STARTING_INDEX_TAG);
49:
50: if (newStart != null) {
51: startingIndex = Integer.parseInt(newStart);
52: if (startingIndex < 0) {
53: startingIndex = 0;
54: }
55: }
56: }
57:
58: if (startingIndex > total) {
59: int lastPage = (int) Math.ceil(((double) total)
60: / ((double) perPage));
61: lastPage--;
62: startingIndex = lastPage * perPage;
63: }
64:
65: int endingIndex = startingIndex + perPage;
66:
67: if (endingIndex > sourceList.size()) {
68: endingIndex = sourceList.size();
69: }
70:
71: model.put("listScroll", new ListScroll(perPage, sourceList
72: .size(), startingIndex));
73:
74: return sourceList.subList(startingIndex, endingIndex);
75: }
76:
77: public int getPerPage() {
78: return perPage;
79: }
80:
81: public void setPerPage(int perPage) {
82: this.perPage = perPage;
83: }
84: }
|