1 package net.sf.openrocket.gui.adaptors;
4 import java.util.HashMap;
7 import javax.swing.ComboBoxModel;
8 import javax.swing.SwingUtilities;
9 import javax.swing.event.ChangeEvent;
10 import javax.swing.event.ChangeListener;
11 import javax.swing.event.EventListenerList;
12 import javax.swing.event.ListDataEvent;
13 import javax.swing.event.ListDataListener;
15 import net.sf.openrocket.gui.dialogs.EditMotorConfigurationDialog;
16 import net.sf.openrocket.gui.main.BasicFrame;
17 import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
18 import net.sf.openrocket.rocketcomponent.Configuration;
19 import net.sf.openrocket.rocketcomponent.Rocket;
21 public class MotorConfigurationModel implements ComboBoxModel, ChangeListener {
23 private static final String EDIT = "Edit configurations";
26 private EventListenerList listenerList = new EventListenerList();
28 private final Configuration config;
29 private final Rocket rocket;
31 private Map<String, ID> map = new HashMap<String, ID>();
34 public MotorConfigurationModel(Configuration config) {
36 this.rocket = config.getRocket();
37 config.addChangeListener(this);
43 public Object getElementAt(int index) {
44 String[] ids = rocket.getMotorConfigurationIDs();
45 if (index < 0 || index > ids.length)
48 if (index == ids.length)
51 return get(ids[index]);
55 public int getSize() {
56 return rocket.getMotorConfigurationIDs().length + 1;
60 public Object getSelectedItem() {
61 return get(config.getMotorConfigurationID());
65 public void setSelectedItem(Object item) {
68 // Open edit dialog in the future, after combo box has closed
69 SwingUtilities.invokeLater(new Runnable() {
72 new EditMotorConfigurationDialog(rocket, BasicFrame.findFrame(rocket))
79 if (!(item instanceof ID))
82 ID idObject = (ID) item;
83 config.setMotorConfigurationID(idObject.getID());
88 //////////////// Event/listener handling ////////////////
92 public void addListDataListener(ListDataListener l) {
93 listenerList.add(ListDataListener.class, l);
97 public void removeListDataListener(ListDataListener l) {
98 listenerList.remove(ListDataListener.class, l);
101 protected void fireListDataEvent() {
102 Object[] listeners = listenerList.getListenerList();
103 ListDataEvent e = null;
105 for (int i = listeners.length-2; i>=0; i-=2) {
106 if (listeners[i] == ListDataListener.class) {
108 e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, getSize());
109 ((ListDataListener) listeners[i+1]).contentsChanged(e);
116 public void stateChanged(ChangeEvent e) {
117 if (e instanceof ComponentChangeEvent) {
118 // Ignore unnecessary changes
119 if (!((ComponentChangeEvent)e).isMotorChange())
128 * The ID class is an adapter, that contains the actual configuration ID,
129 * but gives the configuration description as its String representation.
130 * The get(id) method retrieves ID objects and caches them for reuse.
133 private ID get(String id) {
134 ID idObject = map.get(id);
135 if (idObject != null)
138 idObject = new ID(id);
139 map.put(id, idObject);
145 private final String id;
147 public ID(String id) {
151 public String getID() {
156 public String toString() {
157 return rocket.getMotorConfigurationNameOrDescription(id);