in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / ArchaeopteryxE.java
index ffac851..22ade3b 100644 (file)
@@ -3,6 +3,7 @@ package org.forester.archaeopteryx;
 
 import java.awt.BorderLayout;
 import java.awt.Container;
+import java.awt.Font;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ComponentAdapter;
@@ -18,6 +19,7 @@ import java.util.NoSuchElementException;
 import javax.swing.ButtonGroup;
 import javax.swing.JApplet;
 import javax.swing.JCheckBoxMenuItem;
+import javax.swing.JFileChooser;
 import javax.swing.JMenu;
 import javax.swing.JMenuBar;
 import javax.swing.JMenuItem;
@@ -33,6 +35,8 @@ import org.forester.archaeopteryx.AptxUtil.GraphicsExportType;
 import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
 import org.forester.archaeopteryx.Options.NODE_LABEL_DIRECTION;
 import org.forester.archaeopteryx.Options.PHYLOGENY_GRAPHICS_TYPE;
+import org.forester.archaeopteryx.webservices.PhylogeniesWebserviceClient;
+import org.forester.archaeopteryx.webservices.WebservicesManager;
 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
@@ -116,7 +120,6 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
     private JCheckBoxMenuItem           _search_whole_words_only_cbmi;
     private JCheckBoxMenuItem           _inverse_search_result_cbmi;
     private JCheckBoxMenuItem           _search_with_regex_cbmi;
-    
     private JCheckBoxMenuItem           _show_overview_cbmi;
     private JMenuItem                   _choose_minimal_confidence_mi;
     private JMenuItem                   _collapse_species_specific_subtrees;
@@ -136,7 +139,93 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
     private Phylogeny                   _species_tree;
     private JCheckBoxMenuItem           _right_line_up_domains_cbmi;
     private JCheckBoxMenuItem           _line_up_renderable_data_cbmi;
