X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fappletgui%2FTreeCanvas.java;h=28013510e26555b0352af86ebdddee44012b3ee7;hb=0452450533728e478ef8c893ea2cb3483c740fb3;hp=15c4bfa9f62af1f7b952c2c92c6a03c15150d269;hpb=085727a32930d9a05becd66686ed001f50331bc6;p=jalview.git diff --git a/src/jalview/appletgui/TreeCanvas.java b/src/jalview/appletgui/TreeCanvas.java index 15c4bfa..2801351 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); + PaintRefresher.Register(this, av.getSequenceSetId()); } - 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, @@ -118,15 +133,15 @@ System.out.println("called here"); if (node.element() instanceof SequenceI) { - if ( ( (SequenceI) ( (SequenceNode) node).element()).getColor() == - Color.white) + SequenceI seq = (SequenceI) ( (SequenceNode) node).element(); + + if (av.getSequenceColour(seq) == Color.white) { g.setColor(Color.black); } else { - g.setColor( ( (SequenceI) ( (SequenceNode) node).element()).getColor(). - darker()); + g.setColor(av.getSequenceColour(seq).darker()); } } @@ -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(null).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; @@ -319,7 +337,7 @@ System.out.println("called here"); if (node.element() instanceof SequenceI) { - ( (SequenceI) node.element()).setColor(c); + av.setSequenceColour((SequenceI) node.element(), c); } } else @@ -330,48 +348,48 @@ 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); - - FontMetrics fm = g.getFontMetrics(font); + public void paint(Graphics g) + { + if(tree==null) + return; if (nameHash.size() == 0) { repaint(); } - if (fitToWindow || - (!fitToWindow && - scrollPane.getSize().height > fm.getHeight() * nameHash.size() + offy)) + int width = scrollPane.getSize().width; + int height = scrollPane.getSize().height; + if(!fitToWindow) { - draw(g, scrollPane.getSize().width, scrollPane.getSize().height); + height = g.getFontMetrics(font).getHeight() * nameHash.size(); } - else + + if(getSize().width>width) { - setSize(new Dimension(scrollPane.getSize().width, - fm.getHeight() * nameHash.size())); - draw(g, scrollPane.getSize().width, fm.getHeight() * nameHash.size()); + setSize(new Dimension(width,height)); + scrollPane.validate(); + return; } - scrollPane.validate(); - } + setSize(new Dimension(width,height)); - public int getFontSize() - { - return fontSize; - } - public void setFontSize(int fontSize) - { - this.fontSize = fontSize; - repaint(); + g.setFont(font); + + draw(g, width, height); + } + 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 +405,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 +437,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.getSequenceSetId()); + 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 +501,12 @@ System.out.println("called here"); if (ob instanceof SequenceI) { - TreeSelectionChanged( (Sequence) ob); + treeSelectionChanged( (Sequence) ob); + PaintRefresher.Refresh(this, av.getSequenceSetId()); 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 @@ -461,8 +521,21 @@ System.out.println("called here"); av.setSelectionGroup(null); av.alignment.deleteAllGroups(); + av.sequenceColours=null; + + colourGroups(); + + } + } - for (int i = 0; i < tree.getGroups().size(); i++) + PaintRefresher.Refresh(this, av.getSequenceSetId()); + repaint(); + + } + + void colourGroups() + { + for (int i = 0; i < tree.getGroups().size(); i++) { Color col = new Color( (int) (Math.random() * 255), @@ -481,40 +554,51 @@ 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() instanceof ConservationColourScheme) - { - ConservationColourScheme ccs = (ConservationColourScheme) av. - getGlobalColourScheme(); + if ( av.getGlobalColourScheme()!=null + && av.getGlobalColourScheme().conservationApplied()) + { Conservation c = new Conservation("Group", ResidueProperties.propHash, 3, - sg.sequences, sg.getStartRes(), + sg.getSequences(null), + sg.getStartRes(), sg.getEndRes()); c.calculate(); c.verdict(false, av.ConsPercGaps); - ccs = new ConservationColourScheme(c, ccs.cs); + cs.setConservation(c); - sg.cs = ccs; + sg.cs = cs; } av.alignment.addGroup(sg); } - } - } - - PaintRefresher.Refresh(this, av.alignment); - repaint(); }