Preset dialog
[debian/openrocket] / core / src / net / sf / openrocket / gui / adaptors / PresetModel.java
1 package net.sf.openrocket.gui.adaptors;
2
3 import java.util.List;
4
5 import javax.swing.AbstractListModel;
6 import javax.swing.ComboBoxModel;
7
8 import net.sf.openrocket.l10n.Translator;
9 import net.sf.openrocket.logging.LogHelper;
10 import net.sf.openrocket.preset.ComponentPreset;
11 import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
12 import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
13 import net.sf.openrocket.rocketcomponent.RocketComponent;
14 import net.sf.openrocket.startup.Application;
15
16 public class PresetModel extends AbstractListModel implements ComboBoxModel, ComponentChangeListener {
17         
18         private static final LogHelper log = Application.getLogger();
19         private static final Translator trans = Application.getTranslator();
20         
21         private static final String SELECT_PRESET = trans.get("lbl.select");
22         private static final String SELECT_DATABASE = trans.get("lbl.database");
23         
24         
25         private final RocketComponent component;
26         private ComponentPreset previousPreset;
27         
28         private final List<ComponentPreset> presets;
29         
30         public PresetModel(RocketComponent component) {
31                 // FIXME:  Make generic for any component type
32                 // FIXME:  This should load only the user's favorites, NOT all presets
33                 presets = Application.getDaos().getBodyTubePresetDao().listAll();
34                 this.component = component;
35                 previousPreset = component.getPresetComponent();
36                 component.addComponentChangeListener(this);
37         }
38         
39         @Override
40         public int getSize() {
41                 return presets.size() + 2;
42         }
43         
44         @Override
45         public Object getElementAt(int index) {
46                 if (index == 0) {
47                         return SELECT_PRESET;
48                 }
49                 if (index == getSize() - 1) {
50                         return SELECT_DATABASE;
51                 }
52                 return presets.get(index - 1);
53         }
54         
55         @Override
56         public void setSelectedItem(Object item) {
57                 log.user("User selected preset item '" + item + "' for component " + component);
58                 
59                 if (item == null) {
60                         // FIXME:  What to do?
61                 } else if (item.equals(SELECT_PRESET)) {
62                         component.clearPreset();
63                 } else if (item.equals(SELECT_DATABASE)) {
64                         // FIXME:  Open database dialog
65                 } else {
66                         // FIXME: Add undo point here
67                         component.loadPreset((ComponentPreset) item);
68                 }
69         }
70         
71         @Override
72         public Object getSelectedItem() {
73                 ComponentPreset preset = component.getPresetComponent();
74                 if (preset == null) {
75                         return SELECT_PRESET;
76                 } else {
77                         return preset;
78                 }
79         }
80         
81         @Override
82         public void componentChanged(ComponentChangeEvent e) {
83                 if (previousPreset != component.getPresetComponent()) {
84                         previousPreset = component.getPresetComponent();
85                         fireContentsChanged(this, 0, getSize());
86                 }
87         }
88         
89         // FIXME:  Make model invalidatable
90         
91 }