Newer
Older
marking / src / main / java / ui / RadioGroup.java
package ui;

import java.awt.FlowLayout;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class RadioGroup extends JPanel {

	private Integer min;
	private Integer max;

	private final Map<Integer, JRadioButton> radios = new HashMap<>();
	private final Map<ButtonModel, Integer> models = new HashMap<>();
	private final ButtonGroup group = new ButtonGroup();

	public RadioGroup(int min, int max) {
		this.min = min;
		this.max = max;

		this.setLayout(new FlowLayout());
		for (int i = min; i <= max; i++) {
			JRadioButton rad = new JRadioButton();
			rad.setText(String.valueOf(i));
			radios.put(i, rad);
			group.add(rad);
			models.put(rad.getModel(), i);
			this.add(rad);
		}
	}

	public void setSelected(Integer idx) {
		JRadioButton rad = radios.get(idx);
		group.setSelected(rad.getModel(), true);
	}

	public void setMin(Integer min) {
		this.min = min;
	}

	public void setMax(Integer max) {
		this.max = max;
	}

	public void max() {
		setSelected(max);
	}

	public void zero() {
		setSelected(0);
	}

	public Integer getSelected() {
		return models.get(group.getSelection());
	}

	public void clearSelection() {
		group.clearSelection();
	}

//   public void setRange(int min, int max) {
//      this.min = min;
//      this.max = max;
//
//      radios.clear();
//      models.clear();
//      group = new ButtonGroup();
//      this.removeAll();
//
//      for (int i = min; i <= max; i++) {
//         JRadioButton rad = new JRadioButton();
//         rad.setText(String.valueOf(i));
//         radios.put(i, rad);
//         group.add(rad);
//         models.put(rad.getModel(), i);
//         this.add(rad);
//      }
//   }
}