X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FTreeCanvas.java;h=6f60792b45f11059185b669aef7731caebfb6420;hb=6834a636a0b72693041e2f1090c1123f215c9ec1;hp=e92379246f1a9a9e47745da24c515d14a23720ea;hpb=95a46891288f4fc63d690cab4f56879678f54fb6;p=jalview.git
diff --git a/src/jalview/gui/TreeCanvas.java b/src/jalview/gui/TreeCanvas.java
index e923792..6f60792 100755
--- a/src/jalview/gui/TreeCanvas.java
+++ b/src/jalview/gui/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
@@ -42,29 +42,29 @@ import javax.swing.*;
* @version $Revision$
*/
public class TreeCanvas extends JPanel implements MouseListener, Runnable,
- Printable
+ Printable, MouseMotionListener
{
/** DOCUMENT ME!! */
public static final String PLACEHOLDER = " * ";
NJTree tree;
JScrollPane scrollPane;
AlignViewport av;
+ AlignmentPanel ap;
Font font;
- int fontSize = 12;
+ FontMetrics fm;
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();
+ SequenceNode highlightNode;
/**
* Creates a new TreeCanvas object.
@@ -74,25 +74,25 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
* @param scroller DOCUMENT ME!
* @param label DOCUMENT ME!
*/
- public TreeCanvas(AlignViewport av, NJTree tree, JScrollPane scroller,
- String label)
+ public TreeCanvas(AlignmentPanel ap, JScrollPane scroller)
{
- this.av = av;
- this.tree = tree;
+ this.av = ap.av;
+ this.ap = ap;
+ font = av.getFont();
scrollPane = scroller;
addMouseListener(this);
- tree.findHeight(tree.getTopNode());
- longestName = label;
-
- PaintRefresher.Register(this, av.alignment);
+ addMouseMotionListener(this);
+ PaintRefresher.Register(this, ap.av.getSequenceSetId());
+ ToolTipManager.sharedInstance().registerComponent(this);
}
+
/**
* DOCUMENT ME!
*
* @param sequence DOCUMENT ME!
*/
- public void TreeSelectionChanged(Sequence sequence)
+ public void treeSelectionChanged(SequenceI sequence)
{
SequenceGroup selected = av.getSelectionGroup();
@@ -102,11 +102,8 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
av.setSelectionGroup(selected);
}
- selected.setEndRes(av.alignment.getWidth());
+ selected.setEndRes(av.alignment.getWidth()-1);
selected.addOrRemove(sequence, true);
-
- PaintRefresher.Refresh(this, av.alignment);
- repaint();
}
/**
@@ -118,6 +115,30 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
{
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);
}
/**
@@ -152,14 +173,15 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
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());
}
}
else
@@ -174,7 +196,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
if (showDistances && (node.dist > 0))
{
- nodeLabel = new Format("%5.2f").form(node.dist);
+ nodeLabel = new Format("%-.2f").form(node.dist);
}
if (showBootstrap)
@@ -189,16 +211,16 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
if (!nodeLabel.equals(""))
{
- g.drawString(nodeLabel, xstart, ypos - 10);
+ g.drawString(nodeLabel, xstart+2, ypos - 2);
}
String name = (markPlaceholders && node.isPlaceholder())
? (PLACEHOLDER + node.getName()) : node.getName();
- FontMetrics fm = g.getFontMetrics(font);
+
int charWidth = fm.stringWidth(name) + 3;
- int charHeight = fm.getHeight();
+ int charHeight = font.getSize();
- Rectangle rect = new Rectangle(xend + 20, ypos - charHeight,
+ Rectangle rect = new Rectangle(xend+10, ypos-charHeight/2,
charWidth, charHeight);
nameHash.put((SequenceI) node.element(), rect);
@@ -207,16 +229,16 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
SequenceGroup selected = av.getSelectionGroup();
if ((selected != null) &&
- selected.sequences.contains((SequenceI) node.element()))
+ selected.getSequences(false).contains((SequenceI) node.element()))
{
g.setColor(Color.gray);
- g.fillRect(xend + 10, ypos - charHeight + 3, charWidth,
+ g.fillRect(xend + 10, ypos-charHeight/2, charWidth,
charHeight);
g.setColor(Color.white);
}
- g.drawString(name, xend + 10, ypos);
+ g.drawString(name, xend + 10, ypos+fm.getDescent());
g.setColor(Color.black);
}
else
@@ -237,7 +259,10 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
// 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;
@@ -252,8 +277,8 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
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).trim(), xstart+2,
+ ypos - 2);
}
}
}
@@ -319,7 +344,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
((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);
}
@@ -395,7 +420,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
if (node.element() instanceof SequenceI)
{
- ((SequenceI) node.element()).setColor(c);
+ av.setSequenceColour((SequenceI) node.element(), c);
}
}
else
@@ -503,52 +528,49 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
public void paintComponent(Graphics g)
{
super.paintComponent(g);
- font = new Font("Verdana", Font.PLAIN, fontSize);
g.setFont(font);
- FontMetrics fm = g.getFontMetrics(font);
-
- if (nameHash.size() == 0)
+ if(tree==null)
{
- repaint();
+ g.drawString("Calculating tree....", 20, getHeight()/2);
}
-
- if (fitToWindow ||
- (!fitToWindow &&
- (scrollPane.getHeight() > ((fm.getHeight() * nameHash.size()) +
- offy))))
+ else
{
+ fm = g.getFontMetrics(font);
+
+ if (nameHash.size() == 0)
+ {
+ repaint();
+ }
+
+ if (fitToWindow ||
+ (!fitToWindow &&
+ (scrollPane.getHeight() > ( (fm.getHeight() * nameHash.size()) +
+ offy))))
+ {
draw(g, scrollPane.getWidth(), scrollPane.getHeight());
setPreferredSize(null);
- }
- else
- {
+ }
+ else
+ {
setPreferredSize(new Dimension(scrollPane.getWidth(),
- fm.getHeight() * nameHash.size()));
+ fm.getHeight() * nameHash.size()));
draw(g, scrollPane.getWidth(), fm.getHeight() * nameHash.size());
- }
+ }
- scrollPane.revalidate();
+ scrollPane.revalidate();
+ }
}
- /**
- * DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public int getFontSize()
- {
- return fontSize;
- }
/**
* DOCUMENT ME!
*
* @param fontSize DOCUMENT ME!
*/
- public void setFontSize(int fontSize)
+ public void setFont(Font font)
{
- this.fontSize = fontSize;
+ this.font = font;
repaint();
}
@@ -567,7 +589,13 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
g2.setColor(Color.white);
g2.fillRect(0, 0, width, height);
- labelLength = g2.getFontMetrics(font).stringWidth(longestName) + 20; //20 allows for scrollbar
+ g2.setFont(font);
+
+ offy = font.getSize()+10;
+
+ fm = g2.getFontMetrics(font);
+
+ labelLength = fm.stringWidth(longestName) + 20; //20 allows for scrollbar
float wscale = (float) (width - labelLength - (offx * 2)) / tree.getMaxHeight();
@@ -579,7 +607,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
((SequenceNode) top.right()).count;
}
- float chunk = (float) (height - (offy * 2)) / top.count;
+ float chunk = (float) (height - (offy)) / top.count;
drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
@@ -633,10 +661,74 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
*
* @param e DOCUMENT ME!
*/
- public void mouseClicked(MouseEvent e)
+ public void mouseClicked(MouseEvent evt)
{
+ if(highlightNode!=null)
+ {
+ if (SwingUtilities.isRightMouseButton(evt))
+ {
+ Color col = JColorChooser.showDialog(this, "Select Background Colour",
+ highlightNode.color);
+
+ setColor(highlightNode, col);
+ }
+ else
+ 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 mouseMoved(MouseEvent evt)
+ {
+ av.setCurrentTree(tree);
+
+ Object ob = findElement(evt.getX(), evt.getY());
+
+ if (ob instanceof SequenceNode)
+ {
+ highlightNode = (SequenceNode) ob;
+ this.setToolTipText(
+ "Left click to select leaves"
+ + "
Double-click to invert leaves"
+ + "
Right click to change colour");
+ repaint();
+
+ }
+ else
+ {
+ if (highlightNode != null)
+ {
+ highlightNode = null;
+ setToolTipText(null);
+ repaint();
+ }
+ }
+ }
+
+ public void mouseDragged(MouseEvent ect)
+ {}
+
/**
* DOCUMENT ME!
*
@@ -653,19 +745,12 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
if (ob instanceof SequenceI)
{
- TreeSelectionChanged((Sequence) ob);
- repaint();
-
- return;
- }
- else if (ob instanceof SequenceNode)
- {
- SequenceNode tmpnode = (SequenceNode) ob;
- tree.swapNodes(tmpnode);
- tree.reCount(tree.getTopNode());
- tree.findHeight(tree.getTopNode());
+ treeSelectionChanged( (Sequence) ob);
+ PaintRefresher.Refresh(this, ap.av.getSequenceSetId());
+ repaint();
+ return;
}
- else
+ else if( !(ob instanceof SequenceNode) )
{
// Find threshold
if (tree.getMaxHeight() != 0)
@@ -679,63 +764,86 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
av.setSelectionGroup(null);
av.alignment.deleteAllGroups();
+ av.sequenceColours=null;
- for (int i = 0; i < tree.getGroups().size(); i++)
- {
- Color col = new Color((int) (Math.random() * 255),
- (int) (Math.random() * 255),
- (int) (Math.random() * 255));
- setColor((SequenceNode) tree.getGroups().elementAt(i),
- col.brighter());
+ colourGroups();
+ }
- Vector l = tree.findLeaves((SequenceNode) tree.getGroups()
- .elementAt(i),
- new Vector());
+ PaintRefresher.Refresh(this, ap.av.getSequenceSetId());
+ repaint();
+ }
- Vector sequences = new Vector();
- for (int j = 0; j < l.size(); j++)
- {
- SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j)).element();
+ }
- if (!sequences.contains(s1))
- {
- sequences.addElement(s1);
- }
- }
+ void colourGroups()
+ {
+ for (int i = 0; i < tree.getGroups().size(); i++)
+ {
+ Color col = new Color( (int) (Math.random() * 255),
+ (int) (Math.random() * 255),
+ (int) (Math.random() * 255));
+ setColor( (SequenceNode) tree.getGroups().elementAt(i),
+ col.brighter());
- ColourSchemeI cs = ColourSchemeProperty.getColour(sequences,
- av.alignment.getWidth(),
- ColourSchemeProperty.getColourName(
- av.getGlobalColourScheme()));
+ Vector l = tree.findLeaves( (SequenceNode) tree.getGroups()
+ .elementAt(i),
+ new Vector());
- SequenceGroup sg = new SequenceGroup(sequences,
- "TreeGroup", cs, true, true, false, 0,
- av.alignment.getWidth());
+ Vector sequences = new Vector();
- if (sg.cs != null)
- {
- sg.cs.setThreshold(25, av.getIgnoreGapsConsensus());
- }
+ for (int j = 0; j < l.size(); j++)
+ {
+ SequenceI s1 = (SequenceI) ( (SequenceNode) l.elementAt(j)).element();
- if (av.getGlobalColourScheme().conservationApplied())
- {
- Conservation c = new Conservation("Group",
- ResidueProperties.propHash, 3, sg.sequences,
- sg.getStartRes(), sg.getEndRes());
+ if (!sequences.contains(s1))
+ {
+ sequences.addElement(s1);
+ }
+ }
- c.calculate();
- c.verdict(false, av.ConsPercGaps);
- sg.cs.setConservation(c);
- }
+ ColourSchemeI cs = null;
- av.alignment.addGroup(sg);
- }
- }
+ 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());
}
- PaintRefresher.Refresh(this, av.alignment);
- repaint();
+ SequenceGroup sg = new SequenceGroup(sequences,
+ "TreeGroup", cs, true, true, false,
+ 0,
+ av.alignment.getWidth() - 1);
+
+ if (av.getGlobalColourScheme() != null
+ && av.getGlobalColourScheme().conservationApplied())
+ {
+ Conservation c = new Conservation("Group",
+ ResidueProperties.propHash, 3,
+ sg.getSequences(false),
+ sg.getStartRes(), sg.getEndRes());
+
+ c.calculate();
+ c.verdict(false, av.ConsPercGaps);
+ sg.cs.setConservation(c);
+ }
+
+ av.alignment.addGroup(sg);
+ }
+
}
/**