in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / TreePanel.java
index 3b0abfe..4178b2a 100644 (file)
@@ -111,6 +111,7 @@ import org.forester.phylogeny.data.Accession;
 import org.forester.phylogeny.data.Annotation;
 import org.forester.phylogeny.data.BranchColor;
 import org.forester.phylogeny.data.Confidence;
+import org.forester.phylogeny.data.DomainArchitecture;
 import org.forester.phylogeny.data.Event;
 import org.forester.phylogeny.data.NodeData.NODE_DATA;
 import org.forester.phylogeny.data.NodeVisualData;
@@ -202,6 +203,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
     private float                        _last_drag_point_y                                 = 0;
     private final Line2D                 _line                                              = new Line2D.Float();
     private int                          _longest_ext_node_info                             = 0;
+    private PhylogenyNode                _ext_node_with_longest_txt_info                    = null;
     private MainPanel                    _main_panel                                        = null;
     private double                       _max_distance_to_root                              = -1;
     private Popup                        _node_desc_popup;
@@ -236,7 +238,6 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
     private final StringBuilder          _sb                                                = new StringBuilder();
     private double                       _scale_distance                                    = 0.0;
     private String                       _scale_label                                       = null;
-    // expression values menu:
     private DescriptiveStatistics        _statistics_for_vector_data;
     private final Phylogeny[]            _sub_phylogenies                                   = new Phylogeny[ TreePanel.MAX_SUBTREES ];
     private final PhylogenyNode[]        _sub_phylogenies_temp_roots                        = new PhylogenyNode[ TreePanel.MAX_SUBTREES ];
@@ -250,6 +251,8 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
     private float                        _x_correction_factor                               = 0.0f;
     private float                        _x_distance                                        = 0.0f;
     private float                        _y_distance                                        = 0.0f;
+    private int                          _length_of_longest_text;
+    private int                          _longest_domain;
     //  private Image                           offscreenImage;
     //  private Graphics                        offscreenGraphics;
     //  private Dimension                       offscreenDimension;
@@ -501,7 +504,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                 calculateLongestExtNodeInfo();
                 if ( getOptions().isAllowFontSizeChange() ) {
                     if ( ( getLongestExtNodeInfo() > ( x * 0.6 ) )
-                            && ( getTreeFontSet().getLargeFont().getSize() > 2 + TreeFontSet.FONT_SIZE_CHANGE_STEP ) ) {
+                            && ( getTreeFontSet().getLargeFont().getSize() > ( 2 + TreeFontSet.FONT_SIZE_CHANGE_STEP ) ) ) {
                         while ( ( getLongestExtNodeInfo() > ( x * 0.7 ) )
                                 && ( getTreeFontSet().getLargeFont().getSize() > 2 ) ) {
                             getMainPanel().getTreeFontSet().decreaseFontSize( getConfiguration().getMinBaseFontSize(),
@@ -511,13 +514,14 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                     }
                     else {
                         while ( ( getLongestExtNodeInfo() < ( x * 0.6 ) )
-                                && ( getTreeFontSet().getLargeFont().getSize() <= getTreeFontSet().getLargeFontMemory()
-                                        .getSize() - TreeFontSet.FONT_SIZE_CHANGE_STEP ) ) {
+                                && ( getTreeFontSet().getLargeFont().getSize() <= ( getTreeFontSet()
+                                        .getLargeFontMemory().getSize() - TreeFontSet.FONT_SIZE_CHANGE_STEP ) ) ) {
                             getMainPanel().getTreeFontSet().increaseFontSize();
                             calculateLongestExtNodeInfo();
                         }
                     }
                 }
+                _length_of_longest_text = calcLengthOfLongestText();
             }
             int ext_nodes = _phylogeny.getRoot().getNumberOfExternalNodes();
             final int max_depth = PhylogenyMethods.calculateMaxDepth( _phylogeny );
@@ -598,6 +602,9 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             max_length = 40;
         }
         int longest = 30;
