JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / gui / TreeCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
23 import jalview.analysis.Conservation;
24 import jalview.analysis.NJTree;
25 import jalview.api.AlignViewportI;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.datamodel.SequenceI;
29 import jalview.datamodel.SequenceNode;
30 import jalview.schemes.ColourSchemeI;
31 import jalview.schemes.ColourSchemeProperty;
32 import jalview.schemes.ResidueProperties;
33 import jalview.schemes.UserColourScheme;
34 import jalview.structure.SelectionSource;
35 import jalview.util.Format;
36 import jalview.util.MappingUtils;
37 import jalview.util.MessageManager;
38
39 import java.awt.Color;
40 import java.awt.Dimension;
41 import java.awt.Font;
42 import java.awt.FontMetrics;
43 import java.awt.Graphics;
44 import java.awt.Graphics2D;
45 import java.awt.Point;
46 import java.awt.Rectangle;
47 import java.awt.RenderingHints;
48 import java.awt.event.MouseEvent;
49 import java.awt.event.MouseListener;
50 import java.awt.event.MouseMotionListener;
51 import java.awt.print.PageFormat;
52 import java.awt.print.Printable;
53 import java.awt.print.PrinterException;
54 import java.awt.print.PrinterJob;
55 import java.util.Enumeration;
56 import java.util.Hashtable;
57 import java.util.Vector;
58
59 import javax.swing.JColorChooser;
60 import javax.swing.JPanel;
61 import javax.swing.JScrollPane;
62 import javax.swing.SwingUtilities;
63 import javax.swing.ToolTipManager;
64
65 /**
66  * DOCUMENT ME!
67  * 
68  * @author $author$
69  * @version $Revision$
70  */
71 public class TreeCanvas extends JPanel implements MouseListener, Runnable,
72         Printable, MouseMotionListener, SelectionSource
73 {
74   /** DOCUMENT ME!! */
75   public static final String PLACEHOLDER = " * ";
76
77   NJTree tree;
78
79   JScrollPane scrollPane;
80
81   TreePanel tp;
82
83   AlignViewport av;
84
85   AlignmentPanel ap;
86
87   Font font;
88
89   FontMetrics fm;
90
91   boolean fitToWindow = true;
92
93   boolean showDistances = false;
94
95   boolean showBootstrap = false;
96
97   boolean markPlaceholders = false;
98
99   int offx = 20;
100
101   int offy;
102
103   float threshold;
104
105   String longestName;
106
107   int labelLength = -1;
108
109   Hashtable nameHash = new Hashtable();
110
111   Hashtable nodeHash = new Hashtable();
112
113   SequenceNode highlightNode;
114
115   boolean applyToAllViews = false;
116
117   /**
118    * Creates a new TreeCanvas object.
119    * 
120    * @param av
121    *          DOCUMENT ME!
122    * @param tree
123    *          DOCUMENT ME!
124    * @param scroller
125    *          DOCUMENT ME!
126    * @param label
127    *          DOCUMENT ME!
128    */
129   public TreeCanvas(TreePanel tp, AlignmentPanel ap, JScrollPane scroller)
130   {
131     this.tp = tp;
132     this.av = ap.av;
133     this.ap = ap;
134     font = av.getFont();
135     scrollPane = scroller;
136     addMouseListener(this);
137     addMouseMotionListener(this);
138     ToolTipManager.sharedInstance().registerComponent(this);
139   }
140
141   /**
142    * DOCUMENT ME!
143    * 
144    * @param sequence
145    *          DOCUMENT ME!
146    */
147   public void treeSelectionChanged(SequenceI sequence)
148   {
149     AlignmentPanel[] aps = getAssociatedPanels();
150
151     for (int a = 0; a < aps.length; a++)
152     {
153       SequenceGroup selected = aps[a].av.getSelectionGroup();
154
155       if (selected == null)
156       {
157         selected = new SequenceGroup();
158         aps[a].av.setSelectionGroup(selected);
159       }
160
161       selected.setEndRes(aps[a].av.getAlignment().getWidth() - 1);
162       selected.addOrRemove(sequence, true);
163     }
164   }
165
166   /**
167    * DOCUMENT ME!
168    * 
169    * @param tree
170    *          DOCUMENT ME!
171    */
172   public void setTree(NJTree tree)
173   {
174     this.tree = tree;
175     tree.findHeight(tree.getTopNode());
176
177     // Now have to calculate longest name based on the leaves
178     Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
179     boolean has_placeholders = false;
180     longestName = "";
181
182     for (int i = 0; i < leaves.size(); i++)
183     {
184       SequenceNode lf = (SequenceNode) leaves.elementAt(i);
185
186       if (lf.isPlaceholder())
187       {
188         has_placeholders = true;
189       }
190
191       if (longestName.length() < ((Sequence) lf.element()).getName()
192               .length())
193       {
194         longestName = TreeCanvas.PLACEHOLDER
195                 + ((Sequence) lf.element()).getName();
196       }
197     }
198
199     setMarkPlaceholders(has_placeholders);
200   }
201
202   /**
203    * DOCUMENT ME!
204    * 
205    * @param g
206    *          DOCUMENT ME!
207    * @param node
208    *          DOCUMENT ME!
209    * @param chunk
210    *          DOCUMENT ME!
211    * @param scale
212    *          DOCUMENT ME!
213    * @param width
214    *          DOCUMENT ME!
215    * @param offx
216    *          DOCUMENT ME!
217    * @param offy
218    *          DOCUMENT ME!
219    */
220   public void drawNode(Graphics g, SequenceNode node, float chunk,
221           float scale, int width, int offx, int offy)
222   {
223     if (node == null)
224     {
225       return;
226     }
227
228     if ((node.left() == null) && (node.right() == null))
229     {
230       // Drawing leaf node
231       float height = node.height;
232       float dist = node.dist;
233
234       int xstart = (int) ((height - dist) * scale) + offx;
235       int xend = (int) (height * scale) + offx;
236
237       int ypos = (int) (node.ycount * chunk) + offy;
238
239       if (node.element() instanceof SequenceI)
240       {
241         SequenceI seq = (SequenceI) node.element();
242
243         if (av.getSequenceColour(seq) == Color.white)
244         {
245           g.setColor(Color.black);
246         }
247         else
248         {
249           g.setColor(av.getSequenceColour(seq).darker());
250         }
251       }
252       else
253       {
254         g.setColor(Color.black);
255       }
256
257       // Draw horizontal line
258       g.drawLine(xstart, ypos, xend, ypos);
259
260       String nodeLabel = "";
261
262       if (showDistances && (node.dist > 0))
263       {
264         nodeLabel = new Format("%-.2f").form(node.dist);
265       }
266
267       if (showBootstrap && node.bootstrap > -1)
268       {
269         if (showDistances)
270         {
271           nodeLabel = nodeLabel + " : ";
272         }
273
274         nodeLabel = nodeLabel + String.valueOf(node.bootstrap);
275       }
276
277       if (!nodeLabel.equals(""))
278       {
279         g.drawString(nodeLabel, xstart + 2, ypos - 2);
280       }
281
282       String name = (markPlaceholders && node.isPlaceholder()) ? (PLACEHOLDER + node
283               .getName()) : node.getName();
284
285       int charWidth = fm.stringWidth(name) + 3;
286       int charHeight = font.getSize();
287
288       Rectangle rect = new Rectangle(xend + 10, ypos - charHeight / 2,
289               charWidth, charHeight);
290
291       nameHash.put(node.element(), rect);
292
293       // Colour selected leaves differently
294       SequenceGroup selected = av.getSelectionGroup();
295
296       if ((selected != null)
297               && selected.getSequences(null).contains(node.element()))
298       {
299         g.setColor(Color.gray);
300
301         g.fillRect(xend + 10, ypos - charHeight / 2, charWidth, charHeight);
302         g.setColor(Color.white);
303       }
304
305       g.drawString(name, xend + 10, ypos + fm.getDescent());
306       g.setColor(Color.black);
307     }
308     else
309     {
310       drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
311               offy);
312       drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
313               offy);
314
315       float height = node.height;
316       float dist = node.dist;
317
318       int xstart = (int) ((height - dist) * scale) + offx;
319       int xend = (int) (height * scale) + offx;
320       int ypos = (int) (node.ycount * chunk) + offy;
321
322       g.setColor(node.color.darker());
323
324       // Draw horizontal line
325       g.drawLine(xstart, ypos, xend, ypos);
326       if (node == highlightNode)
327       {
328         g.fillRect(xend - 3, ypos - 3, 6, 6);
329       }
330       else
331       {
332         g.fillRect(xend - 2, ypos - 2, 4, 4);
333       }
334
335       int ystart = (int) (((SequenceNode) node.left()).ycount * chunk)
336               + offy;
337       int yend = (int) (((SequenceNode) node.right()).ycount * chunk)
338               + offy;
339
340       Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
341       nodeHash.put(node, pos);
342
343       g.drawLine((int) (height * scale) + offx, ystart,
344               (int) (height * scale) + offx, yend);
345
346       String nodeLabel = "";
347
348       if (showDistances && (node.dist > 0))
349       {
350         nodeLabel = new Format("%-.2f").form(node.dist);
351       }
352
353       if (showBootstrap && node.bootstrap > -1)
354       {
355         if (showDistances)
356         {
357           nodeLabel = nodeLabel + " : ";
358         }
359
360         nodeLabel = nodeLabel + String.valueOf(node.bootstrap);
361       }
362
363       if (!nodeLabel.equals(""))
364       {
365         g.drawString(nodeLabel, xstart + 2, ypos - 2);
366       }
367     }
368   }
369
370   /**
371    * DOCUMENT ME!
372    * 
373    * @param x
374    *          DOCUMENT ME!
375    * @param y
376    *          DOCUMENT ME!
377    * 
378    * @return DOCUMENT ME!
379    */
380   public Object findElement(int x, int y)
381   {
382     Enumeration keys = nameHash.keys();
383
384     while (keys.hasMoreElements())
385     {
386       Object ob = keys.nextElement();
387       Rectangle rect = (Rectangle) nameHash.get(ob);
388
389       if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y)
390               && (y <= (rect.y + rect.height)))
391       {
392         return ob;
393       }
394     }
395
396     keys = nodeHash.keys();
397
398     while (keys.hasMoreElements())
399     {
400       Object ob = keys.nextElement();
401       Rectangle rect = (Rectangle) nodeHash.get(ob);
402
403       if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y)
404               && (y <= (rect.y + rect.height)))
405       {
406         return ob;
407       }
408     }
409
410     return null;
411   }
412
413   /**
414    * DOCUMENT ME!
415    * 
416    * @param pickBox
417    *          DOCUMENT ME!
418    */
419   public void pickNodes(Rectangle pickBox)
420   {
421     int width = getWidth();
422     int height = getHeight();
423
424     SequenceNode top = tree.getTopNode();
425
426     float wscale = (float) ((width * .8) - (offx * 2))
427             / tree.getMaxHeight();
428
429     if (top.count == 0)
430     {
431       top.count = ((SequenceNode) top.left()).count
432               + ((SequenceNode) top.right()).count;
433     }
434
435     float chunk = (float) (height - (offy)) / top.count;
436
437     pickNode(pickBox, top, chunk, wscale, width, offx, offy);
438   }
439
440   /**
441    * DOCUMENT ME!
442    * 
443    * @param pickBox
444    *          DOCUMENT ME!
445    * @param node
446    *          DOCUMENT ME!
447    * @param chunk
448    *          DOCUMENT ME!
449    * @param scale
450    *          DOCUMENT ME!
451    * @param width
452    *          DOCUMENT ME!
453    * @param offx
454    *          DOCUMENT ME!
455    * @param offy
456    *          DOCUMENT ME!
457    */
458   public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
459           float scale, int width, int offx, int offy)
460   {
461     if (node == null)
462     {
463       return;
464     }
465
466     if ((node.left() == null) && (node.right() == null))
467     {
468       float height = node.height;
469       float dist = node.dist;
470
471       int xstart = (int) ((height - dist) * scale) + offx;
472       int xend = (int) (height * scale) + offx;
473
474       int ypos = (int) (node.ycount * chunk) + offy;
475
476       if (pickBox.contains(new Point(xend, ypos)))
477       {
478         if (node.element() instanceof SequenceI)
479         {
480           SequenceI seq = (SequenceI) node.element();
481           SequenceGroup sg = av.getSelectionGroup();
482
483           if (sg != null)
484           {
485             sg.addOrRemove(seq, true);
486           }
487         }
488       }
489     }
490     else
491     {
492       pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
493               offx, offy);
494       pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
495               offx, offy);
496     }
497   }
498
499   /**
500    * DOCUMENT ME!
501    * 
502    * @param node
503    *          DOCUMENT ME!
504    * @param c
505    *          DOCUMENT ME!
506    */
507   public void setColor(SequenceNode node, Color c)
508   {
509     if (node == null)
510     {
511       return;
512     }
513
514     if ((node.left() == null) && (node.right() == null)) // TODO: internal node
515     {
516       node.color = c;
517
518       if (node.element() instanceof SequenceI)
519       {
520         AlignmentPanel[] aps = getAssociatedPanels();
521         if (aps != null)
522         {
523           for (int a = 0; a < aps.length; a++)
524           {
525             final SequenceI seq = (SequenceI) node.element();
526             aps[a].av.setSequenceColour(seq, c);
527           }
528         }
529       }
530     }
531     else
532     {
533       node.color = c;
534       setColor((SequenceNode) node.left(), c);
535       setColor((SequenceNode) node.right(), c);
536     }
537   }
538
539   /**
540    * DOCUMENT ME!
541    */
542   void startPrinting()
543   {
544     Thread thread = new Thread(this);
545     thread.start();
546   }
547
548   // put printing in a thread to avoid painting problems
549   public void run()
550   {
551     PrinterJob printJob = PrinterJob.getPrinterJob();
552     PageFormat pf = printJob.pageDialog(printJob.defaultPage());
553
554     printJob.setPrintable(this, pf);
555
556     if (printJob.printDialog())
557     {
558       try
559       {
560         printJob.print();
561       } catch (Exception PrintException)
562       {
563         PrintException.printStackTrace();
564       }
565     }
566   }
567
568   /**
569    * DOCUMENT ME!
570    * 
571    * @param pg
572    *          DOCUMENT ME!
573    * @param pf
574    *          DOCUMENT ME!
575    * @param pi
576    *          DOCUMENT ME!
577    * 
578    * @return DOCUMENT ME!
579    * 
580    * @throws PrinterException
581    *           DOCUMENT ME!
582    */
583   public int print(Graphics pg, PageFormat pf, int pi)
584           throws PrinterException
585   {
586     pg.setFont(font);
587     pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
588
589     int pwidth = (int) pf.getImageableWidth();
590     int pheight = (int) pf.getImageableHeight();
591
592     int noPages = getHeight() / pheight;
593
594     if (pi > noPages)
595     {
596       return Printable.NO_SUCH_PAGE;
597     }
598
599     if (pwidth > getWidth())
600     {
601       pwidth = getWidth();
602     }
603
604     if (fitToWindow)
605     {
606       if (pheight > getHeight())
607       {
608         pheight = getHeight();
609       }
610
611       noPages = 0;
612     }
613     else
614     {
615       FontMetrics fm = pg.getFontMetrics(font);
616       int height = fm.getHeight() * nameHash.size();
617       pg.translate(0, -pi * pheight);
618       pg.setClip(0, pi * pheight, pwidth, (pi * pheight) + pheight);
619
620       // translate number of pages,
621       // height is screen size as this is the
622       // non overlapping text size
623       pheight = height;
624     }
625
626     draw(pg, pwidth, pheight);
627
628     return Printable.PAGE_EXISTS;
629   }
630
631   /**
632    * DOCUMENT ME!
633    * 
634    * @param g
635    *          DOCUMENT ME!
636    */
637   public void paintComponent(Graphics g)
638   {
639     super.paintComponent(g);
640     g.setFont(font);
641
642     if (tree == null)
643     {
644       g.drawString(MessageManager.getString("label.calculating_tree")
645               + "....", 20, getHeight() / 2);
646     }
647     else
648     {
649       fm = g.getFontMetrics(font);
650
651       if (nameHash.size() == 0)
652       {
653         repaint();
654       }
655
656       if (fitToWindow
657               || (!fitToWindow && (scrollPane.getHeight() > ((fm
658                       .getHeight() * nameHash.size()) + offy))))
659       {
660         draw(g, scrollPane.getWidth(), scrollPane.getHeight());
661         setPreferredSize(null);
662       }
663       else
664       {
665         setPreferredSize(new Dimension(scrollPane.getWidth(),
666                 fm.getHeight() * nameHash.size()));
667         draw(g, scrollPane.getWidth(), fm.getHeight() * nameHash.size());
668       }
669
670       scrollPane.revalidate();
671     }
672   }
673
674   /**
675    * DOCUMENT ME!
676    * 
677    * @param fontSize
678    *          DOCUMENT ME!
679    */
680   public void setFont(Font font)
681   {
682     this.font = font;
683     repaint();
684   }
685
686   /**
687    * DOCUMENT ME!
688    * 
689    * @param g1
690    *          DOCUMENT ME!
691    * @param width
692    *          DOCUMENT ME!
693    * @param height
694    *          DOCUMENT ME!
695    */
696   public void draw(Graphics g1, int width, int height)
697   {
698     Graphics2D g2 = (Graphics2D) g1;
699     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
700             RenderingHints.VALUE_ANTIALIAS_ON);
701     g2.setColor(Color.white);
702     g2.fillRect(0, 0, width, height);
703     g2.setFont(font);
704
705     if (longestName == null || tree == null)
706     {
707       g2.drawString("Calculating tree.", 20, 20);
708     }
709     offy = font.getSize() + 10;
710
711     fm = g2.getFontMetrics(font);
712
713     labelLength = fm.stringWidth(longestName) + 20; // 20 allows for scrollbar
714
715     float wscale = (width - labelLength - (offx * 2)) / tree.getMaxHeight();
716
717     SequenceNode top = tree.getTopNode();
718
719     if (top.count == 0)
720     {
721       top.count = ((SequenceNode) top.left()).count
722               + ((SequenceNode) top.right()).count;
723     }
724
725     float chunk = (float) (height - (offy)) / top.count;
726
727     drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
728
729     if (threshold != 0)
730     {
731       if (av.getCurrentTree() == tree)
732       {
733         g2.setColor(Color.red);
734       }
735       else
736       {
737         g2.setColor(Color.gray);
738       }
739
740       int x = (int) ((threshold * (getWidth() - labelLength - (2 * offx))) + offx);
741
742       g2.drawLine(x, 0, x, getHeight());
743     }
744   }
745
746   /**
747    * DOCUMENT ME!
748    * 
749    * @param e
750    *          DOCUMENT ME!
751    */
752   public void mouseReleased(MouseEvent e)
753   {
754   }
755
756   /**
757    * DOCUMENT ME!
758    * 
759    * @param e
760    *          DOCUMENT ME!
761    */
762   public void mouseEntered(MouseEvent e)
763   {
764   }
765
766   /**
767    * DOCUMENT ME!
768    * 
769    * @param e
770    *          DOCUMENT ME!
771    */
772   public void mouseExited(MouseEvent e)
773   {
774   }
775
776   /**
777    * DOCUMENT ME!
778    * 
779    * @param e
780    *          DOCUMENT ME!
781    */
782   public void mouseClicked(MouseEvent evt)
783   {
784     if (highlightNode != null)
785     {
786       if (SwingUtilities.isRightMouseButton(evt))
787       {
788         Color col = JColorChooser.showDialog(this,
789                 MessageManager.getString("label.select_subtree_colour"),
790                 highlightNode.color);
791         if (col != null)
792         {
793           setColor(highlightNode, col);
794         }
795       }
796       else if (evt.getClickCount() > 1)
797       {
798         tree.swapNodes(highlightNode);
799         tree.reCount(tree.getTopNode());
800         tree.findHeight(tree.getTopNode());
801       }
802       else
803       {
804         Vector leaves = new Vector();
805         tree.findLeaves(highlightNode, leaves);
806
807         for (int i = 0; i < leaves.size(); i++)
808         {
809           SequenceI seq = (SequenceI) ((SequenceNode) leaves.elementAt(i))
810                   .element();
811           treeSelectionChanged(seq);
812         }
813         av.sendSelection();
814       }
815
816       PaintRefresher.Refresh(tp, av.getSequenceSetId());
817       repaint();
818     }
819   }
820
821   public void mouseMoved(MouseEvent evt)
822   {
823     av.setCurrentTree(tree);
824
825     Object ob = findElement(evt.getX(), evt.getY());
826
827     if (ob instanceof SequenceNode)
828     {
829       highlightNode = (SequenceNode) ob;
830       this.setToolTipText("<html>"
831               + MessageManager.getString("label.highlightnode"));
832       repaint();
833
834     }
835     else
836     {
837       if (highlightNode != null)
838       {
839         highlightNode = null;
840         setToolTipText(null);
841         repaint();
842       }
843     }
844   }
845
846   public void mouseDragged(MouseEvent ect)
847   {
848   }
849
850   /**
851    * DOCUMENT ME!
852    * 
853    * @param e
854    *          DOCUMENT ME!
855    */
856   public void mousePressed(MouseEvent e)
857   {
858     av.setCurrentTree(tree);
859
860     int x = e.getX();
861     int y = e.getY();
862
863     Object ob = findElement(x, y);
864
865     if (ob instanceof SequenceI)
866     {
867       treeSelectionChanged((Sequence) ob);
868       PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
869       repaint();
870       av.sendSelection();
871       return;
872     }
873     else if (!(ob instanceof SequenceNode))
874     {
875       // Find threshold
876       if (tree.getMaxHeight() != 0)
877       {
878         threshold = (float) (x - offx)
879                 / (float) (getWidth() - labelLength - (2 * offx));
880
881         tree.getGroups().removeAllElements();
882         tree.groupNodes(tree.getTopNode(), threshold);
883         setColor(tree.getTopNode(), Color.black);
884
885         AlignmentPanel[] aps = getAssociatedPanels();
886
887         // TODO push calls below into a single AlignViewportI method?
888         // see also AlignViewController.deleteGroups
889         for (int a = 0; a < aps.length; a++)
890         {
891           aps[a].av.setSelectionGroup(null);
892           aps[a].av.getAlignment().deleteAllGroups();
893           aps[a].av.clearSequenceColours();
894         }
895         if (av.getCodingComplement() != null)
896         {
897           av.getCodingComplement().setSelectionGroup(null);
898           av.getCodingComplement().getAlignment().deleteAllGroups();
899           av.getCodingComplement().clearSequenceColours();
900         }
901         colourGroups();
902       }
903
904       PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
905       repaint();
906     }
907
908   }
909
910   void colourGroups()
911   {
912     AlignmentPanel[] aps = getAssociatedPanels();
913     for (int i = 0; i < tree.getGroups().size(); i++)
914     {
915       Color col = new Color((int) (Math.random() * 255),
916               (int) (Math.random() * 255), (int) (Math.random() * 255));
917       setColor((SequenceNode) tree.getGroups().elementAt(i), col.brighter());
918
919       Vector l = tree.findLeaves(
920               (SequenceNode) tree.getGroups().elementAt(i), new Vector());
921
922       Vector sequences = new Vector();
923
924       for (int j = 0; j < l.size(); j++)
925       {
926         SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j))
927                 .element();
928
929         if (!sequences.contains(s1))
930         {
931           sequences.addElement(s1);
932         }
933       }
934
935       ColourSchemeI cs = null;
936       SequenceGroup sg = new SequenceGroup(sequences, null, cs, true, true,
937               false, 0, av.getAlignment().getWidth() - 1);
938
939       if (av.getGlobalColourScheme() != null)
940       {
941         if (av.getGlobalColourScheme() instanceof UserColourScheme)
942         {
943           cs = new UserColourScheme(
944                   ((UserColourScheme) av.getGlobalColourScheme())
945                           .getColours());
946
947         }
948         else
949         {
950           cs = ColourSchemeProperty.getColour(sg, ColourSchemeProperty
951                   .getColourName(av.getGlobalColourScheme()));
952         }
953         // cs is null if shading is an annotationColourGradient
954         if (cs != null)
955         {
956           cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
957                   av.isIgnoreGapsConsensus());
958         }
959       }
960       sg.cs = cs;
961       // sg.recalcConservation();
962       sg.setName("JTreeGroup:" + sg.hashCode());
963       sg.setIdColour(col);
964
965       for (int a = 0; a < aps.length; a++)
966       {
967         if (aps[a].av.getGlobalColourScheme() != null
968                 && aps[a].av.getGlobalColourScheme().conservationApplied())
969         {
970           Conservation c = new Conservation("Group",
971                   ResidueProperties.propHash, 3, sg.getSequences(null),
972                   sg.getStartRes(), sg.getEndRes());
973
974           c.calculate();
975           c.verdict(false, aps[a].av.getConsPercGaps());
976           sg.cs.setConservation(c);
977         }
978
979         aps[a].av.getAlignment().addGroup(new SequenceGroup(sg));
980       }
981
982       // TODO can we push all of the below into AlignViewportI?
983       av.getAlignment().addGroup(sg);
984       final AlignViewportI codingComplement = av.getCodingComplement();
985       if (codingComplement != null)
986       {
987         SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, av,
988                 codingComplement);
989         if (mappedGroup.getSequences().size() > 0)
990         {
991           codingComplement.getAlignment().addGroup(mappedGroup);
992           for (SequenceI seq : mappedGroup.getSequences())
993           {
994             codingComplement.setSequenceColour(seq, col.brighter());
995           }
996         }
997       }
998     }
999
1000     // notify the panel to redo any group specific stuff.
1001     for (int a = 0; a < aps.length; a++)
1002     {
1003       aps[a].updateAnnotation();
1004       // TODO: JAL-868 - need to ensure view colour change message is broadcast
1005       // to any Jmols listening in
1006     }
1007
1008     if (av.getCodingComplement() != null)
1009     {
1010       ((AlignViewport) av.getCodingComplement()).getAlignPanel()
1011               .updateAnnotation();
1012       /*
1013        * idPanel. repaint ()
1014        */
1015     }
1016   }
1017
1018   /**
1019    * DOCUMENT ME!
1020    * 
1021    * @param state
1022    *          DOCUMENT ME!
1023    */
1024   public void setShowDistances(boolean state)
1025   {
1026     this.showDistances = state;
1027     repaint();
1028   }
1029
1030   /**
1031    * DOCUMENT ME!
1032    * 
1033    * @param state
1034    *          DOCUMENT ME!
1035    */
1036   public void setShowBootstrap(boolean state)
1037   {
1038     this.showBootstrap = state;
1039     repaint();
1040   }
1041
1042   /**
1043    * DOCUMENT ME!
1044    * 
1045    * @param state
1046    *          DOCUMENT ME!
1047    */
1048   public void setMarkPlaceholders(boolean state)
1049   {
1050     this.markPlaceholders = state;
1051     repaint();
1052   }
1053
1054   AlignmentPanel[] getAssociatedPanels()
1055   {
1056     if (applyToAllViews)
1057     {
1058       return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
1059     }
1060     else
1061     {
1062       return new AlignmentPanel[] { ap };
1063     }
1064   }
1065 }