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) {
67 // Clear selection - huh?
72 // Open edit dialog in the future, after combo box has closed
73 SwingUtilities.invokeLater(new Runnable() {
76 new EditMotorConfigurationDialog(rocket, BasicFrame.findFrame(rocket))
83 if (!(item instanceof ID)) {
84 throw new IllegalArgumentException("MotorConfigurationModel item="+item);
87 ID idObject = (ID) item;
88 config.setMotorConfigurationID(idObject.getID());
93 //////////////// Event/listener handling ////////////////
97 public void addListDataListener(ListDataListener l) {
98 listenerList.add(ListDataListener.class, l);
102 public void removeListDataListener(ListDataListener l) {
103 listenerList.remove(ListDataListener.class, l);
106 protected void fireListDataEvent() {
107 Object[] listeners = listenerList.getListenerList();
108 ListDataEvent e = null;
110 for (int i = listeners.length-2; i>=0; i-=2) {
111 if (listeners[i] == ListDataListener.class) {
113 e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, getSize());
114 ((ListDataListener) listeners[i+1]).contentsChanged(e);
121 public void stateChanged(ChangeEvent e) {
122 if (e instanceof ComponentChangeEvent) {
123 // Ignore unnecessary changes
124 if (!((ComponentChangeEvent)e).isMotorChange())
133 * The ID class is an adapter, that contains the actual configuration ID,
134 * but gives the configuration description as its String representation.
135 * The get(id) method retrieves ID objects and caches them for reuse.
138 private ID get(String id) {
139 ID idObject = map.get(id);
140 if (idObject != null)
143 idObject = new ID(id);
144 map.put(id, idObject);
150 private final String id;
152 public ID(String id) {
156 public String getID() {
161 public String toString() {
162 return rocket.getMotorConfigurationNameOrDescription(id);