001: package com.xoetrope.awt;
002:
003: import java.io.IOException;
004:
005: import java.awt.event.AdjustmentEvent;
006: import java.awt.event.AdjustmentListener;
007:
008: import net.xoetrope.awt.XEdit;
009: import java.awt.Scrollbar;
010:
011: /**
012: * UpDown Control
013: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
014: * the GNU Public License (GPL), please see license.txt for more details. If
015: * you make commercial use of this software you must purchase a commercial
016: * license from Xoetrope.</p>
017: * <p>$Revision: 1.2 $</p>
018: */
019: public class XSpinner extends Scrollbar implements AdjustmentListener {
020: public XSpinner() {
021: initialize();
022: }
023:
024: private void initialize() {
025: setOrientation(Scrollbar.VERTICAL);
026: addAdjustmentListener(this );
027: setRange(0, 100);
028: }
029:
030: public void setBuddy(XEdit je) {
031: buddyEdit = je;
032: }
033:
034: public void setRange(int min, int max) {
035: setMinimum(min - 1);
036: // This is a complete hack, seems like something messed up in Swing or the JDK.
037: setMaximum(max + 11);
038: maxValue = max;
039: minValue = min;
040: value = Math.max(value, minValue);
041: value = Math.min(value, maxValue);
042: setValue((int) value);
043: if (buddyEdit != null)
044: buddyEdit.setText(new Double(maxValue - value).toString());
045: setBlockIncrement(1);
046: setUnitIncrement(1);
047: }
048:
049: public void adjustmentValueChanged(AdjustmentEvent e) {
050: if (bInAdjustment)
051: return;
052:
053: if (buddyEdit != null) {
054: double bv = new Double(buddyEdit.getText()).doubleValue();
055: double newValue = 0;
056: double newScrollValue = e.getValue();
057: if (newScrollValue > value)
058: newValue = Math.max(minValue, --bv);
059: else if (newScrollValue < value)
060: newValue = Math.min(maxValue, ++bv);
061:
062: bInAdjustment = true;
063: buddyEdit.setText(new Double(newValue).toString());
064: value = newValue;
065: setValue((int) value);
066: bInAdjustment = false;
067: //buddyEdit.setValue( e.getValue() );
068: }
069: }
070:
071: /**
072: * Performs any post creation initialisation of the control.
073: */
074: public void init() throws IOException {
075: doLayout();
076: }
077:
078: /**
079: * Sets the value corresponding to the maximum value of the meter
080: */
081: public void setMaxValue(double _value) {
082: setMaximum((int) _value + 1);
083: maxValue = (int) _value;
084: }
085:
086: /**
087: * Sets the value corresponding to the minimum value of the meter
088: */
089: public void setMinValue(double _value) {
090: setMinimum((int) _value - 1);
091: minValue = (int) _value;
092: }
093:
094: private XEdit buddyEdit = null;
095: private double maxValue = 100;
096: private double minValue = 0;
097: private double value = 0;
098:
099: private static boolean bInAdjustment = false;
100: }
|