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:
043: package org.jfree.ui;
044:
045: import java.awt.BorderLayout;
046: import java.awt.GridLayout;
047: import java.awt.event.MouseEvent;
048: import java.awt.event.MouseListener;
049:
050: import javax.swing.JPanel;
051: import javax.swing.JTextField;
052: import javax.swing.SwingConstants;
053:
054: /**
055: * A very basic spinner component, used for demo purposes only.
056: *
057: * @author David Gilbert
058: */
059: public 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: } 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: }
|