1 package net.sf.openrocket.gui.adaptors;
4 import java.util.EventObject;
5 import java.util.HashMap;
8 import javax.swing.ComboBoxModel;
9 import javax.swing.SwingUtilities;
10 import javax.swing.event.EventListenerList;
11 import javax.swing.event.ListDataEvent;
12 import javax.swing.event.ListDataListener;
14 import net.sf.openrocket.gui.dialogs.EditMotorConfigurationDialog;
15 import net.sf.openrocket.gui.main.BasicFrame;
16 import net.sf.openrocket.l10n.Translator;
17 import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
18 import net.sf.openrocket.rocketcomponent.Configuration;
19 import net.sf.openrocket.rocketcomponent.Rocket;
20 import net.sf.openrocket.startup.Application;
21 import net.sf.openrocket.util.StateChangeListener;
23 public class MotorConfigurationModel implements ComboBoxModel, StateChangeListener {
24 private static final Translator trans = Application.getTranslator();
26 private static final String EDIT = trans.get("MotorCfgModel.Editcfg");
29 private EventListenerList listenerList = new EventListenerList();
31 private final Configuration config;
32 private final Rocket rocket;
34 private Map<String, ID> map = new HashMap<String, ID>();
37 public MotorConfigurationModel(Configuration config) {
39 this.rocket = config.getRocket();
40 config.addChangeListener(this);
46 public Object getElementAt(int index) {
47 String[] ids = rocket.getMotorConfigurationIDs();
48 if (index < 0 || index > ids.length)
51 if (index == ids.length)
54 return get(ids[index]);
58 public int getSize() {
59 return rocket.getMotorConfigurationIDs().length + 1;
63 public Object getSelectedItem() {
64 return get(config.getMotorConfigurationID());
68 public void setSelectedItem(Object item) {
70 // Clear selection - huh?
75 // Open edit dialog in the future, after combo box has closed
76 SwingUtilities.invokeLater(new Runnable() {
79 new EditMotorConfigurationDialog(rocket, BasicFrame.findFrame(rocket))
86 if (!(item instanceof ID)) {
87 throw new IllegalArgumentException("MotorConfigurationModel item="+item);
90 ID idObject = (ID) item;
91 config.setMotorConfigurationID(idObject.getID());
96 //////////////// Event/listener handling ////////////////
100 public void addListDataListener(ListDataListener l) {
101 listenerList.add(ListDataListener.class, l);
105 public void removeListDataListener(ListDataListener l) {
106 listenerList.remove(ListDataListener.class, l);
109 protected void fireListDataEvent() {
110 Object[] listeners = listenerList.getListenerList();
111 ListDataEvent e = null;
113 for (int i = listeners.length-2; i>=0; i-=2) {
114 if (listeners[i] == ListDataListener.class) {
116 e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, getSize());
117 ((ListDataListener) listeners[i+1]).contentsChanged(e);
124 public void stateChanged(EventObject e) {
125 if (e instanceof ComponentChangeEvent) {
126 // Ignore unnecessary changes
127 if (!((ComponentChangeEvent)e).isMotorChange())
136 * The ID class is an adapter, that contains the actual configuration ID,
137 * but gives the configuration description as its String representation.
138 * The get(id) method retrieves ID objects and caches them for reuse.
141 private ID get(String id) {
142 ID idObject = map.get(id);
143 if (idObject != null)
146 idObject = new ID(id);
147 map.put(id, idObject);
153 private final String id;
155 public ID(String id) {
159 public String getID() {
164 public String toString() {
165 return rocket.getMotorConfigurationNameOrDescription(id);