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