2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
26 import java.awt.event.*;
27 import java.awt.print.*;
30 import jalview.analysis.*;
31 import jalview.datamodel.*;
32 import jalview.schemes.*;
33 import jalview.util.*;
41 public class TreeCanvas extends JPanel implements MouseListener, Runnable,
42 Printable, MouseMotionListener
45 public static final String PLACEHOLDER = " * ";
49 JScrollPane scrollPane;
61 boolean fitToWindow = true;
63 boolean showDistances = false;
65 boolean showBootstrap = false;
67 boolean markPlaceholders = false;
79 Hashtable nameHash = new Hashtable();
81 Hashtable nodeHash = new Hashtable();
83 SequenceNode highlightNode;
85 boolean applyToAllViews = false;
88 * Creates a new TreeCanvas object.
99 public TreeCanvas(TreePanel tp, AlignmentPanel ap, JScrollPane scroller)
105 scrollPane = scroller;
106 addMouseListener(this);
107 addMouseMotionListener(this);
108 ToolTipManager.sharedInstance().registerComponent(this);
117 public void treeSelectionChanged(SequenceI sequence)
119 AlignmentPanel[] aps = getAssociatedPanels();
121 for (int a = 0; a < aps.length; a++)
123 SequenceGroup selected = aps[a].av.getSelectionGroup();
125 if (selected == null)
127 selected = new SequenceGroup();
128 aps[a].av.setSelectionGroup(selected);
131 selected.setEndRes(aps[a].av.getAlignment().getWidth() - 1);
132 selected.addOrRemove(sequence, true);
142 public void setTree(NJTree tree)
145 tree.findHeight(tree.getTopNode());
147 // Now have to calculate longest name based on the leaves
148 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
149 boolean has_placeholders = false;
152 for (int i = 0; i < leaves.size(); i++)
154 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
156 if (lf.isPlaceholder())
158 has_placeholders = true;
161 if (longestName.length() < ((Sequence) lf.element()).getName()
164 longestName = TreeCanvas.PLACEHOLDER
165 + ((Sequence) lf.element()).getName();
169 setMarkPlaceholders(has_placeholders);
190 public void drawNode(Graphics g, SequenceNode node, float chunk,
191 float scale, int width, int offx, int offy)
198 if ((node.left() == null) && (node.right() == null))
201 float height = node.height;
202 float dist = node.dist;
204 int xstart = (int) ((height - dist) * scale) + offx;
205 int xend = (int) (height * scale) + offx;
207 int ypos = (int) (node.ycount * chunk) + offy;
209 if (node.element() instanceof SequenceI)
211 SequenceI seq = (SequenceI) ((SequenceNode) node).element();
213 if (av.getSequenceColour(seq) == Color.white)
215 g.setColor(Color.black);
219 g.setColor(av.getSequenceColour(seq).darker());
224 g.setColor(Color.black);
227 // Draw horizontal line
228 g.drawLine(xstart, ypos, xend, ypos);
230 String nodeLabel = "";
232 if (showDistances && (node.dist > 0))
234 nodeLabel = new Format("%-.2f").form(node.dist);
237 if (showBootstrap && node.bootstrap > -1)
241 nodeLabel = nodeLabel + " : ";
244 nodeLabel = nodeLabel + String.valueOf(node.bootstrap);
247 if (!nodeLabel.equals(""))
249 g.drawString(nodeLabel, xstart + 2, ypos - 2);
252 String name = (markPlaceholders && node.isPlaceholder()) ? (PLACEHOLDER + node
253 .getName()) : node.getName();
255 int charWidth = fm.stringWidth(name) + 3;
256 int charHeight = font.getSize();
258 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight / 2,
259 charWidth, charHeight);
261 nameHash.put((SequenceI) node.element(), rect);
263 // Colour selected leaves differently
264 SequenceGroup selected = av.getSelectionGroup();
266 if ((selected != null)
267 && selected.getSequences(null).contains(
268 (SequenceI) node.element()))
270 g.setColor(Color.gray);
272 g.fillRect(xend + 10, ypos - charHeight / 2, charWidth, charHeight);
273 g.setColor(Color.white);
276 g.drawString(name, xend + 10, ypos + fm.getDescent());
277 g.setColor(Color.black);
281 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
283 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
286 float height = node.height;
287 float dist = node.dist;
289 int xstart = (int) ((height - dist) * scale) + offx;
290 int xend = (int) (height * scale) + offx;
291 int ypos = (int) (node.ycount * chunk) + offy;
293 g.setColor(((SequenceNode) node).color.darker());
295 // Draw horizontal line
296 g.drawLine(xstart, ypos, xend, ypos);
297 if (node == highlightNode)
299 g.fillRect(xend - 3, ypos - 3, 6, 6);
303 g.fillRect(xend - 2, ypos - 2, 4, 4);
306 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk)
308 int yend = (int) (((SequenceNode) node.right()).ycount * chunk)
311 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
312 nodeHash.put(node, pos);
314 g.drawLine((int) (height * scale) + offx, ystart,
315 (int) (height * scale) + offx, yend);
317 String nodeLabel = "";
319 if (showDistances && (node.dist > 0))
321 nodeLabel = new Format("%-.2f").form(node.dist);
324 if (showBootstrap && node.bootstrap > -1)
328 nodeLabel = nodeLabel + " : ";
331 nodeLabel = nodeLabel + String.valueOf(node.bootstrap);
334 if (!nodeLabel.equals(""))
336 g.drawString(nodeLabel, xstart + 2, ypos - 2);
349 * @return DOCUMENT ME!
351 public Object findElement(int x, int y)
353 Enumeration keys = nameHash.keys();
355 while (keys.hasMoreElements())
357 Object ob = keys.nextElement();
358 Rectangle rect = (Rectangle) nameHash.get(ob);
360 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y)
361 && (y <= (rect.y + rect.height)))
367 keys = nodeHash.keys();
369 while (keys.hasMoreElements())
371 Object ob = keys.nextElement();
372 Rectangle rect = (Rectangle) nodeHash.get(ob);
374 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y)
375 && (y <= (rect.y + rect.height)))
390 public void pickNodes(Rectangle pickBox)
392 int width = getWidth();
393 int height = getHeight();
395 SequenceNode top = tree.getTopNode();
397 float wscale = (float) ((width * .8) - (offx * 2))
398 / tree.getMaxHeight();
402 top.count = ((SequenceNode) top.left()).count
403 + ((SequenceNode) top.right()).count;
406 float chunk = (float) (height - (offy)) / top.count;
408 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
429 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
430 float scale, int width, int offx, int offy)
437 if ((node.left() == null) && (node.right() == null))
439 float height = node.height;
440 float dist = node.dist;
442 int xstart = (int) ((height - dist) * scale) + offx;
443 int xend = (int) (height * scale) + offx;
445 int ypos = (int) (node.ycount * chunk) + offy;
447 if (pickBox.contains(new Point(xend, ypos)))
449 if (node.element() instanceof SequenceI)
451 SequenceI seq = (SequenceI) node.element();
452 SequenceGroup sg = av.getSelectionGroup();
456 sg.addOrRemove(seq, true);
463 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
465 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
478 public void setColor(SequenceNode node, Color c)
485 if ((node.left() == null) && (node.right() == null)) // TODO: internal node
489 if (node.element() instanceof SequenceI)
491 AlignmentPanel[] aps = getAssociatedPanels();
494 for (int a = 0; a < aps.length; a++)
496 aps[a].av.setSequenceColour((SequenceI) node.element(), c);
504 setColor((SequenceNode) node.left(), c);
505 setColor((SequenceNode) node.right(), c);
514 Thread thread = new Thread(this);
518 // put printing in a thread to avoid painting problems
521 PrinterJob printJob = PrinterJob.getPrinterJob();
522 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
524 printJob.setPrintable(this, pf);
526 if (printJob.printDialog())
531 } catch (Exception PrintException)
533 PrintException.printStackTrace();
548 * @return DOCUMENT ME!
550 * @throws PrinterException
553 public int print(Graphics pg, PageFormat pf, int pi)
554 throws PrinterException
557 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
559 int pwidth = (int) pf.getImageableWidth();
560 int pheight = (int) pf.getImageableHeight();
562 int noPages = getHeight() / pheight;
566 return Printable.NO_SUCH_PAGE;
569 if (pwidth > getWidth())
576 if (pheight > getHeight())
578 pheight = getHeight();
585 FontMetrics fm = pg.getFontMetrics(font);
586 int height = fm.getHeight() * nameHash.size();
587 pg.translate(0, -pi * pheight);
588 pg.setClip(0, pi * pheight, pwidth, (pi * pheight) + pheight);
590 // translate number of pages,
591 // height is screen size as this is the
592 // non overlapping text size
596 draw(pg, pwidth, pheight);
598 return Printable.PAGE_EXISTS;
607 public void paintComponent(Graphics g)
609 super.paintComponent(g);
614 g.drawString(MessageManager.getString("label.calculating_tree") + "....", 20, getHeight() / 2);
618 fm = g.getFontMetrics(font);
620 if (nameHash.size() == 0)
626 || (!fitToWindow && (scrollPane.getHeight() > ((fm
627 .getHeight() * nameHash.size()) + offy))))
629 draw(g, scrollPane.getWidth(), scrollPane.getHeight());
630 setPreferredSize(null);
634 setPreferredSize(new Dimension(scrollPane.getWidth(),
635 fm.getHeight() * nameHash.size()));
636 draw(g, scrollPane.getWidth(), fm.getHeight() * nameHash.size());
639 scrollPane.revalidate();
649 public void setFont(Font font)
665 public void draw(Graphics g1, int width, int height)
667 Graphics2D g2 = (Graphics2D) g1;
668 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
669 RenderingHints.VALUE_ANTIALIAS_ON);
670 g2.setColor(Color.white);
671 g2.fillRect(0, 0, width, height);
674 if (longestName==null || tree ==null)
676 g2.drawString("Calculating tree.",20,20);
678 offy = font.getSize() + 10;
680 fm = g2.getFontMetrics(font);
682 labelLength = fm.stringWidth(longestName) + 20; // 20 allows for scrollbar
684 float wscale = (float) (width - labelLength - (offx * 2))
685 / tree.getMaxHeight();
687 SequenceNode top = tree.getTopNode();
691 top.count = ((SequenceNode) top.left()).count
692 + ((SequenceNode) top.right()).count;
695 float chunk = (float) (height - (offy)) / top.count;
697 drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
701 if (av.getCurrentTree() == tree)
703 g2.setColor(Color.red);
707 g2.setColor(Color.gray);
710 int x = (int) ((threshold * (float) (getWidth() - labelLength - (2 * offx))) + offx);
712 g2.drawLine(x, 0, x, getHeight());
722 public void mouseReleased(MouseEvent e)
732 public void mouseEntered(MouseEvent e)
742 public void mouseExited(MouseEvent e)
752 public void mouseClicked(MouseEvent evt)
754 if (highlightNode != null)
756 if (SwingUtilities.isRightMouseButton(evt))
758 Color col = JColorChooser.showDialog(this,
759 "Select Sub-Tree Colour", highlightNode.color);
762 setColor(highlightNode, col);
765 else if (evt.getClickCount() > 1)
767 tree.swapNodes(highlightNode);
768 tree.reCount(tree.getTopNode());
769 tree.findHeight(tree.getTopNode());
773 Vector leaves = new Vector();
774 tree.findLeaves(highlightNode, leaves);
776 for (int i = 0; i < leaves.size(); i++)
778 SequenceI seq = (SequenceI) ((SequenceNode) leaves.elementAt(i))
780 treeSelectionChanged(seq);
785 PaintRefresher.Refresh(tp, av.getSequenceSetId());
790 public void mouseMoved(MouseEvent evt)
792 av.setCurrentTree(tree);
794 Object ob = findElement(evt.getX(), evt.getY());
796 if (ob instanceof SequenceNode)
798 highlightNode = (SequenceNode) ob;
799 this.setToolTipText("<html>" + MessageManager.getString("label.highlightnode"));
805 if (highlightNode != null)
807 highlightNode = null;
808 setToolTipText(null);
814 public void mouseDragged(MouseEvent ect)
824 public void mousePressed(MouseEvent e)
826 av.setCurrentTree(tree);
831 Object ob = findElement(x, y);
833 if (ob instanceof SequenceI)
835 treeSelectionChanged((Sequence) ob);
836 PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
841 else if (!(ob instanceof SequenceNode))
844 if (tree.getMaxHeight() != 0)
846 threshold = (float) (x - offx)
847 / (float) (getWidth() - labelLength - (2 * offx));
849 tree.getGroups().removeAllElements();
850 tree.groupNodes(tree.getTopNode(), threshold);
851 setColor(tree.getTopNode(), Color.black);
853 AlignmentPanel[] aps = getAssociatedPanels();
855 for (int a = 0; a < aps.length; a++)
857 aps[a].av.setSelectionGroup(null);
858 aps[a].av.getAlignment().deleteAllGroups();
859 aps[a].av.clearSequenceColours();
864 PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
872 AlignmentPanel[] aps = getAssociatedPanels();
873 for (int i = 0; i < tree.getGroups().size(); i++)
875 Color col = new Color((int) (Math.random() * 255),
876 (int) (Math.random() * 255), (int) (Math.random() * 255));
877 setColor((SequenceNode) tree.getGroups().elementAt(i), col.brighter());
879 Vector l = tree.findLeaves(
880 (SequenceNode) tree.getGroups().elementAt(i), new Vector());
882 Vector sequences = new Vector();
884 for (int j = 0; j < l.size(); j++)
886 SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j))
889 if (!sequences.contains(s1))
891 sequences.addElement(s1);
895 ColourSchemeI cs = null;
896 SequenceGroup sg = new SequenceGroup(sequences, null, cs, true, true,
897 false, 0, av.getAlignment().getWidth() - 1);
899 if (av.getGlobalColourScheme() != null)
901 if (av.getGlobalColourScheme() instanceof UserColourScheme)
903 cs = new UserColourScheme(
904 ((UserColourScheme) av.getGlobalColourScheme())
910 cs = ColourSchemeProperty.getColour(sg, ColourSchemeProperty
911 .getColourName(av.getGlobalColourScheme()));
913 // cs is null if shading is an annotationColourGradient
916 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
917 av.getIgnoreGapsConsensus());
921 // sg.recalcConservation();
922 sg.setName("JTreeGroup:" + sg.hashCode());
924 for (int a = 0; a < aps.length; a++)
926 if (aps[a].av.getGlobalColourScheme() != null
927 && aps[a].av.getGlobalColourScheme().conservationApplied())
929 Conservation c = new Conservation("Group",
930 ResidueProperties.propHash, 3, sg.getSequences(null),
931 sg.getStartRes(), sg.getEndRes());
934 c.verdict(false, aps[a].av.getConsPercGaps());
935 sg.cs.setConservation(c);
938 aps[a].av.getAlignment().addGroup(new SequenceGroup(sg));
941 // notify the panel to redo any group specific stuff.
942 for (int a = 0; a < aps.length; a++)
944 aps[a].updateAnnotation();
945 // TODO: JAL-868 - need to ensure view colour change message is broadcast
946 // to any Jmols listening in
957 public void setShowDistances(boolean state)
959 this.showDistances = state;
969 public void setShowBootstrap(boolean state)
971 this.showBootstrap = state;
981 public void setMarkPlaceholders(boolean state)
983 this.markPlaceholders = state;
987 AlignmentPanel[] getAssociatedPanels()
991 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
995 return new AlignmentPanel[]