inprogress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / ArchaeopteryxA.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 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.archaeopteryx;
27
28 import java.awt.Color;
29 import java.awt.Font;
30 import java.awt.Graphics;
31 import java.awt.KeyboardFocusManager;
32 import java.io.File;
33 import java.net.URL;
34
35 import javax.swing.JApplet;
36 import javax.swing.UIManager;
37
38 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
39 import org.forester.phylogeny.Phylogeny;
40 import org.forester.util.ForesterUtil;
41
42 public class ArchaeopteryxA extends JApplet {
43
44     private static final long  serialVersionUID      = 2314899014580484146L;
45     private final static Color background_color      = new Color( 0, 0, 0 );
46     private final static Color font_color            = new Color( 0, 255, 0 );
47     private final static Color ex_background_color   = new Color( 0, 0, 0 );
48     private final static Color ex_font_color         = new Color( 255, 0, 0 );
49     private final static Font  font                  = new Font( Configuration.getDefaultFontFamilyName(), Font.BOLD, 9 );
50     private MainFrameApplet    _mainframe_applet;
51     private String             _tree_url_str         = "";
52     private String             _species_tree_url_str = "";
53     private String             _message_1            = "";
54     private String             _message_2            = "";
55     public final static String NAME                  = "ArchaeopteryxA";
56
57     @Override
58     public void destroy() {
59         AptxUtil.printAppletMessage( NAME, "going to be destroyed" );
60         if ( getMainFrameApplet() != null ) {
61             getMainFrameApplet().close();
62         }
63     }
64
65     /**
66      * This method returns the current external node data which
67      * has been selected by the user by clicking the "Return ..."
68      * menu item. This method is expected to be called from Javascript or
69      * something like it.
70      * 
71      * @return current external node data as String
72      */
73     public String getCurrentExternalNodesDataBuffer() {
74         return getMainFrameApplet().getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString();
75     }
76
77     public int getCurrentExternalNodesDataBufferChangeCounter() {
78         return getMainFrameApplet().getCurrentTreePanel().getCurrentExternalNodesDataBufferChangeCounter();
79     }
80
81     public int getCurrentExternalNodesDataBufferLength() {
82         return getMainFrameApplet().getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString().length();
83     }
84
85     public String getTreeUrlStr() {
86         return _tree_url_str;
87     }
88
89     public String getSpeciesTreeUrlStr() {
90         return _species_tree_url_str;
91     }
92
93     @Override
94     public void init() {
95         boolean has_exception = false;
96         setName( NAME );
97         setTreeUrlStr( getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD ) );
98         setSpeciesTreeUrlStr( getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_SPECIES_TREE_TO_LOAD ) );
99         if ( !ForesterUtil.isEmpty( getTreeUrlStr() ) ) {
100             AptxUtil.printAppletMessage( NAME, "URL of tree(s) to load: \"" + getTreeUrlStr() + "\"" );
101         }
102         else {
103             ForesterUtil.printErrorMessage( NAME, "no URL for tree(s) to load!" );
104             setBackground( ex_background_color );
105             setForeground( ex_font_color );
106             has_exception = true;
107             setMessage1( "no URL for tree(s) to load" );
108             repaint();
109         }
110         if ( !ForesterUtil.isEmpty( getSpeciesTreeUrlStr() ) ) {
111             AptxUtil.printAppletMessage( NAME, "URL of species tree to load: \"" + getSpeciesTreeUrlStr() + "\"" );
112         }
113         setBackground( background_color );
114         setForeground( font_color );
115         setFont( font );
116         repaint();
117         String s = null;
118         try {
119             s = System.getProperty( "java.version" );
120         }
121         catch ( final Exception e ) {
122             ForesterUtil.printWarningMessage( NAME, "minor error: " + e.getLocalizedMessage() );
123         }
124         if ( ( s != null ) && ( s.length() > 0 ) ) {
125             setMessage2( "[Your Java version: " + s + "]" );
126             repaint();
127         }
128         final String config_filename = getParameter( Constants.APPLET_PARAM_NAME_FOR_CONFIG_FILE_URL );
129         AptxUtil.printAppletMessage( NAME, "URL for configuration file is: " + config_filename );
130         final Configuration configuration = new Configuration( config_filename, true, true, true );
131         try {
132             if ( configuration.isUseNativeUI() ) {
133                 UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
134             }
135             else {
136                 UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
137             }
138             setVisible( false );
139             _mainframe_applet = new MainFrameApplet( this, configuration );
140             final URL tree_url = new URL( getTreeUrlStr() );
141             final Phylogeny[] phys = AptxUtil.readPhylogeniesFromUrl( tree_url, configuration
142                     .isValidatePhyloXmlAgainstSchema(), configuration.isReplaceUnderscoresInNhParsing(), configuration
143                     .isInternalNumberAreConfidenceForNhParsing(), configuration.getTaxonomyExtraction() );
144             AptxUtil.addPhylogeniesToTabs( phys,
145                                            new File( tree_url.getFile() ).getName(),
146                                            getTreeUrlStr(),
147                                            getMainFrameApplet().getConfiguration(),
148                                            getMainFrameApplet().getMainPanel() );
149             if ( !ForesterUtil.isEmpty( getSpeciesTreeUrlStr() ) ) {
150                 final URL species_tree_url = new URL( getSpeciesTreeUrlStr() );
151                 final Phylogeny[] species_trees = AptxUtil
152                         .readPhylogeniesFromUrl( species_tree_url,
153                                                  configuration.isValidatePhyloXmlAgainstSchema(),
154                                                  configuration.isReplaceUnderscoresInNhParsing(),
155                                                  false,
156                                                  TAXONOMY_EXTRACTION.NO );
157                 if ( ( species_trees != null ) && ( species_trees.length > 0 ) ) {
158                     AptxUtil.printAppletMessage( NAME, "successfully read species tree" );
159                     getMainFrameApplet().setSpeciesTree( species_trees[ 0 ] );
160                 }
161             }
162             getMainFrameApplet().getMainPanel().getControlPanel().showWholeAll();
163             getMainFrameApplet().getMainPanel().getControlPanel().showWhole();
164             setVisible( true );
165         }
166         catch ( final Exception e ) {
167             ForesterUtil.printErrorMessage( NAME, e.toString() );
168             setBackground( ex_background_color );
169             setForeground( ex_font_color );
170             has_exception = true;
171             setMessage1( "Exception: " + e );
172             e.printStackTrace();
173             repaint();
174         }
175         if ( !has_exception ) {
176             setMessage1( NAME + " is now ready!" );
177             repaint();
178             AptxUtil.printAppletMessage( NAME, "successfully initialized" );
179         }
180         KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
181         getMainFrameApplet().requestFocus();
182         getMainFrameApplet().requestFocusInWindow();
183         getMainFrameApplet().requestFocus();
184         /* GUILHEM_BEG */
185         final String default_relation = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_SEQUENCE_RELATION_TYPE );
186         if ( default_relation != null ) {
187             getMainFrameApplet().getMainPanel().getControlPanel().getSequenceRelationTypeBox()
188                     .setSelectedItem( default_relation );
189         }
190         final String default_sequence = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_QUERY_SEQUENCE );
191         if ( default_sequence != null ) {
192             getMainFrameApplet().getMainPanel().getControlPanel().getSequenceRelationBox()
193                     .setSelectedItem( default_sequence );
194             /* GUILHEM_END */
195         }
196     }
197
198     /**
199      * Prints message when initialization is finished. Called automatically.
200      * 
201      * @param g
202      *            Graphics
203      */
204     @Override
205     public void paint( final Graphics g ) {
206         g.setColor( background_color );
207         g.fillRect( 0, 0, 300, 60 );
208         g.setColor( font_color );
209         g.drawString( getMessage2(), 10, 20 );
210         g.drawString( getMessage1(), 10, 40 );
211     }
212
213     @Override
214     public void start() {
215         getMainFrameApplet().getMainPanel().validate();
216         getMainFrameApplet().requestFocus();
217         getMainFrameApplet().requestFocusInWindow();
218         getMainFrameApplet().requestFocus();
219         AptxUtil.printAppletMessage( NAME, "started" );
220     }
221
222     private MainFrameApplet getMainFrameApplet() {
223         return _mainframe_applet;
224     }
225
226     private String getMessage1() {
227         return _message_1;
228     }
229
230     private String getMessage2() {
231         return _message_2;
232     }
233
234     private void setMessage1( final String message_1 ) {
235         _message_1 = message_1;
236     }
237
238     private void setMessage2( final String message_2 ) {
239         _message_2 = message_2;
240     }
241
242     private void setTreeUrlStr( final String url_string ) {
243         _tree_url_str = url_string;
244     }
245
246     private void setSpeciesTreeUrlStr( final String url_string ) {
247         _species_tree_url_str = url_string;
248     }
249 }