in progress
[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.io.FileNotFoundException;
37 import java.io.IOException;
38 import java.net.MalformedURLException;
39 import java.net.URL;
40
41 import javax.swing.ButtonGroup;
42 import javax.swing.JApplet;
43 import javax.swing.JCheckBoxMenuItem;
44 import javax.swing.JMenuBar;
45 import javax.swing.JMenuItem;
46 import javax.swing.JRadioButtonMenuItem;
47 import javax.swing.event.ChangeEvent;
48 import javax.swing.event.ChangeListener;
49
50 import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
51 import org.forester.archaeopteryx.Options.NODE_LABEL_DIRECTION;
52 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
53 import org.forester.phylogeny.Phylogeny;
54 import org.forester.util.ForesterUtil;
55
56 public final class MainFrameApplet extends MainFrame {
57
58     private static final long    serialVersionUID     = 1941019292746717053L;
59     private final static int     DEFAULT_FRAME_X_SIZE = 640;
60     private final static int     DEFAULT_FRAME_Y_SIZE = 580;
61     private final ArchaeopteryxA _applet;
62     private ButtonGroup          _radio_group_1;
63
64     MainFrameApplet( final ArchaeopteryxA parent_applet,
65                      final Configuration configuration,
66                      final String species_tree_url_str ) {
67         setTitle( ArchaeopteryxA.NAME );
68         _applet = parent_applet;
69         setConfiguration( configuration );
70         setOptions( Options.createInstance( configuration ) );
71         _mainpanel = new MainPanelApplets( _configuration, this );
72         if ( !ForesterUtil.isEmpty( species_tree_url_str ) ) {
73             try {
74                 readSpeciesTree( configuration, species_tree_url_str );
75             }
76             catch ( final Exception e ) {
77                 ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, "failed to read species tree from "
78                         + species_tree_url_str );
79                 ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, e.toString() );
80             }
81         }
82         // build the menu bar
83         _jmenubar = new JMenuBar();
84         if ( !_configuration.isUseNativeUI() ) {
85             _jmenubar.setBackground( _configuration.getGuiMenuBackgroundColor() );
86         }
87         if ( getSpeciesTree() != null ) {
88             buildAnalysisMenu();
89         }
90         buildFileMenu();
91         buildToolsMenu();
92         buildViewMenu();
93         buildFontSizeMenu();
94         buildOptionsMenu();
95         buildTypeMenu();
96         buildHelpMenu();
97         setJMenuBar( _jmenubar );
98         _contentpane = getContentPane();
99         _contentpane.setLayout( new BorderLayout() );
100         _contentpane.add( _mainpanel, BorderLayout.CENTER );
101         setSize( getConfiguration().getFrameXSize() > 40 ? getConfiguration().getFrameXSize() : DEFAULT_FRAME_X_SIZE,
102                 getConfiguration().getFrameYSize() > 40 ? getConfiguration().getFrameYSize() : DEFAULT_FRAME_Y_SIZE );
103         addWindowListener( new WindowAdapter() {
104
105             @Override
106             public void windowClosing( final WindowEvent e ) {
107                 close();
108             }
109         } );
110         addComponentListener( new ComponentAdapter() {
111
112             @Override
113             public void componentResized( final ComponentEvent e ) {
114                 if ( _mainpanel.getCurrentTreePanel() != null ) {
115                     _mainpanel.getCurrentTreePanel().calcParametersForPainting( _mainpanel.getCurrentTreePanel()
116                                                                                 .getWidth(),
117                                                                                 _mainpanel.getCurrentTreePanel()
118                                                                                 .getHeight() );
119                 }
120             }
121         } );
122         setFocusable( true );
123         requestFocus();
124         requestFocusInWindow();
125         setVisible( true );
126         System.gc();
127     }
128
129     @Override
130     public MainPanel getMainPanel() {
131         return _mainpanel;
132     }
133
134     private void readSpeciesTree( final Configuration configuration, final String species_tree_url_str )
135             throws MalformedURLException, FileNotFoundException, IOException {
136         final URL species_tree_url = new URL( species_tree_url_str );
137         final Phylogeny[] species_trees = AptxUtil.readPhylogeniesFromUrl( species_tree_url,
138                                                                            configuration
139                                                                            .isValidatePhyloXmlAgainstSchema(),
140                                                                            configuration
141                                                                            .isReplaceUnderscoresInNhParsing(),
142                                                                            false,
143                                                                            TAXONOMY_EXTRACTION.NO,
144                                                                            false );
145         if ( ( species_trees != null ) && ( species_trees.length > 0 ) ) {
146             AptxUtil.printAppletMessage( ArchaeopteryxA.NAME, "successfully read species tree" );
147             if ( species_trees[ 0 ].isEmpty() ) {
148                 ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, "species tree is empty" );
149             }
150             else if ( !species_trees[ 0 ].isRooted() ) {
151                 ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, "species tree is not rooted" );
152             }
153             else {
154                 setSpeciesTree( species_trees[ 0 ] );
155                 AptxUtil.printAppletMessage( ArchaeopteryxA.NAME, "species tree OK" );
156             }
157         }
158         else {
159             ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, "failed to read species tree from "
160                     + species_tree_url_str );
161         }
162     }
163
164     void buildAnalysisMenu() {
165         _analysis_menu = MainFrame.createMenu( "Analysis", getConfiguration() );
166         _analysis_menu.add( _gsdi_item = new JMenuItem( "GSDI (Generalized Speciation Duplication Inference)" ) );
167         _analysis_menu.add( _gsdir_item = new JMenuItem( "GSDIR (GSDI with re-rooting)" ) );
168         customizeJMenuItem( _gsdi_item );
169         customizeJMenuItem( _gsdir_item );
170         //  _analysis_menu.addSeparator();
171         //  _analysis_menu.add( _lineage_inference = new JMenuItem( INFER_ANCESTOR_TAXONOMIES ) );
172         //  customizeJMenuItem( _lineage_inference );
173         //  _lineage_inference.setToolTipText( "Inference of ancestor taxonomies/lineages" );
174         _jmenubar.add( _analysis_menu );
175     }
176
177     void buildOptionsMenu() {
178         _options_jmenu = MainFrame.createMenu( MainFrame.OPTIONS_HEADER, getConfiguration() );
179         _options_jmenu.addChangeListener( new ChangeListener() {
180
181             @Override
182             public void stateChanged( final ChangeEvent e ) {
183                 MainFrame.setOvPlacementColorChooseMenuItem( _overview_placment_mi, getOptions() );
184                 MainFrame.setTextColorChooseMenuItem( _switch_colors_mi, getCurrentTreePanel() );
185                 MainFrame
186                 .setTextMinSupportMenuItem( _choose_minimal_confidence_mi, getOptions(), getCurrentTreePanel() );
187                 MainFrame.setTextForFontChooserMenuItem( _choose_font_mi, createCurrentFontDesc( getMainPanel()
188                                                                                                  .getTreeFontSet() ) );
189                 setTextForGraphicsSizeChooserMenuItem( _print_size_mi, getOptions() );
190                 setTextForPdfLineWidthChooserMenuItem( _choose_pdf_width_mi, getOptions() );
191                 MainFrame.setCycleNodeFillMenuItem( _cycle_node_fill_mi, getOptions() );
192                 MainFrame.setCycleNodeShapeMenuItem( _cycle_node_shape_mi, getOptions() );
193                 MainFrame.setTextNodeSizeMenuItem( _choose_node_size_mi, getOptions() );
194                 try {
195                     getMainPanel().getControlPanel().setVisibilityOfDomainStrucureCB();
196                     getMainPanel().getControlPanel().setVisibilityOfX();
197                 }
198                 catch ( final Exception ignore ) {
199                     // do nothing, not important.
200                 }
201             }
202         } );
203         _options_jmenu.add( MainFrame.customizeMenuItemAsLabel( new JMenuItem( MainFrame.DISPLAY_SUBHEADER ),
204                                                                 getConfiguration() ) );
205         _options_jmenu
206         .add( _ext_node_dependent_cladogram_rbmi = new JRadioButtonMenuItem( MainFrame.NONUNIFORM_CLADOGRAMS_LABEL ) );
207         _options_jmenu.add( _uniform_cladograms_rbmi = new JRadioButtonMenuItem( MainFrame.UNIFORM_CLADOGRAMS_LABEL ) );
208         _options_jmenu.add( _non_lined_up_cladograms_rbmi = new JRadioButtonMenuItem( NON_LINED_UP_CLADOGRAMS_LABEL ) );
209         _radio_group_1 = new ButtonGroup();
210         _radio_group_1.add( _ext_node_dependent_cladogram_rbmi );
211         _radio_group_1.add( _uniform_cladograms_rbmi );
212         _radio_group_1.add( _non_lined_up_cladograms_rbmi );
213         _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_OVERVIEW_LABEL ) );
214         _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_SCALE_LABEL ) );
215         _options_jmenu
216         .add( _show_default_node_shapes_internal_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL_INT ) );
217         _options_jmenu
218         .add( _show_default_node_shapes_external_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL_EXT ) );
219         _options_jmenu
220         .add( _show_default_node_shapes_for_marked_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL_MARKED ) );
221         _options_jmenu.add( _line_up_renderable_data_cbmi = new JCheckBoxMenuItem( MainFrame.LINE_UP_RENDERABLE_DATA ) );
222         if ( getConfiguration().doDisplayOption( Configuration.show_domain_architectures ) ) {
223             _options_jmenu.add( _right_line_up_domains_cbmi = new JCheckBoxMenuItem( MainFrame.RIGHT_LINE_UP_DOMAINS ) );
224             _options_jmenu.add( _show_domain_labels = new JCheckBoxMenuItem( MainFrame.SHOW_DOMAIN_LABELS_LABEL ) );
225         }
226         _options_jmenu.add( _show_annotation_ref_source = new JCheckBoxMenuItem( MainFrame.SHOW_ANN_REF_SOURCE_LABEL ) );
227         _options_jmenu.add( _show_confidence_stddev_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_CONF_STDDEV_LABEL ) );
228         _options_jmenu
229         .add( _color_by_taxonomic_group_cbmi = new JCheckBoxMenuItem( MainFrame.COLOR_BY_TAXONOMIC_GROUP ) );
230         _options_jmenu
231         .add( _color_labels_same_as_parent_branch = new JCheckBoxMenuItem( MainFrame.COLOR_LABELS_LABEL ) );
232         _color_labels_same_as_parent_branch.setToolTipText( MainFrame.COLOR_LABELS_TIP );
233         _options_jmenu.add( _abbreviate_scientific_names = new JCheckBoxMenuItem( MainFrame.ABBREV_SN_LABEL ) );
234         _options_jmenu.add( _label_direction_cbmi = new JCheckBoxMenuItem( MainFrame.LABEL_DIRECTION_LABEL ) );
235         _label_direction_cbmi.setToolTipText( MainFrame.LABEL_DIRECTION_TIP );
236         _options_jmenu.add( _screen_antialias_cbmi = new JCheckBoxMenuItem( MainFrame.SCREEN_ANTIALIAS_LABEL ) );
237         _options_jmenu.add( _background_gradient_cbmi = new JCheckBoxMenuItem( MainFrame.BG_GRAD_LABEL ) );
238         _options_jmenu.add( _cycle_node_shape_mi = new JMenuItem( MainFrame.CYCLE_NODE_SHAPE_LABEL ) );
239         _options_jmenu.add( _cycle_node_fill_mi = new JMenuItem( MainFrame.CYCLE_NODE_FILL_LABEL ) );
240         _options_jmenu.add( _choose_node_size_mi = new JMenuItem( MainFrame.CHOOSE_NODE_SIZE_LABEL ) );
241         _options_jmenu.add( _choose_minimal_confidence_mi = new JMenuItem( "" ) );
242         _options_jmenu.add( _overview_placment_mi = new JMenuItem( "" ) );
243         _options_jmenu.add( _switch_colors_mi = new JMenuItem( "" ) );
244         _options_jmenu.add( _choose_font_mi = new JMenuItem( "" ) );
245         _options_jmenu.addSeparator();
246         _options_jmenu.add( MainFrame.customizeMenuItemAsLabel( new JMenuItem( MainFrame.SEARCH_SUBHEADER ),
247                                                                 getConfiguration() ) );
248         _options_jmenu
249         .add( _search_case_senstive_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_CASE_SENSITIVE_LABEL ) );
250         _options_jmenu.add( _search_whole_words_only_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_TERMS_ONLY_LABEL ) );
251         _options_jmenu.add( _search_with_regex_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_REGEX_LABEL ) );
252         _search_with_regex_cbmi.setToolTipText( MainFrame.SEARCH_WITH_REGEX_TIP );
253         _options_jmenu.add( _inverse_search_result_cbmi = new JCheckBoxMenuItem( INVERSE_SEARCH_RESULT_LABEL ) );
254         //
255         _options_jmenu.addSeparator();
256         _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( "Graphics Export & Printing:" ),
257                                                       getConfiguration() ) );
258         _options_jmenu.add( _antialias_print_cbmi = new JCheckBoxMenuItem( "Antialias" ) );
259         _options_jmenu.add( _print_black_and_white_cbmi = new JCheckBoxMenuItem( "Export in Black and White" ) );
260         _options_jmenu
261         .add( _print_using_actual_size_cbmi = new JCheckBoxMenuItem( "Use Current Image Size for PDF export and Printing" ) );
262         _options_jmenu
263         .add( _graphics_export_using_actual_size_cbmi = new JCheckBoxMenuItem( "Use Current Image Size for PNG, JPG, and GIF export" ) );
264         _options_jmenu
265         .add( _graphics_export_visible_only_cbmi = new JCheckBoxMenuItem( "Limit to Visible ('Screenshot') for PNG, JPG, and GIF export" ) );
266         _options_jmenu.add( _print_size_mi = new JMenuItem( "" ) );
267         _options_jmenu.add( _choose_pdf_width_mi = new JMenuItem( "" ) );
268         //
269         customizeCheckBoxMenuItem( _antialias_print_cbmi, getOptions().isAntialiasPrint() );
270         customizeCheckBoxMenuItem( _print_black_and_white_cbmi, getOptions().isPrintBlackAndWhite() );
271         customizeCheckBoxMenuItem( _graphics_export_visible_only_cbmi, getOptions().isGraphicsExportVisibleOnly() );
272         customizeCheckBoxMenuItem( _print_using_actual_size_cbmi, getOptions().isPrintUsingActualSize() );
273         customizeCheckBoxMenuItem( _graphics_export_using_actual_size_cbmi, getOptions()
274                                    .isGraphicsExportUsingActualSize() );
275         customizeJMenuItem( _print_size_mi );
276         customizeJMenuItem( _choose_pdf_width_mi );
277         //
278         customizeJMenuItem( _choose_font_mi );
279         customizeJMenuItem( _switch_colors_mi );
280         customizeJMenuItem( _choose_minimal_confidence_mi );
281         customizeJMenuItem( _overview_placment_mi );
282         customizeCheckBoxMenuItem( _show_default_node_shapes_internal_cbmi, getOptions()
283                                    .isShowDefaultNodeShapesInternal() );
284         customizeCheckBoxMenuItem( _show_default_node_shapes_external_cbmi, getOptions()
285                                    .isShowDefaultNodeShapesExternal() );
286         customizeCheckBoxMenuItem( _show_default_node_shapes_for_marked_cbmi, getOptions()
287                                    .isShowDefaultNodeShapesForMarkedNodes() );
288         customizeJMenuItem( _cycle_node_shape_mi );
289         customizeJMenuItem( _cycle_node_fill_mi );
290         customizeJMenuItem( _choose_node_size_mi );
291         customizeCheckBoxMenuItem( _color_by_taxonomic_group_cbmi, getOptions().isColorByTaxonomicGroup() );
292         customizeCheckBoxMenuItem( _color_labels_same_as_parent_branch, getOptions().isColorLabelsSameAsParentBranch() );
293         customizeCheckBoxMenuItem( _screen_antialias_cbmi, getOptions().isAntialiasScreen() );
294         customizeCheckBoxMenuItem( _background_gradient_cbmi, getOptions().isBackgroundColorGradient() );
295         customizeCheckBoxMenuItem( _show_domain_labels, getOptions().isShowDomainLabels() );
296         customizeCheckBoxMenuItem( _show_annotation_ref_source, getOptions().isShowAnnotationRefSource() );
297         customizeCheckBoxMenuItem( _abbreviate_scientific_names, getOptions().isAbbreviateScientificTaxonNames() );
298         customizeCheckBoxMenuItem( _search_case_senstive_cbmi, getOptions().isSearchCaseSensitive() );
299         customizeCheckBoxMenuItem( _show_scale_cbmi, getOptions().isShowScale() );
300         customizeRadioButtonMenuItem( _non_lined_up_cladograms_rbmi,
301                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.NON_LINED_UP );
302         customizeRadioButtonMenuItem( _uniform_cladograms_rbmi,
303                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );
304         customizeRadioButtonMenuItem( _ext_node_dependent_cladogram_rbmi,
305                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
306         customizeCheckBoxMenuItem( _show_overview_cbmi, getOptions().isShowOverview() );
307         customizeCheckBoxMenuItem( _label_direction_cbmi,
308                                    getOptions().getNodeLabelDirection() == NODE_LABEL_DIRECTION.RADIAL );
309         customizeCheckBoxMenuItem( _search_with_regex_cbmi, getOptions().isSearchWithRegex() );
310         customizeCheckBoxMenuItem( _search_whole_words_only_cbmi, getOptions().isMatchWholeTermsOnly() );
311         customizeCheckBoxMenuItem( _inverse_search_result_cbmi, getOptions().isInverseSearchResult() );
312         customizeCheckBoxMenuItem( _show_confidence_stddev_cbmi, getOptions().isShowConfidenceStddev() );
313         customizeCheckBoxMenuItem( _line_up_renderable_data_cbmi, getOptions().isLineUpRendarableNodeData() );
314         customizeCheckBoxMenuItem( _right_line_up_domains_cbmi, getOptions().isRightLineUpDomains() );
315         _jmenubar.add( _options_jmenu );
316     }
317
318     void buildToolsMenu() {
319         _tools_menu = MainFrame.createMenu( "Tools", getConfiguration() );
320         _tools_menu.add( _confcolor_item = new JMenuItem( "Colorize Branches Depending on Confidence" ) );
321         customizeJMenuItem( _confcolor_item );
322         _tools_menu.add( _taxcolor_item = new JMenuItem( "Taxonomy Colorize Branches" ) );
323         customizeJMenuItem( _taxcolor_item );
324         _tools_menu.addSeparator();
325         _tools_menu.add( _remove_visual_styles_item = new JMenuItem( "Delete All Visual Styles From Nodes" ) );
326         _remove_visual_styles_item
327         .setToolTipText( "To remove all node visual styles (fonts, colors) from the current phylogeny." );
328         customizeJMenuItem( _remove_visual_styles_item );
329         _tools_menu.add( _remove_branch_color_item = new JMenuItem( "Delete All Colors From Branches" ) );
330         _remove_branch_color_item.setToolTipText( "To remove all branch color values from the current phylogeny." );
331         customizeJMenuItem( _remove_branch_color_item );
332         _tools_menu.addSeparator();
333         _tools_menu.add( _midpoint_root_item = new JMenuItem( "Midpoint-Root" ) );
334         customizeJMenuItem( _midpoint_root_item );
335         _tools_menu.addSeparator();
336         _tools_menu.add( _collapse_species_specific_subtrees = new JMenuItem( "Collapse Species-Specific Subtrees" ) );
337         customizeJMenuItem( _collapse_species_specific_subtrees );
338         _jmenubar.add( _tools_menu );
339     }
340
341     JApplet getApplet() {
342         return _applet;
343     }
344 }