| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 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; |
| 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 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
} |