mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/hcl/HCLColorBar.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: HCLColorBar.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.8 $
2 26 Feb 07 jari 8  * $Date: 2006/04/10 18:41:36 $
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.RenderingHints;
2 26 Feb 07 jari 21 import java.util.ArrayList;
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.IDisplayMenu;
2 26 Feb 07 jari 26 import org.tigr.microarray.mev.cluster.gui.IFramework;
2 26 Feb 07 jari 27
2 26 Feb 07 jari 28 public class HCLColorBar extends JPanel {
2 26 Feb 07 jari 29     
2 26 Feb 07 jari 30     private static final int BAR_WIDTH = 10;
2 26 Feb 07 jari 31     
2 26 Feb 07 jari 32     private ArrayList clusters;
2 26 Feb 07 jari 33     
2 26 Feb 07 jari 34     private int featuresSize;
2 26 Feb 07 jari 35     private boolean isAntiAliasing = true;
2 26 Feb 07 jari 36     private int elementHeight = 5;
2 26 Feb 07 jari 37     
2 26 Feb 07 jari 38     /**
2 26 Feb 07 jari 39      * Constructs a <code>HCLColorBar</code> for specified hcl clusters.
2 26 Feb 07 jari 40      */
2 26 Feb 07 jari 41     public HCLColorBar(ArrayList clusters, int featuresSize) {
2 26 Feb 07 jari 42     setBackground(Color.white);
2 26 Feb 07 jari 43     setFont(new Font("monospaced",Font.PLAIN, 20));
2 26 Feb 07 jari 44     this.clusters = clusters;
2 26 Feb 07 jari 45     this.featuresSize = featuresSize;
2 26 Feb 07 jari 46     }
2 26 Feb 07 jari 47     
2 26 Feb 07 jari 48     //EH additions for XMLEncoding/Decoding
2 26 Feb 07 jari 49     public HCLColorBar() {
2 26 Feb 07 jari 50     setBackground(Color.white);
2 26 Feb 07 jari 51     setFont(new Font("monospaced",Font.PLAIN, 20));
2 26 Feb 07 jari 52     }
2 26 Feb 07 jari 53     public ArrayList getClusters(){return clusters;}
2 26 Feb 07 jari 54     public int getFeaturesSize(){return featuresSize;}
2 26 Feb 07 jari 55     public void setClusters(ArrayList a) {this.clusters = a;}
2 26 Feb 07 jari 56     public void setFeaturesSize(int fs) {this.featuresSize = fs;}
2 26 Feb 07 jari 57     //EH end additions
2 26 Feb 07 jari 58     
2 26 Feb 07 jari 59     /**
2 26 Feb 07 jari 60      * Paints the component into specified graphics.
2 26 Feb 07 jari 61      */
2 26 Feb 07 jari 62     public void paint(Graphics g) {
2 26 Feb 07 jari 63   super.paint(g);
2 26 Feb 07 jari 64   if (this.isAntiAliasing) {
2 26 Feb 07 jari 65       ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
2 26 Feb 07 jari 66       ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
2 26 Feb 07 jari 67   }
2 26 Feb 07 jari 68   HCLCluster cluster;
2 26 Feb 07 jari 69   int x = 10, y, size;
2 26 Feb 07 jari 70   final int COUNT_CLUSTERS = this.clusters.size();
2 26 Feb 07 jari 71   for (int i=0; i<COUNT_CLUSTERS; i++) {
2 26 Feb 07 jari 72       cluster = (HCLCluster)this.clusters.get(i);
2 26 Feb 07 jari 73       y    = cluster.firstElem*this.elementHeight+this.elementHeight/2;
2 26 Feb 07 jari 74       size = (cluster.lastElem-cluster.firstElem)*this.elementHeight;
2 26 Feb 07 jari 75       g.setColor(cluster.color);
2 26 Feb 07 jari 76       g.fillRect(x, y, x+BAR_WIDTH, size);
2 26 Feb 07 jari 77       if (cluster.text != null) {
2 26 Feb 07 jari 78     g.drawString(cluster.text, x+BAR_WIDTH+10, y+size/2+7);
2 26 Feb 07 jari 79       }
2 26 Feb 07 jari 80   }
2 26 Feb 07 jari 81     }
2 26 Feb 07 jari 82     
2 26 Feb 07 jari 83     /**
2 26 Feb 07 jari 84      * Updates attributies when component was selected by the framework.
2 26 Feb 07 jari 85      */
2 26 Feb 07 jari 86     public void onSelected(IFramework framework) {
2 26 Feb 07 jari 87   IDisplayMenu menu = framework.getDisplayMenu();
2 26 Feb 07 jari 88   this.isAntiAliasing = menu.isAntiAliasing();
2 26 Feb 07 jari 89   setElementHeight(menu.getElementSize().height);
2 26 Feb 07 jari 90   updateSize();
2 26 Feb 07 jari 91     }
2 26 Feb 07 jari 92     
2 26 Feb 07 jari 93     /**
2 26 Feb 07 jari 94      * Updates attributies, if the framework display menu was changed.
2 26 Feb 07 jari 95      */
2 26 Feb 07 jari 96     public void onMenuChanged(IDisplayMenu menu) {
2 26 Feb 07 jari 97   if (this.elementHeight == menu.getElementSize().height &&
2 26 Feb 07 jari 98   this.isAntiAliasing == menu.isAntiAliasing()) {
2 26 Feb 07 jari 99       return;
2 26 Feb 07 jari 100   }
2 26 Feb 07 jari 101   this.isAntiAliasing = menu.isAntiAliasing();
2 26 Feb 07 jari 102   setElementHeight(menu.getElementSize().height);
2 26 Feb 07 jari 103   updateSize();
2 26 Feb 07 jari 104     }
2 26 Feb 07 jari 105     
2 26 Feb 07 jari 106     /**
2 26 Feb 07 jari 107      * Updates the component when clusters were changed.
2 26 Feb 07 jari 108      */
2 26 Feb 07 jari 109     public void onClustersChanged(ArrayList clusters) {
2 26 Feb 07 jari 110   this.clusters = clusters;
2 26 Feb 07 jari 111   updateSize();
2 26 Feb 07 jari 112   repaint();
2 26 Feb 07 jari 113     }
2 26 Feb 07 jari 114     
2 26 Feb 07 jari 115     /**
2 26 Feb 07 jari 116      * Sets a new element height.
2 26 Feb 07 jari 117      */
2 26 Feb 07 jari 118     private void setElementHeight(int height) {
2 26 Feb 07 jari 119   this.elementHeight = height;
2 26 Feb 07 jari 120     }
2 26 Feb 07 jari 121     
2 26 Feb 07 jari 122     /**
2 26 Feb 07 jari 123      * Updates the component sizes.
2 26 Feb 07 jari 124      */
2 26 Feb 07 jari 125     private void updateSize() {
2 26 Feb 07 jari 126   Graphics2D g = (Graphics2D)getGraphics();
2 26 Feb 07 jari 127   int width = 10+BAR_WIDTH+10+getMaxWidth(g);
2 26 Feb 07 jari 128   int height = this.elementHeight*this.featuresSize+1;
2 26 Feb 07 jari 129   setSizes(width, height);
2 26 Feb 07 jari 130     }
2 26 Feb 07 jari 131     
2 26 Feb 07 jari 132     /**
2 26 Feb 07 jari 133      * Calculates max description width.
2 26 Feb 07 jari 134      */
2 26 Feb 07 jari 135     private int getMaxWidth(Graphics2D g) {
2 26 Feb 07 jari 136   if (g == null || this.clusters == null) {
2 26 Feb 07 jari 137       return 0;
2 26 Feb 07 jari 138   }
2 26 Feb 07 jari 139   if (this.isAntiAliasing) {
2 26 Feb 07 jari 140       g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
2 26 Feb 07 jari 141       g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
2 26 Feb 07 jari 142   }
2 26 Feb 07 jari 143   FontMetrics fm = g.getFontMetrics();
2 26 Feb 07 jari 144   int max = 0;
2 26 Feb 07 jari 145   String str;
2 26 Feb 07 jari 146   final int size = this.clusters.size();
2 26 Feb 07 jari 147   HCLCluster cluster;
2 26 Feb 07 jari 148   for (int i=0; i<size; i++) {
2 26 Feb 07 jari 149       cluster = (HCLCluster)this.clusters.get(i);
2 26 Feb 07 jari 150       if (cluster != null) {
2 26 Feb 07 jari 151     str = cluster.text == null ? "" : cluster.text;
2 26 Feb 07 jari 152     max = Math.max(max, fm.stringWidth(str));
2 26 Feb 07 jari 153       }
2 26 Feb 07 jari 154   }
2 26 Feb 07 jari 155   return max;
2 26 Feb 07 jari 156     }
2 26 Feb 07 jari 157     
2 26 Feb 07 jari 158     /**
2 26 Feb 07 jari 159      * Sets the component sizes.
2 26 Feb 07 jari 160      */
2 26 Feb 07 jari 161     private void setSizes(int width, int height) {
2 26 Feb 07 jari 162   setSize(width, height);
2 26 Feb 07 jari 163   setPreferredSize(new Dimension(width, height));        
2 26 Feb 07 jari 164     }
2 26 Feb 07 jari 165     
2 26 Feb 07 jari 166
2 26 Feb 07 jari 167 }