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("%5.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, ypos - 10);
\r
216 String name = (markPlaceholders && node.isPlaceholder())
\r
217 ? (PLACEHOLDER + node.getName()) : node.getName();
\r
218 FontMetrics fm = g.getFontMetrics(font);
\r
219 int charWidth = fm.stringWidth(name) + 3;
\r
220 int charHeight = fm.getHeight();
\r
222 Rectangle rect = new Rectangle(xend + 20, ypos - charHeight,
\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.sequences.contains((SequenceI) node.element()))
\r
233 g.setColor(Color.gray);
\r
235 g.fillRect(xend + 10, ypos - charHeight + 3, charWidth,
\r
237 g.setColor(Color.white);
\r
240 g.drawString(name, xend + 10, ypos);
\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("%5.2f").form(node.dist), xstart,
\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 * 2)) / 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 FontMetrics 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
564 * @return DOCUMENT ME!
\r
566 public int getFontSize()
\r
574 * @param fontSize DOCUMENT ME!
\r
576 public void setFontSize(int fontSize)
\r
578 this.fontSize = fontSize;
\r
579 font = new Font(font.getName(), font.getStyle(), fontSize);
\r
586 * @param g1 DOCUMENT ME!
\r
587 * @param width DOCUMENT ME!
\r
588 * @param height DOCUMENT ME!
\r
590 public void draw(Graphics g1, int width, int height)
\r
592 Graphics2D g2 = (Graphics2D) g1;
\r
593 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
594 RenderingHints.VALUE_ANTIALIAS_ON);
\r
595 g2.setColor(Color.white);
\r
596 g2.fillRect(0, 0, width, height);
\r
600 labelLength = g2.getFontMetrics(font).stringWidth(longestName) + 20; //20 allows for scrollbar
\r
602 float wscale = (float) (width - labelLength - (offx * 2)) / tree.getMaxHeight();
\r
604 SequenceNode top = tree.getTopNode();
\r
606 if (top.count == 0)
\r
608 top.count = ((SequenceNode) top.left()).count +
\r
609 ((SequenceNode) top.right()).count;
\r
612 float chunk = (float) (height - (offy * 2)) / top.count;
\r
614 drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
\r
616 if (threshold != 0)
\r
618 if (av.getCurrentTree() == tree)
\r
620 g2.setColor(Color.red);
\r
624 g2.setColor(Color.gray);
\r
627 int x = (int) ((threshold * (float) (getWidth() - labelLength -
\r
628 (2 * offx))) + offx);
\r
630 g2.drawLine(x, 0, x, getHeight());
\r
637 * @param e DOCUMENT ME!
\r
639 public void mouseReleased(MouseEvent e)
\r
646 * @param e DOCUMENT ME!
\r
648 public void mouseEntered(MouseEvent e)
\r
655 * @param e DOCUMENT ME!
\r
657 public void mouseExited(MouseEvent e)
\r
664 * @param e DOCUMENT ME!
\r
666 public void mouseClicked(MouseEvent e)
\r
673 * @param e DOCUMENT ME!
\r
675 public void mousePressed(MouseEvent e)
\r
677 av.setCurrentTree(tree);
\r
682 Object ob = findElement(x, y);
\r
684 if (ob instanceof SequenceI)
\r
686 TreeSelectionChanged((Sequence) ob);
\r
691 else if (ob instanceof SequenceNode)
\r
693 SequenceNode tmpnode = (SequenceNode) ob;
\r
694 tree.swapNodes(tmpnode);
\r
695 tree.reCount(tree.getTopNode());
\r
696 tree.findHeight(tree.getTopNode());
\r
701 if (tree.getMaxHeight() != 0)
\r
703 threshold = (float) (x - offx) / (float) (getWidth() -
\r
704 labelLength - (2 * offx));
\r
706 tree.getGroups().removeAllElements();
\r
707 tree.groupNodes(tree.getTopNode(), threshold);
\r
708 setColor(tree.getTopNode(), Color.black);
\r
710 av.setSelectionGroup(null);
\r
711 av.alignment.deleteAllGroups();
\r
713 for (int i = 0; i < tree.getGroups().size(); i++)
\r
715 Color col = new Color((int) (Math.random() * 255),
\r
716 (int) (Math.random() * 255),
\r
717 (int) (Math.random() * 255));
\r
718 setColor((SequenceNode) tree.getGroups().elementAt(i),
\r
721 Vector l = tree.findLeaves((SequenceNode) tree.getGroups()
\r
725 Vector sequences = new Vector();
\r
727 for (int j = 0; j < l.size(); j++)
\r
729 SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j)).element();
\r
731 if (!sequences.contains(s1))
\r
733 sequences.addElement(s1);
\r
737 ColourSchemeI cs = null;
\r
739 if (av.getGlobalColourScheme() != null)
\r
741 if (av.getGlobalColourScheme() instanceof UserColourScheme)
\r
743 cs = new UserColourScheme(
\r
744 ( (UserColourScheme) av.getGlobalColourScheme()).getColours());
\r
748 cs = ColourSchemeProperty.getColour(sequences,
\r
749 av.alignment.getWidth(),
\r
750 ColourSchemeProperty.getColourName(
\r
751 av.getGlobalColourScheme()));
\r
753 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
\r
754 av.getIgnoreGapsConsensus());
\r
758 SequenceGroup sg = new SequenceGroup(sequences,
\r
759 "TreeGroup", cs, true, true, false, 0,
\r
760 av.alignment.getWidth());
\r
763 if ( av.getGlobalColourScheme()!=null
\r
764 && av.getGlobalColourScheme().conservationApplied())
\r
766 Conservation c = new Conservation("Group",
\r
767 ResidueProperties.propHash, 3, sg.sequences,
\r
768 sg.getStartRes(), sg.getEndRes());
\r
771 c.verdict(false, av.ConsPercGaps);
\r
772 sg.cs.setConservation(c);
\r
775 av.alignment.addGroup(sg);
\r
780 PaintRefresher.Refresh(this, av.alignment);
\r
787 * @param state DOCUMENT ME!
\r
789 public void setShowDistances(boolean state)
\r
791 this.showDistances = state;
\r
798 * @param state DOCUMENT ME!
\r
800 public void setShowBootstrap(boolean state)
\r
802 this.showBootstrap = state;
\r
809 * @param state DOCUMENT ME!
\r
811 public void setMarkPlaceholders(boolean state)
\r
813 this.markPlaceholders = state;
\r