+    // file menu:
+    JMenuItem                        _open_item;
+    JMenuItem                        _open_url_item;
+    JMenuItem                        _save_item;
+    JMenuItem                        _save_all_item;
+    JMenuItem                        _close_item;
+    JMenuItem                        _exit_item;
+    JMenuItem                        _new_item;
+    JMenuItem                        _print_item;
+    JMenuItem                        _write_to_pdf_item;
+    JMenuItem                        _write_to_jpg_item;
+    JMenuItem                        _write_to_gif_item;
+    JMenuItem                        _write_to_tif_item;
+    JMenuItem                        _write_to_png_item;
+    JMenuItem                        _write_to_bmp_item;
+    JMenu                            _file_jmenu;
+
+    
+    void writePhylogenyToGraphicsFile( final String file_name, final GraphicsExportType type ) {
+        _mainpanel.getCurrentTreePanel().calcParametersForPainting( _mainpanel.getCurrentTreePanel().getWidth(),
+                                                                    _mainpanel.getCurrentTreePanel().getHeight() );
+        String file_written_to = "";
+        boolean error = false;
+        try {
+            file_written_to = AptxUtil.writePhylogenyToGraphicsFile( file_name,
+                                                                     _mainpanel.getCurrentTreePanel().getWidth(),
+                                                                     _mainpanel.getCurrentTreePanel().getHeight(),
+                                                                     _mainpanel.getCurrentTreePanel(),
+                                                                     _mainpanel.getControlPanel(),
+                                                                     type,
+                                                                     getOptions() );
+        }
+        catch ( final IOException e ) {
+            error = true;
+            JOptionPane.showMessageDialog( this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
+        }
+        if ( !error ) {
+            if ( ( file_written_to != null ) && ( file_written_to.length() > 0 ) ) {
+                JOptionPane.showMessageDialog( this,
+                                               "Wrote image to: " + file_written_to,
+                                               "Graphics Export",
+                                               JOptionPane.INFORMATION_MESSAGE );
+            }
+            else {
+                JOptionPane.showMessageDialog( this,
+                                               "There was an unknown problem when attempting to write to an image file: \""
+                                                       + file_name + "\"",
+                                               "Error",
+                                               JOptionPane.ERROR_MESSAGE );
+            }
+        }
+        _contentpane.repaint();
+    }
 
+  
+    void buildFileMenu() {
+        _file_jmenu = MainFrame.createMenu( "File", getConfiguration() );
+        _file_jmenu.add( _save_item = new JMenuItem( "Save Tree As..." ) );
+        _file_jmenu.addSeparator();
+        _file_jmenu.add( _write_to_pdf_item = new JMenuItem( "Export to PDF file ..." ) );
+        if ( AptxUtil.canWriteFormat( "tif" ) || AptxUtil.canWriteFormat( "tiff" ) || AptxUtil.canWriteFormat( "TIF" ) ) {
+            _file_jmenu.add( _write_to_tif_item = new JMenuItem( "Export to TIFF file..." ) );
+        }
+        _file_jmenu.add( _write_to_png_item = new JMenuItem( "Export to PNG file..." ) );
+        _file_jmenu.add( _write_to_jpg_item = new JMenuItem( "Export to JPG file..." ) );
+        if ( AptxUtil.canWriteFormat( "gif" ) ) {
+            _file_jmenu.add( _write_to_gif_item = new JMenuItem( "Export to GIF file..." ) );
+        }
+        if ( AptxUtil.canWriteFormat( "bmp" ) ) {
+            _file_jmenu.add( _write_to_bmp_item = new JMenuItem( "Export to BMP file..." ) );
+        }
+        _file_jmenu.addSeparator();
+        _file_jmenu.add( _print_item = new JMenuItem( "Print..." ) );
+        _file_jmenu.addSeparator();
+        _file_jmenu.add( _exit_item = new JMenuItem( "Exit" ) );
+        customizeJMenuItem( _save_item );
+        customizeJMenuItem( _write_to_pdf_item );
+        customizeJMenuItem( _write_to_png_item );
+        customizeJMenuItem( _write_to_jpg_item );
+        customizeJMenuItem( _write_to_gif_item );
+        customizeJMenuItem( _write_to_tif_item );
+        customizeJMenuItem( _write_to_bmp_item );
+        customizeJMenuItem( _print_item );
+        customizeJMenuItem( _exit_item );
+        _jmenubar.add( _file_jmenu );
+    }
+    
     @Override
     public void actionPerformed( final ActionEvent e ) {
         final Object o = e.getSource();
@@ -230,10 +319,10 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             MainFrame.cycleOverview( getOptions(), getCurrentTreePanel() );
         }
         else if ( o == _cycle_node_fill_mi ) {
-            MainFrame.cycleNodeFill( getOptions(), getCurrentTreePanel() );
+            MainFrame.cycleNodeFill( getOptions() );
         }
         else if ( o == _cycle_node_shape_mi ) {
-            MainFrame.cycleNodeShape( getOptions(), getCurrentTreePanel() );
+            MainFrame.cycleNodeShape( getOptions() );
         }
         else if ( o == _non_lined_up_cladograms_rbmi ) {
             updateOptions( getOptions() );
@@ -253,6 +342,9 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             getMainPanel().getControlPanel().search1();
         }
         else if ( o == _search_whole_words_only_cbmi ) {
+            if ( ( _search_with_regex_cbmi != null ) && _search_whole_words_only_cbmi.isSelected() ) {
+                _search_with_regex_cbmi.setSelected( false );
+            }
             updateOptions( getOptions() );
             getMainPanel().getControlPanel().search0();
             getMainPanel().getControlPanel().search1();
@@ -263,6 +355,12 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             getMainPanel().getControlPanel().search1();
         }
         else if ( o == _search_with_regex_cbmi ) {
+            if ( ( _search_whole_words_only_cbmi != null ) && _search_with_regex_cbmi.isSelected() ) {
+                _search_whole_words_only_cbmi.setSelected( false );
+            }
+            if ( ( _search_case_senstive_cbmi != null ) && _search_with_regex_cbmi.isSelected() ) {
+                _search_case_senstive_cbmi.setSelected( true );
+            }
             updateOptions( getOptions() );
             getMainPanel().getControlPanel().search0();
             getMainPanel().getControlPanel().search1();
@@ -367,6 +465,59 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             }
             updateOptions( getOptions() );
         }
+        //
+        else if ( o == _write_to_pdf_item ) {
+            writeToPdf( _mainpanel.getCurrentPhylogeny() );
+        }
+        
+        else if ( o == _write_to_jpg_item ) {
+            writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.JPG );
+        }
+        else if ( o == _write_to_gif_item ) {
+            writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.GIF );
+        }
+        else if ( o == _write_to_tif_item ) {
+            writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.TIFF );
+        }
+        else if ( o == _write_to_bmp_item ) {
+            writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.BMP );
+        }
+        else if ( o == _write_to_png_item ) {
+            writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.PNG );
+        }
+        else if ( o == _print_item ) {
+            MainFrame.print( getCurrentTreePanel(),
+                             getOptions(),
+                             
+                             this );
+                  
+        }
+        else if ( o == _save_item ) {
+            writeToFile( _mainpanel.getCurrentPhylogeny() );
+        }
+       
+//        else if ( o == _graphics_export_visible_only_cbmi ) {
+//            updateOptions( getOptions() );
+//        }
+//        else if ( o == _antialias_print_cbmi ) {
+//            updateOptions( getOptions() );
+//        }
+//        else if ( o == _print_black_and_white_cbmi ) {
+//            updateOptions( getOptions() );
+//        }
+//        else if ( o == _print_using_actual_size_cbmi ) {
+//            updateOptions( getOptions() );
+//        }
+//        else if ( o == _graphics_export_using_actual_size_cbmi ) {
+//            updateOptions( getOptions() );
+//        }
+//        else if ( o == _print_size_mi ) {
+//            choosePrintSize();
+//        }
+//        else if ( o == _choose_pdf_width_mi ) {
+//            choosePdfWidth();
+//        }
+        
         repaint();
     }
 
