2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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
45 Printable, MouseMotionListener
\r
47 /** DOCUMENT ME!! */
\r
48 public static final String PLACEHOLDER = " * ";
\r
50 JScrollPane scrollPane;
\r
55 boolean fitToWindow = true;
\r
56 boolean showDistances = false;
\r
57 boolean showBootstrap = false;
\r
58 boolean markPlaceholders = false;
\r
63 int labelLength = -1;
\r
65 Hashtable nameHash = new Hashtable();
\r
66 Hashtable nodeHash = new Hashtable();
\r
67 SequenceNode highlightNode;
\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(AlignmentPanel ap, JScrollPane scroller)
\r
81 font = av.getFont();
\r
82 scrollPane = scroller;
\r
83 addMouseListener(this);
\r
84 addMouseMotionListener(this);
\r
85 PaintRefresher.Register(this, ap.av.getSequenceSetId());
\r
92 * @param sequence DOCUMENT ME!
\r
94 public void treeSelectionChanged(SequenceI sequence)
\r
96 SequenceGroup selected = av.getSelectionGroup();
\r
98 if (selected == null)
\r
100 selected = new SequenceGroup();
\r
101 av.setSelectionGroup(selected);
\r
104 selected.setEndRes(av.alignment.getWidth()-1);
\r
105 selected.addOrRemove(sequence, true);
\r
111 * @param tree DOCUMENT ME!
\r
113 public void setTree(NJTree tree)
\r
116 tree.findHeight(tree.getTopNode());
\r
118 // Now have to calculate longest name based on the leaves
\r
119 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
\r
120 boolean has_placeholders = false;
\r
123 for (int i = 0; i < leaves.size(); i++)
\r
125 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
\r
127 if (lf.isPlaceholder())
\r
129 has_placeholders = true;
\r
132 if (longestName.length() < ( (Sequence) lf.element()).getName()
\r
135 longestName = TreeCanvas.PLACEHOLDER +
\r
136 ( (Sequence) lf.element()).getName();
\r
140 setMarkPlaceholders(has_placeholders);
\r
146 * @param g DOCUMENT ME!
\r
147 * @param node DOCUMENT ME!
\r
148 * @param chunk DOCUMENT ME!
\r
149 * @param scale DOCUMENT ME!
\r
150 * @param width DOCUMENT ME!
\r
151 * @param offx DOCUMENT ME!
\r
152 * @param offy DOCUMENT ME!
\r
154 public void drawNode(Graphics g, SequenceNode node, float chunk,
\r
155 float scale, int width, int offx, int offy)
\r
162 if ((node.left() == null) && (node.right() == null))
\r
164 // Drawing leaf node
\r
165 float height = node.height;
\r
166 float dist = node.dist;
\r
168 int xstart = (int) ((height - dist) * scale) + offx;
\r
169 int xend = (int) (height * scale) + offx;
\r
171 int ypos = (int) (node.ycount * chunk) + offy;
\r
173 if (node.element() instanceof SequenceI)
\r
175 if (((SequenceI) ((SequenceNode) node).element()).getColor() == Color.white)
\r
177 g.setColor(Color.black);
\r
181 g.setColor(((SequenceI) ((SequenceNode) node).element()).getColor()
\r
187 g.setColor(Color.black);
\r
190 // Draw horizontal line
\r
191 g.drawLine(xstart, ypos, xend, ypos);
\r
193 String nodeLabel = "";
\r
195 if (showDistances && (node.dist > 0))
\r
197 nodeLabel = new Format("%-.2f").form(node.dist);
\r
204 nodeLabel = nodeLabel + " : ";
\r
207 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
\r
210 if (!nodeLabel.equals(""))
\r
212 g.drawString(nodeLabel, xstart+2, ypos - 2);
\r
215 String name = (markPlaceholders && node.isPlaceholder())
\r
216 ? (PLACEHOLDER + node.getName()) : node.getName();
\r
218 int charWidth = fm.stringWidth(name) + 3;
\r
219 int charHeight = font.getSize();
\r
221 Rectangle rect = new Rectangle(xend+10, ypos-charHeight/2,
\r
222 charWidth, charHeight);
\r
224 nameHash.put((SequenceI) node.element(), rect);
\r
226 // Colour selected leaves differently
\r
227 SequenceGroup selected = av.getSelectionGroup();
\r
229 if ((selected != null) &&
\r
230 selected.getSequences(false).contains((SequenceI) node.element()))
\r
232 g.setColor(Color.gray);
\r
234 g.fillRect(xend + 10, ypos-charHeight/2, charWidth,
\r
236 g.setColor(Color.white);
\r
239 g.drawString(name, xend + 10, ypos+fm.getDescent());
\r
240 g.setColor(Color.black);
\r
244 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
\r
246 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
\r
249 float height = node.height;
\r
250 float dist = node.dist;
\r
252 int xstart = (int) ((height - dist) * scale) + offx;
\r
253 int xend = (int) (height * scale) + offx;
\r
254 int ypos = (int) (node.ycount * chunk) + offy;
\r
256 g.setColor(((SequenceNode) node).color.darker());
\r
258 // Draw horizontal line
\r
259 g.drawLine(xstart, ypos, xend, ypos);
\r
260 if (node == highlightNode)
\r
261 g.fillRect(xend - 3, ypos - 3, 6, 6);
\r
263 g.fillRect(xend - 2, ypos - 2, 4, 4);
\r
265 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk) +
\r
267 int yend = (int) (((SequenceNode) node.right()).ycount * chunk) +
\r
270 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
\r
271 nodeHash.put(node, pos);
\r
273 g.drawLine((int) (height * scale) + offx, ystart,
\r
274 (int) (height * scale) + offx, yend);
\r
276 if (showDistances && (node.dist > 0))
\r
278 g.drawString(new Format("%-.2f").form(node.dist).trim(), xstart+2,
\r
287 * @param x DOCUMENT ME!
\r
288 * @param y DOCUMENT ME!
\r
290 * @return DOCUMENT ME!
\r
292 public Object findElement(int x, int y)
\r
294 Enumeration keys = nameHash.keys();
\r
296 while (keys.hasMoreElements())
\r
298 Object ob = keys.nextElement();
\r
299 Rectangle rect = (Rectangle) nameHash.get(ob);
\r
301 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y) &&
\r
302 (y <= (rect.y + rect.height)))
\r
308 keys = nodeHash.keys();
\r
310 while (keys.hasMoreElements())
\r
312 Object ob = keys.nextElement();
\r
313 Rectangle rect = (Rectangle) nodeHash.get(ob);
\r
315 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y) &&
\r
316 (y <= (rect.y + rect.height)))
\r
328 * @param pickBox DOCUMENT ME!
\r
330 public void pickNodes(Rectangle pickBox)
\r
332 int width = getWidth();
\r
333 int height = getHeight();
\r
335 SequenceNode top = tree.getTopNode();
\r
337 float wscale = (float) ((width * .8) - (offx * 2)) / tree.getMaxHeight();
\r
339 if (top.count == 0)
\r
341 top.count = ((SequenceNode) top.left()).count +
\r
342 ((SequenceNode) top.right()).count;
\r
345 float chunk = (float) (height - (offy)) / top.count;
\r
347 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
\r
353 * @param pickBox DOCUMENT ME!
\r
354 * @param node DOCUMENT ME!
\r
355 * @param chunk DOCUMENT ME!
\r
356 * @param scale DOCUMENT ME!
\r
357 * @param width DOCUMENT ME!
\r
358 * @param offx DOCUMENT ME!
\r
359 * @param offy DOCUMENT ME!
\r
361 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
\r
362 float scale, int width, int offx, int offy)
\r
369 if ((node.left() == null) && (node.right() == null))
\r
371 float height = node.height;
\r
372 float dist = node.dist;
\r
374 int xstart = (int) ((height - dist) * scale) + offx;
\r
375 int xend = (int) (height * scale) + offx;
\r
377 int ypos = (int) (node.ycount * chunk) + offy;
\r
379 if (pickBox.contains(new Point(xend, ypos)))
\r
381 if (node.element() instanceof SequenceI)
\r
383 SequenceI seq = (SequenceI) node.element();
\r
384 SequenceGroup sg = av.getSelectionGroup();
\r
388 sg.addOrRemove(seq, true);
\r
395 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
\r
397 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
\r
405 * @param node DOCUMENT ME!
\r
406 * @param c DOCUMENT ME!
\r
408 public void setColor(SequenceNode node, Color c)
\r
415 if ((node.left() == null) && (node.right() == null))
\r
419 if (node.element() instanceof SequenceI)
\r
421 ((SequenceI) node.element()).setColor(c);
\r
427 setColor((SequenceNode) node.left(), c);
\r
428 setColor((SequenceNode) node.right(), c);
\r
435 void startPrinting()
\r
437 Thread thread = new Thread(this);
\r
441 // put printing in a thread to avoid painting problems
\r
444 PrinterJob printJob = PrinterJob.getPrinterJob();
\r
445 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
\r
447 printJob.setPrintable(this, pf);
\r
449 if (printJob.printDialog())
\r
455 catch (Exception PrintException)
\r
457 PrintException.printStackTrace();
\r
465 * @param pg DOCUMENT ME!
\r
466 * @param pf DOCUMENT ME!
\r
467 * @param pi DOCUMENT ME!
\r
469 * @return DOCUMENT ME!
\r
471 * @throws PrinterException DOCUMENT ME!
\r
473 public int print(Graphics pg, PageFormat pf, int pi)
\r
474 throws PrinterException
\r
477 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
\r
479 int pwidth = (int) pf.getImageableWidth();
\r
480 int pheight = (int) pf.getImageableHeight();
\r
482 int noPages = getHeight() / pheight;
\r
486 return Printable.NO_SUCH_PAGE;
\r
489 if (pwidth > getWidth())
\r
491 pwidth = getWidth();
\r
496 if (pheight > getHeight())
\r
498 pheight = getHeight();
\r
505 FontMetrics fm = pg.getFontMetrics(font);
\r
506 int height = fm.getHeight() * nameHash.size();
\r
507 pg.translate(0, -pi * pheight);
\r
508 pg.setClip(0, pi * pheight, pwidth, (pi * pheight) + pheight);
\r
510 // translate number of pages,
\r
511 // height is screen size as this is the
\r
512 // non overlapping text size
\r
516 draw(pg, pwidth, pheight);
\r
518 return Printable.PAGE_EXISTS;
\r
524 * @param g DOCUMENT ME!
\r
526 public void paintComponent(Graphics g)
\r
528 super.paintComponent(g);
\r
533 g.drawString("Calculating tree....", 20, getHeight()/2);
\r
537 fm = g.getFontMetrics(font);
\r
539 if (nameHash.size() == 0)
\r
546 (scrollPane.getHeight() > ( (fm.getHeight() * nameHash.size()) +
\r
549 draw(g, scrollPane.getWidth(), scrollPane.getHeight());
\r
550 setPreferredSize(null);
\r
554 setPreferredSize(new Dimension(scrollPane.getWidth(),
\r
555 fm.getHeight() * nameHash.size()));
\r
556 draw(g, scrollPane.getWidth(), fm.getHeight() * nameHash.size());
\r
559 scrollPane.revalidate();
\r
567 * @param fontSize DOCUMENT ME!
\r
569 public void setFont(Font font)
\r
578 * @param g1 DOCUMENT ME!
\r
579 * @param width DOCUMENT ME!
\r
580 * @param height DOCUMENT ME!
\r
582 public void draw(Graphics g1, int width, int height)
\r
584 Graphics2D g2 = (Graphics2D) g1;
\r
585 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
586 RenderingHints.VALUE_ANTIALIAS_ON);
\r
587 g2.setColor(Color.white);
\r
588 g2.fillRect(0, 0, width, height);
\r
592 offy = font.getSize()+10;
\r
594 fm = g2.getFontMetrics(font);
\r
596 labelLength = fm.stringWidth(longestName) + 20; //20 allows for scrollbar
\r
598 float wscale = (float) (width - labelLength - (offx * 2)) / tree.getMaxHeight();
\r
600 SequenceNode top = tree.getTopNode();
\r
602 if (top.count == 0)
\r
604 top.count = ((SequenceNode) top.left()).count +
\r
605 ((SequenceNode) top.right()).count;
\r
608 float chunk = (float) (height - (offy)) / top.count;
\r
610 drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
\r
612 if (threshold != 0)
\r
614 if (av.getCurrentTree() == tree)
\r
616 g2.setColor(Color.red);
\r
620 g2.setColor(Color.gray);
\r
623 int x = (int) ((threshold * (float) (getWidth() - labelLength -
\r
624 (2 * offx))) + offx);
\r
626 g2.drawLine(x, 0, x, getHeight());
\r
633 * @param e DOCUMENT ME!
\r
635 public void mouseReleased(MouseEvent e)
\r
642 * @param e DOCUMENT ME!
\r
644 public void mouseEntered(MouseEvent e)
\r
651 * @param e DOCUMENT ME!
\r
653 public void mouseExited(MouseEvent e)
\r
660 * @param e DOCUMENT ME!
\r
662 public void mouseClicked(MouseEvent evt)
\r
664 if(highlightNode!=null)
\r
666 if(evt.getClickCount()>1)
\r
668 tree.swapNodes(highlightNode);
\r
669 tree.reCount(tree.getTopNode());
\r
670 tree.findHeight(tree.getTopNode());
\r
674 Vector leaves = new Vector();
\r
675 tree.findLeaves(highlightNode, leaves);
\r
677 for (int i = 0; i < leaves.size(); i++)
\r
680 (SequenceI) ( (SequenceNode) leaves.elementAt(i)).element();
\r
681 treeSelectionChanged(seq);
\r
685 PaintRefresher.Refresh(this, av.getSequenceSetId());
\r
692 public void mouseMoved(MouseEvent evt)
\r
694 av.setCurrentTree(tree);
\r
696 Object ob = findElement(evt.getX(), evt.getY());
\r
698 if (ob instanceof SequenceNode)
\r
700 highlightNode = (SequenceNode) ob;
\r
705 if (highlightNode != null)
\r
707 highlightNode = null;
\r
713 public void mouseDragged(MouseEvent ect)
\r
719 * @param e DOCUMENT ME!
\r
721 public void mousePressed(MouseEvent e)
\r
723 av.setCurrentTree(tree);
\r
728 Object ob = findElement(x, y);
\r
730 if (ob instanceof SequenceI)
\r
732 treeSelectionChanged( (Sequence) ob);
\r
733 PaintRefresher.Refresh(this, ap.av.getSequenceSetId());
\r
737 else if( !(ob instanceof SequenceNode) )
\r
740 if (tree.getMaxHeight() != 0)
\r
742 threshold = (float) (x - offx) / (float) (getWidth() -
\r
743 labelLength - (2 * offx));
\r
745 tree.getGroups().removeAllElements();
\r
746 tree.groupNodes(tree.getTopNode(), threshold);
\r
747 setColor(tree.getTopNode(), Color.black);
\r
749 av.setSelectionGroup(null);
\r
750 av.alignment.deleteAllGroups();
\r
755 PaintRefresher.Refresh(this, ap.av.getSequenceSetId());
\r
762 void colourGroups()
\r
764 for (int i = 0; i < tree.getGroups().size(); i++)
\r
766 Color col = new Color( (int) (Math.random() * 255),
\r
767 (int) (Math.random() * 255),
\r
768 (int) (Math.random() * 255));
\r
769 setColor( (SequenceNode) tree.getGroups().elementAt(i),
\r
772 Vector l = tree.findLeaves( (SequenceNode) tree.getGroups()
\r
776 Vector sequences = new Vector();
\r
778 for (int j = 0; j < l.size(); j++)
\r
780 SequenceI s1 = (SequenceI) ( (SequenceNode) l.elementAt(j)).element();
\r
782 if (!sequences.contains(s1))
\r
784 sequences.addElement(s1);
\r
788 ColourSchemeI cs = null;
\r
790 if (av.getGlobalColourScheme() != null)
\r
792 if (av.getGlobalColourScheme() instanceof UserColourScheme)
\r
794 cs = new UserColourScheme(
\r
795 ( (UserColourScheme) av.getGlobalColourScheme()).getColours());
\r
799 cs = ColourSchemeProperty.getColour(sequences,
\r
800 av.alignment.getWidth(),
\r
801 ColourSchemeProperty.
\r
803 av.getGlobalColourScheme()));
\r
805 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
\r
806 av.getIgnoreGapsConsensus());
\r
809 SequenceGroup sg = new SequenceGroup(sequences,
\r
810 "TreeGroup", cs, true, true, false,
\r
812 av.alignment.getWidth() - 1);
\r
814 if (av.getGlobalColourScheme() != null
\r
815 && av.getGlobalColourScheme().conservationApplied())
\r
817 Conservation c = new Conservation("Group",
\r
818 ResidueProperties.propHash, 3,
\r
819 sg.getSequences(false),
\r
820 sg.getStartRes(), sg.getEndRes());
\r
823 c.verdict(false, av.ConsPercGaps);
\r
824 sg.cs.setConservation(c);
\r
827 av.alignment.addGroup(sg);
\r
835 * @param state DOCUMENT ME!
\r
837 public void setShowDistances(boolean state)
\r
839 this.showDistances = state;
\r
846 * @param state DOCUMENT ME!
\r
848 public void setShowBootstrap(boolean state)
\r
850 this.showBootstrap = state;
\r
857 * @param state DOCUMENT ME!
\r
859 public void setMarkPlaceholders(boolean state)
\r
861 this.markPlaceholders = state;
\r