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: www.phylosoft.org/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     FRAME_X_SIZE     = 640, FRAME_Y_SIZE = 580;
57     private final ArchaeopteryxA _applet;
58     private ButtonGroup          _radio_group_1;
59
60     MainFrameApplet( final ArchaeopteryxA parent_applet, final Configuration configuration ) {
61         setTitle( ArchaeopteryxA.NAME );
62         _applet = parent_applet;
63         setConfiguration( configuration );
64         setOptions( Options.createInstance( configuration ) );
65         _textframe = null;
66         URL url = null;
67         Phylogeny[] phys = null;
68         // Get URL to tree file
69         if ( _applet.getUrlString() != null ) {
70             try {
71                 url = new URL( _applet.getUrlString() );
72             }
73             catch ( final Exception e ) {
74                 ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, e.toString() );
75                 e.printStackTrace();
76                 JOptionPane.showMessageDialog( this,
77                                                ArchaeopteryxA.NAME + ": Could not create URL from: \""
78                                                        + _applet.getUrlString() + "\"\nError: " + e,
79                                                "Failed to create URL",
80                                                JOptionPane.ERROR_MESSAGE );
81                 close();
82             }
83         }
84         // Load the tree from URL
85         if ( url != null ) {
86             try {
87                 phys = AptxUtil.readPhylogeniesFromUrl( url, getConfiguration().isValidatePhyloXmlAgainstSchema() );
88             }
89             catch ( final Exception e ) {
90                 ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, e.toString() );
91                 e.printStackTrace();
92                 JOptionPane.showMessageDialog( this, ArchaeopteryxA.NAME + ": Failed to read phylogenies: "
93                         + "\nError: " + e, "Failed to read phylogenies", JOptionPane.ERROR_MESSAGE );
94                 close();
95             }
96         }
97         if ( ( phys == null ) || ( phys.length < 1 ) ) {
98             ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, "phylogenies from [" + url + "] are null or empty" );
99             JOptionPane.showMessageDialog( this, ArchaeopteryxA.NAME + ": phylogenies from [" + url
100                     + "] are null or empty", "Failed to read phylogenies", JOptionPane.ERROR_MESSAGE );
101         }
102         else {
103             AptxUtil.printAppletMessage( ArchaeopteryxA.NAME, "loaded " + phys.length + " phylogenies from: " + url );
104         }
105         _mainpanel = new MainPanelApplets( _configuration, this );
106         // build the menu bar
107         _jmenubar = new JMenuBar();
108         if ( !_configuration.isUseNativeUI() ) {
109             _jmenubar.setBackground( _configuration.getGuiMenuBackgroundColor() );
110         }
111         buildToolsMenu();
112         buildViewMenu();
113         buildFontSizeMenu();
114         buildOptionsMenu();
115         buildTypeMenu();
116         buildHelpMenu();
117         setJMenuBar( _jmenubar );
118         _contentpane = getContentPane();
119         _contentpane.setLayout( new BorderLayout() );
120         _contentpane.add( _mainpanel, BorderLayout.CENTER );
121         setSize( FRAME_X_SIZE, FRAME_Y_SIZE );
122         addWindowListener( new WindowAdapter() {
123
124             @Override
125             public void windowClosing( final WindowEvent e ) {
126                 close();
127             }
128         } );
129         addComponentListener( new ComponentAdapter() {
130
131             @Override
132             public void componentResized( final ComponentEvent e ) {
133                 if ( _mainpanel.getCurrentTreePanel() != null ) {
134                     _mainpanel.getCurrentTreePanel().setParametersForPainting( _mainpanel.getCurrentTreePanel()
135                                                                                        .getWidth(),
136                                                                                _mainpanel.getCurrentTreePanel()
137                                                                                        .getHeight(),
138                                                                                false );
139                 }
140             }
141         } );
142         setFocusable( true );
143         requestFocus();
144         requestFocusInWindow();
145         setVisible( true );
146         System.gc();
147     }
148
149     void buildOptionsMenu() {
150         _options_jmenu = MainFrame.createMenu( MainFrame.OPTIONS_HEADER, getConfiguration() );
151         _options_jmenu.addChangeListener( new ChangeListener() {
152
153             @Override
154             public void stateChanged( final ChangeEvent e ) {
155                 MainFrame.setOvPlacementColorChooseMenuItem( _overview_placment_mi, getOptions() );
156                 MainFrame.setTextColorChooseMenuItem( _switch_colors_mi, getCurrentTreePanel() );
157                 MainFrame
158                         .setTextMinSupportMenuItem( _choose_minimal_confidence_mi, getOptions(), getCurrentTreePanel() );
159                 MainFrame.setTextForFontChooserMenuItem( _choose_font_mi, createCurrentFontDesc( getMainPanel()
160                         .getTreeFontSet() ) );
161                 MainFrame.updateOptionsMenuDependingOnPhylogenyType( getMainPanel(),
162                                                                      _show_scale_cbmi,
163                                                                      _show_branch_length_values_cbmi,
164                                                                      _non_lined_up_cladograms_rbmi,
165                                                                      _uniform_cladograms_rbmi,
166                                                                      _ext_node_dependent_cladogram_rbmi,
167                                                                      _label_direction_cbmi );
168                 MainFrame.setCycleNodeFillMenuItem( _cycle_node_fill_mi, getOptions() );
169                 MainFrame.setCycleNodeShapeMenuItem( _cycle_node_shape_mi, getOptions() );
170                 MainFrame.setTextNodeSizeMenuItem( _choose_node_size_mi, getOptions() );
171             }
172         } );
173         _options_jmenu.add( MainFrame.customizeMenuItemAsLabel( new JMenuItem( MainFrame.DISPLAY_SUBHEADER ),
174                                                                 getConfiguration() ) );
175         _options_jmenu
176                 .add( _ext_node_dependent_cladogram_rbmi = new JRadioButtonMenuItem( MainFrame.NONUNIFORM_CLADOGRAMS_LABEL ) );
177         _options_jmenu.add( _uniform_cladograms_rbmi = new JRadioButtonMenuItem( MainFrame.UNIFORM_CLADOGRAMS_LABEL ) );
178         _options_jmenu.add( _non_lined_up_cladograms_rbmi = new JRadioButtonMenuItem( NON_LINED_UP_CLADOGRAMS_LABEL ) );
179         _radio_group_1 = new ButtonGroup();
180         _radio_group_1.add( _ext_node_dependent_cladogram_rbmi );
181         _radio_group_1.add( _uniform_cladograms_rbmi );
182         _radio_group_1.add( _non_lined_up_cladograms_rbmi );
183         _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_OVERVIEW_LABEL ) );
184         _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_SCALE_LABEL ) );
185         _options_jmenu
186                 .add( _show_branch_length_values_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_BRANCH_LENGTH_VALUES_LABEL ) );
187         _options_jmenu.add( _show_confidence_stddev_cbmi = new JCheckBoxMenuItem( SHOW_CONF_STDDEV_LABEL ) );
188         _options_jmenu.add( _show_default_node_shapes_cbmi = new JCheckBoxMenuItem( DISPLAY_NODE_BOXES_LABEL ) );
189         _options_jmenu
190                 .add( _taxonomy_colorize_node_shapes_cbmi = new JCheckBoxMenuItem( MainFrame.TAXONOMY_COLORIZE_NODE_SHAPES_LABEL ) );
191         _options_jmenu.add( _cycle_node_shape_mi = new JMenuItem( MainFrame.CYCLE_NODE_SHAPE_LABEL ) );
192         _options_jmenu.add( _cycle_node_fill_mi = new JMenuItem( MainFrame.CYCLE_NODE_FILL_LABEL ) );
193         _options_jmenu.add( _choose_node_size_mi = new JMenuItem( MainFrame.CHOOSE_NODE_SIZE_LABEL ) );
194         _options_jmenu.add( _label_direction_cbmi = new JCheckBoxMenuItem( LABEL_DIRECTION_LABEL ) );
195         _label_direction_cbmi.setToolTipText( LABEL_DIRECTION_TIP );
196         _options_jmenu.add( _color_labels_same_as_parent_branch = new JCheckBoxMenuItem( COLOR_LABELS_LABEL ) );
197         _color_labels_same_as_parent_branch.setToolTipText( MainFrame.COLOR_LABELS_TIP );
198         _options_jmenu.add( _abbreviate_scientific_names = new JCheckBoxMenuItem( MainFrame.ABBREV_SN_LABEL ) );
199         _options_jmenu.add( _screen_antialias_cbmi = new JCheckBoxMenuItem( MainFrame.SCREEN_ANTIALIAS_LABEL ) );
200         _options_jmenu.add( _background_gradient_cbmi = new JCheckBoxMenuItem( MainFrame.BG_GRAD_LABEL ) );
201         if ( getConfiguration().doDisplayOption( Configuration.show_domain_architectures ) ) {
202             _options_jmenu.add( _show_domain_labels = new JCheckBoxMenuItem( SHOW_DOMAIN_LABELS_LABEL ) );
203         }
204         _options_jmenu.add( _choose_minimal_confidence_mi = new JMenuItem( "" ) );
205         _options_jmenu.add( _overview_placment_mi = new JMenuItem( "" ) );
206         _options_jmenu.add( _switch_colors_mi = new JMenuItem( "" ) );
207         _options_jmenu.add( _choose_font_mi = new JMenuItem( "" ) );
208         _options_jmenu.addSeparator();
209         _options_jmenu.add( MainFrame.customizeMenuItemAsLabel( new JMenuItem( MainFrame.SEARCH_SUBHEADER ),
210                                                                 getConfiguration() ) );
211         _options_jmenu
212                 .add( _search_case_senstive_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_CASE_SENSITIVE_LABEL ) );
213         _options_jmenu.add( _search_whole_words_only_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_TERMS_ONLY_LABEL ) );
214         _options_jmenu.add( _inverse_search_result_cbmi = new JCheckBoxMenuItem( INVERSE_SEARCH_RESULT_LABEL ) );
215         customizeJMenuItem( _choose_font_mi );
216         customizeJMenuItem( _switch_colors_mi );
217         customizeJMenuItem( _choose_minimal_confidence_mi );
218         customizeJMenuItem( _overview_placment_mi );
219         customizeCheckBoxMenuItem( _show_default_node_shapes_cbmi, getOptions().isShowDefaultNodeShapes() );
220         customizeCheckBoxMenuItem( _taxonomy_colorize_node_shapes_cbmi, getOptions().isTaxonomyColorizeNodeShapes() );
221         customizeJMenuItem( _cycle_node_shape_mi );
222         customizeJMenuItem( _cycle_node_fill_mi );
223         customizeJMenuItem( _choose_node_size_mi );
224         customizeCheckBoxMenuItem( _color_labels_same_as_parent_branch, getOptions().isColorLabelsSameAsParentBranch() );
225         customizeCheckBoxMenuItem( _screen_antialias_cbmi, getOptions().isAntialiasScreen() );
226         customizeCheckBoxMenuItem( _background_gradient_cbmi, getOptions().isBackgroundColorGradient() );
227         customizeCheckBoxMenuItem( _show_domain_labels, getOptions().isShowDomainLabels() );
228         customizeCheckBoxMenuItem( _abbreviate_scientific_names, getOptions().isAbbreviateScientificTaxonNames() );
229         customizeCheckBoxMenuItem( _search_case_senstive_cbmi, getOptions().isSearchCaseSensitive() );
230         customizeCheckBoxMenuItem( _show_scale_cbmi, getOptions().isShowScale() );
231         customizeRadioButtonMenuItem( _non_lined_up_cladograms_rbmi,
232                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.NON_LINED_UP );
233         customizeRadioButtonMenuItem( _uniform_cladograms_rbmi,
234                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );
235         customizeRadioButtonMenuItem( _ext_node_dependent_cladogram_rbmi,
236                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
237         customizeCheckBoxMenuItem( _show_branch_length_values_cbmi, getOptions().isShowBranchLengthValues() );
238         customizeCheckBoxMenuItem( _show_overview_cbmi, getOptions().isShowOverview() );
239         customizeCheckBoxMenuItem( _label_direction_cbmi,
240                                    getOptions().getNodeLabelDirection() == NODE_LABEL_DIRECTION.RADIAL );
241         customizeCheckBoxMenuItem( _search_whole_words_only_cbmi, getOptions().isMatchWholeTermsOnly() );
242         customizeCheckBoxMenuItem( _inverse_search_result_cbmi, getOptions().isInverseSearchResult() );
243         customizeCheckBoxMenuItem( _show_confidence_stddev_cbmi, getOptions().isShowConfidenceStddev() );
244         _jmenubar.add( _options_jmenu );
245     }
246
247     void buildToolsMenu() {
248         _tools_menu = MainFrame.createMenu( "Tools", getConfiguration() );
249         _tools_menu.add( _confcolor_item = new JMenuItem( "Colorize Branches Depending on Confidence" ) );
250         customizeJMenuItem( _confcolor_item );
251         _tools_menu.add( _taxcolor_item = new JMenuItem( "Taxonomy Colorize Branches" ) );
252         customizeJMenuItem( _taxcolor_item );
253         _tools_menu.add( _remove_branch_color_item = new JMenuItem( "Delete Branch Colors" ) );
254         _remove_branch_color_item.setToolTipText( "To delete branch color values from the current phylogeny." );
255         customizeJMenuItem( _remove_branch_color_item );
256         _tools_menu.addSeparator();
257         _tools_menu.add( _midpoint_root_item = new JMenuItem( "Midpoint-Root" ) );
258         customizeJMenuItem( _midpoint_root_item );
259         _tools_menu.addSeparator();
260         _tools_menu
261                 .add( _infer_common_sn_names_item = new JMenuItem( "Infer Common Parts of Internal Scientific Names" ) );
262         customizeJMenuItem( _infer_common_sn_names_item );
263         _tools_menu.add( _collapse_species_specific_subtrees = new JMenuItem( "Collapse Species-Specific Subtrees" ) );
264         customizeJMenuItem( _collapse_species_specific_subtrees );
265         _jmenubar.add( _tools_menu );
266     }
267
268     JApplet getApplet() {
269         return _applet;
270     }
271
272     @Override
273     MainPanel getMainPanel() {
274         return _mainpanel;
275     }
276
277     @Override
278     void readPhylogeniesFromURL() {
279         throw new NoSuchMethodError( "not implemented" );
280     }
281 }