40c683d72d371d775a84a5d896f41faeee471582
[jalview.git] / src / jalview / gui / TreeChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.analysis.scoremodels.ScoreModels;
24 import jalview.api.analysis.ScoreModelI;
25 import jalview.util.MessageManager;
26
27 import java.awt.Color;
28 import java.awt.FlowLayout;
29 import java.awt.Font;
30 import java.awt.event.ActionEvent;
31
32 import javax.swing.ButtonGroup;
33 import javax.swing.JButton;
34 import javax.swing.JComboBox;
35 import javax.swing.JInternalFrame;
36 import javax.swing.JLayeredPane;
37 import javax.swing.JPanel;
38 import javax.swing.JRadioButton;
39
40 /**
41  * A dialog to allow a user to select and action Tree calculation options
42  */
43 public class TreeChooser extends JPanel
44 {
45   private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
46
47   AlignFrame af;
48
49   JRadioButton neighbourJoining;
50
51   JRadioButton averageDistance;
52
53   JComboBox<String> matrixNames;
54
55   private JInternalFrame frame;
56
57   private ButtonGroup treeTypes;
58
59   /**
60    * Constructor
61    * 
62    * @param af
63    */
64   public TreeChooser(AlignFrame alignFrame)
65   {
66     this.af = alignFrame;
67     init();
68   }
69
70   void init()
71   {
72     frame = new JInternalFrame();
73     frame.setContentPane(this);
74     this.setBackground(Color.white);
75
76     neighbourJoining = new JRadioButton(
77             MessageManager.getString("label.tree_calc_nj"));
78     neighbourJoining.setOpaque(false);
79     averageDistance = new JRadioButton(
80             MessageManager.getString("label.tree_calc_av"));
81     treeTypes = new ButtonGroup();
82     treeTypes.add(neighbourJoining);
83     treeTypes.add(averageDistance);
84     neighbourJoining.setSelected(true);
85
86     matrixNames = new JComboBox<String>();
87     ScoreModels scoreModels = ScoreModels.getInstance();
88     for (ScoreModelI sm : scoreModels.getModels())
89     {
90       boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
91       if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
92       {
93         matrixNames.addItem(sm.getName());
94       }
95     }
96
97     JButton ok = new JButton(MessageManager.getString("action.ok"));
98     ok.setFont(VERDANA_11PT);
99     ok.addActionListener(new java.awt.event.ActionListener()
100     {
101       @Override
102       public void actionPerformed(ActionEvent e)
103       {
104         ok_actionPerformed(e);
105       }
106     });
107
108     JButton cancel = new JButton(MessageManager.getString("action.cancel"));
109     cancel.setFont(VERDANA_11PT);
110     cancel.addActionListener(new java.awt.event.ActionListener()
111     {
112       @Override
113       public void actionPerformed(ActionEvent e)
114       {
115         cancel_actionPerformed(e);
116       }
117     });
118
119     JPanel p1 = new JPanel();
120     p1.setOpaque(false);
121     p1.add(neighbourJoining);
122     p1.add(averageDistance);
123     this.add(p1);
124     JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
125     p2.setOpaque(false);
126     p2.add(matrixNames, FlowLayout.LEFT);
127     this.add(p2);
128     JPanel p3 = new JPanel();
129     p3.setOpaque(false);
130     p3.add(ok);
131     p3.add(cancel);
132     this.add(p3);
133
134     Desktop.addInternalFrame(frame,
135             MessageManager.getString("label.choose_tree"),
136               400, 200, false);
137
138     frame.setLayer(JLayeredPane.PALETTE_LAYER);
139   }
140
141   /**
142    * Open and calculate the selected tree on 'OK'
143    * 
144    * @param e
145    */
146   protected void ok_actionPerformed(ActionEvent e)
147   {
148     try
149     {
150       frame.setClosed(true);
151       String treeType = neighbourJoining.isSelected() ? "NJ" : "AV";
152       ScoreModelI sm = ScoreModels.getInstance().forName(
153               matrixNames.getSelectedItem().toString());
154       af.newTreePanel(treeType, sm);
155     } catch (Exception ex)
156     {
157     }
158   }
159
160   /**
161    * Closes dialog on cancel
162    * 
163    * @param e
164    */
165   protected void cancel_actionPerformed(ActionEvent e)
166   {
167     try
168     {
169       frame.setClosed(true);
170     } catch (Exception ex)
171     {
172     }
173   }
174 }