refactored file package
[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 == EDIT) {
67                         
68                         // Open edit dialog in the future, after combo box has closed
69                         SwingUtilities.invokeLater(new Runnable() {
70                                 @Override
71                                 public void run() {
72                                         new EditMotorConfigurationDialog(rocket, BasicFrame.findFrame(rocket))
73                                                 .setVisible(true);
74                                 }
75                         });
76
77                         return;
78                 }
79                 if (!(item instanceof ID))
80                         return;
81                 
82                 ID idObject = (ID) item;
83                 config.setMotorConfigurationID(idObject.getID());
84         }
85
86
87         
88         ////////////////  Event/listener handling  ////////////////
89         
90         
91         @Override
92         public void addListDataListener(ListDataListener l) {
93                 listenerList.add(ListDataListener.class, l);
94         }
95
96         @Override
97         public void removeListDataListener(ListDataListener l) {
98                 listenerList.remove(ListDataListener.class, l);
99         }
100
101         protected void fireListDataEvent() {
102                 Object[] listeners = listenerList.getListenerList();
103                 ListDataEvent e = null;
104
105                 for (int i = listeners.length-2; i>=0; i-=2) {
106                         if (listeners[i] == ListDataListener.class) {
107                                 if (e == null)
108                                         e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, getSize());
109                                 ((ListDataListener) listeners[i+1]).contentsChanged(e);
110                         }
111                 }
112         }
113          
114         
115         @Override
116         public void stateChanged(ChangeEvent e) {
117                 if (e instanceof ComponentChangeEvent) {
118                         // Ignore unnecessary changes
119                         if (!((ComponentChangeEvent)e).isMotorChange())
120                                 return;
121                 }
122                 fireListDataEvent();
123         }
124         
125         
126         
127         /*
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.
131          */
132         
133         private ID get(String id) {
134                 ID idObject = map.get(id);
135                 if (idObject != null)
136                         return idObject;
137                 
138                 idObject = new ID(id);
139                 map.put(id, idObject);
140                 return idObject;
141         }
142         
143         
144         private class ID {
145                 private final String id;
146                 
147                 public ID(String id) {
148                         this.id = id;
149                 }
150                 
151                 public String getID() {
152                         return id;
153                 }
154                 
155                 @Override
156                 public String toString() {
157                         return rocket.getMotorConfigurationNameOrDescription(id);
158                 }
159         }
160         
161 }
162