001/*
002 *  $Source: $
003 *  $Name:  $
004 *  $Revision: 567 $
005 *  $Date: 2012-11-03 20:36:02 -0700 (Sat, 03 Nov 2012) $
006 *  $Locker: $
007 */
008/*
009 *  JToggleButton -- A simple two state toggle button.
010 *
011 *  Copyright (C) 2003 WebARTS Design, North Vancouver Canada
012 *  http://www.webarts.bc.ca
013 *
014 *  This program is free software; you can redistribute it and/ormodify
015 *  it under the terms of Version 2 of the GNU General Public Licenseas
016 *  published by the Free Software Foundation.
017 *
018 *  This program is distributed in the hope that it will be useful,
019 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
020 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
021 *  GNU General Public License for more details.
022 *
023 *  You should have received a copy of the GNU General Public License
024 *  along with this program; if not, write to the Free Software
025 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
026 */
027package ca.bc.webarts.widgets;
028
029import java.awt.BorderLayout;
030import java.awt.Dimension;
031import java.awt.event.ActionEvent;
032import java.awt.event.ActionListener;
033import java.awt.event.MouseAdapter;
034import java.awt.event.MouseEvent;
035import java.awt.event.MouseListener;
036import java.awt.event.MouseMotionAdapter;
037import java.awt.event.WindowAdapter;
038import java.awt.event.WindowEvent;
039import java.io.File;
040import javax.swing.BorderFactory;
041import javax.swing.ImageIcon;
042import javax.swing.JButton;
043import javax.swing.JFrame;
044import javax.swing.JWindow;
045import javax.swing.border.BevelBorder;
046import javax.swing.border.EmptyBorder;
047
048import kiwi.ui.KButton;
049import kiwi.util.KiwiUtils;
050
051
052/**
053 *  This is a simple implementation of a single button that performslike a
054 *  Toggle button. It when pressed it "remembers" its state and remainsin that
055 *  switched state until pressed again. Note that this is different thana normal
056 *  button that stays pressed only while you are pressing it downwith your
057 *  mouse.<p>
058 *
059 *  It performs like a Checkbox, except it does NOT havethat pesky little check
060 *  and box.</p>
061 *
062 * @author    tgutwin
063 */
064public class JToggleButton extends KButton implements ActionListener
065{
066
067  /**  The dir where the images are located.  */
068  final static String IMAGE_DIR = "." + File.separator +
069      "images" + File.separator;
070  /**  A Class holder for its name (could be used in Logging).  */
071  private static String className_ = "JToggleButton";
072
073  /**
074   *  The ActionListener handles the state changing when the button is pressed.
075   */
076  ActionListener myButtonActionListener =
077    new ActionListener()
078    {
079      public void actionPerformed(ActionEvent e)
080      {
081        JToggleButton but = (JToggleButton) e.getSource();
082        if (but.getState() == true)
083        {
084          currentState_ = false;
085          but.setToolTipText("Off");
086          if (!flatStyle_)
087          {
088            but.setBorder(BorderFactory.createBevelBorder(
089                BevelBorder.RAISED));
090          }
091          but.setIcon(iconOff_);
092        }
093        else
094        {
095          currentState_ = true;
096          but.setToolTipText("On");
097          if (!flatStyle_)
098          {
099            but.setBorder(BorderFactory.createBevelBorder(
100                BevelBorder.LOWERED));
101          }
102          but.setIcon(iconOn_);
103        }
104      }
105    };
106
107  /**
108   *  An Mose Listener Adapter to listen for mouse events and handles the border
109   *  changes.
110   */
111  MouseAdapter myButtonMouseAdapter =
112    new MouseAdapter()
113    {
114      public void mouseEntered(MouseEvent e)
115      {
116        JToggleButton but = (JToggleButton) e.getSource();
117        if (but.isEnabled())
118        {
119          but.setBorderPainted(true);
120          but.validate();
121        }
122      }
123
124
125      public void mouseExited(MouseEvent e)
126      {
127        JButton but = (JButton) e.getSource();
128        if (but.isEnabled())
129        {
130          but.setBorderPainted(false);
131          but.validate();
132        }
133      }
134
135
136      public void mousePressed(MouseEvent e)
137      {
138        JButton but = (JButton) e.getSource();
139        if (but.isEnabled())
140        {
141          but.setBorder(BorderFactory.createBevelBorder(
142              BevelBorder.LOWERED));
143          but.validate();
144        }
145      }
146
147
148      public void mouseReleased(MouseEvent e)
149      {
150        JButton but = (JButton) e.getSource();
151        if (but.isEnabled())
152        {
153          but.setBorder(BorderFactory.createBevelBorder(
154              BevelBorder.RAISED));
155          but.validate();
156        }
157      }
158    };
159
160  /**  currentState_ remembers the On/OFF state of the button * */
161  private boolean currentState_ = true;
162
163  /**
164   *  flatStyle_ controls if the look of this button will have a borderor not.
165   *  DEFAULT = false.
166   */
167  private boolean flatStyle_ = false;
168
169  /**  The filename for the ON image icon. */
170  private String onImage_ =
171      "/images/org/javalobby/icons/20x20/BulbOn.gif";
172  /**  The filename for the ON image icon. */
173  private String offImage_ =
174      "/images/org/javalobby/icons/20x20/BulbOff.gif";
175
176  /**  The Image Icon used when this button is in its ON state.  */
177  private ImageIcon iconOn_ = null;
178
179  /**  The Image Icon used when this button is in its OFF state.  */
180  private ImageIcon iconOff_ = null;
181
182
183  /**  Basic constructor for this Object.  */
184  public JToggleButton()
185  {
186    super("");
187    //    System.out.println("Constructor1");
188    final String methodName = className_ + ": Constructor()";
189    initUI();
190    currentState_ = true;
191    setToolTipText("On");
192    if (!flatStyle_)
193    {
194      setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
195    }
196    setIcon(iconOn_);
197    addActionListener(myButtonActionListener);
198  }
199
200
201  /**
202   *  The main test program for this class
203   *
204   * @param  args  The command line arguments
205   */
206  public static void main(String[] args)
207  {
208    JFrame appFrame = new JFrame();
209    appFrame.getContentPane().setLayout(new BorderLayout());
210    appFrame.setTitle("JToggleButton TEST Window.");
211    final JToggleButton myTestButton = new JToggleButton();
212    appFrame.addWindowListener(
213      new WindowAdapter()
214      {
215        public void windowClosing(WindowEvent e)
216        {
217          //myTestButton.getLog().close();
218          System.exit(0);
219        }
220      });
221    appFrame.getContentPane().add(myTestButton);
222
223    appFrame.pack();
224    KiwiUtils.centerWindow(appFrame);
225    appFrame.toFront();
226    appFrame.setVisible(true);
227    appFrame.repaint();
228  }
229
230
231  /**
232   *  Gets the state attribute of the JToggleButton object
233   *
234   * @return    The state value
235   */
236  public boolean getState()
237  {
238    return currentState_;
239  }
240
241
242  /**
243   *  Handles all the Actions originating from the Control Buttons. *
244   *
245   * @param  e  Description of Parameter
246   */
247  public void actionPerformed(ActionEvent e)
248  {
249    final String methodName = className_ + ": actionPerformed()";
250  }
251
252
253  /**  Description of the Method */
254  private void initUI()
255  {
256    iconOn_ = new ImageIcon(Util.loadImage(onImage_), "On");
257    iconOff_ = new ImageIcon(Util.loadImage(offImage_), "Off");
258    if (!flatStyle_)
259    {
260      this.setBorder(BorderFactory.createBevelBorder(
261          BevelBorder.LOWERED));
262    }
263    this.setPreferredSize(
264        new Dimension(iconOn_.getIconWidth() + 2, iconOn_.getIconHeight() + 2));
265  }
266
267}
268