001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: /**
016: * A scroller component offering several pages for selection.
017: * <p/>
018: * Apply i.e. to {@link SScrollPane} like <br/>
019: * <code>
020: * scrollbar = new SPageScroller(Adjustable.VERTICAL);
021: * scrollPane.setHorizontalScrollBar(scrollbar, SScrollPaneLayout.NORTH);
022: * </code>
023: *
024: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
025: */
026: public class SPageScroller extends SAbstractAdjustable {
027: private static final int DEFAULT_DIRECT_PAGES = 10;
028:
029: private boolean marginVisible;
030:
031: private boolean stepVisible;
032:
033: /**
034: * Actual amount of page clickables; depends on the number of elemnts in the
035: * model and on the models extent.
036: *
037: * @see #setDirectPages
038: */
039: protected int directPages = DEFAULT_DIRECT_PAGES;
040:
041: /*
042: * how to layout the scroller, vertical or horizontal
043: * @see #setLayoutMode
044: */
045: protected int layoutMode;
046:
047: /**
048: * contains the clickables forward, backward, first, last
049: */
050: protected SClickable[] clickables = new SClickable[4];
051:
052: /**
053: * contains the direct page clickables. Size of this array is extend
054: */
055: protected SClickable[] directPageClickables;
056:
057: protected SLabel pageCountLabel = new SLabel();
058:
059: /**
060: * Creates a scrollbar with the specified orientation,
061: * value, extent, mimimum, and maximum.
062: * The "extent" is the size of the viewable area. It is also known
063: * as the "visible amount".
064: *
065: * @throws IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
066: * @see #setOrientation
067: * @see #setValue
068: * @see #setVisibleAmount
069: * @see #setMinimum
070: * @see #setMaximum
071: */
072: public SPageScroller(int orientation, int value, int extent,
073: int min, int max) {
074: super (new SPagingBoundedRangeModel(value, extent, min, max));
075: unitIncrement = extent;
076: blockIncrement = extent;
077:
078: for (int i = 0; i < clickables.length; i++) {
079: clickables[i] = new SClickable();
080: clickables[i].setEventTarget(this );
081: }
082:
083: setOrientation(orientation);
084: setMarginVisible(false);
085: setHorizontalAlignment(SConstants.CENTER);
086: setVerticalAlignment(SConstants.CENTER);
087: setEpochCheckEnabled(false);
088: }
089:
090: /**
091: * Creates a scrollbar with the specified orientation
092: * and the following initial values:
093: * <pre>
094: * minimum = 0
095: * maximum = 100
096: * value = 0
097: * extent = 10
098: * </pre>
099: */
100: public SPageScroller(int orientation) {
101: this (orientation, 0, 1, 0, 100);
102: }
103:
104: /**
105: * Creates a vertical scrollbar with the following initial values:
106: * <pre>
107: * minimum = 0
108: * maximum = 100
109: * value = 0
110: * extent = 10
111: * </pre>
112: */
113: public SPageScroller() {
114: this (SConstants.VERTICAL);
115: }
116:
117: public SLabel getPageCountLabel() {
118: return pageCountLabel;
119: }
120:
121: protected void setPageCountText(int pages) {
122: pageCountLabel.setText("/" + pages);
123: }
124:
125: public int getLayoutMode() {
126: return layoutMode;
127: }
128:
129: /**
130: * set how to layout components
131: * {@link #VERTICAL} or {@link #HORIZONTAL}
132: */
133: public void setLayoutMode(int orientation) {
134: switch (orientation) {
135: case SConstants.VERTICAL:
136: case SConstants.HORIZONTAL:
137: layoutMode = orientation;
138: break;
139: default:
140: throw new IllegalArgumentException(
141: "layout mode must be one of: VERTICAL, HORIZONTAL");
142: }
143: }
144:
145: /**
146: * Sets the amount of page clickables to <code>count</code>.
147: */
148: public final int getDirectPages() {
149: return directPages;
150: }
151:
152: /**
153: * Sets the amount of page clickables to <code>count</code>.
154: *
155: * @param count : New amount of page clickables.
156: */
157: public void setDirectPages(int count) {
158: reloadIfChange(directPages, count);
159: directPages = count;
160: }
161:
162: public final int getPageCount() {
163: // avoid division by zero
164: if (getExtent() == 0)
165: return 0;
166: return ((getMaximum()) - getMinimum() + (getExtent() - 1))
167: / getExtent();
168: }
169:
170: /**
171: * gets the current page number according to the Position we are
172: * in.
173: *
174: * @return the current page number
175: */
176: public final int getCurrentPage() {
177: // avoid division by zero
178: if (getExtent() == 0)
179: return 0;
180:
181: return (getValue() - getMinimum() + getExtent() - 1)
182: / getExtent();
183: }
184:
185: protected String formatDirectPageLabel(int page) {
186: return Integer.toString(page + 1);
187: }
188:
189: public boolean isMarginVisible() {
190: return marginVisible;
191: }
192:
193: public void setMarginVisible(boolean marginVisible) {
194: this .marginVisible = marginVisible;
195: }
196:
197: public boolean isStepVisible() {
198: return stepVisible;
199: }
200:
201: public void setStepVisible(boolean stepVisible) {
202: this .stepVisible = stepVisible;
203: }
204:
205: /**
206: * Set the visible amount of the scroller. This sets also the
207: * unitincrement and the blockIncrement!
208: *
209: * @param value the new extent
210: */
211: public void setExtent(int value) {
212: super .setExtent(value);
213: unitIncrement = value;
214: blockIncrement = value;
215: // make sure we have a valid value!
216: setValue(getValue());
217: }
218:
219: /**
220: * Set the visible amount of the scroller. This sets also the
221: * unitincrement and the blockIncrement!
222: *
223: * @param value the new extent
224: */
225: public void setVisibleAmount(int value) {
226: super .setVisibleAmount(value);
227: unitIncrement = value;
228: blockIncrement = value;
229: // make sure we have a valid value!
230: setValue(getValue());
231: }
232:
233: protected void adjust() {
234: reload();
235: }
236:
237: /**
238: * Set the current value of the scroller. The value will be
239: * adjusted to a multiple of the extent.
240: *
241: * @param value the new value
242: */
243: public void setValue(int value) {
244: super.setValue(value);
245: }
246: }
|