/*
 * @(#)MySpinner.java
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 2002 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDcreator.java for details.
 */

import javax.swing.*;

/**
 * On JRE < 1.4 use JTextField instead of JSpinner.
 *
 * @version 20021009
 * @author Jörg P. M. Haeger
 */
class MySpinner implements ValueContainer {
	private ValueContainer vc;

	public MySpinner(int value, int min, int max, int step) {
		try {
			getClass().getClassLoader().loadClass(
				"javax.swing.JSpinner");
			vc = new JSpinner(value, min, max, step);
			vc.setValue(new Integer(value));
		} catch (Exception e) {
			System.out.println(
				"JRE < 1.4: use JTextField instead of JSpinner");
			vc = new MyTextField(value, min, max);
			vc.setValue(new Integer(value));
		}
	}

	public JComponent getComponent() {
		return (JComponent)vc;
	}

	public Object getValue() {
		return vc.getValue();
	}

	public void setValue(Object value) {
		vc.setValue(value);
	}
}

interface ValueContainer {
	public Object getValue();
	public void setValue(Object value);
}

class MyTextField extends JTextField implements ValueContainer {
	private int min, max;

	MyTextField(int value, int min, int max) {
		setText(String.valueOf(value));
		this.min = min;
		this.max = max;
	}

	public Object getValue() {
		int n = new Integer(getText()).intValue();
		if (n < min)
			n = min;
		else
		if (n > max)
			n = max;
		return new Integer(n);
	}

	public void setValue(Object value) {
		setText(value.toString());
	}
}

class JSpinner extends javax.swing.JSpinner implements ValueContainer {
	JSpinner(int value, int min, int max, int step) {
		super(new SpinnerNumberModel(value, min, max, step));
	}
}
