[1] | 1 | package genius.gui.boaparties;
|
---|
| 2 |
|
---|
| 3 | import java.math.BigDecimal;
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.List;
|
---|
| 6 | import java.util.Set;
|
---|
| 7 |
|
---|
| 8 | import javax.swing.JTable;
|
---|
| 9 | import javax.swing.event.TableModelEvent;
|
---|
| 10 | import javax.swing.event.TableModelListener;
|
---|
| 11 | import javax.swing.table.TableModel;
|
---|
| 12 |
|
---|
| 13 | import genius.core.boaframework.BOAparameter;
|
---|
| 14 | import genius.core.repository.boa.ParameterList;
|
---|
| 15 | import genius.core.repository.boa.ParameterRepItem;
|
---|
| 16 |
|
---|
| 17 | /**
|
---|
| 18 | * Holds set of BOA parameters as set by the user. Ready for use in a
|
---|
| 19 | * {@link JTable}. This is mutable because it's a model.
|
---|
| 20 | */
|
---|
| 21 | public class BoaParametersModel implements TableModel {
|
---|
| 22 |
|
---|
| 23 | private ArrayList<BOAparameter> parameters;
|
---|
| 24 |
|
---|
| 25 | private static final String[] columns = { "Name", "Description", "Lower bound", "Step size", "Upper bound" };
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * listeners of the TableModel.
|
---|
| 29 | */
|
---|
| 30 | private List<TableModelListener> listeners = new ArrayList<>();
|
---|
| 31 |
|
---|
| 32 | public BoaParametersModel(Set<BOAparameter> params) {
|
---|
| 33 | parameters = new ArrayList<>(params);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | * Replace all the parameters in the model with a new set.
|
---|
| 38 | *
|
---|
| 39 | * @param parameters2
|
---|
| 40 | */
|
---|
| 41 | public void setParameters(Set<BOAparameter> parameters2) {
|
---|
| 42 | this.parameters = new ArrayList<>(parameters2);
|
---|
| 43 | notifyListeners(0);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | @Override
|
---|
| 47 | public int getRowCount() {
|
---|
| 48 | return parameters.size();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | @Override
|
---|
| 52 | public int getColumnCount() {
|
---|
| 53 | return columns.length;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | @Override
|
---|
| 57 | public String getColumnName(int columnIndex) {
|
---|
| 58 | return columns[columnIndex];
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | @Override
|
---|
| 62 | public Class<?> getColumnClass(int columnIndex) {
|
---|
| 63 | return columnIndex < 2 ? String.class : Double.class;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | @Override
|
---|
| 67 | public boolean isCellEditable(int rowIndex, int columnIndex) {
|
---|
| 68 | return columnIndex > 0;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | @Override
|
---|
| 72 | public Object getValueAt(int rowIndex, int columnIndex) {
|
---|
| 73 | BOAparameter param = parameters.get(rowIndex);
|
---|
| 74 | switch (columnIndex) {
|
---|
| 75 | case 0:
|
---|
| 76 | return param.getName();
|
---|
| 77 | case 1:
|
---|
| 78 | return param.getDescription();
|
---|
| 79 | case 2:
|
---|
| 80 | return param.getLow();
|
---|
| 81 | case 3:
|
---|
| 82 | return param.getStep();
|
---|
| 83 | case 4:
|
---|
| 84 | return param.getHigh();
|
---|
| 85 | }
|
---|
| 86 | return "???";
|
---|
| 87 |
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | @Override
|
---|
| 91 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
|
---|
| 92 | // System.out.println("User changed cell (" + rowIndex + "," +
|
---|
| 93 | // columnIndex + ") to " + aValue);
|
---|
| 94 | BOAparameter param = parameters.get(rowIndex);
|
---|
| 95 | switch (columnIndex) {
|
---|
| 96 | case 1:
|
---|
| 97 | param = param.withDescription((String) aValue);
|
---|
| 98 | break;
|
---|
| 99 | case 2:
|
---|
| 100 | param = param.withLow((Double) aValue);
|
---|
| 101 | break;
|
---|
| 102 | case 3:
|
---|
| 103 | param = param.withStep((Double) aValue);
|
---|
| 104 | break;
|
---|
| 105 | case 4:
|
---|
| 106 | param = param.withHigh((Double) aValue);
|
---|
| 107 | break;
|
---|
| 108 | }
|
---|
| 109 | setParameter(rowIndex, param);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | private void setParameter(int index, BOAparameter param) {
|
---|
| 113 | System.out.println("new param for row" + index + ":" + param);
|
---|
| 114 | parameters.set(index, param);
|
---|
| 115 | notifyListeners(index);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
| 119 | * Notify listeners of changed row
|
---|
| 120 | *
|
---|
| 121 | * @param row
|
---|
| 122 | */
|
---|
| 123 | private void notifyListeners(int row) {
|
---|
| 124 | for (TableModelListener listener : listeners) {
|
---|
| 125 | listener.tableChanged(new TableModelEvent(this, row));
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | @Override
|
---|
| 130 | public void addTableModelListener(TableModelListener l) {
|
---|
| 131 | listeners.add(l);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | @Override
|
---|
| 135 | public void removeTableModelListener(TableModelListener l) {
|
---|
| 136 | listeners.remove(l);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /**
|
---|
| 140 | * @return current settings of this model. A whole list of
|
---|
| 141 | * {@link ParameterList}s is returned, one for each configuration
|
---|
| 142 | * asked by the user.
|
---|
| 143 | */
|
---|
| 144 | public List<ParameterList> getSettings() {
|
---|
| 145 | return getSettings(parameters);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * @param boaparams
|
---|
| 150 | * Set of {@link BOAparameter}s with set of ranges for each
|
---|
| 151 | * param.
|
---|
| 152 | * @return full product of all possible parameter combinations. If there are
|
---|
| 153 | * no parameters at all, return a list with a single empty
|
---|
| 154 | * ParameterList.
|
---|
| 155 | */
|
---|
| 156 | protected static List<ParameterList> getSettings(List<BOAparameter> boaparams) {
|
---|
| 157 | if (boaparams.isEmpty()) {
|
---|
| 158 | ArrayList<ParameterList> list = new ArrayList<ParameterList>();
|
---|
| 159 | list.add(new ParameterList());
|
---|
| 160 | return list;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | List<ParameterList> allParams = new ArrayList<>();
|
---|
| 164 | BOAparameter boaparam1 = boaparams.get(0);
|
---|
| 165 | List<ParameterList> allSubSettings = getSettings(boaparams.subList(1, boaparams.size()));
|
---|
| 166 |
|
---|
| 167 | BigDecimal endValue = BigDecimal.valueOf(boaparam1.getHigh());
|
---|
| 168 | BigDecimal step = BigDecimal.valueOf(boaparam1.getStep());
|
---|
| 169 | for (BigDecimal value = BigDecimal.valueOf(boaparam1.getLow()); value.compareTo(endValue) != 1; value = value
|
---|
| 170 | .add(step)) {
|
---|
| 171 | ParameterRepItem newParam = new ParameterRepItem(boaparam1.getName(), value.doubleValue());
|
---|
| 172 | for (ParameterList subSetting : allSubSettings) {
|
---|
| 173 | allParams.add(subSetting.include(newParam));
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | return allParams;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | @Override
|
---|
| 181 | public String toString() {
|
---|
| 182 | return parameters.toString();
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /**
|
---|
| 186 | * @return current setting as set of {@link BOAparameter}s
|
---|
| 187 | */
|
---|
| 188 | public ArrayList<BOAparameter> getSetting() {
|
---|
| 189 | return parameters;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | }
|
---|