package com.rle.monitor; /* * RLE Surface Crack Detector Graphing Software * * Copyright (C) 2002 RLE Technologies * For more information visit http://www.rletech.com/ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ /** * @author Andy Goth * @author Eric Peterson * @author Matt Lane * @version 0.1 */ import java.awt.*; import java.awt.event.*; import java.text.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.plaf.*; import javax.swing.text.*; /** Spinner! */ public class MySpinner extends JComponent { private String text; private int min; private int max; private int val; public MySpinner() {this(null, 0, 255, 0);} public MySpinner(int min, int max, int val) {this(null, min, max, val);} public MySpinner(String text) {this(text, 0, 255, 0);} public MySpinner(String text, int min, int max, int val) { this.text = text; this.min = min; this.max = max; this.val = val; ComponentUI ui = MySpinnerUI.createUI(this); ui.installUI(this); setUI(ui); } public MySpinnerUI getUI() {return (MySpinnerUI)ui;} public void setUI(MySpinnerUI ui) {super.setUI(ui);} public void updateUI() { setUI((MySpinnerUI)UIManager.getUI(this)); invalidate(); } public void setText(String text) {this.text = text;} public String getText() {return text;} public int getValue() {return val;} public void setValue(int val) { setValueQuiet(val); fireStateChanged(); } public void setValueQuiet(int val) { if (val < min) this.val = min; else if (val > max) this.val = max; else this.val = val; repaint(); } public int getMinimum() {return min;} public void setMinimum(int min) { if (min > max) this.max = min; this.min = min; if (val < min) { this.val = min; repaint(); fireStateChanged(); } } public int getMaximum() {return max;} public void setMaximum(int max) { if (max < min) this.min = max; this.max = max; if (val > max) { this.val = max; repaint(); fireStateChanged(); } } public void addChangeListener(ChangeListener listener) { listenerList.add(ChangeListener.class, listener); } public void removeChangeListener(ChangeListener listener) { listenerList.remove(ChangeListener.class, listener); } /** Sends out notifications that the value changed. */ protected void fireStateChanged() { Object[] listeners = listenerList.getListenerList(); ChangeEvent e = null; for (int i = listeners.length - 2; i >= 0; i -= 2) { if (listeners[i] != ChangeListener.class) continue; if (e == null) e = new ChangeEvent(this); ((ChangeListener)listeners[i + 1]).stateChanged(e); } } public boolean isFocusTraversable() {return isEnabled();} private static class IncrementAction extends AbstractAction { private boolean advance; IncrementAction(String name, boolean advance) { super(name); this.advance = advance; } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (!(source instanceof MySpinner)) return; MySpinner s = (MySpinner)source; int val = s.getValue() + (advance ? +1 : -1); if (s.getMinimum() <= val && val <= s.getMaximum()); s.setValue(val); } } }