package versusSNP.gui.dialogs;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import versusSNP.Parameter;
import versusSNP.Size;
import versusSNP.blast.BlastExecutor;
import versusSNP.gui.UICaption;
import versusSNP.util.Utils;
import versusSNP.util.action.BrowseAction;
import versusSNP.util.action.BrowseEnsureAction;
import versusSNP.util.action.EditAction;
import versusSNP.util.swing.JIntEdit;
import versusSNP.util.swing.PercentLayout;
import versusSNP.util.swing.SwingUtils;
interface ApplyTab {
public void save();
}
class PathTab extends JPanel implements ApplyTab {
private static final long serialVersionUID = -2157020670356439686L;
private JTextField txtPath1, txtPath2, txtPath3;
private JButton btnBrowse1, btnBrowse2, btnBrowse3;
private StringBuffer blast_path, formatdb_path, temp_path;
public PathTab() {
super(new GridLayout(9,1));
temp_path = new StringBuffer(Parameter.temp_path);
blast_path = new StringBuffer(Parameter.path_program_blastall);
formatdb_path = new StringBuffer(Parameter.path_program_formatdb);
txtPath1 = new JTextField(Parameter.path_program_blastall);
txtPath2 = new JTextField(Parameter.path_program_formatdb);
txtPath3 = new JTextField(Parameter.temp_path);
btnBrowse1 = new JButton(UICaption.dialog_caption_browse);
btnBrowse2 = new JButton(UICaption.dialog_caption_browse);
btnBrowse3 = new JButton(UICaption.dialog_caption_browse);
btnBrowse1.addActionListener(new BrowseEnsureAction(txtPath1, "blastall"));
btnBrowse2.addActionListener(new BrowseEnsureAction(txtPath2, "formatdb"));
btnBrowse3.addActionListener(new BrowseAction(txtPath3, true));
txtPath1.getDocument().addDocumentListener(new EditAction(blast_path));
txtPath2.getDocument().addDocumentListener(new EditAction(formatdb_path));
txtPath3.getDocument().addDocumentListener(new EditAction(temp_path));
JPanel panel1 = new JPanel(new BorderLayout());
JPanel panel2 = new JPanel(new BorderLayout());
JPanel panel3 = new JPanel(new BorderLayout());
panel1.add(txtPath1, BorderLayout.CENTER);
panel1.add(btnBrowse1, BorderLayout.EAST);
panel2.add(txtPath2, BorderLayout.CENTER);
panel2.add(btnBrowse2, BorderLayout.EAST);
panel3.add(txtPath3, BorderLayout.CENTER);
panel3.add(btnBrowse3, BorderLayout.EAST);
add(new JLabel(UICaption.dialog_label_run_blast_path_blastall));
add(panel1);
add(new JLabel(UICaption.dialog_label_run_blast_path_formatdb));
add(panel2);
add(new JLabel(UICaption.dialog_label_temp_path));
add(panel3);
}
@Override
public void save() {
if (temp_path.length() > 0 && temp_path.charAt(temp_path.length()-1) != File.separatorChar)
Parameter.temp_path = temp_path.append(File.separatorChar).toString();
Parameter.path_program_blastall = blast_path.toString();
Parameter.path_program_formatdb = formatdb_path.toString();
}
}
class AlignTab extends JPanel implements ApplyTab {
private static final long serialVersionUID = 1501247841605206031L;
private JComboBox selector1;
private JTextField txtThresholds;
private JIntEdit txtScore, txtOverlap;
public AlignTab() {
super(new GridLayout(9,1));
txtThresholds = new JTextField(Parameter.blast_run_thresholds);
txtOverlap = new JIntEdit(Parameter.blast_filter_overlap);
txtScore = new JIntEdit(Parameter.blast_filter_score);
selector1 = new JComboBox(BlastExecutor.MATRICES);
txtThresholds.setToolTipText(UICaption.tooltip_label_run_blast_thresholds);
selector1.setSelectedItem(Parameter.blast_run_matrix);
JPanel panel1 = new JPanel(new GridLayout(1,2));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(1,2));
JPanel panel4 = new JPanel(new GridLayout(1,2));
panel1.add(new JLabel(UICaption.dialog_label_run_blast_matrix));
panel1.add(selector1);
panel2.add(new JLabel(UICaption.dialog_label_run_blast_thresholds));
panel2.add(txtThresholds);
panel3.add(new JLabel(UICaption.dialog_label_run_blast_filter_overlap));
panel3.add(txtOverlap);
panel4.add(new JLabel(UICaption.dialog_label_run_blast_filter_score));
panel4.add(txtScore);
add(new JLabel(UICaption.dialog_label_blast_parameter));
add(panel1);
add(panel2);
add(new JLabel(UICaption.dialog_label_blast_filters));
add(panel3);
add(panel4);
}
@Override
public void save() {
Parameter.blast_run_matrix = (String)selector1.getSelectedItem();
if (BlastExecutor.checkThresholdsFormat(txtThresholds.getText())) {
Parameter.blast_run_thresholds = (String)txtThresholds.getText();
} else {
JOptionPane.showMessageDialog(null, UICaption.dialog_label_blast_runner_format_thresholds, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
txtThresholds.setText(Parameter.blast_run_thresholds);
}
Parameter.blast_filter_overlap = Utils.toIntegar(txtOverlap.getText());
Parameter.blast_filter_score = Utils.toIntegar(txtScore.getText());
}
}
public class OptionSheet extends JFrame implements ActionListener{
private static final long serialVersionUID = -4223436951383004270L;
private JButton btnOK, btnCancel, btnApply;
private JTabbedPane tabbedPane;
private AlignTab alignTab;
private PathTab pathTab;
public OptionSheet() {
super(UICaption.optionframe_title);
tabbedPane = new JTabbedPane();
JPanel lowerPanel = new JPanel(new FlowLayout());
btnOK = new JButton(UICaption.dialog_option_ok);
btnCancel = new JButton(UICaption.dialog_option_cancel);
btnApply = new JButton(UICaption.dialog_option_apply);
//btnApply.setEnabled(false);
btnOK.addActionListener(this);
btnCancel.addActionListener(this);
btnApply.addActionListener(this);
setLayout(new PercentLayout(PercentLayout.VERTICAL,2));
alignTab = new AlignTab();
pathTab = new PathTab();
tabbedPane.addTab(UICaption.option_panel_caption_align, alignTab);
tabbedPane.addTab(UICaption.option_panel_caption_path, pathTab);
lowerPanel.add(btnOK);
lowerPanel.add(btnCancel);
lowerPanel.add(btnApply);
add(tabbedPane,"88%");
add(lowerPanel, "12%");
setSize(Size.option_frame);
SwingUtils.centerScreen(this);
}
/**
* indicating parameter or palette has been changed
*/
private void changed() {
//btnApply.setEnabled(true);
}
private void save() {
//((ApplyTab)tabbedPane.getSelectedComponent()).save();
alignTab.save();
pathTab.save();
JOptionPane.showMessageDialog(this, UICaption.dialog_label_save_settings_success);
}
private void exit() {
dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() != btnCancel)
save();
if (e.getSource() != btnApply)
exit();
}
}