create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / adaptors / MotorConfigurationModel.java
1 package net.sf.openrocket.gui.adaptors;
2
3
4 import java.util.EventObject;
5 import java.util.HashMap;
6 import java.util.Map;
7
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;
13
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;
22
23 public class MotorConfigurationModel implements ComboBoxModel, StateChangeListener {
24         private static final Translator trans = Application.getTranslator();
25
26         private static final String EDIT = trans.get("MotorCfgModel.Editcfg");
27         
28         
29         private EventListenerList listenerList = new EventListenerList();
30         
31         private final Configuration config;
32         private final Rocket rocket;
33         
34         private Map<String, ID> map = new HashMap<String, ID>();
35         
36
37         public MotorConfigurationModel(Configuration config) {
38                 this.config = config;
39                 this.rocket = config.getRocket();
40                 config.addChangeListener(this);
41         }
42         
43         
44         
45         @Override
46         public Object getElementAt(int index) {
47                 String[] ids = rocket.getMotorConfigurationIDs();
48                 if (index < 0  ||  index > ids.length)
49                         return null;
50                 
51                 if (index == ids.length)
52                         return EDIT;
53                 
54                 return get(ids[index]);
55         }
56
57         @Override
58         public int getSize() {
59                 return rocket.getMotorConfigurationIDs().length + 1;
60         }
61
62         @Override
63         public Object getSelectedItem() {
64                 return get(config.getMotorConfigurationID());
65         }
66
67         @Override
68         public void setSelectedItem(Object item) {
69                 if (item == null) {
70                         // Clear selection - huh?
71                         return;
72                 }
73                 if (item == EDIT) {
74                         
75                         // Open edit dialog in the future, after combo box has closed
76                         SwingUtilities.invokeLater(new Runnable() {
77                                 @Override
78                                 public void run() {
79                                         new EditMotorConfigurationDialog(rocket, BasicFrame.findFrame(rocket))
80                                                 .setVisible(true);
81                                 }
82                         });
83
84                         return;
85                 }
86                 if (!(item instanceof ID)) {
87                         throw new IllegalArgumentException("MotorConfigurationModel item="+item);
88                 }
89                 
90                 ID idObject = (ID) item;
91                 config.setMotorConfigurationID(idObject.getID());
92         }
93
94
95         
96         ////////////////  Event/listener handling  ////////////////
97         
98         
99         @Override
100         public void addListDataListener(ListDataListener l) {
101                 listenerList.add(ListDataListener.class, l);
102         }
103
104         @Override
105         public void removeListDataListener(ListDataListener l) {
106                 listenerList.remove(ListDataListener.class, l);
107         }
108
109         protected void fireListDataEvent() {
110                 Object[] listeners = listenerList.getListenerList();
111                 ListDataEvent e = null;
112
113                 for (int i = listeners.length-2; i>=0; i-=2) {
114                         if (listeners[i] == ListDataListener.class) {
115                                 if (e == null)
116                                         e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, getSize());
117                                 ((ListDataListener) listeners[i+1]).contentsChanged(e);
118                         }
119                 }
120         }
121          
122         
123         @Override
124         public void stateChanged(EventObject e) {
125                 if (e instanceof ComponentChangeEvent) {
126                         // Ignore unnecessary changes
127                         if (!((ComponentChangeEvent)e).isMotorChange())
128                                 return;
129                 }
130                 fireListDataEvent();
131         }
132         
133         
134         
135         /*
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.
139          */
140         
141         private ID get(String id) {
142                 ID idObject = map.get(id);
143                 if (idObject != null)
144                         return idObject;
145                 
146                 idObject = new ID(id);
147                 map.put(id, idObject);
148                 return idObject;
149         }
150         
151         
152         private class ID {
153                 private final String id;
154                 
155                 public ID(String id) {
156                         this.id = id;
157                 }
158                 
159                 public String getID() {
160                         return id;
161                 }
162                 
163                 @Override
164                 public String toString() {
165                         return rocket.getMotorConfigurationNameOrDescription(id);
166                 }
167         }
168         
169 }
170