001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.format.util;
023:
024: /**
025: * A range browser which is useful to provide a visual representation of a range browser.
026: *
027: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
028: * @version $Revision: 8784 $
029: */
030: public class RangeBrowser {
031:
032: /** . */
033: private State state;
034:
035: /** . */
036: private Range leftRange;
037:
038: /** . */
039: private Range middleRange;
040:
041: /** . */
042: private Range rightRange;
043:
044: public RangeBrowser(int from, int to, int width, int index) {
045: updateState(new State(from, to, width, index));
046: }
047:
048: public int getFrom() {
049: return state.from;
050: }
051:
052: public void setFrom(int from) {
053: updateState(new State(from, state.to, state.width, state.index));
054: }
055:
056: public int getTo() {
057: return state.to;
058: }
059:
060: public void setTo(int to) {
061: updateState(new State(state.from, to, state.width, state.index));
062: }
063:
064: public int getWidth() {
065: return state.width;
066: }
067:
068: public void setWidth(int width) {
069: updateState(new State(state.from, state.to, width, state.index));
070: }
071:
072: public int getIndex() {
073: return state.index;
074: }
075:
076: public void setIndex(int index) {
077: updateState(new State(state.from, state.to, state.width, index));
078: }
079:
080: private void updateState(State state) {
081: this .state = state;
082:
083: //
084: compute();
085: }
086:
087: private void compute() {
088: leftRange = middleRange = rightRange = null;
089:
090: //
091: if (state.to - state.from <= state.width * 3) {
092: middleRange = new Range(state.from, state.to);
093: } else {
094: int leftTo = state.from + state.width;
095: int rightFrom = state.to - state.width;
096: int middleFrom = state.index - (state.width - 1) / 2;
097: int middleTo = middleFrom + state.width;
098:
099: //
100: if (middleFrom <= leftTo) {
101: if (middleTo > leftTo) {
102: leftRange = new Range(state.from, middleTo);
103: } else {
104: leftRange = new Range(state.from, leftTo);
105: }
106: rightRange = new Range(rightFrom, state.to);
107: } else if (middleTo >= rightFrom) {
108: if (middleFrom < rightFrom) {
109: rightRange = new Range(middleFrom, state.to);
110: } else {
111: rightRange = new Range(rightFrom, state.to);
112: }
113: leftRange = new Range(state.from, leftTo);
114: } else {
115: leftRange = new Range(state.from, leftTo);
116: middleRange = new Range(middleFrom, middleTo);
117: rightRange = new Range(rightFrom, state.to);
118: }
119: }
120: }
121:
122: public Range getLeftRange() {
123: return leftRange;
124: }
125:
126: public Range getMiddleRange() {
127: return middleRange;
128: }
129:
130: public Range getRightRange() {
131: return rightRange;
132: }
133:
134: public static class Range {
135:
136: /** . */
137: private final int from;
138:
139: /** . */
140: private final int to;
141:
142: public Range(int from, int to) {
143: this .from = from;
144: this .to = to;
145: }
146:
147: public int getFrom() {
148: return from;
149: }
150:
151: public int getTo() {
152: return to;
153: }
154: }
155:
156: private static class State {
157:
158: /** Lower inclusive bound. */
159: private final int from;
160:
161: /** Upper exclusive bound. */
162: private final int to;
163:
164: /** . */
165: private final int width;
166:
167: /** . */
168: private final int index;
169:
170: public State(int from, int to, int width, int index) {
171: if (from > to) {
172: throw new IllegalArgumentException();
173: }
174: if (index < from) {
175: throw new IllegalArgumentException();
176: }
177: if (index >= to) {
178: throw new IllegalArgumentException();
179: }
180: if (width < 1) {
181: throw new IllegalArgumentException();
182: }
183:
184: //
185: this.from = from;
186: this.to = to;
187: this.width = Math.min(width, to - from);
188: this.index = index;
189: }
190: }
191: }
|