001/* ========================================================================
002 * JCommon : a free general purpose class library for the Java(tm) platform
003 * ========================================================================
004 *
005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006 * 
007 * Project Info:  http://www.jfree.org/jcommon/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it 
010 * under the terms of the GNU Lesser General Public License as published by 
011 * the Free Software Foundation; either version 2.1 of the License, or 
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but 
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022 * USA.  
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025 * in the United States and other countries.]
026 * 
027 * ------------
028 * Spinner.java
029 * ------------
030 * (C) Copyright 2002-2004, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * $Id $
036 *
037 * Changes
038 * -------
039 * 14-Oct-2002 : Version 1 (DG);
040 *
041 */
042
043package org.jfree.ui;
044
045import java.awt.BorderLayout;
046import java.awt.GridLayout;
047import java.awt.event.MouseEvent;
048import java.awt.event.MouseListener;
049
050import javax.swing.JPanel;
051import javax.swing.JTextField;
052import javax.swing.SwingConstants;
053
054/**
055 * A very basic spinner component, used for demo purposes only.
056 *
057 * @author David Gilbert
058 */
059public class Spinner extends JPanel implements MouseListener {
060
061    /** The current value. */
062    private int value;
063
064    /** The text field displaying the value. */
065    private JTextField textField;
066
067    /** The arrow button panel. */
068    private JPanel buttonPanel;
069
070    /** The up button. */
071    private ArrowPanel upButton;
072
073    /** The down button. */
074    private ArrowPanel downButton;
075
076    /**
077     * Creates a new spinner.
078     *
079     * @param value  the initial value.
080     */
081    public Spinner(final int value) {
082        super(new BorderLayout());
083        this.value = value;
084        this.textField = new JTextField(Integer.toString(this.value));
085        this.textField.setHorizontalAlignment(SwingConstants.RIGHT);
086        add(this.textField);
087        this.buttonPanel = new JPanel(new GridLayout(2, 1, 0, 1));
088        this.upButton = new ArrowPanel(ArrowPanel.UP);
089        this.upButton.addMouseListener(this);
090        this.downButton = new ArrowPanel(ArrowPanel.DOWN);
091        this.downButton.addMouseListener(this);
092        this.buttonPanel.add(this.upButton);
093        this.buttonPanel.add(this.downButton);
094        add(this.buttonPanel, BorderLayout.EAST);
095    }
096
097    /**
098     * Returns the current value.
099     *
100     * @return the current value.
101     */
102    public int getValue() {
103        return this.value;
104    }
105
106    /**
107     * Receives notification of mouse clicks.
108     *
109     * @param e  the mouse event.
110     */
111    public void mouseClicked(final MouseEvent e) {
112        if (e.getSource() == this.upButton) {
113            this.value++;
114            this.textField.setText(Integer.toString(this.value));
115            firePropertyChange("value", this.value - 1, this.value);
116        }
117        else if (e.getSource() == this.downButton) {
118            this.value--;
119            this.textField.setText(Integer.toString(this.value));
120            firePropertyChange("value", this.value + 1, this.value);
121        }
122    }
123
124    /**
125     * Receives notification of mouse events.
126     *
127     * @param e  the mouse event.
128     */
129    public void mouseEntered(final MouseEvent e) {
130        // ignored
131    }
132
133    /**
134     * Receives notification of mouse events.
135     *
136     * @param e  the mouse event.
137     */
138    public void mouseExited(final MouseEvent e) {
139        // ignored
140    }
141
142    /**
143     * Receives notification of mouse events.
144     *
145     * @param e  the mouse event.
146     */
147    public void mousePressed(final MouseEvent e) {
148        // ignored
149    }
150
151    /**
152     * Receives notification of mouse events.
153     *
154     * @param e  the mouse event.
155     */
156    public void mouseReleased(final MouseEvent e) {
157        // ignored
158    }
159
160}