Source for org.jfree.ui.Spinner

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * ------------
  28:  * Spinner.java
  29:  * ------------
  30:  * (C) Copyright 2002-2004, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 14-Oct-2002 : Version 1 (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.ui;
  44: 
  45: import java.awt.BorderLayout;
  46: import java.awt.GridLayout;
  47: import java.awt.event.MouseEvent;
  48: import java.awt.event.MouseListener;
  49: 
  50: import javax.swing.JPanel;
  51: import javax.swing.JTextField;
  52: import javax.swing.SwingConstants;
  53: 
  54: /**
  55:  * A very basic spinner component, used for demo purposes only.
  56:  *
  57:  * @author David Gilbert
  58:  */
  59: public class Spinner extends JPanel implements MouseListener {
  60: 
  61:     /** The current value. */
  62:     private int value;
  63: 
  64:     /** The text field displaying the value. */
  65:     private JTextField textField;
  66: 
  67:     /** The arrow button panel. */
  68:     private JPanel buttonPanel;
  69: 
  70:     /** The up button. */
  71:     private ArrowPanel upButton;
  72: 
  73:     /** The down button. */
  74:     private ArrowPanel downButton;
  75: 
  76:     /**
  77:      * Creates a new spinner.
  78:      *
  79:      * @param value  the initial value.
  80:      */
  81:     public Spinner(final int value) {
  82:         super(new BorderLayout());
  83:         this.value = value;
  84:         this.textField = new JTextField(Integer.toString(this.value));
  85:         this.textField.setHorizontalAlignment(SwingConstants.RIGHT);
  86:         add(this.textField);
  87:         this.buttonPanel = new JPanel(new GridLayout(2, 1, 0, 1));
  88:         this.upButton = new ArrowPanel(ArrowPanel.UP);
  89:         this.upButton.addMouseListener(this);
  90:         this.downButton = new ArrowPanel(ArrowPanel.DOWN);
  91:         this.downButton.addMouseListener(this);
  92:         this.buttonPanel.add(this.upButton);
  93:         this.buttonPanel.add(this.downButton);
  94:         add(this.buttonPanel, BorderLayout.EAST);
  95:     }
  96: 
  97:     /**
  98:      * Returns the current value.
  99:      *
 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: }