source: src/main/java/genius/gui/boaparties/BoaComponentModel.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 5.4 KB
RevLine 
[1]1package genius.gui.boaparties;
2
3import java.util.ArrayList;
4import java.util.HashSet;
5import java.util.List;
6import java.util.Set;
7
8import javax.swing.event.ListDataEvent;
9import javax.swing.event.ListDataListener;
10
11import genius.core.boaframework.BOA;
12import genius.core.boaframework.BOAparameter;
13import genius.core.boaframework.BoaType;
14import genius.core.exceptions.InstantiateException;
15import genius.core.repository.RepositoryFactory;
16import genius.core.repository.boa.BoaRepItem;
17import genius.core.repository.boa.BoaRepItemList;
18import genius.core.repository.boa.BoaRepository;
19import genius.core.repository.boa.BoaWithSettingsRepItem;
20import genius.core.repository.boa.ParameterList;
21import genius.core.repository.boa.ParameterRepItem;
22import genius.gui.panels.SingleSelectionModel;
23
24/**
25 * Contains user settings to create a {@link BoaWithSettingsRepItem}. The user
26 * settings differ from the {@link BoaWithSettingsRepItem} because the user can
27 * enter ranges for all parameters where the {@link BoaWithSettingsRepItem}
28 * contains just 1 setting per parameter.
29 *
30 * @param <T>
31 * the type of {@link BOA} component that this model is manipulating,
32 * and that comes out of the {@link BoaWithSettingsRepItem} that is
33 * delivered.
34 */
35public class BoaComponentModel<T extends BOA> {
36
37 /**
38 * List of selectable components of the given type.
39 */
40 private SingleSelectionModel<BoaRepItem<T>> componentsListModel;
41
42 private BoaParametersModel parametersModel;
43
44 /**
45 * Creates a model from given existing settings.
46 *
47 * @param existingItem
48 * the existing settings. Type must match T.
49 * @throws InstantiateException
50 * if problem with repo
51 */
52 public BoaComponentModel(final BoaWithSettingsRepItem<T> existingItem) {
53 BoaType type = existingItem.getBoa().getType();
54 loadComponents(type);
55 componentsListModel.setSelectedItem(existingItem.getBoa());
56
57 Set<BOAparameter> boaparams = new HashSet<BOAparameter>();
58 for (ParameterRepItem e : existingItem.getParameters()) {
59 String descr = getDescription(componentsListModel.getSelection(), e.getName());
60 boaparams.add(new BOAparameter(e.getName(), e.getValue(), descr));
61 }
62 parametersModel = new BoaParametersModel(boaparams);
63 connect();
64
65 }
66
67 /**
68 * The description is not saved in the RepItem. We find back the original
69 * spec and get desc from there.
70 *
71 * @param boaRepItem
72 * a {@link BoaRepItem} that has a parameter with given name
73 * @param name
74 * a parameter name
75 * @return the original description of the parameter with the given name.
76 */
77 private String getDescription(BoaRepItem<T> boaRepItem, String name) {
78 try {
79 Set<BOAparameter> params = boaRepItem.getInstance().getParameterSpec();
80 for (BOAparameter param : params) {
81 if (param.getName().equals(name)) {
82 return param.getDescription();
83 }
84 }
85 throw new IllegalArgumentException("unknown parameter " + name);
86 } catch (Exception e) {
87 return "ERR" + e.getMessage();
88 }
89 }
90
91 /**
92 * Construct model with default settings for given type.
93 *
94 * @param type
95 * the type, must match T.
96 */
97 public BoaComponentModel(BoaType type) {
98 loadComponents(type);
99 // select first, so that we always have a proper selection (for
100 // #refreshParams)
101 componentsListModel.setSelectedItem(componentsListModel.getAllItems().get(0));
102 parametersModel = new BoaParametersModel(new HashSet<BOAparameter>());
103 resetParams(); // load the default;
104 connect();
105 }
106
107 /**
108 * Connects listener to ensure {@link #resetParams()} is called when
109 * something changes. Also calls {@link #resetParams()} a first time.
110 */
111 private void connect() {
112
113 componentsListModel.addListDataListener(new ListDataListener() {
114 @Override
115 public void intervalRemoved(ListDataEvent e) {
116 }
117
118 @Override
119 public void intervalAdded(ListDataEvent e) {
120 }
121
122 @Override
123 public void contentsChanged(ListDataEvent e) {
124 resetParams();
125 }
126 });
127
128 }
129
130 /**
131 * load all default parameter settings. ASSUMES current selection is valid.
132 */
133 private void resetParams() {
134 try {
135 parametersModel.setParameters(componentsListModel.getSelection().getInstance().getParameterSpec());
136 } catch (InstantiateException e1) {
137 e1.printStackTrace();
138 }
139 }
140
141 /**
142 * Load all available components for given type
143 *
144 * @param type
145 * the {@link BoaType} that this model is dealing with. Should
146 * match T.
147 */
148 private void loadComponents(BoaType type) {
149 if (type == BoaType.UNKNOWN || type == null) {
150 throw new IllegalArgumentException("unsupported type=" + type);
151 }
152 BoaRepItemList<BoaRepItem<T>> possibleComponents = getBoaRepo().getBoaComponents(type);
153 this.componentsListModel = new SingleSelectionModel<BoaRepItem<T>>(possibleComponents);
154 }
155
156 /**
157 * Factory method, for testing.
158 *
159 * @return boa repository
160 */
161 protected BoaRepository getBoaRepo() {
162 return RepositoryFactory.getBoaRepository();
163 }
164
165 /**
166 * all available settings.
167 */
168 public List<BoaWithSettingsRepItem<T>> getValues() {
169 List<BoaWithSettingsRepItem<T>> list = new ArrayList<>();
170 for (ParameterList setting : parametersModel.getSettings()) {
171 list.add(new BoaWithSettingsRepItem<T>(componentsListModel.getSelection(), setting));
172 }
173 return list;
174 }
175
176 public SingleSelectionModel<BoaRepItem<T>> getComponentsListModel() {
177 return componentsListModel;
178 }
179
180 /**
181 * @return the {@link BoaParametersModel}.
182 */
183 public BoaParametersModel getParameters() {
184 return parametersModel;
185 }
186
187}
Note: See TracBrowser for help on using the repository browser.