GPL license added
[jalview.git] / src / jalview / gui / TreeCanvas.java
index 9435716..5ebf323 100755 (executable)
@@ -1,3 +1,22 @@
+/*\r
+* Jalview - A Sequence Alignment Editor and Viewer\r
+* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+*\r
+* This program is free software; you can redistribute it and/or\r
+* modify it under the terms of the GNU General Public License\r
+* as published by the Free Software Foundation; either version 2\r
+* of the License, or (at your option) any later version.\r
+*\r
+* This program is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License\r
+* along with this program; if not, write to the Free Software\r
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
+*/\r
+\r
 package jalview.gui;\r
 \r
 \r
@@ -16,18 +35,19 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
   NJTree tree;\r
   JScrollPane scrollPane;\r
   AlignViewport av;\r
-\r
+  public static final String PLACEHOLDER=" * ";\r
   Font font;\r
   int  fontSize = 12;\r
 \r
   boolean fitToWindow = true;\r
   boolean showDistances = false;\r
   boolean showBootstrap = false;\r
+  boolean markPlaceholders = false;\r
 \r
   int offx = 20;\r
   int offy = 20;\r
 \r
-  int threshold;\r
+  float threshold;\r
 \r
   String longestName;\r
   int labelLength=-1;\r
@@ -122,10 +142,9 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
       g.drawString(nodeLabel,xstart,ypos - 10);\r
       }\r
 \r
-      // Colour selected leaves differently\r
-      String name    = node.getName();\r
+      String name    = (markPlaceholders && node.isPlaceholder()) ? (PLACEHOLDER+node.getName()) : node.getName();\r
       FontMetrics fm = g.getFontMetrics(font);\r
