KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SPageScroller


1 /*
2  * $Id: SPageScroller.java,v 1.8 2005/05/26 13:18:08 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16
17 /**
18  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
19  * @version $Revision: 1.8 $
20  */

21 public class SPageScroller
22         extends SAbstractAdjustable
23 {
24     private static final int DEFAULT_DIRECT_PAGES = 10;
25
26     private boolean marginVisible;
27
28     private boolean stepVisible;
29
30     /**
31      * Actual amount of page clickables; depends on the number of elemnts in the
32      * model and on the models extent.
33      *
34      * @see #setDirectPages
35      */

36     protected int directPages = DEFAULT_DIRECT_PAGES;
37
38     /*
39      * how to layout the scroller, vertical or horizontal
40      * @see #setLayoutMode
41      */

42     protected int layoutMode;
43
44     /**
45      * contains the clickables forward, backward, first, last
46      */

47     protected SClickable[] clickables = new SClickable[4];
48
49     /**
50      * contains the direct page clickables. Size of this array is extend
51      */

52     protected SClickable[] directPageClickables;
53
54     protected SLabel pageCountLabel = new SLabel();
55
56     /**
57      * Creates a scrollbar with the specified orientation,
58      * value, extent, mimimum, and maximum.
59      * The "extent" is the size of the viewable area. It is also known
60      * as the "visible amount".
61      *
62      * @throws IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
63      * @see #setOrientation
64      * @see #setValue
65      * @see #setVisibleAmount
66      * @see #setMinimum
67      * @see #setMaximum
68      */

69     public SPageScroller(int orientation, int value, int extent, int min, int max) {
70         super(new SPagingBoundedRangeModel(value, extent, min, max));
71         unitIncrement = extent;
72         blockIncrement = extent;
73
74         for (int i = 0; i < clickables.length; i++) {
75             clickables[i] = new SClickable();
76             clickables[i].setEventTarget(this);
77         }
78
79         setOrientation(orientation);
80         setMarginVisible(false);
81         setHorizontalAlignment(SConstants.CENTER);
82         setVerticalAlignment(SConstants.CENTER);
83         setEpochCheckEnabled(false);
84     }
85
86     /**
87      * Creates a scrollbar with the specified orientation
88      * and the following initial values:
89      * <pre>
90      * minimum = 0
91      * maximum = 100
92      * value = 0
93      * extent = 10
94      * </pre>
95      */

96     public SPageScroller(int orientation) {
97         this(orientation, 0, 1, 0, 100);
98     }
99
100     /**
101      * Creates a vertical scrollbar with the following initial values:
102      * <pre>
103      * minimum = 0
104      * maximum = 100
105      * value = 0
106      * extent = 10
107      * </pre>
108      */

109     public SPageScroller() {
110         this(SConstants.VERTICAL);
111     }
112
113     public SLabel getPageCountLabel() {
114         return pageCountLabel;
115     }
116
117     protected void setPageCountText(int pages) {
118         pageCountLabel.setText("/" + pages);
119     }
120
121     public int getLayoutMode() {
122         return layoutMode;
123     }
124
125     /**
126      * set how to layout components
127      * {@link #VERTICAL} or {@link #HORIZONTAL}
128      */

129     public void setLayoutMode(int orientation) {
130         switch (orientation) {
131             case SConstants.VERTICAL:
132             case SConstants.HORIZONTAL:
133                 layoutMode = orientation;
134                 break;
135             default:
136                 throw new IllegalArgumentException JavaDoc("layout mode must be one of: VERTICAL, HORIZONTAL");
137         }
138     }
139
140     /**
141      * Sets the amount of page clickables to <code>count</code>.
142      */

143     public final int getDirectPages() {
144         return directPages;
145     }
146
147     /**
148      * Sets the amount of page clickables to <code>count</code>.
149      *
150      * @param count : New amount of page clickables.
151      */

152     public void setDirectPages(int count) {
153         if (directPages != count)
154             directPages = count;
155     }
156
157     public final int getPageCount() {
158         // avoid division by zero
159
if (getExtent() == 0)
160             return 0;
161         return ((getMaximum() + 1) + (getExtent() - 1) - getMinimum()) / getExtent();
162     }
163
164     /**
165      * gets the current page number according to the Position we are
166      * in.
167      *
168      * @return the current page number
169      */

170     public final int getCurrentPage() {
171         // avoid division by zero
172
if (getExtent() == 0)
173             return 0;
174
175         return (getValue() - getMinimum() + getExtent() - 1) / getExtent();
176     }
177
178     protected String JavaDoc formatDirectPageLabel(int page) {
179         return Integer.toString(page + 1);
180     }
181
182
183     public boolean isMarginVisible() {
184         return marginVisible;
185     }
186
187     public void setMarginVisible(boolean marginVisible) {
188         this.marginVisible = marginVisible;
189     }
190
191     public boolean isStepVisible() {
192         return stepVisible;
193     }
194
195     public void setStepVisible(boolean stepVisible) {
196         this.stepVisible = stepVisible;
197     }
198
199     /**
200      * Set the visible amount of the scroller. This sets also the
201      * unitincrement and the blockIncrement!
202      *
203      * @param value the new extent
204      */

205     public void setExtent(int value) {
206         super.setExtent(value);
207         unitIncrement = value;
208         blockIncrement = value;
209         // make sure we have a valid value!
210
setValue(getValue());
211     }
212
213     /**
214      * Set the visible amount of the scroller. This sets also the
215      * unitincrement and the blockIncrement!
216      *
217      * @param value the new extent
218      */

219     public void setVisibleAmount(int value) {
220         super.setVisibleAmount(value);
221         unitIncrement = value;
222         blockIncrement = value;
223         // make sure we have a valid value!
224
setValue(getValue());
225     }
226
227     /**
228      * Set the current value of the scroller. The value will be
229      * adjusted to a multiple of the extent.
230      *
231      * @param value the new value
232      */

233     public void setValue(int value) {
234         super.setValue(value - (value % getExtent()));
235     }
236 }
237
Popular Tags