@@ -384,7 +535,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
      * 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() {
@@ -401,7 +552,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
 
     /**
      * 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
@@ -428,10 +579,10 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
     }
 
     /**
-     * This method returns a view of the current phylogeny in a chosen 
+     * 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
@@ -579,6 +730,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             setMainPanel( new MainPanelApplets( getConfiguration(), this ) );
             _jmenubar = new JMenuBar();
             if ( !getConfiguration().isHideControlPanelAndMenubar() ) {
+                buildFileMenu();
                 if ( !getConfiguration().isUseNativeUI() ) {
                     _jmenubar.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
                 }
@@ -601,10 +753,12 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
                 @Override
                 public void componentResized( final ComponentEvent e ) {
                     if ( getMainPanel().getCurrentTreePanel() != null ) {
-                        getMainPanel().getCurrentTreePanel()
-                                .calcParametersForPainting( getMainPanel().getCurrentTreePanel().getWidth(),
-                                                            getMainPanel().getCurrentTreePanel().getHeight(),
-                                                            getOptions().isAllowFontSizeChange() );
+                        getMainPanel().getCurrentTreePanel().calcParametersForPainting( getMainPanel()
+                                                                                        .getCurrentTreePanel()
+                                                                                        .getWidth(),
+                                                                                        getMainPanel()
+                                                                                        .getCurrentTreePanel()
+                                                                                        .getHeight() );
                     }
                 }
             } );
@@ -766,7 +920,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         _options_jmenu
                 .add( _show_default_node_shapes_external_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL_EXT ) );
         _options_jmenu
-                .add( _show_default_node_shapes_for_marked_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL_EXT ) );
+                .add( _show_default_node_shapes_for_marked_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL_MARKED ) );
         _options_jmenu.add( _line_up_renderable_data_cbmi = new JCheckBoxMenuItem( MainFrame.LINE_UP_RENDERABLE_DATA ) );
         if ( getConfiguration().doDisplayOption( Configuration.show_domain_architectures ) ) {
             _options_jmenu.add( _right_line_up_domains_cbmi = new JCheckBoxMenuItem( MainFrame.RIGHT_LINE_UP_DOMAINS ) );
@@ -797,16 +951,8 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         _options_jmenu
                 .add( _search_case_senstive_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_CASE_SENSITIVE_LABEL ) );
         _options_jmenu.add( _search_whole_words_only_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_TERMS_ONLY_LABEL ) );
-        
-        
-        
-        _options_jmenu
-        .add( _search_with_regex_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_REGEX_LABEL ) );
-
-        
-        
-        
-        
+        _options_jmenu.add( _search_with_regex_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_REGEX_LABEL ) );
+        _search_with_regex_cbmi.setToolTipText( MainFrame.SEARCH_WITH_REGEX_TIP );
         _options_jmenu
                 .add( _inverse_search_result_cbmi = new JCheckBoxMenuItem( MainFrame.INVERSE_SEARCH_RESULT_LABEL ) );
         customizeJMenuItem( _choose_font_mi );
@@ -840,12 +986,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         customizeRadioButtonMenuItem( _ext_node_dependent_cladogram_rbmi,
                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
         customizeCheckBoxMenuItem( _show_overview_cbmi, getOptions().isShowOverview() );
-        
-        
         customizeCheckBoxMenuItem( _search_with_regex_cbmi, getOptions().isSearchWithRegex() );
-        
-        
-        
         customizeCheckBoxMenuItem( _search_whole_words_only_cbmi, getOptions().isMatchWholeTermsOnly() );
         customizeCheckBoxMenuItem( _inverse_search_result_cbmi, getOptions().isInverseSearchResult() );
         customizeCheckBoxMenuItem( _show_confidence_stddev_cbmi, getOptions().isShowConfidenceStddev() );
@@ -949,12 +1090,14 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
     }
 
     void customizeJMenuItem( final JMenuItem jmi ) {
+        if ( jmi != null ) {
         jmi.setFont( MainFrame.menu_font );
         if ( !getConfiguration().isUseNativeUI() ) {
             jmi.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
             jmi.setForeground( getConfiguration().getGuiMenuTextColor() );
         }
         jmi.addActionListener( this );
+        }
     }
 
     void displayBasicInformation() {
@@ -1364,9 +1507,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
                 && _show_confidence_stddev_cbmi.isSelected() );
         options.setMatchWholeTermsOnly( ( _search_whole_words_only_cbmi != null )
                 && _search_whole_words_only_cbmi.isSelected() );
-        options.setSearchWithRegex( ( _search_with_regex_cbmi != null )
-                                        && _search_with_regex_cbmi.isSelected() );
-        
+        options.setSearchWithRegex( ( _search_with_regex_cbmi != null ) && _search_with_regex_cbmi.isSelected() );
         options.setInverseSearchResult( ( _inverse_search_result_cbmi != null )
                 && _inverse_search_result_cbmi.isSelected() );
         if ( ( _rectangular_type_cbmi != null ) && _rectangular_type_cbmi.isSelected() ) {