create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / components / UnitCellEditor.java
1 package net.sf.openrocket.gui.components;
2
3 import java.awt.Component;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6
7 import javax.swing.AbstractCellEditor;
8 import javax.swing.JComboBox;
9 import javax.swing.JTable;
10 import javax.swing.table.TableCellEditor;
11
12 import net.sf.openrocket.unit.Unit;
13 import net.sf.openrocket.unit.UnitGroup;
14
15
16 /**
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.
20  * 
21  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
22  */
23 public abstract class UnitCellEditor extends AbstractCellEditor
24                 implements TableCellEditor, ActionListener {
25         
26         private final JComboBox editor;
27         
28         public UnitCellEditor() {
29                 editor = new JComboBox();
30                 editor.setEditable(false);
31                 editor.addActionListener(this);
32         }
33         
34         
35         @Override
36         public Component getTableCellEditorComponent(JTable table, Object value,
37                         boolean isSelected, int row, int column) {
38                 
39                 Unit unit = (Unit) value;
40                 UnitGroup group = getUnitGroup(unit, row, column);
41                 
42                 editor.removeAllItems();
43                 for (Unit u : group.getUnits()) {
44                         editor.addItem(u);
45                 }
46                 
47                 editor.setSelectedItem(unit);
48                 
49                 return editor;
50         }
51         
52         
53         @Override
54         public Object getCellEditorValue() {
55                 return editor.getSelectedItem();
56         }
57         
58         
59
60         @Override
61         public void actionPerformed(ActionEvent e) {
62                 // End editing when a value has been selected
63                 this.fireEditingStopped();
64         }
65         
66         
67         /**
68          * Return the unit group corresponding to the specified cell.
69          * 
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.
74          */
75         protected abstract UnitGroup getUnitGroup(Unit value, int row, int column);
76         
77 }