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: ToggleIndicator.java,v $
023   Revision 1.6  2004/05/12 18:52:46  markl
024   comment block updates
025
026   Revision 1.5  2003/01/19 09:50:54  markl
027   Javadoc & comment header updates.
028
029   Revision 1.4  2001/03/12 09:28:00  markl
030   Source code and Javadoc cleanup.
031
032   Revision 1.3  1999/04/25 04:23:55  markl
033   Added javadoc screenshot.
034
035   Revision 1.2  1999/04/23 07:40:14  markl
036   Added synchronization.
037
038   Revision 1.1  1999/04/23 07:25:31  markl
039   Initial revision
040   ----------------------------------------------------------------------------
041*/
042
043package kiwi.ui;
044
045import java.awt.*;
046import javax.swing.*;
047
048import kiwi.util.KiwiUtils;
049
050/** A graphical toggle component. This component displays one of two images,
051 * depending on whether it is "toggled" on or off. This component can be used
052 * to create LEDs and other types of on-off indicators.
053 *
054 * <p><center>
055 * <img src="snapshot/ToggleIndicator.gif"><br>
056 * <i>Some example ToggleIndicators.</i>
057 * </center>
058 *
059 * @author Mark Lindner
060 */
061
062public class ToggleIndicator extends JLabel
063  {
064  private Icon icon, altIcon;
065  private boolean state = false;
066
067  /** Construct a new <code>ToggleIndicator</code> with the specified icons for
068   * the "on" and "off" states.
069   *
070   * @param icon The icon to display when the toggle is off.
071   * @param altIcon The icon to display when the toggle is on.
072   */
073  
074  public ToggleIndicator(Icon icon, Icon altIcon)
075    {
076    this.icon = icon;
077    this.altIcon = altIcon;
078
079    setHorizontalAlignment(SwingConstants.CENTER);
080    setVerticalAlignment(SwingConstants.CENTER);
081
082    setIcon(icon);
083    }
084
085  /** Set the toggle state.
086   *
087   * @param state <code>true</code> to turn the toggle "on",
088   * <code>false</code> to turn it "off." The toggle will be repainted
089   * immediately.
090   */
091  
092  public synchronized void setState(boolean state)
093    {
094    this.state = state;
095    
096    setIcon(state ? icon : altIcon);
097    KiwiUtils.paintImmediately(this);
098    }
099
100  /** Get the current state of the toggle.
101   *
102   * @return The current toggle state.
103   */
104
105  public synchronized boolean getState()
106    {
107    return(state);
108    }
109
110  }
111
112/* end of source file */