4 import jalview.analysis.*;
\r
5 import jalview.datamodel.*;
\r
6 import jalview.util.*;
\r
7 import jalview.schemes.*;
\r
8 import javax.swing.*;
\r
10 import java.awt.event.*;
\r
12 import java.awt.print.*;
\r
14 public class TreeCanvas extends JPanel implements MouseListener, Runnable, Printable
\r
17 JScrollPane scrollPane;
\r
19 public static final String PLACEHOLDER=" * ";
\r
23 boolean fitToWindow = true;
\r
24 boolean showDistances = false;
\r
25 boolean showBootstrap = false;
\r
26 boolean markPlaceholders = false;
\r
36 //RubberbandRectangle rubberband;
\r
40 Hashtable nameHash = new Hashtable();
\r
41 Hashtable nodeHash = new Hashtable();
\r
43 public TreeCanvas(AlignViewport av, NJTree tree, JScrollPane scroller, String label)
\r
47 scrollPane = scroller;
\r
48 addMouseListener(this);
\r
49 tree.findHeight(tree.getTopNode());
\r
50 longestName = label;
\r
52 PaintRefresher.Register(this);
\r
54 public void TreeSelectionChanged(Sequence sequence)
\r
56 SequenceGroup selected = av.getSelectionGroup();
\r
57 if(selected == null)
\r
59 selected = new SequenceGroup();
\r
60 av.setSelectionGroup(selected);
\r
63 selected.setEndRes(av.alignment.getWidth());
\r
64 selected.addOrRemove(sequence);
\r
67 PaintRefresher.Refresh(this);
\r
73 public void setTree(NJTree tree) {
\r
75 tree.findHeight(tree.getTopNode());
\r
78 public void drawNode(Graphics g,SequenceNode node, float chunk, float scale, int width,int offx, int offy) {
\r
83 if (node.left() == null && node.right() == null) {
\r
84 // Drawing leaf node
\r
86 float height = node.height;
\r
87 float dist = node.dist;
\r
89 int xstart = (int)((height-dist)*scale) + offx;
\r
90 int xend = (int)(height*scale) + offx;
\r
92 int ypos = (int)(node.ycount * chunk) + offy;
\r
94 if (node.element() instanceof SequenceI)
\r
96 if ( ( (SequenceI) ( (SequenceNode) node).element()).getColor() == Color.white)
\r
98 g.setColor(Color.black);
\r
101 g.setColor( ( (SequenceI) ( (SequenceNode) node).element()).getColor().
\r
106 g.setColor(Color.black);
\r
109 // Draw horizontal line
\r
110 g.drawLine(xstart,ypos,xend,ypos);
\r
112 String nodeLabel = "";
\r
113 if (showDistances && node.dist > 0) {
\r
114 nodeLabel = new Format("%5.2f").form(node.dist);
\r
116 if (showBootstrap) {
\r
117 if (showDistances) {
\r
118 nodeLabel = nodeLabel + " : ";
\r
120 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
\r
122 if (! nodeLabel.equals("")) {
\r
123 g.drawString(nodeLabel,xstart,ypos - 10);
\r
126 String name = (markPlaceholders && node.isPlaceholder()) ? (PLACEHOLDER+node.getName()) : node.getName();
\r
127 FontMetrics fm = g.getFontMetrics(font);
\r
128 int charWidth = fm.stringWidth(name) + 3;
\r
129 int charHeight = fm.getHeight();
\r
131 Rectangle rect = new Rectangle(xend+20,ypos-charHeight,
\r
132 charWidth,charHeight);
\r
134 nameHash.put((SequenceI)node.element(),rect);
\r
136 // Colour selected leaves differently
\r
137 SequenceGroup selected = av.getSelectionGroup();
\r
138 if (selected!=null && selected.sequences.contains((SequenceI)node.element())) {
\r
139 g.setColor(Color.gray);
\r
141 g.fillRect(xend + 10, ypos - charHeight + 3,charWidth,charHeight);
\r
142 g.setColor(Color.white);
\r
144 g.drawString(name,xend+10,ypos);
\r
145 g.setColor(Color.black);
\r
147 drawNode(g,(SequenceNode)node.left(), chunk,scale,width,offx,offy);
\r
148 drawNode(g,(SequenceNode)node.right(),chunk,scale,width,offx,offy);
\r
150 float height = node.height;
\r
151 float dist = node.dist;
\r
153 int xstart = (int)((height-dist)*scale) + offx;
\r
154 int xend = (int)(height *scale) + offx;
\r
155 int ypos = (int)(node.ycount *chunk) + offy;
\r
157 g.setColor(((SequenceNode)node).color.darker());
\r
159 // Draw horizontal line
\r
160 g.drawLine(xstart,ypos,xend,ypos);
\r
161 g.fillRect(xend-2, ypos-2, 4,4);
\r
163 int ystart = (int)(((SequenceNode)node.left()) .ycount * chunk) + offy;
\r
164 int yend = (int)(((SequenceNode)node.right()).ycount * chunk) + offy;
\r
166 Rectangle pos = new Rectangle(xend-2,ypos-2,5,5);
\r
167 nodeHash.put(node,pos);
\r
169 g.drawLine((int)(height*scale) + offx, ystart,
\r
170 (int)(height*scale) + offx, yend);
\r
172 if (showDistances && node.dist > 0) {
\r
173 g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5);
\r
178 public Object findElement(int x, int y) {
\r
179 Enumeration keys = nameHash.keys();
\r
181 while (keys.hasMoreElements()) {
\r
182 Object ob = keys.nextElement();
\r
183 Rectangle rect = (Rectangle)nameHash.get(ob);
\r
185 if (x >= rect.x && x <= (rect.x + rect.width) &&
\r
186 y >= rect.y && y <= (rect.y + rect.height)) {
\r
190 keys = nodeHash.keys();
\r
192 while (keys.hasMoreElements()) {
\r
193 Object ob = keys.nextElement();
\r
194 Rectangle rect = (Rectangle)nodeHash.get(ob);
\r
196 if (x >= rect.x && x <= (rect.x + rect.width) &&
\r
197 y >= rect.y && y <= (rect.y + rect.height)) {
\r
205 public void pickNodes(Rectangle pickBox) {
\r
206 int width = getWidth();
\r
207 int height = getHeight();
\r
209 SequenceNode top = tree.getTopNode();
\r
211 float wscale = (float)(width*.8-offx*2)/tree.getMaxHeight()
\r
213 if (top.count == 0) {
\r
214 top.count = ((SequenceNode)top.left()).count + ((SequenceNode)top.right()).count ;
\r
216 float chunk = (float)(height-offy*2)/top.count;
\r
218 pickNode(pickBox,top,chunk,wscale,width,offx,offy);
\r
221 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk, float scale, int width,int offx, int offy) {
\r
222 if (node == null) {
\r
226 if (node.left() == null && node.right() == null) {
\r
227 float height = node.height;
\r
228 float dist = node.dist;
\r
230 int xstart = (int)((height-dist)*scale) + offx;
\r
231 int xend = (int)(height*scale) + offx;
\r
233 int ypos = (int)(node.ycount * chunk) + offy;
\r
235 if (pickBox.contains(new Point(xend,ypos))) {
\r
236 if (node.element() instanceof SequenceI) {
\r
237 SequenceI seq = (SequenceI)node.element();
\r
238 SequenceGroup sg = av.getSelectionGroup();
\r
240 sg.addOrRemove(seq);
\r
244 pickNode(pickBox,(SequenceNode)node.left(), chunk,scale,width,offx,offy);
\r
245 pickNode(pickBox,(SequenceNode)node.right(),chunk,scale,width,offx,offy);
\r
249 public void setColor(SequenceNode node, Color c)
\r
254 if (node.left() == null && node.right() == null)
\r
258 if (node.element() instanceof SequenceI)
\r
259 ((SequenceI)node.element()).setColor(c);
\r
263 setColor((SequenceNode)node.left(),c);
\r
264 setColor((SequenceNode)node.right(),c);
\r
268 void startPrinting()
\r
270 Thread thread = new Thread(this);
\r
274 // put printing in a thread to avoid painting problems
\r
277 PrinterJob printJob = PrinterJob.getPrinterJob();
\r
278 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
\r
280 printJob.setPrintable(this, pf);
\r
281 if (printJob.printDialog())
\r
287 catch (Exception PrintException)
\r
289 PrintException.printStackTrace();
\r
295 public int print(Graphics pg, PageFormat pf, int pi) throws PrinterException
\r
299 pg.translate((int)pf.getImageableX(), (int)pf.getImageableY());
\r
300 int pwidth = (int) pf.getImageableWidth();
\r
301 int pheight = (int) pf.getImageableHeight();
\r
303 int noPages = getHeight() / pheight;
\r
305 return Printable.NO_SUCH_PAGE;
\r
308 if (pwidth > getWidth())
\r
309 pwidth = getWidth();
\r
313 if (pheight > getHeight())
\r
314 pheight = getHeight();
\r
321 FontMetrics fm = pg.getFontMetrics(font);
\r
322 int height = fm.getHeight() * nameHash.size();
\r
323 pg.translate(0, -pi*pheight );
\r
324 pg.setClip(0,pi*pheight, pwidth,pi*pheight + pheight);
\r
325 // translate number of pages,
\r
326 // height is screen size as this is the
\r
327 // non overlapping text size
\r
331 draw(pg, pwidth, pheight);
\r
333 return Printable.PAGE_EXISTS;
\r
337 public void paintComponent(Graphics g)
\r
340 font = new Font("Verdana",Font.PLAIN,fontSize);
\r
343 FontMetrics fm = g.getFontMetrics(font);
\r
345 if(nameHash.size()==0)
\r
349 if( fitToWindow || (!fitToWindow && scrollPane.getHeight() > fm.getHeight() * nameHash.size()+offy ) )
\r
351 draw(g,scrollPane.getWidth(),scrollPane.getHeight());
\r
352 setPreferredSize(null);
\r
356 setPreferredSize(new Dimension(scrollPane.getWidth(), fm.getHeight() * nameHash.size()));
\r
357 draw( g,scrollPane.getWidth(), fm.getHeight() * nameHash.size());
\r
360 scrollPane.revalidate();
\r
362 public int getFontSize() {
\r
365 public void setFontSize(int fontSize) {
\r
366 this.fontSize = fontSize;
\r
369 public void draw(Graphics g1, int width, int height) {
\r
371 Graphics2D g2 = (Graphics2D)g1;
\r
372 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
\r
373 g2.setColor(Color.white);
\r
374 g2.fillRect(0,0,width,height);
\r
377 labelLength = g2.getFontMetrics(font).stringWidth(longestName)+ 20;//20 allows for scrollbar
\r
379 float wscale =(float)(width - labelLength -offx*2)/tree.getMaxHeight();
\r
381 SequenceNode top = tree.getTopNode();
\r
383 if (top.count == 0) {
\r
384 top.count = ((SequenceNode)top.left()).count + ((SequenceNode)top.right()).count ;
\r
386 float chunk = (float)(height-offy*2)/top.count ;
\r
388 drawNode(g2,tree.getTopNode(),chunk,wscale,width,offx,offy);
\r
390 if (threshold != 0)
\r
392 if(av.getCurrentTree() == tree)
\r
393 g2.setColor(Color.red);
\r
395 g2.setColor(Color.gray);
\r
397 int x = (int)( threshold * (float)(getWidth()-labelLength - 2*offx) +offx ) ;
\r
399 g2.drawLine(x,0,x,getHeight());
\r
404 public void mouseReleased(MouseEvent e) { }
\r
405 public void mouseEntered(MouseEvent e) { }
\r
406 public void mouseExited(MouseEvent e) { }
\r
407 public void mouseClicked(MouseEvent e) {
\r
410 public void mousePressed(MouseEvent e) {
\r
412 av.setCurrentTree(tree);
\r
417 Object ob = findElement(x,y);
\r
419 if (ob instanceof SequenceI)
\r
421 TreeSelectionChanged((Sequence)ob);
\r
425 } else if (ob instanceof SequenceNode) {
\r
426 SequenceNode tmpnode = (SequenceNode)ob;
\r
427 tree.swapNodes(tmpnode);
\r
428 tree.reCount(tree.getTopNode());
\r
429 tree.findHeight(tree.getTopNode());
\r
433 if (tree.getMaxHeight() != 0) {
\r
434 threshold = (float)(x - offx)/(float)(getWidth()-labelLength - 2*offx);
\r
436 tree.getGroups().removeAllElements();
\r
437 tree.groupNodes(tree.getTopNode(),threshold);
\r
438 setColor(tree.getTopNode(),Color.black);
\r
440 av.setSelectionGroup(null);
\r
441 av.alignment.deleteAllGroups();
\r
443 for (int i=0; i < tree.getGroups().size(); i++)
\r
446 Color col = new Color((int)(Math.random()*255),
\r
447 (int)(Math.random()*255),
\r
448 (int)(Math.random()*255));
\r
449 setColor((SequenceNode)tree.getGroups().elementAt(i),col.brighter());
\r
451 Vector l = tree.findLeaves((SequenceNode)tree.getGroups().elementAt(i),new Vector());
\r
452 SequenceGroup sg = null;
\r
453 for (int j = 0; j < l.size(); j++)
\r
455 SequenceNode sn = (SequenceNode) l.elementAt(j);
\r
457 sg = new SequenceGroup("TreeGroup", av.getGlobalColourScheme(), true, true,false,0,av.alignment.getWidth());
\r
459 sg.addSequence( (Sequence) sn.element());
\r
462 if (av.getGlobalColourScheme() instanceof ConservationColourScheme)
\r
464 ConservationColourScheme ccs = (ConservationColourScheme) av.getGlobalColourScheme();
\r
465 Conservation c = new Conservation("Group",
\r
466 ResidueProperties.propHash, 3,
\r
467 sg.sequences, sg.getStartRes(),
\r
471 c.verdict(false, av.ConsPercGaps);
\r
472 ccs = new ConservationColourScheme(c, ccs.cs);
\r
480 av.alignment.addGroup(sg);
\r
486 PaintRefresher.Refresh(this);
\r
491 public void setShowDistances(boolean state) {
\r
492 this.showDistances = state;
\r
496 public void setShowBootstrap(boolean state) {
\r
497 this.showBootstrap = state;
\r
500 public void setMarkPlaceholders(boolean state) {
\r
501 this.markPlaceholders = state;
\r