1 package net.sf.openrocket.gui.components;
3 import java.awt.Component;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
7 import javax.swing.AbstractCellEditor;
8 import javax.swing.JComboBox;
9 import javax.swing.JTable;
10 import javax.swing.table.TableCellEditor;
12 import net.sf.openrocket.unit.Unit;
13 import net.sf.openrocket.unit.UnitGroup;
17 * A cell editor that returns a combo box containing a selection of units.
18 * Using classes must implement the {@link #getUnitGroup(Unit, int, int)} method
19 * to return the appropriate unit group for the selection.
21 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
23 public abstract class UnitCellEditor extends AbstractCellEditor
24 implements TableCellEditor, ActionListener {
26 private final JComboBox editor;
28 public UnitCellEditor() {
29 editor = new JComboBox();
30 editor.setEditable(false);
31 editor.addActionListener(this);
36 public Component getTableCellEditorComponent(JTable table, Object value,
37 boolean isSelected, int row, int column) {
39 Unit unit = (Unit) value;
40 UnitGroup group = getUnitGroup(unit, row, column);
42 editor.removeAllItems();
43 for (Unit u : group.getUnits()) {
47 editor.setSelectedItem(unit);
54 public Object getCellEditorValue() {
55 return editor.getSelectedItem();
61 public void actionPerformed(ActionEvent e) {
62 // End editing when a value has been selected
63 this.fireEditingStopped();
68 * Return the unit group corresponding to the specified cell.
70 * @param value the cell's value.
71 * @param row the cell's row.
72 * @param column the cell's column.
73 * @return the unit group of this cell.
75 protected abstract UnitGroup getUnitGroup(Unit value, int row, int column);