Debian Codegraph @

src/dr/app/coalgen/ModelPanel.java

raw symbols SCIP
1 /*
2 * ModelPanel.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.coalgen;
27
28 import dr.app.gui.components.RealNumberField;
29 import jam.framework.Exportable;
30 import jam.panels.OptionsPanel;
31
32 import javax.swing.*;
33 import java.awt.*;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.ItemEvent;
37 import java.awt.event.ItemListener;
38
39 /**
40 * @author Andrew Rambaut
41 * @author Alexei Drummond
42 * @version $Id$
43 */
44 public class ModelPanel extends JPanel implements Exportable {
45
46 private final CoalGenFrame frame;
47 private final CoalGenData data;
48
49 private JComboBox demographicCombo;
50
51 private OptionsPanel optionPanel;
52
53 private RealNumberField[] argumentFields = new RealNumberField[CoalGenData.argumentNames.length];
54 private JCheckBox[] argumentCheckBoxes = new JCheckBox[CoalGenData.argumentNames.length];
55 private JComboBox[] argumentCombos = new JComboBox[CoalGenData.argumentNames.length];
56
57 public ModelPanel(final CoalGenFrame frame, final CoalGenData data) {
58
59 super();
60
61 this.frame = frame;
62 this.data = data;
63
64 setOpaque(false);
65 setLayout(new BorderLayout());
66
67 optionPanel = new OptionsPanel(12, 12, SwingConstants.CENTER);
68 add(optionPanel, BorderLayout.NORTH);
69
70 demographicCombo = new JComboBox();
71 demographicCombo.setOpaque(false);
72
73 for (String demographicModel : CoalGenData.demographicModels) {
74 demographicCombo.addItem(demographicModel);
75 }
76
77 demographicCombo.addItemListener(new ItemListener() {
78 public void itemStateChanged(ItemEvent e) {
79 setDemographicArguments();
80 frame.fireModelChanged();
81 }
82 });
83
84 for (int i = 0; i < CoalGenData.argumentNames.length; i++) {
85 argumentFields[i] = new RealNumberField();
86 argumentFields[i].setColumns(8);
87 argumentFields[i].setValue(data.argumentValues[i]);
88 argumentCheckBoxes[i] = new JCheckBox("From Trace:");
89 argumentCheckBoxes[i].setOpaque(false);
90 argumentCombos[i] = new JComboBox();
91 argumentCombos[i].setEnabled(false);
92 argumentCombos[i].setOpaque(false);
93 argumentCheckBoxes[i].addActionListener(
94 new ArgumentActionListener(argumentCheckBoxes[i], argumentFields[i], argumentCombos[i]));
95 }
96
97 setDemographicArguments();
98 }
99
100 class ArgumentActionListener implements ActionListener {
101 JCheckBox checkBox;
102 RealNumberField field;
103 JComboBox combo;
104
105 ArgumentActionListener(JCheckBox checkBox, RealNumberField field, JComboBox combo) {
106 this.checkBox = checkBox;
107 this.field = field;
108 this.combo = combo;
109 }
110
111 public void actionPerformed(ActionEvent ae) {
112 field.setEnabled(!checkBox.isSelected());
113 combo.setEnabled(checkBox.isSelected());
114 }
115 }
116
117 private int findArgument(JComboBox comboBox, String argument) {
118 for (int i = 0; i < comboBox.getItemCount(); i++) {
119 String item = ((String) comboBox.getItemAt(i)).toLowerCase();
120 if (item.indexOf(argument) != -1) return i;
121 }
122 return -1;
123 }
124
125 private void setDemographicArguments() {
126 optionPanel.removeAll();
127
128 optionPanel.addComponents(new JLabel("Demographic Model:"), demographicCombo);
129
130 optionPanel.addSeparator();
131
132 optionPanel.addLabel("Select the parameter values (or obtain from a trace file):");
133
134 int demo = demographicCombo.getSelectedIndex();
135
136 for (int i = 0; i < data.argumentIndices[demo].length; i++) {
137 int k = data.argumentIndices[demo][i];
138
139 JPanel panel = new JPanel(new BorderLayout(6, 6));
140 panel.add(argumentFields[k], BorderLayout.WEST);
141 panel.add(argumentCheckBoxes[k], BorderLayout.CENTER);
142 panel.add(argumentCombos[k], BorderLayout.EAST);
143 panel.setOpaque(false);
144 optionPanel.addComponentWithLabel(CoalGenData.argumentNames[k] + ":",
145 panel);
146 }
147 validate();
148 repaint();
149 }
150
151 public final void tracesChanged() {
152 demographicCombo.setSelectedIndex(data.demographicModel);
153
154 if (data.traces != null) {
155 for (int i = 0; i < CoalGenData.argumentNames.length; i++) {
156 argumentCombos[i].removeAllItems();
157 for (int j = 0; j < data.traces.getTraceCount(); j++) {
158 String statistic = data.traces.getTraceName(j);
159 argumentCombos[i].addItem(statistic);
160 }
161
162 int index = data.argumentTraces[i];
163
164 for (int j = 0; j < CoalGenData.argumentGuesses[i].length; j++) {
165 if (index != -1) break;
166
167 index = findArgument(argumentCombos[i], CoalGenData.argumentGuesses[i][j]);
168 }
169 if (index == -1) {
170 argumentCheckBoxes[i].setEnabled(false);
171 index = 0;
172 } else {
173 argumentCheckBoxes[i].setEnabled(true);
174 }
175
176 argumentCombos[i].setSelectedIndex(index);
177 }
178 }
179 for (int i = 0; i < CoalGenData.argumentNames.length; i++) {
180 argumentFields[i].setValue(data.argumentValues[i]);
181 }
182 setDemographicArguments();
183 }
184
185 public final void collectSettings() {
186 data.demographicModel = demographicCombo.getSelectedIndex();
187 for (int i = 0; i < CoalGenData.argumentNames.length; i++) {
188 data.argumentValues[i] = argumentFields[i].getValue();
189 if (argumentCheckBoxes[i].isSelected()) {
190 data.argumentTraces[i] = argumentCombos[i].getSelectedIndex();
191 } else {
192 data.argumentTraces[i] = -1;
193 }
194 }
195 }
196
197 public JComponent getExportableComponent() {
198 return this;
199 }
200 }