| 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.datapanel; |
| 27 |
|
| 28 |
import dr.app.beauti.options.PartitionSubstitutionModel; |
| 29 |
import dr.app.beauti.options.TraitData; |
| 30 |
import jam.panels.OptionsPanel; |
| 31 |
|
| 32 |
import javax.swing.*; |
| 33 |
import javax.swing.border.EmptyBorder; |
| 34 |
import java.util.Collection; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class SelectTraitDialog { |
| 42 |
|
| 43 |
private JFrame frame; |
| 44 |
|
| 45 |
JComboBox traitCombo; |
| 46 |
JCheckBox copyCheck; |
| 47 |
JTextField nameField; |
| 48 |
|
| 49 |
OptionsPanel optionPanel; |
| 50 |
|
| 51 |
public SelectTraitDialog(JFrame frame) { |
| 52 |
this.frame = frame; |
| 53 |
|
| 54 |
traitCombo = new JComboBox(); |
| 55 |
|
| 56 |
copyCheck = new JCheckBox("Name trait partition:"); |
| 57 |
nameField = new JTextField(); |
| 58 |
nameField.setColumns(20); |
| 59 |
|
| 60 |
optionPanel = new OptionsPanel(12, 12); |
| 61 |
|
| 62 |
copyCheck.addItemListener( |
| 63 |
new java.awt.event.ItemListener() { |
| 64 |
public void itemStateChanged(java.awt.event.ItemEvent ev) { |
| 65 |
nameField.setEnabled(copyCheck.isSelected()); |
| 66 |
} |
| 67 |
} |
| 68 |
); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
public int showDialog(Collection<TraitData> traits, String defaultName) { |
| 73 |
optionPanel.removeAll(); |
| 74 |
if (traits == null) { |
| 75 |
optionPanel.addSpanningComponent(new JLabel("Create a new data partition using the selected trait(s).")); |
| 76 |
optionPanel.addComponentWithLabel("Name trait partition:", nameField); |
| 77 |
nameField.setText(defaultName != null ? defaultName : "untitled_traits"); |
| 78 |
nameField.setEnabled(true); |
| 79 |
nameField.selectAll(); |
| 80 |
} else { |
| 81 |
traitCombo.removeAllItems(); |
| 82 |
for (Object model : traits) { |
| 83 |
traitCombo.addItem(model); |
| 84 |
} |
| 85 |
optionPanel.addSpanningComponent(new JLabel("Create a new data partition using the following trait.")); |
| 86 |
optionPanel.addComponentWithLabel("Trait:", traitCombo); |
| 87 |
optionPanel.addComponents(copyCheck, nameField); |
| 88 |
nameField.setEnabled(copyCheck.isSelected()); |
| 89 |
} |
| 90 |
|
| 91 |
JOptionPane optionPane = new JOptionPane(optionPanel, |
| 92 |
JOptionPane.QUESTION_MESSAGE, |
| 93 |
JOptionPane.OK_CANCEL_OPTION, |
| 94 |
null, |
| 95 |
null, |
| 96 |
null); |
| 97 |
optionPane.setBorder(new EmptyBorder(12, 12, 12, 12)); |
| 98 |
|
| 99 |
final JDialog dialog = optionPane.createDialog(frame, "Create New Partition"); |
| 100 |
dialog.pack(); |
| 101 |
|
| 102 |
int result; |
| 103 |
boolean isValid; |
| 104 |
|
| 105 |
do { |
| 106 |
dialog.setVisible(true); |
| 107 |
|
| 108 |
isValid = true; |
| 109 |
result = JOptionPane.CANCEL_OPTION; |
| 110 |
|
| 111 |
Integer value = (Integer) optionPane.getValue(); |
| 112 |
if (value != null && value != -1) { |
| 113 |
result = value; |
| 114 |
} |
| 115 |
if (result != JOptionPane.CANCEL_OPTION) { |
| 116 |
String name = getName().trim(); |
| 117 |
if (name.isEmpty()) { |
| 118 |
isValid = false; |
| 119 |
} |
| 120 |
} |
| 121 |
} while (!isValid); |
| 122 |
|
| 123 |
return result; |
| 124 |
} |
| 125 |
|
| 126 |
public TraitData getTrait() { |
| 127 |
return (TraitData) traitCombo.getSelectedItem(); |
| 128 |
} |
| 129 |
|
| 130 |
public boolean getMakeCopy() { |
| 131 |
return copyCheck.isSelected(); |
| 132 |
} |
| 133 |
|
| 134 |
public String getName() { |
| 135 |
return nameField.getText(); |
| 136 |
} |
| 137 |
|
| 138 |
} |