2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
21 import jalview.analysis.*;
\r
23 import jalview.datamodel.*;
\r
25 import jalview.schemes.*;
\r
27 import jalview.util.*;
\r
30 import java.awt.event.*;
\r
31 import java.awt.print.*;
\r
35 import javax.swing.*;
\r
42 * @version $Revision$
\r
44 public class TreeCanvas extends JPanel implements MouseListener, Runnable,
\r
47 /** DOCUMENT ME!! */
\r
48 public static final String PLACEHOLDER = " * ";
\r
50 JScrollPane scrollPane;
\r
54 boolean fitToWindow = true;
\r
55 boolean showDistances = false;
\r
56 boolean showBootstrap = false;
\r
57 boolean markPlaceholders = false;
\r
62 int labelLength = -1;
\r
64 //RubberbandRectangle rubberband;
\r
66 Hashtable nameHash = new Hashtable();
\r
67 Hashtable nodeHash = new Hashtable();
\r
70 * Creates a new TreeCanvas object.
\r
72 * @param av DOCUMENT ME!
\r
73 * @param tree DOCUMENT ME!
\r
74 * @param scroller DOCUMENT ME!
\r
75 * @param label DOCUMENT ME!
\r
77 public TreeCanvas(AlignViewport av, JScrollPane scroller)
\r
80 font = av.getFont();
\r
81 scrollPane = scroller;
\r
82 addMouseListener(this);
\r
83 PaintRefresher.Register(this, av.alignment);
\r
90 * @param sequence DOCUMENT ME!
\r
92 public void TreeSelectionChanged(Sequence sequence)
\r
94 SequenceGroup selected = av.getSelectionGroup();
\r
96 if (selected == null)
\r
98 selected = new SequenceGroup();
\r
99 av.setSelectionGroup(selected);
\r
102 selected.setEndRes(av.alignment.getWidth()-1);
\r
103 selected.addOrRemove(sequence, true);
\r
105 PaintRefresher.Refresh(this, av.alignment);
\r
112 * @param tree DOCUMENT ME!
\r
114 public void setTree(NJTree tree)
\r
117 tree.findHeight(tree.getTopNode());
\r
119 // Now have to calculate longest name based on the leaves
\r
120 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
\r
121 boolean has_placeholders = false;
\r
124 for (int i = 0; i < leaves.size(); i++)
\r
126 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
\r
128 if (lf.isPlaceholder())
\r
130 has_placeholders = true;
\r
133 if (longestName.length() < ( (Sequence) lf.element()).getName()
\r
136 longestName = TreeCanvas.PLACEHOLDER +
\r
137 ( (Sequence) lf.element()).getName();
\r
141 setMarkPlaceholders(has_placeholders);
\r
147 * @param g DOCUMENT ME!
\r
148 * @param node DOCUMENT ME!
\r
149 * @param chunk DOCUMENT ME!
\r
150 * @param scale DOCUMENT ME!
\r
151 * @param width DOCUMENT ME!
\r
152 * @param offx DOCUMENT ME!
\r
153 * @param offy DOCUMENT ME!
\r
155 public void drawNode(Graphics g, SequenceNode node, float chunk,
\r
156 float scale, int width, int offx, int offy)
\r
163 if ((node.left() == null) && (node.right() == null))
\r
165 // Drawing leaf node
\r
166 float height = node.height;
\r
167 float dist = node.dist;
\r
169 int xstart = (int) ((height - dist) * scale) + offx;
\r
170 int xend = (int) (height * scale) + offx;
\r
172 int ypos = (int) (node.ycount * chunk) + offy;
\r
174 if (node.element() instanceof SequenceI)
\r
176 if (((SequenceI) ((SequenceNode) node).element()).getColor() == Color.white)
\r
178 g.setColor(Color.black);
\r
182 g.setColor(((SequenceI) ((SequenceNode) node).element()).getColor()
\r
188 g.setColor(Color.black);
\r
191 // Draw horizontal line
\r
192 g.drawLine(xstart, ypos, xend, ypos);
\r
194 String nodeLabel = "";
\r
196 if (showDistances && (node.dist > 0))
\r
198 nodeLabel = new Format("%-.2f").form(node.dist);
\r
205 nodeLabel = nodeLabel + " : ";
\r
208 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
\r
211 if (!nodeLabel.equals(""))
\r
213 g.drawString(nodeLabel, xstart+2, ypos - 2);
\r
216 String name = (markPlaceholders && node.isPlaceholder())
\r
217 ? (PLACEHOLDER + node.getName()) : node.getName();
\r
219 int charWidth = fm.stringWidth(name) + 3;
\r
220 int charHeight = font.getSize();
\r
222 Rectangle rect = new Rectangle(xend+10, ypos-charHeight/2,
\r
223 charWidth, charHeight);
\r
225 nameHash.put((SequenceI) node.element(), rect);
\r
227 // Colour selected leaves differently
\r
228 SequenceGroup selected = av.getSelectionGroup();
\r
230 if ((selected != null) &&
\r
231 selected.getSequences(false).contains((SequenceI) node.element()))
\r
233 g.setColor(Color.gray);
\r
235 g.fillRect(xend + 10, ypos-charHeight/2, charWidth,
\r
237 g.setColor(Color.white);
\r
240 g.drawString(name, xend + 10, ypos+fm.getDescent());
\r
241 g.setColor(Color.black);
\r
245 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
\r
247 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
\r
250 float height = node.height;
\r
251 float dist = node.dist;
\r
253 int xstart = (int) ((height - dist) * scale) + offx;
\r
254 int xend = (int) (height * scale) + offx;
\r
255 int ypos = (int) (node.ycount * chunk) + offy;
\r
257 g.setColor(((SequenceNode) node).color.darker());
\r
259 // Draw horizontal line
\r
260 g.drawLine(xstart, ypos, xend, ypos);
\r
261 g.fillRect(xend - 2, ypos - 2, 4, 4);
\r
263 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk) +
\r
265 int yend = (int) (((SequenceNode) node.right()).ycount * chunk) +
\r
268 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
\r
269 nodeHash.put(node, pos);
\r
271 g.drawLine((int) (height * scale) + offx, ystart,
\r
272 (int) (height * scale) + offx, yend);
\r
274 if (showDistances && (node.dist > 0))
\r
276 g.drawString(new Format("%-.2f").form(node.dist).trim(), xstart+2,
\r
285 * @param x DOCUMENT ME!
\r
286 * @param y DOCUMENT ME!
\r
288 * @return DOCUMENT ME!
\r
290 public Object findElement(int x, int y)
\r
292 Enumeration keys = nameHash.keys();
\r
294 while (keys.hasMoreElements())
\r
296 Object ob = keys.nextElement();
\r
297 Rectangle rect = (Rectangle) nameHash.get(ob);
\r
299 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y) &&
\r
300 (y <= (rect.y + rect.height)))
\r
306 keys = nodeHash.keys();
\r
308 while (keys.hasMoreElements())
\r
310 Object ob = keys.nextElement();
\r
311 Rectangle rect = (Rectangle) nodeHash.get(ob);
\r
313 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y) &&
\r
314 (y <= (rect.y + rect.height)))
\r
326 * @param pickBox DOCUMENT ME!
\r
328 public void pickNodes(Rectangle pickBox)
\r
330 int width = getWidth();
\r
331 int height = getHeight();
\r
333 SequenceNode top = tree.getTopNode();
\r
335 float wscale = (float) ((width * .8) - (offx * 2)) / tree.getMaxHeight();
\r
337 if (top.count == 0)
\r
339 top.count = ((SequenceNode) top.left()).count +
\r
340 ((SequenceNode) top.right()).count;
\r
343 float chunk = (float) (height - (offy)) / top.count;
\r
345 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
\r
351 * @param pickBox DOCUMENT ME!
\r
352 * @param node DOCUMENT ME!
\r
353 * @param chunk DOCUMENT ME!
\r
354 * @param scale DOCUMENT ME!
\r
355 * @param width DOCUMENT ME!
\r
356 * @param offx DOCUMENT ME!
\r
357 * @param offy DOCUMENT ME!
\r
359 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
\r
360 float scale, int width, int offx, int offy)
\r
367 if ((node.left() == null) && (node.right() == null))
\r
369 float height = node.height;
\r
370 float dist = node.dist;
\r
372 int xstart = (int) ((height - dist) * scale) + offx;
\r
373 int xend = (int) (height * scale) + offx;
\r
375 int ypos = (int) (node.ycount * chunk) + offy;
\r
377 if (pickBox.contains(new Point(xend, ypos)))
\r
379 if (node.element() instanceof SequenceI)
\r
381 SequenceI seq = (SequenceI) node.element();
\r
382 SequenceGroup sg = av.getSelectionGroup();
\r
386 sg.addOrRemove(seq, true);
\r
393 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
\r
395 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
\r
403 * @param node DOCUMENT ME!
\r
404 * @param c DOCUMENT ME!
\r
406 public void setColor(SequenceNode node, Color c)
\r
413 if ((node.left() == null) && (node.right() == null))
\r
417 if (node.element() instanceof SequenceI)
\r
419 ((SequenceI) node.element()).setColor(c);
\r
425 setColor((SequenceNode) node.left(), c);
\r
426 setColor((SequenceNode) node.right(), c);
\r
433 void startPrinting()
\r
435 Thread thread = new Thread(this);
\r
439 // put printing in a thread to avoid painting problems
\r
442 PrinterJob printJob = PrinterJob.getPrinterJob();
\r
443 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
\r
445 printJob.setPrintable(this, pf);
\r
447 if (printJob.printDialog())
\r
453 catch (Exception PrintException)
\r
455 PrintException.printStackTrace();
\r
463 * @param pg DOCUMENT ME!
\r
464 * @param pf DOCUMENT ME!
\r
465 * @param pi DOCUMENT ME!
\r
467 * @return DOCUMENT ME!
\r
469 * @throws PrinterException DOCUMENT ME!
\r
471 public int print(Graphics pg, PageFormat pf, int pi)
\r
472 throws PrinterException
\r
475 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
\r
477 int pwidth = (int) pf.getImageableWidth();
\r
478 int pheight = (int) pf.getImageableHeight();
\r
480 int noPages = getHeight() / pheight;
\r
484 return Printable.NO_SUCH_PAGE;
\r
487 if (pwidth > getWidth())
\r
489 pwidth = getWidth();
\r
494 if (pheight > getHeight())
\r
496 pheight = getHeight();
\r
503 FontMetrics fm = pg.getFontMetrics(font);
\r
504 int height = fm.getHeight() * nameHash.size();
\r
505 pg.translate(0, -pi * pheight);
\r
506 pg.setClip(0, pi * pheight, pwidth, (pi * pheight) + pheight);
\r
508 // translate number of pages,
\r
509 // height is screen size as this is the
\r
510 // non overlapping text size
\r
514 draw(pg, pwidth, pheight);
\r
516 return Printable.PAGE_EXISTS;
\r
522 * @param g DOCUMENT ME!
\r
524 public void paintComponent(Graphics g)
\r
526 super.paintComponent(g);
\r
531 g.drawString("Calculating tree....", 20, getHeight()/2);
\r
535 fm = g.getFontMetrics(font);
\r
537 if (nameHash.size() == 0)
\r
544 (scrollPane.getHeight() > ( (fm.getHeight() * nameHash.size()) +
\r
547 draw(g, scrollPane.getWidth(), scrollPane.getHeight());
\r
548 setPreferredSize(null);
\r
552 setPreferredSize(new Dimension(scrollPane.getWidth(),
\r
553 fm.getHeight() * nameHash.size()));
\r
554 draw(g, scrollPane.getWidth(), fm.getHeight() * nameHash.size());
\r
557 scrollPane.revalidate();
\r
565 * @param fontSize DOCUMENT ME!
\r
567 public void setFont(Font font)
\r
576 * @param g1 DOCUMENT ME!
\r
577 * @param width DOCUMENT ME!
\r
578 * @param height DOCUMENT ME!
\r
580 public void draw(Graphics g1, int width, int height)
\r
582 Graphics2D g2 = (Graphics2D) g1;
\r
583 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
584 RenderingHints.VALUE_ANTIALIAS_ON);
\r
585 g2.setColor(Color.white);
\r
586 g2.fillRect(0, 0, width, height);
\r
590 offy = font.getSize()+10;
\r
592 fm = g2.getFontMetrics(font);
\r
594 labelLength = fm.stringWidth(longestName) + 20; //20 allows for scrollbar
\r
596 float wscale = (float) (width - labelLength - (offx * 2)) / tree.getMaxHeight();
\r
598 SequenceNode top = tree.getTopNode();
\r
600 if (top.count == 0)
\r
602 top.count = ((SequenceNode) top.left()).count +
\r
603 ((SequenceNode) top.right()).count;
\r
606 float chunk = (float) (height - (offy)) / top.count;
\r
608 drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
\r
610 if (threshold != 0)
\r
612 if (av.getCurrentTree() == tree)
\r
614 g2.setColor(Color.red);
\r
618 g2.setColor(Color.gray);
\r
621 int x = (int) ((threshold * (float) (getWidth() - labelLength -
\r
622 (2 * offx))) + offx);
\r
624 g2.drawLine(x, 0, x, getHeight());
\r
631 * @param e DOCUMENT ME!
\r
633 public void mouseReleased(MouseEvent e)
\r
640 * @param e DOCUMENT ME!
\r
642 public void mouseEntered(MouseEvent e)
\r
649 * @param e DOCUMENT ME!
\r
651 public void mouseExited(MouseEvent e)
\r
658 * @param e DOCUMENT ME!
\r
660 public void mouseClicked(MouseEvent e)
\r
667 * @param e DOCUMENT ME!
\r
669 public void mousePressed(MouseEvent e)
\r
671 av.setCurrentTree(tree);
\r
676 Object ob = findElement(x, y);
\r
678 if (ob instanceof SequenceI)
\r
680 TreeSelectionChanged((Sequence) ob);
\r
685 else if (ob instanceof SequenceNode)
\r
687 SequenceNode tmpnode = (SequenceNode) ob;
\r
688 tree.swapNodes(tmpnode);
\r
689 tree.reCount(tree.getTopNode());
\r
690 tree.findHeight(tree.getTopNode());
\r
695 if (tree.getMaxHeight() != 0)
\r
697 threshold = (float) (x - offx) / (float) (getWidth() -
\r
698 labelLength - (2 * offx));
\r
700 tree.getGroups().removeAllElements();
\r
701 tree.groupNodes(tree.getTopNode(), threshold);
\r
702 setColor(tree.getTopNode(), Color.black);
\r
704 av.setSelectionGroup(null);
\r
705 av.alignment.deleteAllGroups();
\r
707 for (int i = 0; i < tree.getGroups().size(); i++)
\r
709 Color col = new Color((int) (Math.random() * 255),
\r
710 (int) (Math.random() * 255),
\r
711 (int) (Math.random() * 255));
\r
712 setColor((SequenceNode) tree.getGroups().elementAt(i),
\r
715 Vector l = tree.findLeaves((SequenceNode) tree.getGroups()
\r
719 Vector sequences = new Vector();
\r
721 for (int j = 0; j < l.size(); j++)
\r
723 SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j)).element();
\r
725 if (!sequences.contains(s1))
\r
727 sequences.addElement(s1);
\r
731 ColourSchemeI cs = null;
\r
733 if (av.getGlobalColourScheme() != null)
\r
735 if (av.getGlobalColourScheme() instanceof UserColourScheme)
\r
737 cs = new UserColourScheme(
\r
738 ( (UserColourScheme) av.getGlobalColourScheme()).getColours());
\r
742 cs = ColourSchemeProperty.getColour(sequences,
\r
743 av.alignment.getWidth(),
\r
744 ColourSchemeProperty.getColourName(
\r
745 av.getGlobalColourScheme()));
\r
747 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
\r
748 av.getIgnoreGapsConsensus());
\r
752 SequenceGroup sg = new SequenceGroup(sequences,
\r
753 "TreeGroup", cs, true, true, false, 0,
\r
754 av.alignment.getWidth()-1);
\r
757 if ( av.getGlobalColourScheme()!=null
\r
758 && av.getGlobalColourScheme().conservationApplied())
\r
760 Conservation c = new Conservation("Group",
\r
761 ResidueProperties.propHash, 3,
\r
762 sg.getSequences(false),
\r
763 sg.getStartRes(), sg.getEndRes());
\r
766 c.verdict(false, av.ConsPercGaps);
\r
767 sg.cs.setConservation(c);
\r
770 av.alignment.addGroup(sg);
\r
775 PaintRefresher.Refresh(this, av.alignment);
\r
782 * @param state DOCUMENT ME!
\r
784 public void setShowDistances(boolean state)
\r
786 this.showDistances = state;
\r
793 * @param state DOCUMENT ME!
\r
795 public void setShowBootstrap(boolean state)
\r
797 this.showBootstrap = state;
\r
804 * @param state DOCUMENT ME!
\r
806 public void setMarkPlaceholders(boolean state)
\r
808 this.markPlaceholders = state;
\r