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, NJTree tree, JScrollPane scroller,
\r
82 scrollPane = scroller;
\r
83 addMouseListener(this);
\r
84 tree.findHeight(tree.getTopNode());
\r
85 longestName = label;
\r
87 PaintRefresher.Register(this, av.alignment);
\r
93 * @param sequence DOCUMENT ME!
\r
95 public void TreeSelectionChanged(Sequence sequence)
\r
97 SequenceGroup selected = av.getSelectionGroup();
\r
99 if (selected == null)
\r
101 selected = new SequenceGroup();
\r
102 av.setSelectionGroup(selected);
\r
105 selected.setEndRes(av.alignment.getWidth());
\r
106 selected.addOrRemove(sequence, true);
\r
108 PaintRefresher.Refresh(this, av.alignment);
\r
115 * @param tree DOCUMENT ME!
\r
117 public void setTree(NJTree tree)
\r
120 tree.findHeight(tree.getTopNode());
\r
126 * @param g DOCUMENT ME!
\r
127 * @param node DOCUMENT ME!
\r
128 * @param chunk DOCUMENT ME!
\r
129 * @param scale DOCUMENT ME!
\r
130 * @param width DOCUMENT ME!
\r
131 * @param offx DOCUMENT ME!
\r
132 * @param offy DOCUMENT ME!
\r
134 public void drawNode(Graphics g, SequenceNode node, float chunk,
\r
135 float scale, int width, int offx, int offy)
\r
142 if ((node.left() == null) && (node.right() == null))
\r
144 // Drawing leaf node
\r
145 float height = node.height;
\r
146 float dist = node.dist;
\r
148 int xstart = (int) ((height - dist) * scale) + offx;
\r
149 int xend = (int) (height * scale) + offx;
\r
151 int ypos = (int) (node.ycount * chunk) + offy;
\r
153 if (node.element() instanceof SequenceI)
\r
155 if (((SequenceI) ((SequenceNode) node).element()).getColor() == Color.white)
\r
157 g.setColor(Color.black);
\r
161 g.setColor(((SequenceI) ((SequenceNode) node).element()).getColor()
\r
167 g.setColor(Color.black);
\r
170 // Draw horizontal line
\r
171 g.drawLine(xstart, ypos, xend, ypos);
\r
173 String nodeLabel = "";
\r
175 if (showDistances && (node.dist > 0))
\r
177 nodeLabel = new Format("%5.2f").form(node.dist);
\r
184 nodeLabel = nodeLabel + " : ";
\r
187 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
\r
190 if (!nodeLabel.equals(""))
\r
192 g.drawString(nodeLabel, xstart, ypos - 10);
\r
195 String name = (markPlaceholders && node.isPlaceholder())
\r
196 ? (PLACEHOLDER + node.getName()) : node.getName();
\r
197 FontMetrics fm = g.getFontMetrics(font);
\r
198 int charWidth = fm.stringWidth(name) + 3;
\r
199 int charHeight = fm.getHeight();
\r
201 Rectangle rect = new Rectangle(xend + 20, ypos - charHeight,
\r
202 charWidth, charHeight);
\r
204 nameHash.put((SequenceI) node.element(), rect);
\r
206 // Colour selected leaves differently
\r
207 SequenceGroup selected = av.getSelectionGroup();
\r
209 if ((selected != null) &&
\r
210 selected.sequences.contains((SequenceI) node.element()))
\r
212 g.setColor(Color.gray);
\r
214 g.fillRect(xend + 10, ypos - charHeight + 3, charWidth,
\r
216 g.setColor(Color.white);
\r
219 g.drawString(name, xend + 10, ypos);
\r
220 g.setColor(Color.black);
\r
224 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
\r
226 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
\r
229 float height = node.height;
\r
230 float dist = node.dist;
\r
232 int xstart = (int) ((height - dist) * scale) + offx;
\r
233 int xend = (int) (height * scale) + offx;
\r
234 int ypos = (int) (node.ycount * chunk) + offy;
\r
236 g.setColor(((SequenceNode) node).color.darker());
\r
238 // Draw horizontal line
\r
239 g.drawLine(xstart, ypos, xend, ypos);
\r
240 g.fillRect(xend - 2, ypos - 2, 4, 4);
\r
242 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk) +
\r
244 int yend = (int) (((SequenceNode) node.right()).ycount * chunk) +
\r
247 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
\r
248 nodeHash.put(node, pos);
\r
250 g.drawLine((int) (height * scale) + offx, ystart,
\r
251 (int) (height * scale) + offx, yend);
\r
253 if (showDistances && (node.dist > 0))
\r
255 g.drawString(new Format("%5.2f").form(node.dist), xstart,
\r
264 * @param x DOCUMENT ME!
\r
265 * @param y DOCUMENT ME!
\r
267 * @return DOCUMENT ME!
\r
269 public Object findElement(int x, int y)
\r
271 Enumeration keys = nameHash.keys();
\r
273 while (keys.hasMoreElements())
\r
275 Object ob = keys.nextElement();
\r
276 Rectangle rect = (Rectangle) nameHash.get(ob);
\r
278 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y) &&
\r
279 (y <= (rect.y + rect.height)))
\r
285 keys = nodeHash.keys();
\r
287 while (keys.hasMoreElements())
\r
289 Object ob = keys.nextElement();
\r
290 Rectangle rect = (Rectangle) nodeHash.get(ob);
\r
292 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y) &&
\r
293 (y <= (rect.y + rect.height)))
\r
305 * @param pickBox DOCUMENT ME!
\r
307 public void pickNodes(Rectangle pickBox)
\r
309 int width = getWidth();
\r
310 int height = getHeight();
\r
312 SequenceNode top = tree.getTopNode();
\r
314 float wscale = (float) ((width * .8) - (offx * 2)) / tree.getMaxHeight();
\r
316 if (top.count == 0)
\r
318 top.count = ((SequenceNode) top.left()).count +
\r
319 ((SequenceNode) top.right()).count;
\r
322 float chunk = (float) (height - (offy * 2)) / top.count;
\r
324 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
\r
330 * @param pickBox DOCUMENT ME!
\r
331 * @param node DOCUMENT ME!
\r
332 * @param chunk DOCUMENT ME!
\r
333 * @param scale DOCUMENT ME!
\r
334 * @param width DOCUMENT ME!
\r
335 * @param offx DOCUMENT ME!
\r
336 * @param offy DOCUMENT ME!
\r
338 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
\r
339 float scale, int width, int offx, int offy)
\r
346 if ((node.left() == null) && (node.right() == null))
\r
348 float height = node.height;
\r
349 float dist = node.dist;
\r
351 int xstart = (int) ((height - dist) * scale) + offx;
\r
352 int xend = (int) (height * scale) + offx;
\r
354 int ypos = (int) (node.ycount * chunk) + offy;
\r
356 if (pickBox.contains(new Point(xend, ypos)))
\r
358 if (node.element() instanceof SequenceI)
\r
360 SequenceI seq = (SequenceI) node.element();
\r
361 SequenceGroup sg = av.getSelectionGroup();
\r
365 sg.addOrRemove(seq, true);
\r
372 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
\r
374 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
\r
382 * @param node DOCUMENT ME!
\r
383 * @param c DOCUMENT ME!
\r
385 public void setColor(SequenceNode node, Color c)
\r
392 if ((node.left() == null) && (node.right() == null))
\r
396 if (node.element() instanceof SequenceI)
\r
398 ((SequenceI) node.element()).setColor(c);
\r
404 setColor((SequenceNode) node.left(), c);
\r
405 setColor((SequenceNode) node.right(), c);
\r
412 void startPrinting()
\r
414 Thread thread = new Thread(this);
\r
418 // put printing in a thread to avoid painting problems
\r
421 PrinterJob printJob = PrinterJob.getPrinterJob();
\r
422 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
\r
424 printJob.setPrintable(this, pf);
\r
426 if (printJob.printDialog())
\r
432 catch (Exception PrintException)
\r
434 PrintException.printStackTrace();
\r
442 * @param pg DOCUMENT ME!
\r
443 * @param pf DOCUMENT ME!
\r
444 * @param pi DOCUMENT ME!
\r
446 * @return DOCUMENT ME!
\r
448 * @throws PrinterException DOCUMENT ME!
\r
450 public int print(Graphics pg, PageFormat pf, int pi)
\r
451 throws PrinterException
\r
454 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
\r
456 int pwidth = (int) pf.getImageableWidth();
\r
457 int pheight = (int) pf.getImageableHeight();
\r
459 int noPages = getHeight() / pheight;
\r
463 return Printable.NO_SUCH_PAGE;
\r
466 if (pwidth > getWidth())
\r
468 pwidth = getWidth();
\r
473 if (pheight > getHeight())
\r
475 pheight = getHeight();
\r
482 FontMetrics fm = pg.getFontMetrics(font);
\r
483 int height = fm.getHeight() * nameHash.size();
\r
484 pg.translate(0, -pi * pheight);
\r
485 pg.setClip(0, pi * pheight, pwidth, (pi * pheight) + pheight);
\r
487 // translate number of pages,
\r
488 // height is screen size as this is the
\r
489 // non overlapping text size
\r
493 draw(pg, pwidth, pheight);
\r
495 return Printable.PAGE_EXISTS;
\r
501 * @param g DOCUMENT ME!
\r
503 public void paintComponent(Graphics g)
\r
505 super.paintComponent(g);
\r
506 font = new Font("Verdana", Font.PLAIN, fontSize);
\r
509 FontMetrics fm = g.getFontMetrics(font);
\r
511 if (nameHash.size() == 0)
\r
518 (scrollPane.getHeight() > ((fm.getHeight() * nameHash.size()) +
\r
521 draw(g, scrollPane.getWidth(), scrollPane.getHeight());
\r
522 setPreferredSize(null);
\r
526 setPreferredSize(new Dimension(scrollPane.getWidth(),
\r
527 fm.getHeight() * nameHash.size()));
\r
528 draw(g, scrollPane.getWidth(), fm.getHeight() * nameHash.size());
\r
531 scrollPane.revalidate();
\r
537 * @return DOCUMENT ME!
\r
539 public int getFontSize()
\r
547 * @param fontSize DOCUMENT ME!
\r
549 public void setFontSize(int fontSize)
\r
551 this.fontSize = fontSize;
\r
558 * @param g1 DOCUMENT ME!
\r
559 * @param width DOCUMENT ME!
\r
560 * @param height DOCUMENT ME!
\r
562 public void draw(Graphics g1, int width, int height)
\r
564 Graphics2D g2 = (Graphics2D) g1;
\r
565 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
566 RenderingHints.VALUE_ANTIALIAS_ON);
\r
567 g2.setColor(Color.white);
\r
568 g2.fillRect(0, 0, width, height);
\r
570 labelLength = g2.getFontMetrics(font).stringWidth(longestName) + 20; //20 allows for scrollbar
\r
572 float wscale = (float) (width - labelLength - (offx * 2)) / tree.getMaxHeight();
\r
574 SequenceNode top = tree.getTopNode();
\r
576 if (top.count == 0)
\r
578 top.count = ((SequenceNode) top.left()).count +
\r
579 ((SequenceNode) top.right()).count;
\r
582 float chunk = (float) (height - (offy * 2)) / top.count;
\r
584 drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
\r
586 if (threshold != 0)
\r
588 if (av.getCurrentTree() == tree)
\r
590 g2.setColor(Color.red);
\r
594 g2.setColor(Color.gray);
\r
597 int x = (int) ((threshold * (float) (getWidth() - labelLength -
\r
598 (2 * offx))) + offx);
\r
600 g2.drawLine(x, 0, x, getHeight());
\r
607 * @param e DOCUMENT ME!
\r
609 public void mouseReleased(MouseEvent e)
\r
616 * @param e DOCUMENT ME!
\r
618 public void mouseEntered(MouseEvent e)
\r
625 * @param e DOCUMENT ME!
\r
627 public void mouseExited(MouseEvent e)
\r
634 * @param e DOCUMENT ME!
\r
636 public void mouseClicked(MouseEvent e)
\r
643 * @param e DOCUMENT ME!
\r
645 public void mousePressed(MouseEvent e)
\r
647 av.setCurrentTree(tree);
\r
652 Object ob = findElement(x, y);
\r
654 if (ob instanceof SequenceI)
\r
656 TreeSelectionChanged((Sequence) ob);
\r
661 else if (ob instanceof SequenceNode)
\r
663 SequenceNode tmpnode = (SequenceNode) ob;
\r
664 tree.swapNodes(tmpnode);
\r
665 tree.reCount(tree.getTopNode());
\r
666 tree.findHeight(tree.getTopNode());
\r
671 if (tree.getMaxHeight() != 0)
\r
673 threshold = (float) (x - offx) / (float) (getWidth() -
\r
674 labelLength - (2 * offx));
\r
676 tree.getGroups().removeAllElements();
\r
677 tree.groupNodes(tree.getTopNode(), threshold);
\r
678 setColor(tree.getTopNode(), Color.black);
\r
680 av.setSelectionGroup(null);
\r
681 av.alignment.deleteAllGroups();
\r
683 for (int i = 0; i < tree.getGroups().size(); i++)
\r
685 Color col = new Color((int) (Math.random() * 255),
\r
686 (int) (Math.random() * 255),
\r
687 (int) (Math.random() * 255));
\r
688 setColor((SequenceNode) tree.getGroups().elementAt(i),
\r
691 Vector l = tree.findLeaves((SequenceNode) tree.getGroups()
\r
695 Vector sequences = new Vector();
\r
697 for (int j = 0; j < l.size(); j++)
\r
699 SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j)).element();
\r
701 if (!sequences.contains(s1))
\r
703 sequences.addElement(s1);
\r
707 ColourSchemeI cs = ColourSchemeProperty.getColour(sequences,
\r
708 av.alignment.getWidth(),
\r
709 ColourSchemeProperty.getColourName(
\r
710 av.getGlobalColourScheme()));
\r
712 SequenceGroup sg = new SequenceGroup(sequences,
\r
713 "TreeGroup", cs, true, true, false, 0,
\r
714 av.alignment.getWidth());
\r
718 ((ResidueColourScheme) sg.cs).setThreshold(25);
\r
721 if (av.getGlobalColourScheme() instanceof ConservationColourScheme)
\r
723 ConservationColourScheme ccs = (ConservationColourScheme) av.getGlobalColourScheme();
\r
724 Conservation c = new Conservation("Group",
\r
725 ResidueProperties.propHash, 3, sg.sequences,
\r
726 sg.getStartRes(), sg.getEndRes());
\r
729 c.verdict(false, av.ConsPercGaps);
\r
730 ccs = new ConservationColourScheme(c, ccs.cs);
\r
735 av.alignment.addGroup(sg);
\r
740 PaintRefresher.Refresh(this, av.alignment);
\r
747 * @param state DOCUMENT ME!
\r
749 public void setShowDistances(boolean state)
\r
751 this.showDistances = state;
\r
758 * @param state DOCUMENT ME!
\r
760 public void setShowBootstrap(boolean state)
\r
762 this.showBootstrap = state;
\r
769 * @param state DOCUMENT ME!
\r
771 public void setMarkPlaceholders(boolean state)
\r
773 this.markPlaceholders = state;
\r