Debian Codegraph @

src/dr/app/beauti/traitspanel/GuessTraitDialog.java

raw symbols SCIP
1 /*
2 * GuessTraitDialog.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.beauti.traitspanel;
27
28 import dr.app.beauti.options.TraitGuesser;
29 import jam.panels.OptionsPanel;
30
31 import javax.swing.*;
32 import javax.swing.border.EmptyBorder;
33 import java.awt.event.ItemEvent;
34 import java.awt.event.ItemListener;
35
36
37 /**
38 * @author Andrew Rambaut
39 * @author Alexei Drummond
40 * @author Walter Xie
41 * @version $Id: GuessTraitDialog.java,v 1.4 2009/05/25 13:29:34 rambaut Exp $
42 */
43 public class GuessTraitDialog {
44
45 private JFrame frame;
46 private final OptionsPanel optionPanel;
47
48 JLabel descriptionText = new JLabel();
49
50 private final JRadioButton orderRadio = new JRadioButton("Defined by its order", true);
51 private final JComboBox orderCombo = new JComboBox(new String[]{"first", "second", "third",
52 "fourth", "fourth from last", "third from last", "second from last", "last"});
53 private final JTextField delimiterText = new JTextField(6);
54
55 private final JRadioButton regexRadio = new JRadioButton("Defined by regular expression (REGEX)", false);
56 private final JTextField regexText = new JTextField(16);
57
58 public GuessTraitDialog(JFrame frame) {
59 this.frame = frame;
60
61 optionPanel = new OptionsPanel(12, 12);
62 optionPanel.addSpanningComponent(descriptionText);
63
64 optionPanel.addLabel("The trait value is given by a part of string in the taxon label that is:");
65
66 optionPanel.addComponents(orderRadio, orderCombo);
67 optionPanel.addComponentWithLabel("with delimiter ", delimiterText);
68 delimiterText.setEnabled(true);
69 optionPanel.addSeparator();
70
71 regexText.setEnabled(false);
72 optionPanel.addComponents(regexRadio, regexText);
73
74 ButtonGroup group = new ButtonGroup();
75 group.add(orderRadio);
76 group.add(regexRadio);
77 ItemListener listener = new ItemListener() {
78 public void itemStateChanged(ItemEvent e) {
79 delimiterText.setEditable(orderRadio.isSelected());
80 orderCombo.setEnabled(orderRadio.isSelected());
81 regexText.setEnabled(regexRadio.isSelected());
82 }
83 };
84 orderRadio.addItemListener(listener);
85 regexRadio.addItemListener(listener);
86 }
87
88 public void setDescription(String description) {
89 descriptionText.setText(description);
90 }
91
92 public int showDialog() {
93
94 JOptionPane optionPane = new JOptionPane(optionPanel,
95 JOptionPane.QUESTION_MESSAGE,
96 JOptionPane.OK_CANCEL_OPTION,
97 null,
98 null,
99 null);
100 optionPane.setBorder(new EmptyBorder(12, 12, 12, 12));
101
102 final JDialog dialog = optionPane.createDialog(frame, "Guess Trait Value for Taxa");
103 dialog.pack();
104
105 dialog.setVisible(true);
106
107 int result = JOptionPane.CANCEL_OPTION;
108 Integer value = (Integer) optionPane.getValue();
109 if (value != null && value != -1) {
110 result = value;
111 }
112
113 return result;
114 }
115
116 public void setupGuesserFromDialog(final TraitGuesser guesser) {
117 if (orderRadio.isSelected()) {
118 int order = orderCombo.getSelectedIndex();
119 if (order > 3) {
120 order = order - 8; // http://code.google.com/p/beast-mcmc/issues/detail?id=394
121 }
122 guesser.setGuessType(TraitGuesser.GuessType.DELIMITER);
123 guesser.setOrder(order);
124 guesser.setDelimiter(delimiterText.getText());
125 } else if (regexRadio.isSelected()) {
126 guesser.setGuessType(TraitGuesser.GuessType.REGEX);
127 guesser.setRegex(regexText.getText());
128 } else {
129 throw new IllegalArgumentException("unknown radio button selected");
130 }
131 }
132
133 // private void setTrait(String selectedTrait, TraitGuesser.TraitType selectedTraitType) {
134 // guesser.setTraitName(selectedTrait);
135 // if (selectedTrait.equalsIgnoreCase(TraitGuesser.Traits.TRAIT_SPECIES.toString())) {
136 // guesser.setTraitType(TraitGuesser.TraitType.DISCRETE);
137 // } else {
138 // guesser.setTraitType(selectedTraitType);
139 // }
140 // }
141 }