Debian Codegraph @

src/dr/app/bss/FrequencyModelEditor.java

raw symbols SCIP
1 /*
2 * FrequencyModelEditor.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.JDialog;
40 import javax.swing.JFrame;
41 import javax.swing.JLabel;
42 import javax.swing.JPanel;
43 import javax.swing.JScrollPane;
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 FrequencyModelEditor {
54
55 // Data
56 private PartitionDataList dataList = null;
57 private int row;
58
59 // Settings
60 private OptionsPanel optionPanel;
61 private JScrollPane scrollPane;
62 private DisabledItemsComboBox frequencyCombo;
63 private RealNumberField[] frequencyParameterFields;
64
65 //Buttons
66 private JButton done;
67 private JButton cancel;
68
69 // Window
70 private JDialog window;
71 private Frame owner;
72
73 public FrequencyModelEditor(PartitionDataList dataList, int row) {
74
75 this.dataList = dataList;
76 this.row = row;
77
78 frequencyParameterFields = new RealNumberField[PartitionData.frequencyParameterNames.length];
79 window = new JDialog(owner, "Setup base frequencies for partition " + (row + 1));
80 optionPanel = new OptionsPanel(12, 12, SwingConstants.CENTER);
81
82 scrollPane = new JScrollPane();
83 optionPanel.setOpaque(false);
84 scrollPane = new JScrollPane(optionPanel,
85 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
86 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
87 scrollPane.getViewport().setOpaque(false);
88
89 frequencyCombo = new DisabledItemsComboBox();
90
91 int indexOf = 0;
92 for (String frequencyModel : PartitionData.frequencyModels) {
93
94 if (PartitionData.frequencyCompatibleDataTypes[indexOf] == dataList.get(row).dataTypeIndex) {
95
96 frequencyCombo.addItem(frequencyModel, false);
97
98 } else {
99
100 frequencyCombo.addItem(frequencyModel, true);
101
102 }// END: compatible check
103
104 indexOf++;
105 }// END: fill loop
106
107 frequencyCombo.addItemListener(new ListenFrequencyCombo());
108
109 for (int i = 0; i < PartitionData.frequencyParameterNames.length; i++) {
110 frequencyParameterFields[i] = new RealNumberField(0.0, 1.0);
111 frequencyParameterFields[i].setColumns(8);
112 frequencyParameterFields[i].setValue(dataList.get(0).frequencyParameterValues[i]);
113 }// END: fill loop
114
115 setFrequencyArguments();
116
117 // Buttons
118 JPanel buttonsHolder = new JPanel();
119 buttonsHolder.setOpaque(false);
120
121 cancel = new JButton("Cancel", Utils.createImageIcon(Utils.CLOSE_ICON));
122 cancel.addActionListener(new ListenCancel());
123 buttonsHolder.add(cancel);
124
125 done = new JButton("Done", Utils.createImageIcon(Utils.CHECK_ICON));
126 done.addActionListener(new ListenOk());
127 buttonsHolder.add(done);
128
129 // Window
130 owner = Utils.getActiveFrame();
131 window.setLocationRelativeTo(owner);
132 window.getContentPane().setLayout(new BorderLayout());
133 window.getContentPane().add(scrollPane, BorderLayout.CENTER);
134 window.getContentPane().add(buttonsHolder, BorderLayout.SOUTH);
135 window.pack();
136
137 //return to the previously chosen index on start
138 frequencyCombo.setSelectedIndex(dataList.get(row).frequencyModelIndex);
139
140 }//END: Constructor
141
142 private void setFrequencyArguments() {
143
144 optionPanel.removeAll();
145 optionPanel.addComponents(new JLabel("Base frequencies:"), frequencyCombo);
146
147 optionPanel.addSeparator();
148 optionPanel.addComponentWithLabel("Set parameter values:", new JLabel());
149
150 int index = frequencyCombo.getSelectedIndex();
151
152 for (int i = 0; i < dataList.get(row).frequencyParameterIndices[index].length; i++) {
153
154 int k = dataList.get(row).frequencyParameterIndices[index][i];
155
156 JPanel panel = new JPanel(new BorderLayout(6, 6));
157 panel.add(frequencyParameterFields[k], BorderLayout.WEST);
158 panel.setOpaque(false);
159 optionPanel.addComponentWithLabel(
160 PartitionData.frequencyParameterNames[k] + ":",
161 panel);
162
163 }// END: indices loop
164
165 window.validate();
166 window.repaint();
167 }// END: setFrequencyArguments
168
169 private class ListenFrequencyCombo implements ItemListener {
170 public void itemStateChanged(ItemEvent ie) {
171
172 setFrequencyArguments();
173
174 }// END: actionPerformed
175 }// END: ListenFrequencyCombo
176
177 public void collectSettings() {
178
179 dataList.get(row).frequencyModelIndex = frequencyCombo.getSelectedIndex();
180 for (int i = 0; i < PartitionData.frequencyParameterNames.length; i++) {
181
182 dataList.get(row).frequencyParameterValues[i] = frequencyParameterFields[i].getValue();
183
184 }// END: fill loop
185 }// END: collectSettings
186
187 //TODO: maybe check whether sum to 1?
188 private class ListenOk implements ActionListener {
189 public void actionPerformed(ActionEvent ev) {
190
191 window.setVisible(false);
192 collectSettings();
193
194 }// END: actionPerformed
195 }// END: ListenSaveLocationCoordinates
196
197 private class ListenCancel implements ActionListener {
198 public void actionPerformed(ActionEvent ev) {
199
200 window.setVisible(false);
201
202 }// END: actionPerformed
203 }// END: ListenCancel
204
205 public void showWindow() {
206 window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
207 window.setSize(new Dimension(450, 400));
208 window.setMinimumSize(new Dimension(100, 100));
209 window.setResizable(true);
210 window.setModal(true);
211 window.setVisible(true);
212 }// END: showWindow
213
214 public void launch() {
215
216 if (SwingUtilities.isEventDispatchThread()) {
217 showWindow();
218 } else {
219 SwingUtilities.invokeLater(new Runnable() {
220 public void run() {
221 showWindow();
222 }
223 });
224 }// END: edt check
225
226 }// END: launch
227
228 }//END: class