JAL-2403 ScoreModelI now DistanceModelI, ScoreMatrix delegate of
[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.NJTree;
24 import jalview.analysis.scoremodels.ScoreModels;
25 import jalview.api.analysis.DistanceModelI;
26 import jalview.util.MessageManager;
27
28 import java.awt.Color;
29 import java.awt.FlowLayout;
30 import java.awt.Font;
31 import java.awt.event.ActionEvent;
32
33 import javax.swing.ButtonGroup;
34 import javax.swing.JButton;
35 import javax.swing.JComboBox;
36 import javax.swing.JInternalFrame;
37 import javax.swing.JLayeredPane;
38 import javax.swing.JPanel;
39 import javax.swing.JRadioButton;
40
41 /**
42  * A dialog to allow a user to select and action Tree calculation options
43  */
44 public class TreeChooser extends JPanel
45 {
46   private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
47
48   AlignFrame af;
49
50   JRadioButton neighbourJoining;
51
52   JRadioButton averageDistance;
53
54   JComboBox<String> matrixNames;
55
56   private JInternalFrame frame;
57
58   private ButtonGroup treeTypes;
59
60   /**
61    * Constructor
62    * 
63    * @param af
64    */
65   public TreeChooser(AlignFrame alignFrame)
66   {
67     this.af = alignFrame;
68     init();
69   }
70
71   void init()
72   {
73     frame = new JInternalFrame();
74     frame.setContentPane(this);
75     this.setBackground(Color.white);
76
77     neighbourJoining = new JRadioButton(
78             MessageManager.getString("label.tree_calc_nj"));
79     neighbourJoining.setOpaque(false);
80     averageDistance = new JRadioButton(
81             MessageManager.getString("label.tree_calc_av"));
82     treeTypes = new ButtonGroup();
83     treeTypes.add(neighbourJoining);
84     treeTypes.add(averageDistance);
85     neighbourJoining.setSelected(true);
86
87     matrixNames = new JComboBox<String>();
88     ScoreModels scoreModels = ScoreModels.getInstance();
89     for (DistanceModelI sm : scoreModels.getModels())
90     {
91       boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
92       if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
93       {
94         matrixNames.addItem(sm.getName());
95       }
96     }
97
98     JButton ok = new JButton(MessageManager.getString("action.ok"));
99     ok.setFont(VERDANA_11PT);
100     ok.addActionListener(new java.awt.event.ActionListener()
101     {
102       @Override
103       public void actionPerformed(ActionEvent e)
104       {
105         ok_actionPerformed(e);
106       }
107     });
108
109     JButton cancel = new JButton(MessageManager.getString("action.cancel"));
110     cancel.setFont(VERDANA_11PT);
111     cancel.addActionListener(new java.awt.event.ActionListener()
112     {
113       @Override
114       public void actionPerformed(ActionEvent e)
115       {
116         cancel_actionPerformed(e);
117       }
118     });
119
120     JPanel p1 = new JPanel();
121     p1.setOpaque(false);
122     p1.add(neighbourJoining);
123     p1.add(averageDistance);
124     this.add(p1);
125     JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
126     p2.setOpaque(false);
127     p2.add(matrixNames, FlowLayout.LEFT);
128     this.add(p2);
129     JPanel p3 = new JPanel();
130     p3.setOpaque(false);
131     p3.add(ok);
132     p3.add(cancel);
133     this.add(p3);
134
135     Desktop.addInternalFrame(frame,
136             MessageManager.getString("label.choose_tree"),
137               400, 200, false);
138
139     frame.setLayer(JLayeredPane.PALETTE_LAYER);
140   }
141
142   /**
143    * Open and calculate the selected tree on 'OK'
144    * 
145    * @param e
146    */
147   protected void ok_actionPerformed(ActionEvent e)
148   {
149     try
150     {
151       frame.setClosed(true);
152       String treeType = neighbourJoining.isSelected() ? NJTree.NEIGHBOUR_JOINING
153               : NJTree.AVERAGE_DISTANCE;
154       DistanceModelI sm = ScoreModels.getInstance().forName(
155               matrixNames.getSelectedItem().toString());
156       af.newTreePanel(treeType, sm);
157     } catch (Exception ex)
158     {
159     }
160   }
161
162   /**
163    * Closes dialog on cancel
164    * 
165    * @param e
166    */
167   protected void cancel_actionPerformed(ActionEvent e)
168   {
169     try
170     {
171       frame.setClosed(true);
172     } catch (Exception ex)
173     {
174     }
175   }
176 }