work on: add ability to perform GSDI in applets
[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.phylogeny.Phylogeny;
39 import org.forester.util.ForesterUtil;
40
41 public class ArchaeopteryxA extends JApplet {
42
43     private static final long  serialVersionUID      = 2314899014580484146L;
44     private final static Color background_color      = new Color( 0, 0, 0 );
45     private final static Color font_color            = new Color( 0, 255, 0 );
46     private final static Color ex_background_color   = new Color( 0, 0, 0 );
47     private final static Color ex_font_color         = new Color( 255, 0, 0 );
48     private final static Font  font                  = new Font( Configuration.getDefaultFontFamilyName(), Font.BOLD, 9 );
49     private MainFrameApplet    _mainframe_applet;
50     private String             _tree_url_str         = "";
51     private String             _species_tree_url_str = "";
52     private String             _message_1            = "";
53     private String             _message_2            = "";
54     public final static String NAME                  = "ArchaeopteryxA";
55
56     @Override
57     public void destroy() {
58         AptxUtil.printAppletMessage( NAME, "going to be destroyed" );
59         if ( getMainFrameApplet() != null ) {
60             getMainFrameApplet().close();
61         }
62     }
63
64     /**
65      * This method returns the current external node data which
66      * has been selected by the user by clicking the "Return ..."
67      * menu item. This method is expected to be called from Javascript or
68      * something like it.
69      * 
70      * @return current external node data as String
71      */
72     public String getCurrentExternalNodesDataBuffer() {
73         return getMainFrameApplet().getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString();
74     }
75
76     public int getCurrentExternalNodesDataBufferChangeCounter() {
77         return getMainFrameApplet().getCurrentTreePanel().getCurrentExternalNodesDataBufferChangeCounter();
78     }
79
80     public int getCurrentExternalNodesDataBufferLength() {
81         return getMainFrameApplet().getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString().length();
82     }
83
84     public String getSpeciesTreeUrlStr() {
85         return _species_tree_url_str;
86     }
87
88     public String getTreeUrlStr() {
89         return _tree_url_str;
90     }
91
92     @Override
93     public void init() {
94         boolean has_exception = false;
95         setName( NAME );
96         setTreeUrlStr( getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD ) );
97         setSpeciesTreeUrlStr( getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_SPECIES_TREE_TO_LOAD ) );
98         if ( !ForesterUtil.isEmpty( getTreeUrlStr() ) ) {
99             AptxUtil.printAppletMessage( NAME, "URL of tree(s) to load: " + getTreeUrlStr() );
100         }
101         else {
102             ForesterUtil.printErrorMessage( NAME, "no URL for tree(s) to load!" );
103             setBackground( ex_background_color );
104             setForeground( ex_font_color );
105             has_exception = true;
106             setMessage1( "no URL for tree(s) to load" );
107             repaint();
108         }
109         if ( !ForesterUtil.isEmpty( getSpeciesTreeUrlStr() ) ) {
110             AptxUtil.printAppletMessage( NAME, "URL of species tree to load: " + getSpeciesTreeUrlStr() );
111         }
112         setBackground( background_color );
113         setForeground( font_color );
114         setFont( font );
115         repaint();
116         String s = null;
117         try {
118             s = System.getProperty( "java.version" );
119         }
120         catch ( final Exception e ) {
121             ForesterUtil.printWarningMessage( NAME, "minor error: " + e.getLocalizedMessage() );
122         }
123         if ( ( s != null ) && ( s.length() > 0 ) ) {
124             setMessage2( "[Your Java version: " + s + "]" );
125             repaint();
126         }
127         final String config_filename = getParameter( Constants.APPLET_PARAM_NAME_FOR_CONFIG_FILE_URL );
128         AptxUtil.printAppletMessage( NAME, "URL for configuration file is: " + config_filename );
129         final Configuration configuration = new Configuration( config_filename, true, true, true );
130         try {
131             if ( configuration.isUseNativeUI() ) {
132                 UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
133             }
134             else {
135                 UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
136             }
137             setVisible( false );
138             _mainframe_applet = new MainFrameApplet( this, configuration, getSpeciesTreeUrlStr() );
139             final URL tree_url = new URL( getTreeUrlStr() );
140             final Phylogeny[] phys = AptxUtil.readPhylogeniesFromUrl( tree_url, configuration
141                     .isValidatePhyloXmlAgainstSchema(), configuration.isReplaceUnderscoresInNhParsing(), configuration
142                     .isInternalNumberAreConfidenceForNhParsing(), configuration.getTaxonomyExtraction(), configuration
143                     .isMidpointReroot() );
144             AptxUtil.printAppletMessage( ArchaeopteryxA.NAME, "loaded " + phys.length + " phylogenies from: "
145                     + tree_url );
146             AptxUtil.addPhylogeniesToTabs( phys,
147                                            new File( tree_url.getFile() ).getName(),
148                                            getTreeUrlStr(),
149                                            getMainFrameApplet().getConfiguration(),
150                                            getMainFrameApplet().getMainPanel() );
151             getMainFrameApplet().getMainPanel().getControlPanel().showWholeAll();
152             getMainFrameApplet().getMainPanel().getControlPanel().showWhole();
153             setVisible( true );
154         }
155         catch ( final Exception e ) {
156             ForesterUtil.printErrorMessage( NAME, e.toString() );
157             setBackground( ex_background_color );
158             setForeground( ex_font_color );
159             has_exception = true;
160             setMessage1( "Exception: " + e );
161             e.printStackTrace();
162             repaint();
163         }
164         if ( !has_exception ) {
165             setMessage1( NAME + " is now ready!" );
166             repaint();
167             AptxUtil.printAppletMessage( NAME, "successfully initialized" );
168         }
169         KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
170         getMainFrameApplet().requestFocus();
171         getMainFrameApplet().requestFocusInWindow();
172         getMainFrameApplet().requestFocus();
173         /* GUILHEM_BEG */
174         final String default_relation = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_SEQUENCE_RELATION_TYPE );
175         if ( default_relation != null ) {
176             getMainFrameApplet().getMainPanel().getControlPanel().getSequenceRelationTypeBox()
177                     .setSelectedItem( default_relation );
178         }
179         final String default_sequence = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_QUERY_SEQUENCE );
180         if ( default_sequence != null ) {
181             getMainFrameApplet().getMainPanel().getControlPanel().getSequenceRelationBox()
182                     .setSelectedItem( default_sequence );
183         }
184         /* GUILHEM_END */
185     }
186
187     /**
188      * Prints message when initialization is finished. Called automatically.
189      * 
190      * @param g
191      *            Graphics
192      */
193     @Override
194     public void paint( final Graphics g ) {
195         g.setColor( background_color );
196         g.fillRect( 0, 0, 300, 60 );
197         g.setColor( font_color );
198         g.drawString( getMessage2(), 10, 20 );
199         g.drawString( getMessage1(), 10, 40 );
200     }
201
202     @Override
203     public void start() {
204         getMainFrameApplet().getMainPanel().validate();
205         getMainFrameApplet().requestFocus();
206         getMainFrameApplet().requestFocusInWindow();
207         getMainFrameApplet().requestFocus();
208         AptxUtil.printAppletMessage( NAME, "started" );
209     }
210
211     private MainFrameApplet getMainFrameApplet() {
212         return _mainframe_applet;
213     }
214
215     private String getMessage1() {
216         return _message_1;
217     }
218
219     private String getMessage2() {
220         return _message_2;
221     }
222
223     private void setMessage1( final String message_1 ) {
224         _message_1 = message_1;
225     }
226
227     private void setMessage2( final String message_2 ) {
228         _message_2 = message_2;
229     }
230
231     private void setSpeciesTreeUrlStr( final String url_string ) {
232         _species_tree_url_str = url_string;
233     }
234
235     private void setTreeUrlStr( final String url_string ) {
236         _tree_url_str = url_string;
237     }
238 }