001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: ArrowButtonView.java,v $
023   Revision 1.5  2004/05/12 19:13:43  markl
024   comment block updates
025
026   Revision 1.4  2003/01/19 09:50:52  markl
027   Javadoc & comment header updates.
028
029   Revision 1.3  2001/03/12 09:27:51  markl
030   Source code and Javadoc cleanup.
031
032   Revision 1.2  1999/01/10 01:00:58  markl
033   added GPL header & RCS tag
034   ----------------------------------------------------------------------------
035*/
036
037package kiwi.ui;
038
039import java.awt.*;
040import java.awt.event.*;
041import java.io.*;
042import javax.swing.*;
043
044import kiwi.event.*;
045import kiwi.util.*;
046
047/** This class represents a panel of VCR-style buttons, including <i>First</i>,
048  * <i>Previous</i>, <i>Next</i>, and <i>Last</i> buttons. These buttons
049  * represent a bounded range of items that may be traversed.
050  * <p>
051  * The component maintains the current position within the list being
052  * traversed. This position may be retrieved at any time via a call
053  * to the <code>getPosition()</code> method. Additionally, an
054  * <code>ActionEvent</code> is generated each time one of the four
055  * buttons is pressed; one of the command strings "first", "prev",
056  * "next", or "last" is passed as the argument of the
057  * <code>ActionEvent</code> to specify which button was pressed.
058  * <p>
059  * The <i>First</i> and <i>Previous</i> buttons are dimmed when the component
060  * is at the "beginning" of the range, and the <i>Next</i> and <i>Last</i>
061  * buttons are dimmed when the component is at the "end" of the range.
062  *
063  * <p><center>
064  * <img src="snapshot/ArrowButtonView.gif"><br>
065  * <i>An example ArrowButtonView.</i>
066  * </center>
067  *
068  * @author Mark Lindner
069  */
070
071public class ArrowButtonView extends KPanel
072  {
073  private static final int FIRST = 0, PREV = 1, NEXT = 2, LAST = 3;
074  private static final String commands[] = { "first", "prev", "next", "last" };
075  private KButton b_first, b_prev, b_next, b_last;
076  private int range, pos = 0, action;
077  private _ActionListener actionListener;
078  private ActionSupport support;
079  
080  /** Construct a new <code>ArrowButtonView</code>.
081    *
082    * @param range The number of items being traversed.
083    *
084    * @exception java.lang.IllegalArgumentException If <code>range</code> is
085    * less than 0.
086    */
087
088  public ArrowButtonView(int range) throws IllegalArgumentException
089    {
090    super();
091
092    if(range < 0)
093      throw(new IllegalArgumentException("Range must be nonnegative."));
094
095    this.range = range;
096
097    setLayout(new GridLayout(1, 4, 0, 0));
098
099    support = new ActionSupport(this);
100    actionListener = new _ActionListener();
101    
102    ResourceManager rm = KiwiUtils.getResourceManager();
103
104    b_first = new KButton(rm.getIcon("firstp.gif"));
105    b_first.addActionListener(actionListener);
106    add(b_first);
107
108    b_prev = new KButton(rm.getIcon("leftp.gif"));
109    b_prev.addActionListener(actionListener);
110    add(b_prev);
111
112    b_next = new KButton(rm.getIcon("rightp.gif"));
113    b_next.addActionListener(actionListener);
114    add(b_next);
115
116    b_last = new KButton(rm.getIcon("lastp.gif"));
117    b_last.addActionListener(actionListener);
118    add(b_last);
119
120    resetButtons();
121    }
122
123  /* reset button states */
124
125  private void resetButtons()
126    {
127    b_first.setEnabled(true);
128    b_prev.setEnabled(true);
129    b_next.setEnabled(true);
130    b_last.setEnabled(true);
131
132    if(pos == 0)
133      {
134      b_first.setEnabled(false);
135      b_prev.setEnabled(false);
136      }
137
138    if(pos == (range - 1))
139      {
140      b_next.setEnabled(false);
141      b_last.setEnabled(false);
142      }
143    }
144
145  /** Get the traversal position. Returns the current traversal position, which
146    * will be an integer in the range [0, <code>getRange() - 1</code>],
147    * inclusive.
148    *
149    * @return The current position.
150    */
151
152  public int getPosition()
153    {
154    return(pos);
155    }
156
157  /** Get the traversal range. Returns the number of items being traversed by
158    * this component.
159    *
160    * @return The range.
161    */
162
163  public int getRange()
164    {
165    return(range);
166    }
167
168  /** Add an <code>ActionListener</code> to this component's list of listeners.
169    *
170    * @param listener The listener to add.
171    */
172
173  public void addActionListener(ActionListener listener)
174    {
175    support.addActionListener(listener);
176    }
177
178  /** Remove an <code>ActionListener</code> from this component's list of
179    * listeners.
180    *
181    * @param listener The listener to remove.
182    */
183  
184  public void removeActionListener(ActionListener listener)
185    {
186    support.removeActionListener(listener);
187    }
188
189  /* Handle events. */
190
191  private class _ActionListener implements ActionListener
192    {
193    public void actionPerformed(ActionEvent ev)
194      {
195      Object o = ev.getSource();
196      
197      if(o == b_first)
198        {
199        pos = 0;
200        resetButtons();
201        support.fireActionEvent(commands[FIRST]);
202        }
203      else if(o == b_prev)
204        {
205        if(pos > 0) pos--;
206        resetButtons();
207        support.fireActionEvent(commands[PREV]);
208        }
209      else if(o == b_next)
210        {
211        if(pos < (range - 1)) pos++;
212        resetButtons();
213        support.fireActionEvent(commands[NEXT]);
214        }
215      else if(o == b_last)
216        {
217        pos = range - 1;
218        resetButtons();
219        support.fireActionEvent(commands[LAST]);
220        }
221      }
222    }
223
224  }
225
226/* end of source file */