Frames | No Frames |
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: import javax.swing.JPanel; 50: import javax.swing.JTextField; 51: import javax.swing.SwingConstants; 52: 53: /** 54: * A very basic spinner component, used for demo purposes only. 55: * 56: * @author David Gilbert 57: */ 58: public class Spinner extends JPanel implements MouseListener { 59: 60: /** The current value. */ 61: private int value; 62: 63: /** The text field displaying the value. */ 64: private JTextField textField; 65: 66: /** The arrow button panel. */ 67: private JPanel buttonPanel; 68: 69: /** The up button. */ 70: private ArrowPanel upButton; 71: 72: /** The down button. */ 73: private ArrowPanel downButton; 74: 75: /** 76: * Creates a new spinner. 77: * 78: * @param value the initial value. 79: */ 80: public Spinner(final int value) { 81: super(new BorderLayout()); 82: this.value = value; 83: this.textField = new JTextField(Integer.toString(this.value)); 84: this.textField.setHorizontalAlignment(SwingConstants.RIGHT); 85: add(this.textField); 86: this.buttonPanel = new JPanel(new GridLayout(2, 1, 0, 1)); 87: this.upButton = new ArrowPanel(ArrowPanel.UP); 88: this.upButton.addMouseListener(this); 89: this.downButton = new ArrowPanel(ArrowPanel.DOWN); 90: this.downButton.addMouseListener(this); 91: this.buttonPanel.add(this.upButton); 92: this.buttonPanel.add(this.downButton); 93: add(this.buttonPanel, BorderLayout.EAST); 94: } 95: 96: /** 97: * Returns the current value. 98: * 99: * @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: }