JAL-1632 first cut of Tree chooser dialog and ScoreModels singleton
[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 (String scoreType : scoreModels.getModelNames())
89     {
90       ScoreModelI sm = scoreModels.forName(scoreType);
91       if (sm.isDNA() == af.getViewport().getAlignment().isNucleotide()
92               || sm.isProtein() == !af.getViewport().getAlignment()
93                       .isNucleotide())
94       {
95         matrixNames.addItem(sm.getName());
96       }
97     }
98
99     JButton ok = new JButton(MessageManager.getString("action.ok"));
100     ok.setFont(VERDANA_11PT);
101     ok.addActionListener(new java.awt.event.ActionListener()
102     {
103       @Override
104       public void actionPerformed(ActionEvent e)
105       {
106         ok_actionPerformed(e);
107       }
108     });
109
110     JButton cancel = new JButton(MessageManager.getString("action.cancel"));
111     cancel.setFont(VERDANA_11PT);
112     cancel.addActionListener(new java.awt.event.ActionListener()
113     {
114       @Override
115       public void actionPerformed(ActionEvent e)
116       {
117         cancel_actionPerformed(e);
118       }
119     });
120
121     JPanel p1 = new JPanel();
122     p1.setOpaque(false);
123     p1.add(neighbourJoining);
124     p1.add(averageDistance);
125     this.add(p1);
126     JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
127     p2.setOpaque(false);
128     p2.add(matrixNames, FlowLayout.LEFT);
129     this.add(p2);
130     JPanel p3 = new JPanel();
131     p3.setOpaque(false);
132     p3.add(ok);
133     p3.add(cancel);
134     this.add(p3);
135
136     Desktop.addInternalFrame(frame,
137             MessageManager.getString("label.choose_tree"),
138               400, 200, false);
139
140     frame.setLayer(JLayeredPane.PALETTE_LAYER);
141   }
142
143   /**
144    * Open and calculate the selected tree on 'OK'
145    * 
146    * @param e
147    */
148   protected void ok_actionPerformed(ActionEvent e)
149   {
150     try
151     {
152       frame.setClosed(true);
153       String treeType = neighbourJoining.isSelected() ? "NJ" : "AV";
154       ScoreModelI 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 }