+        int longest_txt = 0;
+        _longest_domain = 0;
+        PhylogenyNode longest_txt_node = _phylogeny.getFirstExternalNode();
         for( final PhylogenyNode node : _phylogeny.getExternalNodes() ) {
             int sum = 0;
             if ( node.isCollapse() ) {
@@ -608,6 +615,11 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             if ( node.getNodeData().isHasTaxonomy() ) {
                 nodeTaxonomyDataAsSB( node.getNodeData().getTaxonomy(), sb );
             }
+            final int txt = sb.length();
+            if ( txt > longest_txt ) {
+                longest_txt = txt;
+                longest_txt_node = node;
+            }
             boolean use_vis = false;
             final Graphics2D g = ( Graphics2D ) getGraphics();
             if ( getControlPanel().isUseVisualStyles() ) {
@@ -623,19 +635,40 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                 sum += getFontMetricsForLargeDefaultFont().stringWidth( node.getNodeData().getBinaryCharacters()
                         .getGainedCharactersAsStringBuffer().toString() );
             }
+            if ( getControlPanel().isShowVectorData() && ( node.getNodeData().getVector() != null )
+                    && ( node.getNodeData().getVector().size() > 0 ) ) {
+                if ( getConfiguration() != null ) {
+                    sum += getConfiguration().getVectorDataWidth() + 10;
+                }
+                else {
+                    sum += RenderableVector.VECTOR_DEFAULT_WIDTH + 10;
+                }
+            }
+            if ( getControlPanel().isShowDomainArchitectures() && node.getNodeData().isHasSequence()
+                    && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
+                // FIXME 
+                // TODO this might need some clean up
+                final DomainArchitecture d = node.getNodeData().getSequence().getDomainArchitecture();
+                sum += ( ( _domain_structure_width / ( ( RenderableDomainArchitecture ) d ).getOriginalSize()
+                        .getWidth() ) * d.getTotalLength() ) + 10;
+                if ( d.getTotalLength() > _longest_domain ) {
+                    _longest_domain = d.getTotalLength();
+                }
+            }
             if ( sum >= max_length ) {
-                setLongestExtNodeInfo( max_length );
+                _longest_ext_node_info = max_length;
                 return;
             }
             if ( sum > longest ) {
                 longest = sum;
             }
         }
+        _ext_node_with_longest_txt_info = longest_txt_node;
         if ( longest >= max_length ) {
-            setLongestExtNodeInfo( max_length );
+            _longest_ext_node_info = max_length;
         }
         else {
-            setLongestExtNodeInfo( longest );
+            _longest_ext_node_info = longest;
         }
     }
 
@@ -704,30 +737,30 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             }
             if ( c == null ) {
                 if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
-                    c = TreePanelUtil.calculateColorFromString( tax.getTaxonomyCode(), true );
+                    c = AptxUtil.calculateColorFromString( tax.getTaxonomyCode(), true );
                     getControlPanel().getSpeciesColors().put( tax.getTaxonomyCode(), c );
                 }
                 else {
-                    c = TreePanelUtil.calculateColorFromString( tax.getScientificName(), true );
+                    c = AptxUtil.calculateColorFromString( tax.getScientificName(), true );
                     getControlPanel().getSpeciesColors().put( tax.getScientificName(), c );
                 }
             }
             return c;
         }
     }
