mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/hcl/HCLAnnotationBar.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2004, The Institute for Genomic Research (TIGR).
2 26 Feb 07 jari 3 All rights reserved.
2 26 Feb 07 jari 4  */
2 26 Feb 07 jari 5 /*
2 26 Feb 07 jari 6  * $RCSfile: HCLAnnotationBar.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.5 $
2 26 Feb 07 jari 8  * $Date: 2006/03/24 15:50:40 $
2 26 Feb 07 jari 9  * $Author: eleanorahowe $
2 26 Feb 07 jari 10  * $State: Exp $
2 26 Feb 07 jari 11  */
2 26 Feb 07 jari 12 package org.tigr.microarray.mev.cluster.gui.impl.hcl;
2 26 Feb 07 jari 13
2 26 Feb 07 jari 14 import java.awt.Color;
2 26 Feb 07 jari 15 import java.awt.Dimension;
2 26 Feb 07 jari 16 import java.awt.Font;
2 26 Feb 07 jari 17 import java.awt.FontMetrics;
2 26 Feb 07 jari 18 import java.awt.Graphics;
2 26 Feb 07 jari 19 import java.awt.Graphics2D;
2 26 Feb 07 jari 20 import java.awt.Rectangle;
2 26 Feb 07 jari 21 import java.awt.RenderingHints;
2 26 Feb 07 jari 22
2 26 Feb 07 jari 23 import javax.swing.JPanel;
2 26 Feb 07 jari 24
2 26 Feb 07 jari 25 import org.tigr.microarray.mev.cluster.gui.IData;
2 26 Feb 07 jari 26 import org.tigr.microarray.mev.cluster.gui.IDisplayMenu;
2 26 Feb 07 jari 27 import org.tigr.microarray.mev.cluster.gui.IFramework;
2 26 Feb 07 jari 28
2 26 Feb 07 jari 29 public class HCLAnnotationBar extends JPanel {
2 26 Feb 07 jari 30     
2 26 Feb 07 jari 31     private IData data;
2 26 Feb 07 jari 32     private int[] rowsOrder;
2 26 Feb 07 jari 33     private boolean isAntiAliasing = true;
2 26 Feb 07 jari 34     private int elementHeight = 5;
2 26 Feb 07 jari 35     private int maxUniqueIDWidth, maxGeneNameWidth;
2 26 Feb 07 jari 36     
2 26 Feb 07 jari 37     /**
2 26 Feb 07 jari 38      * Constructs a <code>HCLAnnotationBar</code>.
2 26 Feb 07 jari 39      */
2 26 Feb 07 jari 40     public HCLAnnotationBar(int[] rowsOrder) {
2 26 Feb 07 jari 41         this.rowsOrder = rowsOrder;
2 26 Feb 07 jari 42         setBackground(Color.white);
2 26 Feb 07 jari 43         setSizes(10, 10);
2 26 Feb 07 jari 44     }
2 26 Feb 07 jari 45         
2 26 Feb 07 jari 46         /**
2 26 Feb 07 jari 47          * Paints the bar into specified graphics.
2 26 Feb 07 jari 48          */
2 26 Feb 07 jari 49         public void paint(Graphics g) {
2 26 Feb 07 jari 50             super.paint(g);
2 26 Feb 07 jari 51             if (this.isAntiAliasing) {
2 26 Feb 07 jari 52                 ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
2 26 Feb 07 jari 53                 ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
2 26 Feb 07 jari 54             }
2 26 Feb 07 jari 55             
2 26 Feb 07 jari 56             Rectangle bounds = g.getClipBounds();
2 26 Feb 07 jari 57             final int top = getTopIndex(bounds.y);
2 26 Feb 07 jari 58             final int bottom = getBottomIndex(bounds.y+bounds.height, this.rowsOrder.length);
2 26 Feb 07 jari 59             
2 26 Feb 07 jari 60             g.setColor(Color.black);
2 26 Feb 07 jari 61             int uniqX = 10;
2 26 Feb 07 jari 62             int nameX = uniqX+10+this.maxUniqueIDWidth;
2 26 Feb 07 jari 63             int annY;
2 26 Feb 07 jari 64             for (int row=top; row<bottom; row++) {
2 26 Feb 07 jari 65                 annY = (row+1)*this.elementHeight;
2 26 Feb 07 jari 66                 g.drawString(data.getUniqueId(this.rowsOrder[row]), uniqX, annY);
2 26 Feb 07 jari 67                 g.drawString(data.getGeneName(this.rowsOrder[row]), nameX, annY);
2 26 Feb 07 jari 68             }
2 26 Feb 07 jari 69         }
2 26 Feb 07 jari 70         
2 26 Feb 07 jari 71         private int getTopIndex(int top) {
2 26 Feb 07 jari 72             if (top < 0) {
2 26 Feb 07 jari 73                 return 0;
2 26 Feb 07 jari 74             }
2 26 Feb 07 jari 75             return top/this.elementHeight;
2 26 Feb 07 jari 76         }
2 26 Feb 07 jari 77         
2 26 Feb 07 jari 78         private int getBottomIndex(int bottom, int limit) {
2 26 Feb 07 jari 79             if (bottom < 0) {
2 26 Feb 07 jari 80                 return 0;
2 26 Feb 07 jari 81             }
2 26 Feb 07 jari 82             int result = bottom/this.elementHeight+1;
2 26 Feb 07 jari 83             return result > limit ? limit : result;
2 26 Feb 07 jari 84         }
2 26 Feb 07 jari 85         
2 26 Feb 07 jari 86         /**
2 26 Feb 07 jari 87          * Updates its attributies when the viewer was selected.
2 26 Feb 07 jari 88          */
2 26 Feb 07 jari 89         public void onSelected(IFramework framework) {
2 26 Feb 07 jari 90             this.data = framework.getData();
2 26 Feb 07 jari 91             IDisplayMenu menu = framework.getDisplayMenu();
2 26 Feb 07 jari 92             this.isAntiAliasing = menu.isAntiAliasing();
2 26 Feb 07 jari 93             setElementHeight(menu.getElementSize().height);
2 26 Feb 07 jari 94             updateSize();
2 26 Feb 07 jari 95         }
2 26 Feb 07 jari 96         
2 26 Feb 07 jari 97         /**
2 26 Feb 07 jari 98          * Updates its attributies when the framework display menu was changed.
2 26 Feb 07 jari 99          */
2 26 Feb 07 jari 100         public void onMenuChanged(IDisplayMenu menu) {
2 26 Feb 07 jari 101             if (this.elementHeight == menu.getElementSize().height &&
2 26 Feb 07 jari 102             this.isAntiAliasing == menu.isAntiAliasing()) {
2 26 Feb 07 jari 103                 return;
2 26 Feb 07 jari 104             }
2 26 Feb 07 jari 105             this.isAntiAliasing = menu.isAntiAliasing();
2 26 Feb 07 jari 106             setElementHeight(menu.getElementSize().height);
2 26 Feb 07 jari 107             updateSize();
2 26 Feb 07 jari 108         }
2 26 Feb 07 jari 109         
2 26 Feb 07 jari 110         /**
2 26 Feb 07 jari 111          * Sets a new element height.
2 26 Feb 07 jari 112          */
2 26 Feb 07 jari 113         private void setElementHeight(int height) {
2 26 Feb 07 jari 114             this.elementHeight = height;
2 26 Feb 07 jari 115             setFont(new Font("monospaced", Font.PLAIN, height));
2 26 Feb 07 jari 116         }
2 26 Feb 07 jari 117         
2 26 Feb 07 jari 118         /**
2 26 Feb 07 jari 119          * Updates the bar sizes.
2 26 Feb 07 jari 120          */
2 26 Feb 07 jari 121         private void updateSize() {
2 26 Feb 07 jari 122             Graphics2D g = (Graphics2D)getGraphics();
2 26 Feb 07 jari 123             this.maxGeneNameWidth = getMaxWidth(g, true);
2 26 Feb 07 jari 124             this.maxUniqueIDWidth = getMaxWidth(g, false);
2 26 Feb 07 jari 125             int width = 20+this.maxGeneNameWidth+this.maxUniqueIDWidth;
2 26 Feb 07 jari 126             int height = this.elementHeight*this.rowsOrder.length+1;
2 26 Feb 07 jari 127             setSizes(width, height);
2 26 Feb 07 jari 128         }
2 26 Feb 07 jari 129         
2 26 Feb 07 jari 130         /**
2 26 Feb 07 jari 131          * Calculates max annotation width.
2 26 Feb 07 jari 132          */
2 26 Feb 07 jari 133         private int getMaxWidth(Graphics2D g, boolean genename) {
2 26 Feb 07 jari 134             if (g == null || this.data == null) {
2 26 Feb 07 jari 135                 return 0;
2 26 Feb 07 jari 136             }
2 26 Feb 07 jari 137             if (this.isAntiAliasing) {
2 26 Feb 07 jari 138                 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
2 26 Feb 07 jari 139                 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
2 26 Feb 07 jari 140             }
2 26 Feb 07 jari 141             FontMetrics fm = g.getFontMetrics();
2 26 Feb 07 jari 142             int max = 0;
2 26 Feb 07 jari 143             String str;
2 26 Feb 07 jari 144             final int size = this.data.getFeaturesSize();
2 26 Feb 07 jari 145             for (int i=0; i<size; i++) {
2 26 Feb 07 jari 146                 str = genename ? this.data.getGeneName(i) : this.data.getUniqueId(i);
2 26 Feb 07 jari 147                 max = Math.max(max, fm.stringWidth(str));
2 26 Feb 07 jari 148             }
2 26 Feb 07 jari 149             return max;
2 26 Feb 07 jari 150         }
2 26 Feb 07 jari 151         
2 26 Feb 07 jari 152         /**
2 26 Feb 07 jari 153          * Sets the component sizes.
2 26 Feb 07 jari 154          */
2 26 Feb 07 jari 155         private void setSizes(int width, int height) {
2 26 Feb 07 jari 156             setSize(width, height);
2 26 Feb 07 jari 157             setPreferredSize(new Dimension(width, height));
2 26 Feb 07 jari 158         }
2 26 Feb 07 jari 159     }