Debian Codegraph @

src/dr/app/bss/SiteRateModelEditor.java

raw symbols SCIP
1 /*
2 * SiteRateModelEditor.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.bss;
27
28 import jam.panels.OptionsPanel;
29
30 import java.awt.BorderLayout;
31 import java.awt.Dimension;
32 import java.awt.Frame;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.ItemEvent;
36 import java.awt.event.ItemListener;
37
38 import javax.swing.JButton;
39 import javax.swing.JComboBox;
40 import javax.swing.JDialog;
41 import javax.swing.JFrame;
42 import javax.swing.JLabel;
43 import javax.swing.JPanel;
44 import javax.swing.JSpinner;
45 import javax.swing.SpinnerModel;
46 import javax.swing.SpinnerNumberModel;
47 import javax.swing.SwingConstants;
48 import javax.swing.SwingUtilities;
49 import javax.swing.text.BadLocationException;
50
51 import dr.app.gui.components.RealNumberField;
52
53 /**
54 * @author Filip Bielejec
55 * @version $Id$
56 */
57 public class SiteRateModelEditor {
58
59 // Data
60 private PartitionDataList dataList = null;
61 private int row;
62
63 // Settings
64 private OptionsPanel optionPanel;
65 private JComboBox siteCombo;
66 private RealNumberField[] siteParameterFields;
67 private JSpinner gammaCategoriesSpinner;
68
69 //Buttons
70 private JButton done;
71 private JButton cancel;
72
73 // Window
74 private JDialog window;
75 private Frame owner;
76
77 public SiteRateModelEditor(PartitionDataList dataList, int row) throws NumberFormatException, BadLocationException {
78
79 this.dataList = dataList;
80 this.row = row;
81
82 siteParameterFields = new RealNumberField[PartitionData.siteRateModelParameterNames.length];
83 window = new JDialog(owner, "Setup site rate model for partition " + (row + 1));
84 optionPanel = new OptionsPanel(12, 12, SwingConstants.CENTER);
85
86 siteCombo = new JComboBox();
87 siteCombo.setOpaque(false);
88
89 for (String siteModel : PartitionData.siteRateModels) {
90 siteCombo.addItem(siteModel);
91 }// END: fill loop
92
93 siteCombo.addItemListener(new ListenSiteCombo());
94
95 for (int i = 0; i < PartitionData.siteRateModelParameterNames.length; i++) {
96
97 switch (i) {
98
99 case 0: // GammaCategories
100 siteParameterFields[i] = new RealNumberField(1.0, Double.valueOf(Integer.MAX_VALUE));
101 break;
102
103 case 1: // Alpha
104 siteParameterFields[i] = new RealNumberField(0.0, Double.MAX_VALUE);
105 break;
106
107 case 2: // Invariant sites proportion
108 siteParameterFields[i] = new RealNumberField(0.0, 1.0);
109 break;
110
111 default:
112 siteParameterFields[i] = new RealNumberField();
113
114 }//END: parameter switch
115
116 siteParameterFields[i].setColumns(8);
117 siteParameterFields[i].setValue(dataList.get(0).siteRateModelParameterValues[i]);
118 }// END: fill loop
119
120 setSiteArguments();
121
122 // Buttons
123 JPanel buttonsHolder = new JPanel();
124 buttonsHolder.setOpaque(false);
125
126 cancel = new JButton("Cancel", Utils.createImageIcon(Utils.CLOSE_ICON));
127 cancel.addActionListener(new ListenCancel());
128 buttonsHolder.add(cancel);
129
130 done = new JButton("Done", Utils.createImageIcon(Utils.CHECK_ICON));
131 done.addActionListener(new ListenOk());
132 buttonsHolder.add(done);
133
134 // Window
135 owner = Utils.getActiveFrame();
136 window.setLocationRelativeTo(owner);
137 window.getContentPane().setLayout(new BorderLayout());
138 window.getContentPane().add(optionPanel, BorderLayout.CENTER);
139 window.getContentPane().add(buttonsHolder, BorderLayout.SOUTH);
140 window.pack();
141
142 //return to the previously chosen index on start
143 siteCombo.setSelectedIndex(dataList.get(row).siteRateModelIndex);
144
145 }// END: Constructor
146
147 private void setSiteArguments() throws NumberFormatException, BadLocationException {
148
149 optionPanel.removeAll();
150 optionPanel.addComponents(new JLabel("Site Rate model:"), siteCombo);
151 optionPanel.addSeparator();
152 optionPanel.addLabel("Set parameter values:");
153
154 int index = siteCombo.getSelectedIndex();
155
156 for (int i = 0; i < PartitionData.siteRateModelParameterIndices[index].length; i++) {
157
158 if(index == 1 && i == 0) {
159
160 int k = PartitionData.siteRateModelParameterIndices[index][i];
161
162 Integer initValue = Integer.valueOf(siteParameterFields[k].getText(0, 1));
163 Integer min = 1;
164 Integer max = 10;//Integer.MAX_VALUE;
165 Integer step = 1;
166
167 SpinnerModel model = new SpinnerNumberModel(initValue, min, max, step);
168 gammaCategoriesSpinner = new JSpinner(model);
169
170 JPanel panel = new JPanel(new BorderLayout(6, 6));
171 panel.add(gammaCategoriesSpinner, BorderLayout.WEST);
172 panel.setOpaque(false);
173 optionPanel.addComponentWithLabel(
174 PartitionData.siteRateModelParameterNames[k] + ":",
175 panel);
176
177 } else {
178
179 int k = PartitionData.siteRateModelParameterIndices[index][i];
180
181 JPanel panel = new JPanel(new BorderLayout(6, 6));
182 panel.add(siteParameterFields[k], BorderLayout.WEST);
183 panel.setOpaque(false);
184 optionPanel.addComponentWithLabel(
185 PartitionData.siteRateModelParameterNames[k] + ":",
186 panel);
187
188 }// END: gama categories field check
189
190 }// END: indices loop
191
192 window.validate();
193 window.repaint();
194 }// END: setSiteArguments
195
196 private class ListenSiteCombo implements ItemListener {
197 public void itemStateChanged(ItemEvent ie) {
198
199 try {
200
201 setSiteArguments();
202
203 } catch (NumberFormatException e) {
204 e.printStackTrace();
205 } catch (BadLocationException e) {
206 e.printStackTrace();
207 }
208
209 }// END: actionPerformed
210 }// END: ListenSiteCombo
211
212 public void collectSettings() {
213
214 int index = siteCombo.getSelectedIndex();
215 dataList.get(row).siteRateModelIndex = index;
216
217 for (int i = 0; i < PartitionData.siteRateModelParameterNames.length; i++) {
218
219 if(index == 1 && i == 0) {
220
221 dataList.get(row).siteRateModelParameterValues[i] = Double.valueOf(gammaCategoriesSpinner.getValue().toString());
222
223 } else {
224
225 dataList.get(0).siteRateModelParameterValues[i] = siteParameterFields[i].getValue();
226
227 }// END: gama categories field check
228
229 }// END: fill loop
230 }// END: collectSettings
231
232 private class ListenOk implements ActionListener {
233 public void actionPerformed(ActionEvent ev) {
234
235 window.setVisible(false);
236 collectSettings();
237
238 }// END: actionPerformed
239 }// END: ListenSaveLocationCoordinates
240
241 private class ListenCancel implements ActionListener {
242 public void actionPerformed(ActionEvent ev) {
243
244 window.setVisible(false);
245
246 }// END: actionPerformed
247 }// END: ListenCancel
248
249 public void showWindow() {
250 window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
251 window.setSize(new Dimension(450, 400));
252 window.setMinimumSize(new Dimension(100, 100));
253 window.setResizable(true);
254 window.setModal(true);
255 window.setVisible(true);
256 }// END: showWindow
257
258 public void launch() {
259
260 if (SwingUtilities.isEventDispatchThread()) {
261 showWindow();
262 } else {
263 SwingUtilities.invokeLater(new Runnable() {
264 public void run() {
265 showWindow();
266 }
267 });
268 }// END: edt check
269
270 }// END: launch
271
272 }// END: class