1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.Dimension
;
4 import java
.awt
.LayoutManager
;
5 import java
.awt
.event
.ActionEvent
;
6 import java
.awt
.event
.ActionListener
;
8 import javax
.swing
.BoxLayout
;
9 import javax
.swing
.Icon
;
10 import javax
.swing
.JButton
;
11 import javax
.swing
.JLabel
;
12 import javax
.swing
.JTextField
;
15 * A Swing-based navigation bar, that displays first/previous/next/last page
20 public class NavBar
extends ListenerPanel
{
21 private static final long serialVersionUID
= 1L;
23 /** The event that is fired on page change. */
24 public static final String PAGE_CHANGED
= "page changed";
26 private JTextField page
;
27 private JLabel maxPage
;
30 private int index
= 0;
33 private String extraLabel
= null;
35 private JButton first
;
36 private JButton previous
;
41 * Create a new navigation bar.
43 * The minimum must be lower or equal to the maximum.
45 * Note than a max of "-1" means "infinite".
48 * the minimum page number (cannot be negative)
50 * the maximum page number (cannot be lower than min, except if
53 * @throws IndexOutOfBoundsException
54 * if min > max and max is not "-1"
56 public NavBar(int min
, int max
) {
57 if (min
> max
&& max
!= -1) {
58 throw new IndexOutOfBoundsException(
59 String
.format("min (%d) > max (%d)", min
, max
));
62 LayoutManager layout
= new BoxLayout(this, BoxLayout
.X_AXIS
);
66 first
= new JButton();
67 first
.addActionListener(new ActionListener() {
69 public void actionPerformed(ActionEvent e
) {
74 previous
= new JButton();
75 previous
.addActionListener(new ActionListener() {
77 public void actionPerformed(ActionEvent e
) {
82 page
= new JTextField(Integer
.toString(min
));
83 page
.setPreferredSize(new Dimension(page
.getPreferredSize().width
* 2,
84 page
.getPreferredSize().height
));
85 page
.addActionListener(new ActionListener() {
87 public void actionPerformed(ActionEvent e
) {
89 int pageNb
= Integer
.parseInt(page
.getText());
90 if (pageNb
< NavBar
.this.min
|| pageNb
> NavBar
.this.max
) {
91 throw new NumberFormatException("invalid");
95 fireActionPerformed(PAGE_CHANGED
);
96 } catch (NumberFormatException nfe
) {
97 page
.setText(Integer
.toString(index
+ 1));
102 maxPage
= new JLabel(" of " + max
+ " ");
104 next
= new JButton();
105 next
.addActionListener(new ActionListener() {
107 public void actionPerformed(ActionEvent e
) {
112 last
= new JButton();
113 last
.addActionListener(new ActionListener() {
115 public void actionPerformed(ActionEvent e
) {
120 // Set the << < > >> "icons"
121 setIcons(null, null, null, null);
130 label
= new JLabel("");
139 fireActionPerformed(PAGE_CHANGED
);
143 * The current index, must be between {@link NavBar#min} and
144 * {@link NavBar#max}, both inclusive.
148 public int getIndex() {
153 * The current index, must be between {@link NavBar#min} and
154 * {@link NavBar#max}, both inclusive.
159 * @return TRUE if the index changed
161 * @throws IndexOutOfBoundsException
162 * if the index is out of bounds according to
163 * {@link NavBar#getMin()} and {@link NavBar#getMax()}.
165 public boolean setIndex(int index
) {
166 if (index
!= this.index
) {
167 if (index
< min
|| (index
> max
&& max
!= -1)) {
168 throw new IndexOutOfBoundsException(String
.format(
169 "Index %d but min/max is [%d/%d]", index
, min
, max
));
183 * The minimun page number. Cannot be negative.
187 public int getMin() {
192 * The minimum page number. Cannot be negative.
194 * May update the index if needed (if the index is < the new min).
196 * Will also (always) update the label and enable/disable the required
202 public void setMin(int min
) {
213 * The maximum page number. Cannot be lower than min, except if -1
218 public int getMax() {
223 * The maximum page number. Cannot be lower than min, except if -1
226 * May update the index if needed (if the index is > the new max).
228 * Will also (always) update the label and enable/disable the required
234 public void setMax(int max
) {
236 if (index
> max
&& max
!= -1) {
240 maxPage
.setText(" of " + max
);
246 * The current extra label to display.
248 * @return the current label
250 public String
getExtraLabel() {
255 * The current extra label to display.
257 * @param currentLabel
258 * the new current label
260 public void setExtraLabel(String currentLabel
) {
261 this.extraLabel
= currentLabel
;
266 * Change the page to the next one.
268 * @return TRUE if it changed
270 public boolean next() {
271 if (setIndex(index
+ 1)) {
272 fireActionPerformed(PAGE_CHANGED
);
280 * Change the page to the previous one.
282 * @return TRUE if it changed
284 public boolean previous() {
285 if (setIndex(index
- 1)) {
286 fireActionPerformed(PAGE_CHANGED
);
294 * Change the page to the first one.
296 * @return TRUE if it changed
298 public boolean first() {
300 fireActionPerformed(PAGE_CHANGED
);
308 * Change the page to the last one.
310 * @return TRUE if it changed
312 public boolean last() {
314 fireActionPerformed(PAGE_CHANGED
);
322 * Set icons for the buttons instead of square brackets.
324 * Any NULL value will make the button use square brackets again.
327 * the icon of the button "go to first page"
329 * the icon of the button "go to previous page"
331 * the icon of the button "go to next page"
333 * the icon of the button "go to last page"
335 public void setIcons(Icon first
, Icon previous
, Icon next
, Icon last
) {
336 this.first
.setIcon(first
);
337 this.first
.setText(first
== null ?
"<<" : "");
338 this.previous
.setIcon(previous
);
339 this.previous
.setText(previous
== null ?
"<" : "");
340 this.next
.setIcon(next
);
341 this.next
.setText(next
== null ?
">" : "");
342 this.last
.setIcon(last
);
343 this.last
.setText(last
== null ?
">>" : "");
347 * Update the label displayed in the UI.
349 private void updateLabel() {
350 label
.setText(getExtraLabel());
351 page
.setText(Integer
.toString(index
));
355 * Update the navigation buttons "enabled" state according to the current
358 private void updateEnabled() {
359 first
.setEnabled(index
> min
);
360 previous
.setEnabled(index
> min
);
361 next
.setEnabled(index
< max
|| max
== -1);
362 last
.setEnabled(index
< max
|| max
== -1);