-    
+
     final Color calculateSequenceBasedColor( final Sequence seq ) {
-            if ( ForesterUtil.isEmpty( seq.getName() ) ) {
-                return getTreeColorSet().getSequenceColor();
-            }
-            Color c = null;
-            final String seq_name = seq.getName();
-            c = getControlPanel().getSequenceColors().get( seq_name  );
-            if ( c == null ) {
-                    c = TreePanelUtil.calculateColorFromString( seq_name, false );
-                    getControlPanel().getSequenceColors().put( seq_name, c );
-            }
-            return c;
+        if ( ForesterUtil.isEmpty( seq.getName() ) ) {
+            return getTreeColorSet().getSequenceColor();
+        }
+        Color c = null;
+        final String seq_name = seq.getName();
+        c = getControlPanel().getSequenceColors().get( seq_name );
+        if ( c == null ) {
+            c = AptxUtil.calculateColorFromString( seq_name, false );
+            getControlPanel().getSequenceColors().put( seq_name, c );
+        }
+        return c;
     }
 
     void checkForVectorProperties( final Phylogeny phy ) {
@@ -855,8 +888,10 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             if ( _control_panel.getColorAccSpeciesCb() != null ) {
                 _control_panel.getColorAccSpeciesCb().setSelected( false );
             }
-           
             _options.setColorLabelsSameAsParentBranch( true );
+            if ( getMainPanel().getMainFrame()._color_labels_same_as_parent_branch != null ) {
+                getMainPanel().getMainFrame()._color_labels_same_as_parent_branch.setSelected( true );
+            }
             _control_panel.repaint();
         }
         setArrowCursor();
@@ -1001,7 +1036,6 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         return _statistics_for_vector_data;
     }
 
