X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fappletgui%2FTreeCanvas.java;h=928d21fb4ad6183ae34140297738b69be11cca5d;hb=e6879a932d0144d64a5ab703ae311aed0eeb83e5;hp=451d66361ccec419747b272e6c1ea3b7c4d4ab91;hpb=ef23c17739a6fba3a8d455b725782bce4607ac9f;p=jalview.git diff --git a/src/jalview/appletgui/TreeCanvas.java b/src/jalview/appletgui/TreeCanvas.java index 451d663..928d21f 100755 --- a/src/jalview/appletgui/TreeCanvas.java +++ b/src/jalview/appletgui/TreeCanvas.java @@ -1,6 +1,6 @@ /* * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -30,50 +30,45 @@ import jalview.schemes.*; import jalview.util.*; public class TreeCanvas - extends Panel implements MouseListener + extends Panel implements MouseListener, MouseMotionListener { NJTree tree; ScrollPane scrollPane; AlignViewport av; public static final String PLACEHOLDER = " * "; Font font; - int fontSize = 12; - boolean fitToWindow = true; boolean showDistances = false; boolean showBootstrap = false; boolean markPlaceholders = false; int offx = 20; - int offy = 20; + int offy; float threshold; String longestName; int labelLength = -1; - //RubberbandRectangle rubberband; - - Vector listeners; - Hashtable nameHash = new Hashtable(); Hashtable nodeHash = new Hashtable(); - public TreeCanvas(AlignViewport av, NJTree tree, ScrollPane scroller, - String label) + SequenceNode highlightNode; + + + public TreeCanvas(AlignViewport av, ScrollPane scroller) { this.av = av; - this.tree = tree; + font = av.getFont(); scrollPane = scroller; addMouseListener(this); - tree.findHeight(tree.getTopNode()); - longestName = label; + addMouseMotionListener(this); setLayout(null); PaintRefresher.Register(this, av.alignment); } - public void TreeSelectionChanged(Sequence sequence) + public void treeSelectionChanged(SequenceI sequence) { SequenceGroup selected = av.getSelectionGroup(); if (selected == null) @@ -82,18 +77,38 @@ public class TreeCanvas av.setSelectionGroup(selected); } - selected.setEndRes(av.alignment.getWidth()); + selected.setEndRes(av.alignment.getWidth()-1); selected.addOrRemove(sequence, true); - -System.out.println("called here"); - PaintRefresher.Refresh(this, av.alignment); - repaint(); } public void setTree(NJTree tree) { this.tree = tree; tree.findHeight(tree.getTopNode()); + + // Now have to calculate longest name based on the leaves + Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector()); + boolean has_placeholders = false; + longestName = ""; + + for (int i = 0; i < leaves.size(); i++) + { + SequenceNode lf = (SequenceNode) leaves.elementAt(i); + + if (lf.isPlaceholder()) + { + has_placeholders = true; + } + + if (longestName.length() < ( (Sequence) lf.element()).getName() + .length()) + { + longestName = TreeCanvas.PLACEHOLDER + + ( (Sequence) lf.element()).getName(); + } + } + + setMarkPlaceholders(has_placeholders); } public void drawNode(Graphics g, SequenceNode node, float chunk, float scale, @@ -141,7 +156,7 @@ System.out.println("called here"); String nodeLabel = ""; if (showDistances && node.dist > 0) { - nodeLabel = new Format("%5.2f").form(node.dist); + nodeLabel = new Format("%-.2f").form(node.dist); } if (showBootstrap) { @@ -153,7 +168,7 @@ System.out.println("called here"); } if (!nodeLabel.equals("")) { - g.drawString(nodeLabel, xstart, ypos - 10); + g.drawString(nodeLabel, xstart+2, ypos - 2); } String name = (markPlaceholders && node.isPlaceholder()) ? @@ -162,7 +177,7 @@ System.out.println("called here"); int charWidth = fm.stringWidth(name) + 3; int charHeight = fm.getHeight(); - Rectangle rect = new Rectangle(xend + 20, ypos - charHeight, + Rectangle rect = new Rectangle(xend + 10, ypos - charHeight, charWidth, charHeight); nameHash.put( (SequenceI) node.element(), rect); @@ -170,7 +185,7 @@ System.out.println("called here"); // Colour selected leaves differently SequenceGroup selected = av.getSelectionGroup(); if (selected != null && - selected.sequences.contains( (SequenceI) node.element())) + selected.getSequences(false).contains( (SequenceI) node.element())) { g.setColor(Color.gray); @@ -196,7 +211,10 @@ System.out.println("called here"); // Draw horizontal line g.drawLine(xstart, ypos, xend, ypos); - g.fillRect(xend - 2, ypos - 2, 4, 4); + if (node == highlightNode) + g.fillRect(xend - 3, ypos - 3, 6, 6); + else + g.fillRect(xend - 2, ypos - 2, 4, 4); int ystart = (int) ( ( (SequenceNode) node.left()).ycount * chunk) + offy; int yend = (int) ( ( (SequenceNode) node.right()).ycount * chunk) + offy; @@ -209,7 +227,7 @@ System.out.println("called here"); if (showDistances && node.dist > 0) { - g.drawString(new Format("%5.2f").form(node.dist), xstart, ypos - 5); + g.drawString(new Format("%-.2f").form(node.dist), xstart+2, ypos - 2); } } @@ -261,7 +279,7 @@ System.out.println("called here"); top.count = ( (SequenceNode) top.left()).count + ( (SequenceNode) top.right()).count; } - float chunk = (float) (height - offy * 2) / top.count; + float chunk = (float) (height - offy) / top.count; pickNode(pickBox, top, chunk, wscale, width, offx, offy); } @@ -277,9 +295,9 @@ System.out.println("called here"); if (node.left() == null && node.right() == null) { float height = node.height; - float dist = node.dist; + //float dist = node.dist; - int xstart = (int) ( (height - dist) * scale) + offx; + //int xstart = (int) ( (height - dist) * scale) + offx; int xend = (int) (height * scale) + offx; int ypos = (int) (node.ycount * chunk) + offy; @@ -330,19 +348,39 @@ System.out.println("called here"); } } - public void paint(Graphics g) + public void update(Graphics g) { + paint(g); + } - font = new Font("Verdana", Font.PLAIN, fontSize); - g.setFont(font); + Image offscreen; + public void paint(Graphics g1) + { - FontMetrics fm = g.getFontMetrics(font); + if(tree==null) + return; if (nameHash.size() == 0) { repaint(); } + FontMetrics fm = g1.getFontMetrics(font); + + int width = scrollPane.getSize().width; + int height = scrollPane.getSize().height; + if(!fitToWindow) + height = fm.getHeight() * nameHash.size(); + + + if(offscreen==null || offscreen.getWidth(this)!=width + || offscreen.getHeight(this)!=height) + offscreen = createImage(width, height); + + Graphics g = offscreen.getGraphics(); + + g.setFont(font); + if (fitToWindow || (!fitToWindow && scrollPane.getSize().height > fm.getHeight() * nameHash.size() + offy)) @@ -356,22 +394,14 @@ System.out.println("called here"); draw(g, scrollPane.getSize().width, fm.getHeight() * nameHash.size()); } + g1.drawImage(offscreen, 0, 0, this); scrollPane.validate(); } - public int getFontSize() - { - return fontSize; - } - - public void setFontSize(int fontSize) - { - this.fontSize = fontSize; - repaint(); - } public void draw(Graphics g, int width, int height) { + offy = font.getSize()+10; g.setColor(Color.white); g.fillRect(0, 0, width, height); @@ -387,7 +417,7 @@ System.out.println("called here"); top.count = ( (SequenceNode) top.left()).count + ( (SequenceNode) top.right()).count; } - float chunk = (float) (height - offy * 2) / top.count; + float chunk = (float) (height - offy) / top.count; drawNode(g, tree.getTopNode(), chunk, wscale, width, offx, offy); @@ -419,8 +449,57 @@ System.out.println("called here"); public void mouseExited(MouseEvent e) {} - public void mouseClicked(MouseEvent e) + public void mouseClicked(MouseEvent evt) + { + if (highlightNode != null) + { + if (evt.getClickCount() > 1) + { + tree.swapNodes(highlightNode); + tree.reCount(tree.getTopNode()); + tree.findHeight(tree.getTopNode()); + } + else + { + Vector leaves = new Vector(); + tree.findLeaves(highlightNode, leaves); + + for (int i = 0; i < leaves.size(); i++) + { + SequenceI seq = + (SequenceI) ( (SequenceNode) leaves.elementAt(i)).element(); + treeSelectionChanged(seq); + } + } + + PaintRefresher.Refresh(this, av.alignment); + repaint(); + } + } + + public void mouseDragged(MouseEvent ect) + {} + + + public void mouseMoved(MouseEvent evt) { + av.setCurrentTree(tree); + + Object ob = findElement(evt.getX(), evt.getY()); + + if (ob instanceof SequenceNode) + { + highlightNode = (SequenceNode) ob; + repaint(); + } + else + { + if (highlightNode != null) + { + highlightNode = null; + repaint(); + } + } } public void mousePressed(MouseEvent e) @@ -434,19 +513,12 @@ System.out.println("called here"); if (ob instanceof SequenceI) { - TreeSelectionChanged( (Sequence) ob); + treeSelectionChanged( (Sequence) ob); + PaintRefresher.Refresh(this, av.alignment); repaint(); return; - - } - else if (ob instanceof SequenceNode) - { - SequenceNode tmpnode = (SequenceNode) ob; - tree.swapNodes(tmpnode); - tree.reCount(tree.getTopNode()); - tree.findHeight(tree.getTopNode()); } - else + else if ( !(ob instanceof SequenceNode)) { // Find threshold @@ -462,7 +534,19 @@ System.out.println("called here"); av.setSelectionGroup(null); av.alignment.deleteAllGroups(); - for (int i = 0; i < tree.getGroups().size(); i++) + colourGroups(); + + } + } + + PaintRefresher.Refresh(this, av.alignment); + repaint(); + + } + + void colourGroups() + { + for (int i = 0; i < tree.getGroups().size(); i++) { Color col = new Color( (int) (Math.random() * 255), @@ -481,13 +565,29 @@ System.out.println("called here"); sequences.addElement(s1); } - ColourSchemeI cs = ColourSchemeProperty.getColour(sequences, av.alignment.getWidth(), - ColourSchemeProperty.getColourName(av.getGlobalColourScheme())); + ColourSchemeI cs = null; + if (av.getGlobalColourScheme() != null) + { + if (av.getGlobalColourScheme() instanceof UserColourScheme) + { + cs = new UserColourScheme( + ( (UserColourScheme) av.getGlobalColourScheme()).getColours()); + + } + else + cs = ColourSchemeProperty.getColour(sequences, + av.alignment.getWidth(), + ColourSchemeProperty.getColourName( + av.getGlobalColourScheme())); + + cs.setThreshold(av.getGlobalColourScheme().getThreshold(), + av.getIgnoreGapsConsensus()); + } SequenceGroup sg = new SequenceGroup(sequences, "TreeGroup", cs, true, true, - false, 0, av.alignment.getWidth()); + false, 0, av.alignment.getWidth()-1); if ( av.getGlobalColourScheme()!=null @@ -495,7 +595,8 @@ System.out.println("called here"); { Conservation c = new Conservation("Group", ResidueProperties.propHash, 3, - sg.sequences, sg.getStartRes(), + sg.getSequences(false), + sg.getStartRes(), sg.getEndRes()); c.calculate(); @@ -509,11 +610,6 @@ System.out.println("called here"); av.alignment.addGroup(sg); } - } - } - - PaintRefresher.Refresh(this, av.alignment); - repaint(); }