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