    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;
    049import javax.swing.JPanel;
    050import javax.swing.JTextField;
    051import javax.swing.SwingConstants;
    052
    053/**
    054 * A very basic spinner component, used for demo purposes only.
    055 *
    056 * @author David Gilbert
    057 */
    058public class Spinner extends JPanel implements MouseListener {
    059
    060    /** The current value. */
    061    private int value;
    062
    063    /** The text field displaying the value. */
    064    private JTextField textField;
    065
    066    /** The arrow button panel. */
    067    private JPanel buttonPanel;
    068
    069    /** The up button. */
    070    private ArrowPanel upButton;
    071
    072    /** The down button. */
    073    private ArrowPanel downButton;
    074
    075    /**
    076     * Creates a new spinner.
    077     *
    078     * @param value  the initial value.
    079     */
    080    public Spinner(final int value) {
    081        super(new BorderLayout());
    082        this.value = value;
    083        this.textField = new JTextField(Integer.toString(this.value));
    084        this.textField.setHorizontalAlignment(SwingConstants.RIGHT);
    085        add(this.textField);
    086        this.buttonPanel = new JPanel(new GridLayout(2, 1, 0, 1));
    087        this.upButton = new ArrowPanel(ArrowPanel.UP);
    088        this.upButton.addMouseListener(this);
    089        this.downButton = new ArrowPanel(ArrowPanel.DOWN);
    090        this.downButton.addMouseListener(this);
    091        this.buttonPanel.add(this.upButton);
    092        this.buttonPanel.add(this.downButton);
    093        add(this.buttonPanel, BorderLayout.EAST);
    094    }
    095
    096    /**
    097     * Returns the current value.
    098     *
    099     * @return the current value.
    100     */
    101    public int getValue() {
    102        return this.value;
    103    }
    104
    105    /**
    106     * Receives notification of mouse clicks.
    107     *
    108     * @param e  the mouse event.
    109     */
    110    public void mouseClicked(final MouseEvent e) {
    111        if (e.getSource() == this.upButton) {
    112            this.value++;
    113            this.textField.setText(Integer.toString(this.value));
    114            firePropertyChange("value", this.value - 1, this.value);
    115        }
    116        else if (e.getSource() == this.downButton) {
    117            this.value--;
    118            this.textField.setText(Integer.toString(this.value));
    119            firePropertyChange("value", this.value + 1, this.value);
    120        }
    121    }
    122
    123    /**
    124     * Receives notification of mouse events.
    125     *
    126     * @param e  the mouse event.
    127     */
    128    public void mouseEntered(final MouseEvent e) {
    129        // ignored
    130    }
    131
    132    /**
    133     * Receives notification of mouse events.
    134     *
    135     * @param e  the mouse event.
    136     */
    137    public void mouseExited(final MouseEvent e) {
    138        // ignored
    139    }
    140
    141    /**
    142     * Receives notification of mouse events.
    143     *
    144     * @param e  the mouse event.
    145     */
    146    public void mousePressed(final MouseEvent e) {
    147        // ignored
    148    }
    149
    150    /**
    151     * Receives notification of mouse events.
    152     *
    153     * @param e  the mouse event.
    154     */
    155    public void mouseReleased(final MouseEvent e) {
    156        // ignored
    157    }
    158
    159}