-      int charWidth  = fm.stringWidth(node.getName()) + 3;\r
+      int charWidth  = fm.stringWidth(name) + 3;\r
       int charHeight = fm.getHeight();\r
 \r
       Rectangle rect = new Rectangle(xend+20,ypos-charHeight,\r
@@ -133,6 +152,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
 \r
       nameHash.put((SequenceI)node.element(),rect);\r
 \r
+      // Colour selected leaves differently\r
       SequenceGroup selected = av.getSelectionGroup();\r
       if (selected!=null && selected.sequences.contains((SequenceI)node.element())) {\r
         g.setColor(Color.gray);\r
@@ -140,7 +160,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
         g.fillRect(xend + 10, ypos - charHeight + 3,charWidth,charHeight);\r
         g.setColor(Color.white);\r
       }\r
-      g.drawString(node.getName(),xend+10,ypos);\r
+      g.drawString(name,xend+10,ypos);\r
       g.setColor(Color.black);\r
     } else {\r
       drawNode(g,(SequenceNode)node.left(), chunk,scale,width,offx,offy);\r
@@ -275,6 +295,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
   {\r
     PrinterJob printJob = PrinterJob.getPrinterJob();\r
     PageFormat pf = printJob.pageDialog(printJob.defaultPage());\r
+\r
     printJob.setPrintable(this, pf);\r
     if (printJob.printDialog())\r
     {\r
@@ -292,25 +313,41 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
 \r
   public int print(Graphics pg, PageFormat pf, int pi) throws PrinterException\r
   {\r
-    pg.setFont(font);\r
-    FontMetrics fm = pg.getFontMetrics(font);\r
 \r
+    pg.setFont(font);\r
     pg.translate((int)pf.getImageableX(), (int)pf.getImageableY());\r
-\r
     int pwidth = (int) pf.getImageableWidth();\r
     int pheight = (int) pf.getImageableHeight();\r
 \r
-    // adjust pheight to row height\r
-    pheight -= (pheight%fm.getHeight());\r
-    pg.setClip(0,0, pwidth, pheight);\r
-\r
-    int noPages = (fm.getHeight() * nameHash.size()) / pheight;\r
-\r
+    int noPages = getHeight() / pheight;\r
     if(pi>noPages)\r
       return Printable.NO_SUCH_PAGE;\r
 \r
-    pg.translate(0, -pheight*pi );\r
-    draw(pg, pwidth, fm.getHeight() * nameHash.size());\r
+\r
+    if (pwidth > getWidth())\r
+        pwidth = getWidth();\r
+\r
+    if(fitToWindow)\r
+    {\r
+      if (pheight > getHeight())\r
+        pheight = getHeight();\r
+\r
+      noPages = 0;\r
+    }\r
+    else\r
+    {\r
+\r
+        FontMetrics fm = pg.getFontMetrics(font);\r
+        int height = fm.getHeight() * nameHash.size();\r
+        pg.translate(0, -pi*pheight  );\r
+        pg.setClip(0,pi*pheight, pwidth,pi*pheight + pheight);\r
+       // translate number of pages,\r
+       // height is screen size as this is the\r
+       // non overlapping text size\r
+        pheight = height;\r
+    }\r
+\r
+    draw(pg, pwidth, pheight);\r
 \r
     return Printable.PAGE_EXISTS;\r
 \r
@@ -339,13 +376,6 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
          draw( g,scrollPane.getWidth(), fm.getHeight() * nameHash.size());\r
      }\r
 \r
-\r
-    if (threshold != 0)\r
-    {\r
-        g.setColor(Color.red);\r
-        g.drawLine(threshold,0,threshold,getHeight());\r
-    }\r
-\r
     scrollPane.revalidate();\r
   }\r
     public int getFontSize() {\r
@@ -355,12 +385,15 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
         this.fontSize = fontSize;\r
         repaint();\r
     }\r
-  public void draw(Graphics g, int width, int height) {\r
-      g.setColor(Color.white);\r
-      g.fillRect(0,0,width,height);\r
+  public void draw(Graphics g1, int width, int height) {\r
+\r
+      Graphics2D g2 = (Graphics2D)g1;\r
+      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);\r
+      g2.setColor(Color.white);\r
+      g2.fillRect(0,0,width,height);\r
 \r
 \r
-      labelLength = g.getFontMetrics(font).stringWidth(longestName)+ 20;//20 allows for scrollbar\r
+      labelLength = g2.getFontMetrics(font).stringWidth(longestName)+ 20;//20 allows for scrollbar\r
 \r
       float wscale =(float)(width - labelLength -offx*2)/tree.getMaxHeight();\r
 \r
@@ -371,7 +404,20 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
       }\r
       float chunk = (float)(height-offy*2)/top.count ;\r
 \r
-      drawNode(g,tree.getTopNode(),chunk,wscale,width,offx,offy);\r
+      drawNode(g2,tree.getTopNode(),chunk,wscale,width,offx,offy);\r
+\r
+      if (threshold != 0)\r
+      {\r
+        if(av.getCurrentTree() == tree)\r
+          g2.setColor(Color.red);\r
+        else\r
+          g2.setColor(Color.gray);\r
+\r
+          int x = (int)(    threshold * (float)(getWidth()-labelLength - 2*offx) +offx   ) ;\r
+\r
+          g2.drawLine(x,0,x,getHeight());\r
+      }\r
+\r
   }\r
 \r
   public void mouseReleased(MouseEvent e) { }\r
@@ -381,6 +427,9 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
   }\r
 \r
   public void mousePressed(MouseEvent e) {\r
+\r
+      av.setCurrentTree(tree);\r
+\r
       int x = e.getX();\r
       int y = e.getY();\r
 \r
@@ -401,10 +450,10 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
           // Find threshold\r
 \r
           if (tree.getMaxHeight() != 0) {\r
-              float fthreshold = (float)(x - offx)/(float)(getWidth()-labelLength - 2*offx);\r
-              this.threshold = x;\r
+              threshold = (float)(x - offx)/(float)(getWidth()-labelLength - 2*offx);\r
+\r
               tree.getGroups().removeAllElements();\r
-              tree.groupNodes(tree.getTopNode(),fthreshold);\r
+              tree.groupNodes(tree.getTopNode(),threshold);\r
               setColor(tree.getTopNode(),Color.black);\r
 \r
               av.setSelectionGroup(null);\r
@@ -438,7 +487,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
                                                       sg.getEndRes());\r
 \r
                     c.calculate();\r
-                    c.verdict(false, 100);\r
+                    c.verdict(false, av.ConsPercGaps);\r
                     ccs = new ConservationColourScheme(c, ccs.cs);\r
 \r
                     sg.cs = ccs;\r
@@ -467,6 +516,10 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print
       this.showBootstrap = state;\r
       repaint();\r
     }\r
+    public void setMarkPlaceholders(boolean state) {\r
+            this.markPlaceholders = state;\r
+            repaint();\r
+    }\r
 \r
 }\r
 \r