Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * CreateTraitDialog.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.BeautiFrame;
29 import dr.app.beauti.options.TraitData;
30 import dr.app.beauti.util.TextUtil;
31 import jam.panels.OptionsPanel;
32
33 import javax.swing.*;
34 import javax.swing.border.EmptyBorder;
35 import java.awt.*;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.ItemEvent;
39 import java.awt.event.ItemListener;
40
41 /**
42 * @author Andrew Rambaut
43 * @author Walter Xie
44 * @version $Id: PriorDialog.java,v 1.4 2006/09/05 13:29:34 rambaut Exp $
45 */
46 public class CreateTraitDialog {
47 public static final String EXAMPLE_FORMAT = "<html>A trait file is tab delimited. " +
48 "The first row is always <font color=red>traits</font> followed by the name of the trait " +
49 "(e.g. <font color=red>size</font>) in the second column. The rest rows are mapping taxa to trait values, with " +
50 "taxon name in the first column and trait values in the second column. For example: <br>" +
51 "traits\tsize<br>" +
52 "taxon1\tsmall<br>" +
53 "taxon2\tmedium<br>" +
54 "taxon3\tbig<br>" +
55 "... ...<br>.</html>";
56
57
58 private final BeautiFrame frame;
59
60 private JTextField nameField;
61 private JComboBox typeCombo;
62
63 private final JRadioButton createRadio = new JRadioButton("Create a new trait", true);
64 private final JRadioButton importRadio = new JRadioButton("Import trait(s) from a mapping file", false);
65 private final JButton exampleButton = new JButton("Show example of mapping file format");
66 private final JCheckBox createTraitPartitionCheck = new JCheckBox("Create a corresponding data partition", true);
67
68 private String message = null;
69
70 public static final int OK_IMPORT = 10;
71
72 OptionsPanel optionPanel;
73
74 public CreateTraitDialog(final BeautiFrame frame) {
75 this.frame = frame;
76
77 nameField = new JTextField("untitled_trait");
78 nameField.setColumns(20);
79
80 // nameCombo = new JComboBox(TraitData.Traits.values());
81 typeCombo = new JComboBox(TraitData.TraitType.values());
82
83 optionPanel = new OptionsPanel(12, 12);
84
85 ButtonGroup group = new ButtonGroup();
86 group.add(createRadio);
87 group.add(importRadio);
88 ItemListener listener = new ItemListener() {
89 public void itemStateChanged(ItemEvent e) {
90 nameField.setEnabled(createRadio.isSelected());
91 typeCombo.setEnabled(createRadio.isSelected());
92 exampleButton.setEnabled(importRadio.isSelected());
93 }
94 };
95 createRadio.addItemListener(listener);
96 importRadio.addItemListener(listener);
97
98 exampleButton.setEnabled(false);
99 exampleButton.addActionListener(new ActionListener() {
100 public void actionPerformed(ActionEvent ae) {
101 JScrollPane scrollPane = TextUtil.createHTMLScrollPane(EXAMPLE_FORMAT, new Dimension(400,300));
102
103 JOptionPane.showMessageDialog(frame, scrollPane,
104 "Example of mapping file format",
105 JOptionPane.PLAIN_MESSAGE);
106 }
107 });
108 }
109
110 public void setTraitName(String traitName) {
111 nameField.setText(traitName);
112 }
113
114 public void setMessage(String message) {
115 this.message = message;
116 }
117
118 public int showDialog() {
119
120 setupPanel();
121
122 JOptionPane optionPane = new JOptionPane(optionPanel,
123 JOptionPane.QUESTION_MESSAGE,
124 JOptionPane.OK_CANCEL_OPTION,
125 null,
126 null,
127 null);
128 optionPane.setBorder(new EmptyBorder(12, 12, 12, 12));
129
130 boolean done;
131 int result = JOptionPane.CANCEL_OPTION;
132
133 do {
134 final JDialog dialog = optionPane.createDialog(frame, "Create or Import Trait(s)");
135 dialog.pack();
136
137 dialog.setVisible(true);
138
139 Integer value = (Integer) optionPane.getValue();
140 if (value != null && value != -1) {
141 result = value;
142 }
143 done = true;
144 if (result == JOptionPane.OK_OPTION && createRadio.isSelected()) {
145 done = frame.validateTraitName(getName());
146 }
147 } while (!done);
148
149 if (importRadio.isSelected() && result == JOptionPane.OK_OPTION) result = OK_IMPORT;
150
151 return result;
152 }
153
154 private void setupPanel() {
155 optionPanel.removeAll();
156
157 if (message != null && !message.isEmpty()) {
158 optionPanel.addSpanningComponent(new JLabel(message));
159 }
160
161 optionPanel.addComponent(createRadio);
162 JLabel label = optionPanel.addComponentWithLabel("Name:", nameField);
163
164 optionPanel.addComponent(importRadio);
165 optionPanel.addComponent(exampleButton);
166 exampleButton.putClientProperty("Quaqua.Button.style", "help");
167
168 optionPanel.addComponentWithLabel("Type:", typeCombo);
169 optionPanel.addComponent(createTraitPartitionCheck);
170
171 createTraitPartitionCheck.setSelected(true);
172 }
173
174 public String getName() {
175 return nameField.getText();
176 // return nameCombo.getSelectedItem().toString();
177 }
178
179 public TraitData.TraitType getType() {
180 return (TraitData.TraitType) typeCombo.getSelectedItem();
181 }
182
183 public boolean createTraitPartition() {
184 return createTraitPartitionCheck.isSelected();
185 }
186
187 }