4a5c93204424b13e73b6b29ded75fe31cf450a64
[jalview.git] / forester / java / src / org / forester / archaeopteryx / MainFrameApplet.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
9 // Copyright (C) 2003-2007 Ethalinda K.S. Cannon
10 // All rights reserved
11 //
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 //
26 // Contact: phylosoft @ gmail . com
27 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
28
29 package org.forester.archaeopteryx;
30
31 import java.awt.BorderLayout;
32 import java.awt.event.ComponentAdapter;
33 import java.awt.event.ComponentEvent;
34 import java.awt.event.WindowAdapter;
35 import java.awt.event.WindowEvent;
36 import java.net.URL;
37
38 import javax.swing.ButtonGroup;
39 import javax.swing.JApplet;
40 import javax.swing.JCheckBoxMenuItem;
41 import javax.swing.JMenuBar;
42 import javax.swing.JMenuItem;
43 import javax.swing.JOptionPane;
44 import javax.swing.JRadioButtonMenuItem;
45 import javax.swing.event.ChangeEvent;
46 import javax.swing.event.ChangeListener;
47
48 import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
49 import org.forester.archaeopteryx.Options.NODE_LABEL_DIRECTION;
50 import org.forester.phylogeny.Phylogeny;
51 import org.forester.util.ForesterUtil;
52
53 public final class MainFrameApplet extends MainFrame {
54
55     private static final long    serialVersionUID     = 1941019292746717053L;
56     private final static int     DEFAULT_FRAME_X_SIZE = 640;
57     private final static int     DEFAULT_FRAME_Y_SIZE = 580;
58     private final ArchaeopteryxA _applet;
59     private ButtonGroup          _radio_group_1;
60
61     MainFrameApplet( final ArchaeopteryxA parent_applet, final Configuration configuration ) {
62         setTitle( ArchaeopteryxA.NAME );
63         _applet = parent_applet;
64         setConfiguration( configuration );
65         setOptions( Options.createInstance( configuration ) );
66         //_textframes = null; //~~~~
67         URL url = null;
68         Phylogeny[] phys = null;
69         // Get URL to tree file
70         if ( _applet.getTreeUrlStr() != null ) {
71             try {
72                 url = new URL( _applet.getTreeUrlStr() );
73             }
74             catch ( final Exception e ) {
75                 ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, e.toString() );
76                 e.printStackTrace();
77                 JOptionPane.showMessageDialog( this,
78                                                ArchaeopteryxA.NAME + ": Could not create URL from: \""
79                                                        + _applet.getTreeUrlStr() + "\"\nError: " + e,
80                                                "Failed to create URL",
81                                                JOptionPane.ERROR_MESSAGE );
82                 close();
83             }
84         }
85         // Load the tree from URL
86         if ( url != null ) {
87             try {
88                 phys = AptxUtil.readPhylogeniesFromUrl( url,
89                                                         configuration.isValidatePhyloXmlAgainstSchema(),
90                                                         configuration.isReplaceUnderscoresInNhParsing(),
91                                                         configuration.isInternalNumberAreConfidenceForNhParsing(),
92                                                         configuration.getTaxonomyExtraction() );
93             }
94             catch ( final Exception e ) {
95                 ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, e.toString() );
96                 e.printStackTrace();
97                 JOptionPane.showMessageDialog( this, ArchaeopteryxA.NAME + ": Failed to read phylogenies: "
98                         + "\nError: " + e, "Failed to read phylogenies", JOptionPane.ERROR_MESSAGE );
99                 close();
100             }
101         }
102         if ( ( phys == null ) || ( phys.length < 1 ) ) {
103             ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, "phylogenies from [" + url + "] are null or empty" );
104             JOptionPane.showMessageDialog( this, ArchaeopteryxA.NAME + ": phylogenies from [" + url
105                     + "] are null or empty", "Failed to read phylogenies", JOptionPane.ERROR_MESSAGE );
106         }
107         else {
108             AptxUtil.printAppletMessage( ArchaeopteryxA.NAME, "loaded " + phys.length + " phylogenies from: " + url );
109         }
110         _mainpanel = new MainPanelApplets( _configuration, this );
111         // build the menu bar
112         _jmenubar = new JMenuBar();
113         if ( !_configuration.isUseNativeUI() ) {
114             _jmenubar.setBackground( _configuration.getGuiMenuBackgroundColor() );
115         }
116         //TODO if species tree...
117         buildAnalysisMenu();
118         buildToolsMenu();
119         buildViewMenu();
120         buildFontSizeMenu();
121         buildOptionsMenu();
122         buildTypeMenu();
123         buildHelpMenu();
124         setJMenuBar( _jmenubar );
125         _contentpane = getContentPane();
126         _contentpane.setLayout( new BorderLayout() );
127         _contentpane.add( _mainpanel, BorderLayout.CENTER );
128         setSize( getConfiguration().getFrameXSize() > 40 ? getConfiguration().getFrameXSize() : DEFAULT_FRAME_X_SIZE,
129                  getConfiguration().getFrameYSize() > 40 ? getConfiguration().getFrameYSize() : DEFAULT_FRAME_Y_SIZE );
130         addWindowListener( new WindowAdapter() {
131
132             @Override
133             public void windowClosing( final WindowEvent e ) {
134                 close();
135             }
136         } );
137         addComponentListener( new ComponentAdapter() {
138
139             @Override
140             public void componentResized( final ComponentEvent e ) {
141                 if ( _mainpanel.getCurrentTreePanel() != null ) {
142                     _mainpanel.getCurrentTreePanel().calcParametersForPainting( _mainpanel.getCurrentTreePanel()
143                                                                                         .getWidth(),
144                                                                                 _mainpanel.getCurrentTreePanel()
145                                                                                         .getHeight(),
146                                                                                 getOptions().isAllowFontSizeChange() );
147                 }
148             }
149         } );
150         setFocusable( true );
151         requestFocus();
152         requestFocusInWindow();
153         setVisible( true );
154         System.gc();
155     }
156
157     void buildAnalysisMenu() {
158         _analysis_menu = MainFrame.createMenu( "Analysis", getConfiguration() );
159         _analysis_menu.add( _gsdi_item = new JMenuItem( "GSDI (Generalized Speciation Duplication Inference)" ) );
160         _analysis_menu.add( _gsdir_item = new JMenuItem( "GSDIR (GSDI with re-rooting)" ) );
161         customizeJMenuItem( _gsdi_item );
162         customizeJMenuItem( _gsdir_item );
163         //  _analysis_menu.addSeparator();
164         //  _analysis_menu.add( _lineage_inference = new JMenuItem( INFER_ANCESTOR_TAXONOMIES ) );
165         //  customizeJMenuItem( _lineage_inference );
166         //  _lineage_inference.setToolTipText( "Inference of ancestor taxonomies/lineages" );
167         _jmenubar.add( _analysis_menu );
168     }
169
170     void buildOptionsMenu() {
171         _options_jmenu = MainFrame.createMenu( MainFrame.OPTIONS_HEADER, getConfiguration() );
172         _options_jmenu.addChangeListener( new ChangeListener() {
173
174             @Override
175             public void stateChanged( final ChangeEvent e ) {
176                 MainFrame.setOvPlacementColorChooseMenuItem( _overview_placment_mi, getOptions() );
177                 MainFrame.setTextColorChooseMenuItem( _switch_colors_mi, getCurrentTreePanel() );
178                 MainFrame
179                         .setTextMinSupportMenuItem( _choose_minimal_confidence_mi, getOptions(), getCurrentTreePanel() );
180                 MainFrame.setTextForFontChooserMenuItem( _choose_font_mi, createCurrentFontDesc( getMainPanel()
181                         .getTreeFontSet() ) );
182                 MainFrame.updateOptionsMenuDependingOnPhylogenyType( getMainPanel(),
183                                                                      _show_scale_cbmi,
184                                                                      _show_branch_length_values_cbmi,
185                                                                      _non_lined_up_cladograms_rbmi,
186                                                                      _uniform_cladograms_rbmi,
187                                                                      _ext_node_dependent_cladogram_rbmi,
188                                                                      _label_direction_cbmi );
189                 MainFrame.setCycleNodeFillMenuItem( _cycle_node_fill_mi, getOptions() );
190                 MainFrame.setCycleNodeShapeMenuItem( _cycle_node_shape_mi, getOptions() );
191                 MainFrame.setTextNodeSizeMenuItem( _choose_node_size_mi, getOptions() );
192             }
193         } );
194         _options_jmenu.add( MainFrame.customizeMenuItemAsLabel( new JMenuItem( MainFrame.DISPLAY_SUBHEADER ),
195                                                                 getConfiguration() ) );
196         _options_jmenu
197                 .add( _ext_node_dependent_cladogram_rbmi = new JRadioButtonMenuItem( MainFrame.NONUNIFORM_CLADOGRAMS_LABEL ) );
198         _options_jmenu.add( _uniform_cladograms_rbmi = new JRadioButtonMenuItem( MainFrame.UNIFORM_CLADOGRAMS_LABEL ) );
199         _options_jmenu.add( _non_lined_up_cladograms_rbmi = new JRadioButtonMenuItem( NON_LINED_UP_CLADOGRAMS_LABEL ) );
200         _radio_group_1 = new ButtonGroup();
201         _radio_group_1.add( _ext_node_dependent_cladogram_rbmi );
202         _radio_group_1.add( _uniform_cladograms_rbmi );
203         _radio_group_1.add( _non_lined_up_cladograms_rbmi );
204         _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_OVERVIEW_LABEL ) );
205         _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_SCALE_LABEL ) );
206         _options_jmenu
207                 .add( _show_branch_length_values_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_BRANCH_LENGTH_VALUES_LABEL ) );
208         _options_jmenu.add( _show_confidence_stddev_cbmi = new JCheckBoxMenuItem( SHOW_CONF_STDDEV_LABEL ) );
209         _options_jmenu
210                 .add( _show_default_node_shapes_internal_cbmi = new JCheckBoxMenuItem( DISPLAY_NODE_BOXES_LABEL_INT ) );
211         _options_jmenu
212                 .add( _show_default_node_shapes_external_cbmi = new JCheckBoxMenuItem( DISPLAY_NODE_BOXES_LABEL_EXT ) );
213         _options_jmenu
214                 .add( _taxonomy_colorize_node_shapes_cbmi = new JCheckBoxMenuItem( MainFrame.TAXONOMY_COLORIZE_NODE_SHAPES_LABEL ) );
215         _options_jmenu.add( _cycle_node_shape_mi = new JMenuItem( MainFrame.CYCLE_NODE_SHAPE_LABEL ) );
216         _options_jmenu.add( _cycle_node_fill_mi = new JMenuItem( MainFrame.CYCLE_NODE_FILL_LABEL ) );
217         _options_jmenu.add( _choose_node_size_mi = new JMenuItem( MainFrame.CHOOSE_NODE_SIZE_LABEL ) );
218         _options_jmenu.add( _label_direction_cbmi = new JCheckBoxMenuItem( LABEL_DIRECTION_LABEL ) );
219         _label_direction_cbmi.setToolTipText( LABEL_DIRECTION_TIP );
220         _options_jmenu.add( _color_labels_same_as_parent_branch = new JCheckBoxMenuItem( COLOR_LABELS_LABEL ) );
221         _color_labels_same_as_parent_branch.setToolTipText( MainFrame.COLOR_LABELS_TIP );
222         _options_jmenu.add( _abbreviate_scientific_names = new JCheckBoxMenuItem( MainFrame.ABBREV_SN_LABEL ) );
223         _options_jmenu.add( _screen_antialias_cbmi = new JCheckBoxMenuItem( MainFrame.SCREEN_ANTIALIAS_LABEL ) );
224         _options_jmenu.add( _background_gradient_cbmi = new JCheckBoxMenuItem( MainFrame.BG_GRAD_LABEL ) );
225         if ( getConfiguration().doDisplayOption( Configuration.show_domain_architectures ) ) {
226             _options_jmenu.add( _show_domain_labels = new JCheckBoxMenuItem( SHOW_DOMAIN_LABELS_LABEL ) );
227         }
228         _options_jmenu.add( _choose_minimal_confidence_mi = new JMenuItem( "" ) );
229         _options_jmenu.add( _overview_placment_mi = new JMenuItem( "" ) );
230         _options_jmenu.add( _switch_colors_mi = new JMenuItem( "" ) );
231         _options_jmenu.add( _choose_font_mi = new JMenuItem( "" ) );
232         _options_jmenu.addSeparator();
233         _options_jmenu.add( MainFrame.customizeMenuItemAsLabel( new JMenuItem( MainFrame.SEARCH_SUBHEADER ),
234                                                                 getConfiguration() ) );
235         _options_jmenu
236                 .add( _search_case_senstive_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_CASE_SENSITIVE_LABEL ) );
237         _options_jmenu.add( _search_whole_words_only_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_TERMS_ONLY_LABEL ) );
238         _options_jmenu.add( _inverse_search_result_cbmi = new JCheckBoxMenuItem( INVERSE_SEARCH_RESULT_LABEL ) );
239         customizeJMenuItem( _choose_font_mi );
240         customizeJMenuItem( _switch_colors_mi );
241         customizeJMenuItem( _choose_minimal_confidence_mi );
242         customizeJMenuItem( _overview_placment_mi );
243         customizeCheckBoxMenuItem( _show_default_node_shapes_internal_cbmi, getOptions()
244                 .isShowDefaultNodeShapesInternal() );
245         customizeCheckBoxMenuItem( _show_default_node_shapes_external_cbmi, getOptions()
246                 .isShowDefaultNodeShapesExternal() );
247         customizeCheckBoxMenuItem( _taxonomy_colorize_node_shapes_cbmi, getOptions().isTaxonomyColorizeNodeShapes() );
248         customizeJMenuItem( _cycle_node_shape_mi );
249         customizeJMenuItem( _cycle_node_fill_mi );
250         customizeJMenuItem( _choose_node_size_mi );
251         customizeCheckBoxMenuItem( _color_labels_same_as_parent_branch, getOptions().isColorLabelsSameAsParentBranch() );
252         customizeCheckBoxMenuItem( _screen_antialias_cbmi, getOptions().isAntialiasScreen() );
253         customizeCheckBoxMenuItem( _background_gradient_cbmi, getOptions().isBackgroundColorGradient() );
254         customizeCheckBoxMenuItem( _show_domain_labels, getOptions().isShowDomainLabels() );
255         customizeCheckBoxMenuItem( _abbreviate_scientific_names, getOptions().isAbbreviateScientificTaxonNames() );
256         customizeCheckBoxMenuItem( _search_case_senstive_cbmi, getOptions().isSearchCaseSensitive() );
257         customizeCheckBoxMenuItem( _show_scale_cbmi, getOptions().isShowScale() );
258         customizeRadioButtonMenuItem( _non_lined_up_cladograms_rbmi,
259                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.NON_LINED_UP );
260         customizeRadioButtonMenuItem( _uniform_cladograms_rbmi,
261                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );
262         customizeRadioButtonMenuItem( _ext_node_dependent_cladogram_rbmi,
263                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
264         customizeCheckBoxMenuItem( _show_branch_length_values_cbmi, getOptions().isShowBranchLengthValues() );
265         customizeCheckBoxMenuItem( _show_overview_cbmi, getOptions().isShowOverview() );
266         customizeCheckBoxMenuItem( _label_direction_cbmi,
267                                    getOptions().getNodeLabelDirection() == NODE_LABEL_DIRECTION.RADIAL );
268         customizeCheckBoxMenuItem( _search_whole_words_only_cbmi, getOptions().isMatchWholeTermsOnly() );
269         customizeCheckBoxMenuItem( _inverse_search_result_cbmi, getOptions().isInverseSearchResult() );
270         customizeCheckBoxMenuItem( _show_confidence_stddev_cbmi, getOptions().isShowConfidenceStddev() );
271         _jmenubar.add( _options_jmenu );
272     }
273
274     void buildToolsMenu() {
275         _tools_menu = MainFrame.createMenu( "Tools", getConfiguration() );
276         _tools_menu.add( _confcolor_item = new JMenuItem( "Colorize Branches Depending on Confidence" ) );
277         customizeJMenuItem( _confcolor_item );
278         _tools_menu.add( _taxcolor_item = new JMenuItem( "Taxonomy Colorize Branches" ) );
279         customizeJMenuItem( _taxcolor_item );
280         _tools_menu.add( _remove_branch_color_item = new JMenuItem( "Delete Branch Colors" ) );
281         _remove_branch_color_item.setToolTipText( "To delete branch color values from the current phylogeny." );
282         customizeJMenuItem( _remove_branch_color_item );
283         _tools_menu.addSeparator();
284         _tools_menu.add( _midpoint_root_item = new JMenuItem( "Midpoint-Root" ) );
285         customizeJMenuItem( _midpoint_root_item );
286         _tools_menu.addSeparator();
287         _tools_menu.add( _collapse_species_specific_subtrees = new JMenuItem( "Collapse Species-Specific Subtrees" ) );
288         customizeJMenuItem( _collapse_species_specific_subtrees );
289         _jmenubar.add( _tools_menu );
290     }
291
292     JApplet getApplet() {
293         return _applet;
294     }
295
296     @Override
297     public MainPanel getMainPanel() {
298         return _mainpanel;
299     }
300
301     @Override
302     void readPhylogeniesFromURL() {
303         throw new NoSuchMethodError( "not implemented" );
304     }
305
306     void setSpeciesTree( final Phylogeny species_tree ) {
307         _species_tree = species_tree;
308     }
309 }