X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FTreePanel.java;h=f4543ed83ef71d56e17e0e82bbc4c3d7a7f8b8e1;hb=77c35fa912621cbadcf5ec74da4c6d45de6322e1;hp=936eebebee60d408f3736d5386d0821083459599;hpb=6f3fc393f90ea0460caeca7e229e505741c51bd4;p=jalview.git diff --git a/src/jalview/gui/TreePanel.java b/src/jalview/gui/TreePanel.java index 936eebe..f4543ed 100755 --- a/src/jalview/gui/TreePanel.java +++ b/src/jalview/gui/TreePanel.java @@ -1,274 +1,631 @@ -package jalview.gui; - -import jalview.datamodel.*; -import jalview.analysis.*; -import jalview.jbgui.GTreePanel; -import jalview.io.*; -import java.awt.event.*; -import java.util.*; -import java.io.*; -import jalview.util.*; -import javax.swing.*; - - -public class TreePanel extends GTreePanel -{ - SequenceI[] seq; - String type; - String pwtype; - AlignViewport av; - int start; - int end; - TreeCanvas treeCanvas; - NJTree tree; - - FileProperties fp; - PostscriptProperties pp; - - PrintWriter bw; - PrintStream ps; - boolean makeString = false; - StringBuffer out; - - - - public TreePanel(AlignViewport av, Vector seqVector, String type, String pwtype, int s, int e) - { - super(); - - this.type = type; - this.pwtype = pwtype; - - start = s; - end = e; - - String longestName = ""; - seq = new Sequence [seqVector.size()]; - for (int i=0;i < seqVector.size();i++) - { - seq[i] = (Sequence) seqVector.elementAt(i); - if(seq[i].getName().length()>longestName.length()) - longestName = seq[i].getName(); - } - - tree = new NJTree(seq, type, pwtype, start, end); - av.setCurrentTree(tree); - - propertiesInit(); - - treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName); - - tree.reCount(tree.getTopNode()); - tree.findHeight(tree.getTopNode()); - scrollPane.setViewportView(treeCanvas); - - } - - -public void drawPostscript(PostscriptProperties pp) { - try { - int width = 0; - int height = 0; - - printout("%!\n"); - - printout("/" + pp.font + " findfont\n"); - printout(pp.fsize + " scalefont setfont\n"); - - int offx = pp.xoffset; - int offy = pp.yoffset; - - if (pp.orientation == PostscriptProperties.PORTRAIT) { - width = PostscriptProperties.SHORTSIDE; - height = PostscriptProperties.LONGSIDE; - } else { - height = PostscriptProperties.SHORTSIDE; - width = PostscriptProperties.LONGSIDE; - printout(height + " 0 translate\n90 rotate\n"); - } - float wscale = (float)(width*.8-offx*2)/tree.getMaxHeight(); - SequenceNode top = tree.getTopNode(); - - if (top.count == 0) { - top.count = ((SequenceNode)top.left()).count + ((SequenceNode)top.right()).count ; - } - - float chunk = (float)(height-offy*2)/(((SequenceNode)top).count+1); - - drawPostscriptNode(top,chunk,wscale,width,offx,offy); - - printout("showpage\n"); - } catch (java.io.IOException e) { - System.out.println("Exception " + e); - } -} - -public void drawPostscriptNode(SequenceNode node, float chunk, float scale, int width, int offx, int offy) throws java.io.IOException { - if (node == null) { - return; - } - - if (node.left() == null && node.right() == null) { - // Drawing leaf node - - float height = node.height; - float dist = node.dist; - - int xstart = (int)((height-dist)*scale) + offx; - int xend = (int)(height*scale) + offx; - - int ypos = (int)(node.ycount * chunk) + offy; - - // g.setColor(Color.black); - printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " + - new Format("%5.3f").form((double)node.color.getGreen()/255) + " " + - new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n"); - - // Draw horizontal line - // g.drawLine(xstart,ypos,xend,ypos); - printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke \n"); - - if (treeCanvas.showDistances && node.dist > 0) { - // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5); - printout("(" + new Format("%5.2f").form(node.dist) + ") " + xstart + " " + (ypos+5) + " moveto show\n"); - } - //g.drawString((String)node.element(),xend+20,ypos); - printout("(" + (((SequenceI)node.element()).getName()) + ") " + (xend+20) + " " + (ypos) + " moveto show\n"); - } else { - drawPostscriptNode((SequenceNode)node.left(),chunk,scale,width,offx,offy); - drawPostscriptNode((SequenceNode)node.right(),chunk,scale,width,offx,offy); - - - float height = node.height; - float dist = node.dist; - - int xstart = (int)((height-dist)*scale) + offx; - int xend = (int)(height*scale) + offx; - int ypos = (int)(node.ycount * chunk) + offy; - - printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " + - new Format("%5.3f").form((double)node.color.getGreen()/255) + " " + - new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n"); - // g.setColor(Color.black); - // bw.append("\nblack setrgbcolor\n"); - // Draw horizontal line - // g.drawLine(xstart,ypos,xend,ypos); - printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke\n"); - int ystart = (int)(((SequenceNode)node.left()).ycount * chunk) + offy; - int yend = (int)(((SequenceNode)node.right()).ycount * chunk) + offy; - - // g.drawLine((int)(height*scale) + offx, ystart, - // (int)(height*scale) + offx, yend); - printout - (((int)(height*scale) + offx) + " " + ystart + " moveto " + ((int)(height*scale) + offx) + " " + - yend + " lineto stroke\n"); - if (treeCanvas.showDistances && node.dist > 0) { - // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5); - printout("(" +new Format("%5.2f").form(node.dist) + ") " + (xstart) + " " + (ypos+5) + " moveto show\n"); - } - } -} - -public void printout(String s) throws IOException { - if (bw != null) { - bw.write(s); - } - if (ps != null) { - ps.print(s); - } - if (makeString == true) { - out.append(s); - } -} - - -public PostscriptProperties getPostscriptProperties() { - return pp; -} - -public FileProperties getFileProperties() { - return fp; -} - -public void setPostscriptProperties(PostscriptProperties pp) { - this.pp = pp; -} - -public void setFileProperties(FileProperties fp) { - this.fp = fp; -} - -public String getText(String format) { - return null; -} - -public void getPostscript(PrintWriter bw) { - this.bw = bw; - drawPostscript(pp); - -} - -public void getPostscript(PrintStream bw) { - this.ps = bw; - drawPostscript(pp); - bw.flush(); -} - -public StringBuffer getPostscript() { - makeString = true; - out = new StringBuffer(); - drawPostscript(pp); - return out; -} - -public void propertiesInit() { - this.pp = new PostscriptProperties(); - this.fp = new FileProperties(); -} - - - public void saveButton_actionPerformed(ActionEvent e) - { - - try{ - JFileChooser chooser = new JFileChooser(jalview.bin.Cache.getProperty("LAST_DIRECTORY")); - chooser.setDialogTitle("Save Tree as postscript file"); - chooser.setToolTipText("Save"); - int value = chooser.showSaveDialog(this); - if (value == JFileChooser.APPROVE_OPTION) - { - String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice); - - bw = new PrintWriter(new FileWriter(choice)); - getPostscript(bw); - bw.close(); - - } - - }catch(Exception ex){ex.printStackTrace();} - - } - - public void showDistanceCheckbox_actionPerformed(ActionEvent e) - { - treeCanvas.setShowDistances(showDistanceCheckbox.isSelected()); - } - - public void fontSizeCombobox_actionPerformed(ActionEvent e) - { - if( treeCanvas==null ) - return; - - int size = Integer.parseInt( fontSizeCombobox.getSelectedItem().toString() ); - treeCanvas.setFontSize( size ); - - scrollPane.setViewportView(treeCanvas); - } - - public void bootStrapCheckBox_actionPerformed(ActionEvent e) - { - treeCanvas.setShowBootstrap(bootStrapCheckBox.isSelected()); - } - -} +/* + * Jalview - A Sequence Alignment Editor and Viewer + * 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ +package jalview.gui; + +import jalview.analysis.*; + +import jalview.datamodel.*; + +import jalview.io.*; + +import jalview.jbgui.*; + +import org.jibble.epsgraphics.*; + +import java.awt.*; +import java.awt.event.*; +import java.awt.image.*; + +import java.io.*; + +import java.util.*; + +import javax.swing.*; +import javax.imageio.*; + +import java.beans.PropertyChangeEvent; + + +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +public class TreePanel extends GTreePanel +{ + String type; + String pwtype; + TreeCanvas treeCanvas; + NJTree tree; + AlignViewport av; + + /** + * Creates a new TreePanel object. + * + * @param av DOCUMENT ME! + * @param seqVector DOCUMENT ME! + * @param type DOCUMENT ME! + * @param pwtype DOCUMENT ME! + * @param s DOCUMENT ME! + * @param e DOCUMENT ME! + */ + public TreePanel(AlignmentPanel ap, String type, String pwtype) + { + super(); + initTreePanel(ap, type, pwtype, null, null); + + // We know this tree has distances. JBPNote TODO: prolly should add this as a userdefined default + // showDistances(true); + } + + /** + * Creates a new TreePanel object. + * + * @param av DOCUMENT ME! + * @param seqVector DOCUMENT ME! + * @param newtree DOCUMENT ME! + * @param type DOCUMENT ME! + * @param pwtype DOCUMENT ME! + */ + public TreePanel(AlignmentPanel ap, + String type, + String pwtype, + NewickFile newtree) + { + super(); + initTreePanel(ap, type, pwtype, newtree, null); + } + + public TreePanel(AlignmentPanel av, + String type, + String pwtype, + NewickFile newtree, AlignmentView inputData) { + super(); + initTreePanel(av,type,pwtype,newtree,inputData); + } + + public AlignmentI getAlignment() + { + return treeCanvas.av.getAlignment(); + } + public AlignViewport getViewPort() { + return treeCanvas.av; + } + + void initTreePanel(AlignmentPanel ap, String type, String pwtype, + NewickFile newTree, AlignmentView inputData) + { + + av = ap.av; + this.type = type; + this.pwtype = pwtype; + + treeCanvas = new TreeCanvas(this, ap, scrollPane); + scrollPane.setViewportView(treeCanvas); + + PaintRefresher.Register(this, ap.av.getSequenceSetId()); + + buildAssociatedViewMenu(); + + av.addPropertyChangeListener(new java.beans.PropertyChangeListener() + { + public void propertyChange(PropertyChangeEvent evt) + { + if (evt.getPropertyName().equals("alignment")) + { + if(tree==null) + System.out.println("tree is null"); + if(evt.getNewValue()==null) + System.out.println("new alignment sequences vector value is null"); + + tree.UpdatePlaceHolders( (Vector) evt.getNewValue()); + treeCanvas.nameHash.clear(); // reset the mapping between canvas rectangles and leafnodes + repaint(); + } + } + }); + + TreeLoader tl = new TreeLoader(newTree); + if (inputData!=null) { + tl.odata=inputData; + } + tl.start(); + + } + + public void viewMenu_menuSelected() + { + buildAssociatedViewMenu(); + } + + + void buildAssociatedViewMenu() + { + AlignmentPanel [] aps = PaintRefresher.getAssociatedPanels(av.getSequenceSetId()); + if(aps.length==1 && treeCanvas.ap==aps[0]) + { + associateLeavesMenu.setVisible(false); + return; + } + + associateLeavesMenu.setVisible(true); + + if( (viewMenu.getItem(viewMenu.getItemCount()-2) instanceof JMenuItem)) + { + viewMenu.insertSeparator(viewMenu.getItemCount()-1); + } + + associateLeavesMenu.removeAll(); + + + JRadioButtonMenuItem item; + ButtonGroup buttonGroup = new ButtonGroup(); + int i, iSize = aps.length; + final TreePanel thisTreePanel = this; + for(i=0; i