clean up
[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         Util.printAppletMessage( NAME, "going to be destroyed" );
58         if ( getMainFrameApplet() != null ) {
59             getMainFrameApplet().close();
60         }
61     }
62
63     private MainFrameApplet getMainFrameApplet() {
64         return _mainframe_applet;
65     }
66
67     private String getMessage1() {
68         return _message_1;
69     }
70
71     private String getMessage2() {
72         return _message_2;
73     }
74
75     public String getUrlString() {
76         return _url_string;
77     }
78
79     @Override
80     public void init() {
81         boolean has_exception = false;
82         setName( NAME );
83         setUrlString( getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD ) );
84         Util.printAppletMessage( NAME, "URL of phylogenies to load: \"" + getUrlString() + "\"" );
85         setBackground( background_color );
86         setForeground( font_color );
87         setFont( font );
88         repaint();
89         String s = null;
90         try {
91             s = System.getProperty( "java.version" );
92         }
93         catch ( final Exception e ) {
94             ForesterUtil.printWarningMessage( NAME, "minor error: " + e.getLocalizedMessage() );
95         }
96         if ( ( s != null ) && ( s.length() > 0 ) ) {
97             setMessage2( "[Your Java version: " + s + "]" );
98             repaint();
99         }
100         final String config_filename = getParameter( Constants.APPLET_PARAM_NAME_FOR_CONFIG_FILE_URL );
101         Util.printAppletMessage( NAME, "URL for configuration file is: " + config_filename );
102         final Configuration configuration = new Configuration( config_filename, true, true );
103         try {
104             if ( configuration.isUseNativeUI() ) {
105                 UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
106             }
107             else {
108                 UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
109             }
110             setVisible( false );
111             _mainframe_applet = new MainFrameApplet( this, configuration );
112             URL url = null;
113             url = new URL( getUrlString() );
114             final Phylogeny[] phys = Util.readPhylogeniesFromUrl( url, configuration.isValidatePhyloXmlAgainstSchema() );
115             Util.addPhylogeniesToTabs( phys, new File( url.getFile() ).getName(), getUrlString(), getMainFrameApplet()
116                     .getConfiguration(), getMainFrameApplet().getMainPanel() );
117             getMainFrameApplet().getMainPanel().getControlPanel().showWholeAll();
118             getMainFrameApplet().getMainPanel().getControlPanel().showWhole();
119             setVisible( true );
120         }
121         catch ( final Exception e ) {
122             ForesterUtil.printErrorMessage( NAME, e.toString() );
123             setBackground( ex_background_color );
124             setForeground( ex_font_color );
125             has_exception = true;
126             setMessage1( "Exception: " + e );
127             e.printStackTrace();
128             repaint();
129         }
130         if ( !has_exception ) {
131             setMessage1( NAME + " is now ready!" );
132             repaint();
133             Util.printAppletMessage( NAME, "successfully initialized" );
134         }
135         KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
136         getMainFrameApplet().requestFocus();
137         getMainFrameApplet().requestFocusInWindow();
138         getMainFrameApplet().requestFocus();
139         /* GUILHEM_BEG */
140         final String default_relation = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_SEQUENCE_RELATION_TYPE );
141         if ( default_relation != null ) {
142             getMainFrameApplet().getMainPanel().getControlPanel().getSequenceRelationTypeBox()
143                     .setSelectedItem( default_relation );
144         }
145         final String default_sequence = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_QUERY_SEQUENCE );
146         if ( default_sequence != null ) {
147             getMainFrameApplet().getMainPanel().getControlPanel().getSequenceRelationBox()
148                     .setSelectedItem( default_sequence );
149             /* GUILHEM_END */
150         }
151     }
152
153     /**
154      * Prints message when initialization is finished. Called automatically.
155      * 
156      * @param g
157      *            Graphics
158      */
159     @Override
160     public void paint( final Graphics g ) {
161         g.setColor( background_color );
162         g.fillRect( 0, 0, 300, 60 );
163         g.setColor( font_color );
164         g.drawString( getMessage2(), 10, 20 );
165         g.drawString( getMessage1(), 10, 40 );
166     }
167
168     private void setMessage1( final String message_1 ) {
169         _message_1 = message_1;
170     }
171
172     private void setMessage2( final String message_2 ) {
173         _message_2 = message_2;
174     }
175
176     private void setUrlString( final String url_string ) {
177         _url_string = url_string;
178     }
179
180     @Override
181     public void start() {
182         getMainFrameApplet().getMainPanel().validate();
183         getMainFrameApplet().requestFocus();
184         getMainFrameApplet().requestFocusInWindow();
185         getMainFrameApplet().requestFocus();
186         Util.printAppletMessage( NAME, "started" );
187     }
188 }