Debian Codegraph @

src/dr/app/bss/DemographicModelEditor.java

raw symbols SCIP
1 /*
2 * DemographicModelEditor.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.SwingConstants;
45 import javax.swing.SwingUtilities;
46
47 import dr.app.gui.components.RealNumberField;
48
49 /**
50 * @author Filip Bielejec
51 * @version $Id$
52 */
53 public class DemographicModelEditor {
54
55 // Data
56 private PartitionDataList dataList = null;
57 private int row;
58
59 // Settings
60 private OptionsPanel optionPanel;
61 private JComboBox demographicCombo;
62 private RealNumberField[] demographicParameterFields;
63
64 //Buttons
65 private JButton done;
66 private JButton cancel;
67
68 // Window
69 private JDialog window;
70 private Frame owner;
71
72 public DemographicModelEditor(PartitionDataList dataList, int row) {
73
74 this.dataList = dataList;
75 this.row = row;
76
77 demographicParameterFields = new RealNumberField[PartitionData.demographicParameterNames.length];
78 window = new JDialog(owner, "Setup tree model for partition " + (row + 1));
79 optionPanel = new OptionsPanel(12, 12, SwingConstants.CENTER);
80
81 demographicCombo = new JComboBox();
82 demographicCombo.setOpaque(false);
83
84 for (String demographicModel : PartitionData.demographicModels) {
85 demographicCombo.addItem(demographicModel);
86 }// END: fill loop
87
88 demographicCombo.addItemListener(new ListenDemographicCombo());
89
90 for (int i = 0; i < PartitionData.demographicParameterNames.length; i++) {
91 demographicParameterFields[i] = new RealNumberField(0.0, Double.MAX_VALUE);
92 demographicParameterFields[i].setColumns(8);
93 demographicParameterFields[i].setValue(this.dataList.get(row).demographicParameterValues[i]);
94 }// END: fill loop
95
96 setDemographicArguments();
97
98 // Buttons
99 JPanel buttonsHolder = new JPanel();
100 buttonsHolder.setOpaque(false);
101
102 cancel = new JButton("Cancel", Utils.createImageIcon(Utils.CLOSE_ICON));
103 cancel.addActionListener(new ListenCancel());
104 buttonsHolder.add(cancel);
105
106 done = new JButton("Done", Utils.createImageIcon(Utils.CHECK_ICON));
107 done.addActionListener(new ListenOk());
108 buttonsHolder.add(done);
109
110 // Window
111 owner = Utils.getActiveFrame();
112 window.setLocationRelativeTo(owner);
113 window.getContentPane().setLayout(new BorderLayout());
114 window.getContentPane().add(optionPanel, BorderLayout.CENTER);
115 window.getContentPane().add(buttonsHolder, BorderLayout.SOUTH);
116 window.pack();
117
118 //return to the previously chosen index on start
119 demographicCombo.setSelectedIndex(dataList.get(row).demographicModelIndex);
120
121 }//END: Constructor
122
123 private void setDemographicArguments() {
124
125 optionPanel.removeAll();
126 optionPanel.addComponents(new JLabel("Tree model:"), demographicCombo);
127 optionPanel.addSeparator();
128 optionPanel.addLabel("Set parameter values:");
129
130 int index = demographicCombo.getSelectedIndex();
131
132 for (int i = 0; i < PartitionData.demographicParameterIndices[index].length; i++) {
133
134 int k = PartitionData.demographicParameterIndices[index][i];
135
136 JPanel panel = new JPanel(new BorderLayout(6, 6));
137 panel.add(demographicParameterFields[k], BorderLayout.WEST);
138 panel.setOpaque(false);
139 optionPanel.addComponentWithLabel(PartitionData.demographicParameterNames[k] + ":", panel);
140
141 }// END: indices loop
142
143 window.validate();
144 window.repaint();
145 }// END: setDemographicArguments
146
147 private class ListenDemographicCombo implements ItemListener {
148 public void itemStateChanged(ItemEvent ie) {
149
150 setDemographicArguments();
151
152 }// END: actionPerformed
153 }// END: ListenDemographicCombo
154
155 public void collectSettings() {
156
157 dataList.get(row).demographicModelIndex = demographicCombo.getSelectedIndex();
158 for (int i = 0; i < PartitionData.demographicParameterNames.length; i++) {
159
160 dataList.get(row).demographicParameterValues[i] = demographicParameterFields[i].getValue();
161
162 }// END: fill loop
163 }// END: collectSettings
164
165 private class ListenOk implements ActionListener {
166 public void actionPerformed(ActionEvent ev) {
167
168 window.setVisible(false);
169 collectSettings();
170
171 }// END: actionPerformed
172 }// END: ListenOk
173
174 private class ListenCancel implements ActionListener {
175 public void actionPerformed(ActionEvent ev) {
176
177 window.setVisible(false);
178
179 }// END: actionPerformed
180 }// END: ListenCancel
181
182 public void showWindow() {
183 window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
184 window.setSize(new Dimension(450, 400));
185 window.setMinimumSize(new Dimension(100, 100));
186 window.setResizable(true);
187 window.setModal(true);
188 window.setVisible(true);
189 }// END: showWindow
190
191 public void launch() {
192
193 if (SwingUtilities.isEventDispatchThread()) {
194 showWindow();
195 } else {
196 SwingUtilities.invokeLater(new Runnable() {
197 public void run() {
198 showWindow();
199 }
200 });
201 }// END: edt check
202
203 }// END: launch
204
205 }// END: class