+ @Override
+ public void destroy() {
+ AptxUtil.printAppletMessage( NAME, "going to be destroyed " );
+ removeAllTextFrames();
+ if ( getMainPanel() != null ) {
+ getMainPanel().terminate();
+ }
+ }
+
+ /**
+ * This method returns the current external node data which
+ * has been selected by the user by clicking the "Return ..."
+ * menu item. This method is expected to be called from Javascript or
+ * something like it.
+ *
+ * @return current external node data as String
+ */
+ public String getCurrentExternalNodesDataBuffer() {
+ return getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString();
+ }
+
+ public int getCurrentExternalNodesDataBufferChangeCounter() {
+ return getCurrentTreePanel().getCurrentExternalNodesDataBufferChangeCounter();
+ }
+
+ public int getCurrentExternalNodesDataBufferLength() {
+ return getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString().length();
+ }
+
+ /**
+ * This method returns the current phylogeny as a string in the chosen format
+ *
+ * @param format must be NH, NHX, NEXUS or PHYLOXML
+ * @return the phylogeny string
+ * @author Herve Menager
+ */
+ public String getCurrentPhylogeny( final String format ) {
+ removeAllTextFrames();
+ if ( ( getMainPanel().getCurrentPhylogeny() == null ) || getMainPanel().getCurrentPhylogeny().isEmpty()
+ || ( getMainPanel().getCurrentPhylogeny().getNumberOfExternalNodes() > 10000 ) ) {
+ return new String();
+ }
+ switch ( ForesterConstants.PhylogeneticTreeFormats.valueOf( format ) ) {
+ case NH:
+ return getMainPanel().getCurrentPhylogeny().toNewHampshire();
+ case NHX:
+ return getMainPanel().getCurrentPhylogeny().toNewHampshireX();
+ case NEXUS:
+ return getMainPanel().getCurrentPhylogeny().toNexus();
+ case PHYLOXML:
+ return getMainPanel().getCurrentPhylogeny().toPhyloXML( -1 );
+ default:
+ break;
+ }
+ return new String();
+ }
+
+ /**
+ * This method returns a view of the current phylogeny in a chosen
+ * graphics format, base64-encoded in a string so that in can be used
+ * from javascript.
+ *
+ * @param format must be GraphicsExportType (gif, jpg, pdf, png, tif, bmp)
+ * @return the phylogeny string
+ * @author Herve Menager
+ */
+ public String getCurrentPhylogenyGraphicsAsBase64EncodedString( final String format ) {
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try {
+ AptxUtil.writePhylogenyToGraphicsByteArrayOutputStream( baos,
+ _mainpanel.getWidth(),
+ _mainpanel.getHeight(),
+ getCurrentTreePanel(),
+ getCurrentTreePanel().getControlPanel(),
+ GraphicsExportType.valueOf( format ),
+ getOptions() );
+ }
+ catch ( final IOException ioe ) {
+ ForesterUtil.printErrorMessage( NAME, ioe.toString() );
+ ioe.printStackTrace();
+ JOptionPane.showMessageDialog( this,
+ NAME + ": Failed to generate graphics: " + "\nException: " + ioe,
+ "Failed to generate graphics",
+ JOptionPane.ERROR_MESSAGE );
+ return null;
+ }
+ final byte[] bytes = baos.toByteArray();
+ final String dataImg = Base64.encodeBase64String( bytes );
+ return dataImg;
+ }
+
+ public Options getOptions() {
+ return _options;
+ }
+
+ @Override
+ public void init() {
+ final String config_filename = getParameter( Constants.APPLET_PARAM_NAME_FOR_CONFIG_FILE_URL );
+ AptxUtil.printAppletMessage( NAME, "URL for configuration file is: " + config_filename );
+ final Configuration configuration = new Configuration( config_filename, true, true, true );
+ setConfiguration( configuration );
+ setOptions( Options.createInstance( configuration ) );
+ setupUI();
+ final String tree_url_str = getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD );
+ if ( ForesterUtil.isEmpty( tree_url_str ) ) {
+ ForesterUtil.printErrorMessage( NAME, "could not get tree URL from "
+ + Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD );
+ JOptionPane.showMessageDialog( this, NAME + ": could not get tree URL from "
+ + Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD, "Failed get URL", JOptionPane.ERROR_MESSAGE );
+ return;
+ }
+ AptxUtil.printAppletMessage( NAME, "URL for phylogenies is " + tree_url_str );
+ // Get URL to tree file
+ URL phys_url = null;
+ try {
+ phys_url = new URL( tree_url_str );
+ }
+ catch ( final Exception e ) {
+ ForesterUtil.printErrorMessage( NAME, "error: " + e );
+ e.printStackTrace();
+ JOptionPane.showMessageDialog( this, NAME + ": Could not create URL from: \"" + tree_url_str
+ + "\"\nException: " + e, "Failed to create URL", JOptionPane.ERROR_MESSAGE );
+ }
+ if ( phys_url == null ) {
+ ForesterUtil.printErrorMessage( NAME, "failed to get tree URL from "
+ + Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD );
+ JOptionPane.showMessageDialog( this,
+ NAME + ": Could not create URL from: \"" + tree_url_str,
+ "Failed to create URL",
+ JOptionPane.ERROR_MESSAGE );
+ return;
+ }
+ // Load the tree from URL
+ Phylogeny[] phys = null;
+ try {
+ phys = AptxUtil.readPhylogeniesFromUrl( phys_url,
+ getConfiguration().isValidatePhyloXmlAgainstSchema(),
+ getConfiguration().isReplaceUnderscoresInNhParsing(),
+ getConfiguration().isInternalNumberAreConfidenceForNhParsing(),
+ getConfiguration().getTaxonomyExtraction(),
+ getConfiguration().isMidpointReroot() );
+ }
+ catch ( final Exception e ) {
+ ForesterUtil.printErrorMessage( NAME, e.toString() );
+ e.printStackTrace();
+ JOptionPane.showMessageDialog( this,
+ NAME + ": Failed to read phylogenies: " + "\nException: " + e,
+ "Failed to read phylogenies",
+ JOptionPane.ERROR_MESSAGE );
+ }
+ if ( phys == null ) {
+ ForesterUtil.printErrorMessage( NAME, "phylogenies from [" + phys_url + "] are null" );
+ JOptionPane.showMessageDialog( this,
+ NAME + ": phylogenies from [" + phys_url + "] are null",
+ "Failed to read phylogenies",
+ JOptionPane.ERROR_MESSAGE );
+ return;
+ }
+ else if ( phys.length < 1 ) {
+ ForesterUtil.printErrorMessage( NAME, "phylogenies from [" + phys_url + "] are empty" );
+ JOptionPane.showMessageDialog( this,
+ NAME + ": phylogenies from [" + phys_url + "] are empty",
+ "Failed to read phylogenies",
+ JOptionPane.ERROR_MESSAGE );
+ return;
+ }
+ else {
+ AptxUtil.printAppletMessage( NAME, "loaded " + phys.length + " phylogenies from: " + phys_url );
+ }
+ //
+ final String species_tree_url_str = getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_SPECIES_TREE_TO_LOAD );
+ if ( !ForesterUtil.isEmpty( species_tree_url_str ) ) {
+ AptxUtil.printAppletMessage( NAME, "URL of species tree to load: \"" + species_tree_url_str + "\"" );
+ Phylogeny[] species_trees = null;
+ try {
+ final URL species_tree_url = new URL( species_tree_url_str );
+ species_trees = AptxUtil.readPhylogeniesFromUrl( species_tree_url,
+ configuration.isValidatePhyloXmlAgainstSchema(),
+ configuration.isReplaceUnderscoresInNhParsing(),
+ false,
+ TAXONOMY_EXTRACTION.NO,
+ false );
+ }
+ catch ( final IOException e ) {
+ ForesterUtil.printErrorMessage( NAME, "could not read species tree from [" + species_tree_url_str
+ + "]" );
+ JOptionPane.showMessageDialog( this, NAME + ": could not read species tree from ["
+ + species_tree_url_str + "]", "Failed to read species tree", JOptionPane.ERROR_MESSAGE );
+ }
+ if ( ( species_trees != null ) && ( species_trees.length > 0 ) ) {
+ AptxUtil.printAppletMessage( NAME, "successfully read species tree" );
+ if ( species_trees[ 0 ].isEmpty() ) {
+ ForesterUtil.printErrorMessage( NAME, "species tree is empty" );
+ }
+ else if ( !species_trees[ 0 ].isRooted() ) {
+ ForesterUtil.printErrorMessage( NAME, "species tree is not rooted" );
+ }
+ else {
+ setSpeciesTree( species_trees[ 0 ] );
+ AptxUtil.printAppletMessage( NAME, "species tree OK" );
+ }
+ }
+ }
+ //
+ try {
+ setVisible( false );
+ setMainPanel( new MainPanelApplets( getConfiguration(), this ) );
+ _jmenubar = new JMenuBar();
+ if ( !getConfiguration().isHideControlPanelAndMenubar() ) {
+ buildFileMenu();
+ if ( !getConfiguration().isUseNativeUI() ) {
+ _jmenubar.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
+ }
+ if ( getSpeciesTree() != null ) {
+ buildAnalysisMenu();
+ }
+ buildToolsMenu();
+ buildViewMenu();
+ buildFontSizeMenu();
+ buildOptionsMenu();
+ buildTypeMenu();
+ buildHelpMenu();
+ setJMenuBar( _jmenubar );
+ }
+ final Container contentpane = getContentPane();
+ contentpane.setLayout( new BorderLayout() );
+ contentpane.add( getMainPanel(), BorderLayout.CENTER );
+ addComponentListener( new ComponentAdapter() {
+
+ @Override
+ public void componentResized( final ComponentEvent e ) {
+ if ( getMainPanel().getCurrentTreePanel() != null ) {
+ getMainPanel().getCurrentTreePanel().calcParametersForPainting( getMainPanel()
+ .getCurrentTreePanel()
+ .getWidth(),
+ getMainPanel()
+ .getCurrentTreePanel()
+ .getHeight() );
+ }
+ }
+ } );
+ if ( getConfiguration().isUseTabbedDisplay() ) {
+ try {
+ AptxUtil.printAppletMessage( NAME, "using tabbed display" );
+ AptxUtil.addPhylogeniesToTabs( phys,
+ new File( phys_url.getFile() ).getName(),
+ phys_url.toString(),
+ getConfiguration(),
+ getMainPanel() );
+ }
+ catch ( final Exception e ) {
+ ForesterUtil.printErrorMessage( NAME, e.toString() );
+ e.printStackTrace();
+ }
+ }
+ else {
+ AptxUtil.printAppletMessage( NAME, "not using tabbed display" );
+ if ( getSpeciesTree() != null ) {
+ AptxUtil.printAppletMessage( NAME,
+ "Warning: gsdi (gene duplication inference) only available tabbed display" );
+ }
+ AptxUtil.addPhylogenyToPanel( phys, getConfiguration(), getMainPanel() );
+ }
+ validate();
+ setName( NAME );
+ getMainPanel().getControlPanel().showWholeAll();
+ getMainPanel().getControlPanel().showWhole();
+ /* GUILHEM_BEG */
+ getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().removeAllItems();
+ for( final SequenceRelation.SEQUENCE_RELATION_TYPE type : getMainPanel().getCurrentPhylogeny()
+ .getRelevantSequenceRelationTypes() ) {
+ getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().addItem( type );
+ }
+ final String default_relation = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_SEQUENCE_RELATION_TYPE );
+ if ( default_relation != null ) {
+ getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().setSelectedItem( default_relation );
+ }
+ final String default_sequence = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_QUERY_SEQUENCE );
+ if ( default_sequence != null ) {
+ getCurrentTreePanel().getControlPanel().getSequenceRelationBox().setSelectedItem( default_sequence );
+ }
+ /* GUILHEM_END */
+ System.gc();
+ AptxUtil.printAppletMessage( NAME, "successfully initialized" );
+ setVisible( true );
+ }
+ catch ( final Exception e ) {
+ ForesterUtil.printErrorMessage( NAME, e.toString() );
+ e.printStackTrace();
+ }
+ }
+
+ public void showTextFrame( final String s, final String title ) {
+ checkTextFrames();
+ _textframes.addLast( TextFrame.instantiate( s, title, _textframes ) );
+ }
+
+ @Override
+ public void start() {
+ if ( getMainPanel() != null ) {
+ getMainPanel().validate();
+ }
+ requestFocus();
+ requestFocusInWindow();
+ requestFocus();
+ AptxUtil.printAppletMessage( NAME, "started" );
+ }
+
+ void buildAnalysisMenu() {
+ _analysis_menu = MainFrame.createMenu( "Analysis", getConfiguration() );
+ _analysis_menu.add( _gsdi_item = new JMenuItem( "GSDI (Generalized Speciation Duplication Inference)" ) );
+ _analysis_menu.add( _gsdir_item = new JMenuItem( "GSDIR (GSDI with re-rooting)" ) );
+ customizeJMenuItem( _gsdi_item );
+ customizeJMenuItem( _gsdir_item );
+ // _analysis_menu.addSeparator();
+ // _analysis_menu.add( _lineage_inference = new JMenuItem( INFER_ANCESTOR_TAXONOMIES ) );
+ // customizeJMenuItem( _lineage_inference );
+ // _lineage_inference.setToolTipText( "Inference of ancestor taxonomies/lineages" );
+ _jmenubar.add( _analysis_menu );
+ }
+