-  
     final Color getTaxonomyBasedColor( final PhylogenyNode node ) {
         if ( node.getNodeData().isHasTaxonomy() ) {
             return calculateTaxonomyBasedColor( node.getNodeData().getTaxonomy() );
@@ -1009,7 +1043,6 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         // return non-colorized color
         return getTreeColorSet().getTaxonomyColor();
     }
-    
 
     final Color getSequenceBasedColor( final PhylogenyNode node ) {
         if ( node.getNodeData().isHasSequence() ) {
@@ -1018,12 +1051,8 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         // return non-colorized color
         return getTreeColorSet().getSequenceColor();
     }
-    
 
-    /**
-     * @return pointer to colorset for tree drawing
-     */
-    final TreeColorSet getTreeColorSet() {
+    public final TreeColorSet getTreeColorSet() {
         return getMainPanel().getTreeColorSet();
     }
 
@@ -1053,14 +1082,13 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         if ( ( _phylogeny == null ) || _phylogeny.isEmpty() ) {
             return;
         }
-        double max_original_domain_structure_width = 0.0;
+        double _max_original_domain_structure_width = 0.0;
         for( final PhylogenyNode node : _phylogeny.getExternalNodes() ) {
             if ( node.getNodeData().isHasSequence()
                     && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
                 RenderableDomainArchitecture rds = null;
                 if ( !( node.getNodeData().getSequence().getDomainArchitecture() instanceof RenderableDomainArchitecture ) ) {
-                    rds = new RenderableDomainArchitecture( node.getNodeData().getSequence().getDomainArchitecture(),
-                                                            getConfiguration() );
+                    rds = new RenderableDomainArchitecture( node.getNodeData().getSequence().getDomainArchitecture() );
                     node.getNodeData().getSequence().setDomainArchitecture( rds );
                 }
                 else {
@@ -1068,14 +1096,14 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                 }
                 if ( getControlPanel().isShowDomainArchitectures() ) {
                     final double dsw = rds.getOriginalSize().getWidth();
-                    if ( dsw > max_original_domain_structure_width ) {
-                        max_original_domain_structure_width = dsw;
+                    if ( dsw > _max_original_domain_structure_width ) {
+                        _max_original_domain_structure_width = dsw;
                     }
                 }
             }
         }
         if ( getControlPanel().isShowDomainArchitectures() ) {
-            final double ds_factor_width = _domain_structure_width / max_original_domain_structure_width;
+            final double ds_factor_width = _domain_structure_width / _max_original_domain_structure_width;
             for( final PhylogenyNode node : _phylogeny.getExternalNodes() ) {
                 if ( node.getNodeData().isHasSequence()
                         && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
@@ -1865,10 +1893,6 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         _last_drag_point_y = y;
     }
 
-    final void setLongestExtNodeInfo( final int i ) {
-        _longest_ext_node_info = i;
-    }
-
     final void setMediumFonts() {
         getTreeFontSet().mediumFonts();
     }
@@ -2363,7 +2387,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             if ( !ForesterUtil.isEmpty( ann_str ) ) {
                 c = getControlPanel().getAnnotationColors().get( ann_str );
                 if ( c == null ) {
-                    c = TreePanelUtil.calculateColorFromString( ann_str, false );
+                    c = AptxUtil.calculateColorFromString( ann_str, false );
                     getControlPanel().getAnnotationColors().put( ann_str, c );
                 }
                 if ( c == null ) {
@@ -2371,7 +2395,6 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                 }
             }
         }
-        
         return c;
     }
 
@@ -3972,7 +3995,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             d = box_size;
         }
         final float xx = node.getXcoord() - ( 2 * box_size );
-        final float xxx = xx > node.getParent().getXcoord() + 1 ? xx : node.getParent().getXcoord() + 1;
+        final float xxx = xx > ( node.getParent().getXcoord() + 1 ) ? xx : node.getParent().getXcoord() + 1;
         _polygon.reset();
         _polygon.moveTo( xxx, node.getYcoord() );
         _polygon.lineTo( node.getXcoord() + 1, node.getYcoord() - d );
@@ -4674,10 +4697,9 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         }
         float new_x = 0;
         float new_x_min = Float.MAX_VALUE;
-        final boolean disallow_shortcutting = dynamic_hiding_factor < 40;
+        final boolean disallow_shortcutting = ( dynamic_hiding_factor < 40 );
         float min_dist = 1.5f;
         if ( !disallow_shortcutting ) {
-            //   System.out.println( dynamic_hiding_factor );
             if ( dynamic_hiding_factor > 4000 ) {
                 min_dist = 4;
             }
@@ -4749,7 +4771,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                                                     final PhylogenyNode node,
                                                     final boolean to_graphics_file,
                                                     final boolean to_pdf ) {
-        if ( isNodeDataInvisible( node ) && !to_graphics_file ) {
+        if ( isNodeDataInvisible( node ) && !( to_graphics_file || to_pdf ) ) {
             return;
         }
         if ( ( !getControlPanel().isShowInternalData() && !node.isExternal() ) ) {
@@ -4758,42 +4780,95 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         if ( getControlPanel().isShowDomainArchitectures() && node.getNodeData().isHasSequence()
                 && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
             RenderableDomainArchitecture rds = null;
-            if ( node.getNodeData().getSequence().getDomainArchitecture() instanceof RenderableDomainArchitecture ) {
-                try {
-                    rds = ( RenderableDomainArchitecture ) node.getNodeData().getSequence().getDomainArchitecture();
-                }
-                catch ( final ClassCastException cce ) {
-                    cce.printStackTrace();
+            try {
+                rds = ( RenderableDomainArchitecture ) node.getNodeData().getSequence().getDomainArchitecture();
+            }
+            catch ( final ClassCastException cce ) {
+                cce.printStackTrace();
+            }
+            if ( rds != null ) {
+                final int default_height = 7;
+                float y = getYdistance();
+                if ( getControlPanel().isDynamicallyHideData() ) {
+                    y = getTreeFontSet().getFontMetricsLarge().getHeight();
+                }
+                final int h = y < default_height ? ForesterUtil.roundToInt( y ) : default_height;
+                rds.setRenderingHeight( h > 1 ? h : 2 );
+                if ( getControlPanel().isDrawPhylogram() ) {
+                    if ( getOptions().isLineUpRendarableNodeData() ) {
+                        if ( getOptions().isRightLineUpDomains() ) {
+                            rds.render( getMaxDistanceToRoot() * getXcorrectionFactor() + _length_of_longest_text
+                                                + ( _longest_domain - rds.getTotalLength() )
+                                                * rds.getRenderingFactorWidth(),
+                                        node.getYcoord() - ( h / 2 ),
+                                        g,
+                                        this,
+                                        to_pdf );
+                        }
+                        else {
+                            rds.render( getMaxDistanceToRoot() * getXcorrectionFactor() + _length_of_longest_text,
+                                        node.getYcoord() - ( h / 2 ),
+                                        g,
+                                        this,
+                                        to_pdf );
+                        }
+                    }
+                    else {
+                        rds.render( node.getXcoord() + x, node.getYcoord() - ( h / 2 ), g, this, to_pdf );
+                    }
                 }
-                if ( rds != null ) {
-                    rds.setRenderingHeight( 6 );
-                    rds.render( node.getXcoord() + x, node.getYcoord() - 3, g, this, to_pdf );
+                else {
+                    if ( getOptions().isRightLineUpDomains() ) {
+                        rds.render( getPhylogeny().getFirstExternalNode().getXcoord() + _length_of_longest_text
+                                            - 20 + ( _longest_domain - rds.getTotalLength() ) 
+                                            * rds.getRenderingFactorWidth(),
+                                    node.getYcoord() - ( h / 2 ),
+                                    g,
+                                    this,
+                                    to_pdf );
+                    }
+                    else {
+                        rds.render( getPhylogeny().getFirstExternalNode().getXcoord() + _length_of_longest_text,
+                                    node.getYcoord() - ( h / 2 ),
+                                    g,
+                                    this,
+                                    to_pdf );
+                    }
                 }
             }
         }
-        //////////////
         if ( getControlPanel().isShowVectorData() && ( node.getNodeData().getVector() != null )
                 && ( node.getNodeData().getVector().size() > 0 ) && ( getStatisticsForExpressionValues() != null ) ) {
             final RenderableVector rv = RenderableVector.createInstance( node.getNodeData().getVector(),
                                                                          getStatisticsForExpressionValues(),
                                                                          getConfiguration() );
             if ( rv != null ) {
-                int xx = 0;
-                PhylogenyNode my_node = node;
-                if ( !getControlPanel().isDrawPhylogram() ) {
-                    my_node = getPhylogeny().getFirstExternalNode();
+                double domain_add = 0;
+                if ( getControlPanel().isShowDomainArchitectures() && node.getNodeData().isHasSequence()
+                        && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
+                    domain_add = _domain_structure_width + 10;
                 }
-                if ( getControlPanel().isShowTaxonomyCode() && ( PhylogenyMethods.getSpecies( my_node ).length() > 0 ) ) {
-                    xx += getFontMetricsForLargeDefaultFont()
-                            .stringWidth( PhylogenyMethods.getSpecies( my_node ) + " " );
+                if ( getControlPanel().isDrawPhylogram() ) {
+                    rv.render( node.getXcoord() + x + domain_add, node.getYcoord() - 3, g, this, to_pdf );
                 }
-                if ( getControlPanel().isShowNodeNames() && ( my_node.getName().length() > 0 ) ) {
-                    xx += getFontMetricsForLargeDefaultFont().stringWidth( my_node.getName() + " " );
+                else {
+                    rv.render( getPhylogeny().getFirstExternalNode().getXcoord() + _length_of_longest_text + domain_add,
+                               node.getYcoord() - 3,
+                               g,
+                               this,
+                               to_pdf );
                 }
-                rv.render( my_node.getXcoord() + xx, node.getYcoord() - 5, g, this, to_pdf );
             }
         }
-        //////////////
+    }
+
+    final private int calcLengthOfLongestText() {
+        final StringBuilder sb = new StringBuilder();
+        nodeDataAsSB( _ext_node_with_longest_txt_info, sb );
+        if ( _ext_node_with_longest_txt_info.getNodeData().isHasTaxonomy() ) {
+            nodeTaxonomyDataAsSB( _ext_node_with_longest_txt_info.getNodeData().getTaxonomy(), sb );
+        }
+        return getFontMetricsForLargeDefaultFont().stringWidth( sb.toString() );
     }
 
     final private void paintOvRectangle( final Graphics2D g ) {