Debian Codegraph @

src/dr/app/bss/BranchSubstitutionModelEditor.java

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