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;
29 public UnitCellEditor() {
30 editor = new JComboBox();
31 editor.setEditable(false);
32 editor.addActionListener(this);
37 public Component getTableCellEditorComponent(JTable table, Object value,
38 boolean isSelected, int row, int column) {
40 Unit unit = (Unit) value;
41 UnitGroup group = getUnitGroup(unit, row, column);
43 editor.removeAllItems();
44 for (Unit u: group.getUnits()) {
48 editor.setSelectedItem(unit);
55 public Object getCellEditorValue() {
56 return editor.getSelectedItem();
62 public void actionPerformed(ActionEvent e) {
63 // End editing when a value has been selected
64 this.fireEditingStopped();
69 * Return the unit group corresponding to the specified cell.
71 * @param value the cell's value.
72 * @param row the cell's row.
73 * @param column the cell's column.
74 * @return the unit group of this cell.
76 protected abstract UnitGroup getUnitGroup(Unit value, int row, int column);