001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/util/tags/sakai_2-4-1/util-api/api/src/java/org/sakaiproject/javax/PagingPosition.java $
003: * $Id: PagingPosition.java 7061 2006-03-27 17:45:10Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.javax;
021:
022: /**
023: * <p>
024: * PagingPosition models a current position in a paging display, with a first and last item value, 1 based.
025: * </p>
026: * <p>
027: * Implementation note: the default Object.equals() is fine for this class.
028: * </p>
029: */
030: public class PagingPosition implements Cloneable {
031: /** The first item position on the current page, 1 based. */
032: protected int m_first = 1;
033:
034: /** The last item position on the current page, 1 based. */
035: protected int m_last = 1;
036:
037: /** If true, paging is ebabled, otherwise all items should be used. */
038: protected boolean m_paging = true;
039:
040: /**
041: * Construct, setting position to select all possible items.
042: */
043: public PagingPosition() {
044: m_first = 1;
045: m_last = 1;
046: }
047:
048: /**
049: * Construct, setting the first and last.
050: *
051: * @param first
052: * The first item position, 1 based.
053: * @param last
054: * The last item position, 1 based.
055: */
056: public PagingPosition(int first, int last) {
057: m_first = first;
058: m_last = last;
059: validate();
060: }
061:
062: /**
063: * Adjust the first and list item position by distance, positive or negative.
064: *
065: * @param distance
066: * The positive or negative distance to move the first and last item positions.
067: */
068: public void adjustPostition(int distance) {
069: m_first += distance;
070: m_last += distance;
071: validate();
072: }
073:
074: /**
075: * {@inheritDoc}
076: */
077: public Object clone() {
078: try {
079: return super .clone();
080: } catch (CloneNotSupportedException ignore) {
081: throw new Error("Assertion failure");
082: }
083: }
084:
085: /**
086: * Access the first item position, 1 based.
087: *
088: * @return the first item position, 1 based.
089: */
090: public int getFirst() {
091: return m_first;
092: }
093:
094: /**
095: * Access the last item position, 1 based.
096: *
097: * @return the last item position, 1 based.
098: */
099: public int getLast() {
100: return m_last;
101: }
102:
103: /**
104: * Check if we have paging enabled.
105: *
106: * @return true if paging is enabled, false if not.
107: */
108: public boolean isPaging() {
109: return m_paging;
110: }
111:
112: /**
113: * Set the paging enabled value.
114: *
115: * @param paging
116: * the new paging enabled value.
117: */
118: public void setPaging(boolean paging) {
119: m_paging = paging;
120: }
121:
122: /**
123: * Set the first and last positions.
124: *
125: * @param first
126: * The new first item position, 1 based.
127: * @param last
128: * The new last item position, 1 based.
129: */
130: public void setPosition(int first, int last) {
131: m_first = first;
132: m_last = last;
133: validate();
134: }
135:
136: /**
137: * Adjust the first and last to be valid.
138: */
139: protected void validate() {
140: if (m_first < 0)
141: m_first = 1;
142: if (m_last < m_first)
143: m_last = m_first;
144: }
145:
146: /**
147: * Adjust the first and last to be valid and within the range 1..biggestLast
148: *
149: * @param biggestLast
150: * The largest valid value for last
151: */
152: public void validate(int biggestLast) {
153: if (m_first < 0)
154: m_first = 1;
155: if (m_last > biggestLast)
156: m_last = biggestLast;
157: if (m_last < m_first)
158: m_last = m_first;
159: }
160: }
|