2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: MultipleArrayHeader.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.3 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2005/03/10 15:44:13 $ |
2 |
26 Feb 07 |
jari |
* $Author: braistedj $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.microarray.mev; |
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.Insets; |
2 |
26 Feb 07 |
jari |
21 |
import java.awt.RenderingHints; |
2 |
26 Feb 07 |
jari |
22 |
import java.awt.image.BufferedImage; |
2 |
26 Feb 07 |
jari |
23 |
|
2 |
26 Feb 07 |
jari |
24 |
import javax.swing.JPanel; |
2 |
26 Feb 07 |
jari |
25 |
|
2 |
26 Feb 07 |
jari |
26 |
import org.tigr.microarray.mev.cluster.gui.IData; |
2 |
26 Feb 07 |
jari |
27 |
|
2 |
26 Feb 07 |
jari |
28 |
public class MultipleArrayHeader extends JPanel { |
2 |
26 Feb 07 |
jari |
29 |
|
2 |
26 Feb 07 |
jari |
30 |
private IData data; |
2 |
26 Feb 07 |
jari |
31 |
private Insets insets; |
2 |
26 Feb 07 |
jari |
32 |
private int contentWidth; |
2 |
26 Feb 07 |
jari |
33 |
private int elementWidth = 20; |
2 |
26 Feb 07 |
jari |
34 |
private boolean isAntiAliasing = true; |
2 |
26 Feb 07 |
jari |
35 |
private boolean isTracing = false; |
2 |
26 Feb 07 |
jari |
36 |
private int tracespace; |
2 |
26 Feb 07 |
jari |
37 |
private final int RECT_HEIGHT = 15; |
2 |
26 Feb 07 |
jari |
38 |
|
2 |
26 Feb 07 |
jari |
39 |
BufferedImage negColorImage; |
2 |
26 Feb 07 |
jari |
40 |
BufferedImage posColorImage; |
2 |
26 Feb 07 |
jari |
41 |
private float maxValue; |
2 |
26 Feb 07 |
jari |
42 |
private float minValue; |
2 |
26 Feb 07 |
jari |
43 |
private float midValue; |
2 |
26 Feb 07 |
jari |
44 |
private boolean useDoubleGradient = true; |
2 |
26 Feb 07 |
jari |
45 |
|
2 |
26 Feb 07 |
jari |
46 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>MultipleArrayHeader</code> with specified |
2 |
26 Feb 07 |
jari |
* insets and trace space. |
2 |
26 Feb 07 |
jari |
49 |
*/ |
2 |
26 Feb 07 |
jari |
50 |
public MultipleArrayHeader(Insets insets, int tracespace) { |
2 |
26 Feb 07 |
jari |
51 |
setBackground(Color.white); |
2 |
26 Feb 07 |
jari |
52 |
this.insets = insets; |
2 |
26 Feb 07 |
jari |
53 |
this.tracespace = tracespace; |
2 |
26 Feb 07 |
jari |
54 |
this.maxValue = 3.0f; |
2 |
26 Feb 07 |
jari |
55 |
this.minValue = -3.0f; |
2 |
26 Feb 07 |
jari |
56 |
this.midValue = 0.0f; |
2 |
26 Feb 07 |
jari |
57 |
} |
2 |
26 Feb 07 |
jari |
58 |
|
2 |
26 Feb 07 |
jari |
59 |
/** |
2 |
26 Feb 07 |
jari |
* Sets the component data. |
2 |
26 Feb 07 |
jari |
61 |
*/ |
2 |
26 Feb 07 |
jari |
62 |
public void setData(IData data) { |
2 |
26 Feb 07 |
jari |
63 |
this.data = data; |
2 |
26 Feb 07 |
jari |
64 |
updateSize(); |
2 |
26 Feb 07 |
jari |
65 |
this.repaint(); |
2 |
26 Feb 07 |
jari |
66 |
} |
2 |
26 Feb 07 |
jari |
67 |
|
2 |
26 Feb 07 |
jari |
68 |
/** |
2 |
26 Feb 07 |
jari |
* Sets the anti-aliasing attribute. |
2 |
26 Feb 07 |
jari |
70 |
*/ |
2 |
26 Feb 07 |
jari |
71 |
public void setAntiAliasing(boolean isAntiAliasing) { |
2 |
26 Feb 07 |
jari |
72 |
this.isAntiAliasing = isAntiAliasing; |
2 |
26 Feb 07 |
jari |
73 |
} |
2 |
26 Feb 07 |
jari |
74 |
|
2 |
26 Feb 07 |
jari |
75 |
/** |
2 |
26 Feb 07 |
jari |
* Sets the element width attribute. |
2 |
26 Feb 07 |
jari |
77 |
*/ |
2 |
26 Feb 07 |
jari |
78 |
void setElementWidth(int width) { |
2 |
26 Feb 07 |
jari |
79 |
this.elementWidth = width; |
2 |
26 Feb 07 |
jari |
80 |
setFontSize(width); |
2 |
26 Feb 07 |
jari |
81 |
updateSize(); |
2 |
26 Feb 07 |
jari |
82 |
this.repaint(); |
2 |
26 Feb 07 |
jari |
83 |
} |
2 |
26 Feb 07 |
jari |
84 |
|
2 |
26 Feb 07 |
jari |
85 |
/** |
2 |
26 Feb 07 |
jari |
* Sets the content width attribute. |
2 |
26 Feb 07 |
jari |
87 |
*/ |
2 |
26 Feb 07 |
jari |
88 |
void setContentWidth(int width) { |
2 |
26 Feb 07 |
jari |
89 |
this.contentWidth = width; |
2 |
26 Feb 07 |
jari |
90 |
this.repaint(); |
2 |
26 Feb 07 |
jari |
91 |
} |
2 |
26 Feb 07 |
jari |
92 |
|
2 |
26 Feb 07 |
jari |
93 |
public void setUseDoubleGradient(boolean useDouble) { |
2 |
26 Feb 07 |
jari |
94 |
this.useDoubleGradient = useDouble; |
2 |
26 Feb 07 |
jari |
95 |
} |
2 |
26 Feb 07 |
jari |
96 |
|
2 |
26 Feb 07 |
jari |
97 |
/** |
2 |
26 Feb 07 |
jari |
* Sets min and max ratio values |
2 |
26 Feb 07 |
jari |
99 |
*/ |
2 |
26 Feb 07 |
jari |
100 |
public void setMinAndMaxRatios(float min, float max){ |
2 |
26 Feb 07 |
jari |
101 |
this.minValue = min; |
2 |
26 Feb 07 |
jari |
102 |
this.maxValue = max; |
2 |
26 Feb 07 |
jari |
103 |
this.repaint(); |
2 |
26 Feb 07 |
jari |
104 |
} |
2 |
26 Feb 07 |
jari |
105 |
|
2 |
26 Feb 07 |
jari |
106 |
/** |
2 |
26 Feb 07 |
jari |
* Sets min and max ratio values |
2 |
26 Feb 07 |
jari |
108 |
*/ |
2 |
26 Feb 07 |
jari |
109 |
public void setMinAndMaxAndMidRatios(float min, float mid, float max){ |
2 |
26 Feb 07 |
jari |
110 |
this.minValue = min; |
2 |
26 Feb 07 |
jari |
111 |
this.midValue = mid; |
2 |
26 Feb 07 |
jari |
112 |
this.maxValue = max; |
2 |
26 Feb 07 |
jari |
113 |
this.repaint(); |
2 |
26 Feb 07 |
jari |
114 |
} |
2 |
26 Feb 07 |
jari |
115 |
|
2 |
26 Feb 07 |
jari |
116 |
/** |
2 |
26 Feb 07 |
jari |
* Sets positive and negative color images |
2 |
26 Feb 07 |
jari |
118 |
*/ |
2 |
26 Feb 07 |
jari |
119 |
public void setNegativeAndPositiveColorImages(BufferedImage neg, BufferedImage pos){ |
2 |
26 Feb 07 |
jari |
120 |
this.negColorImage = neg; |
2 |
26 Feb 07 |
jari |
121 |
this.posColorImage = pos; |
2 |
26 Feb 07 |
jari |
122 |
} |
2 |
26 Feb 07 |
jari |
123 |
|
2 |
26 Feb 07 |
jari |
124 |
/** |
2 |
26 Feb 07 |
jari |
* Sets the isTracing attribute. |
2 |
26 Feb 07 |
jari |
126 |
*/ |
2 |
26 Feb 07 |
jari |
127 |
void setTracing(boolean isTracing) { |
2 |
26 Feb 07 |
jari |
128 |
this.isTracing = isTracing; |
2 |
26 Feb 07 |
jari |
129 |
} |
2 |
26 Feb 07 |
jari |
130 |
|
2 |
26 Feb 07 |
jari |
131 |
/** |
2 |
26 Feb 07 |
jari |
* Returns a trace space value. |
2 |
26 Feb 07 |
jari |
133 |
*/ |
2 |
26 Feb 07 |
jari |
134 |
private int getSpacing() { |
2 |
26 Feb 07 |
jari |
135 |
if (isTracing) { |
2 |
26 Feb 07 |
jari |
136 |
return tracespace; |
2 |
26 Feb 07 |
jari |
137 |
} |
2 |
26 Feb 07 |
jari |
138 |
return 0; |
2 |
26 Feb 07 |
jari |
139 |
} |
2 |
26 Feb 07 |
jari |
140 |
|
2 |
26 Feb 07 |
jari |
141 |
/** |
2 |
26 Feb 07 |
jari |
* Sets the component font size. |
2 |
26 Feb 07 |
jari |
143 |
*/ |
2 |
26 Feb 07 |
jari |
144 |
private void setFontSize(int width) { |
2 |
26 Feb 07 |
jari |
145 |
if (width > 12) { |
2 |
26 Feb 07 |
jari |
146 |
width = 12; |
2 |
26 Feb 07 |
jari |
147 |
} |
2 |
26 Feb 07 |
jari |
148 |
setFont(new Font("monospaced", Font.PLAIN, width)); |
2 |
26 Feb 07 |
jari |
149 |
} |
2 |
26 Feb 07 |
jari |
150 |
|
2 |
26 Feb 07 |
jari |
151 |
/** |
2 |
26 Feb 07 |
jari |
* Updates the header size. |
2 |
26 Feb 07 |
jari |
153 |
*/ |
2 |
26 Feb 07 |
jari |
154 |
void updateSize() { |
2 |
26 Feb 07 |
jari |
155 |
Graphics2D g = (Graphics2D)getGraphics(); |
2 |
26 Feb 07 |
jari |
156 |
if (g == null) { |
2 |
26 Feb 07 |
jari |
157 |
return; |
2 |
26 Feb 07 |
jari |
158 |
} |
2 |
26 Feb 07 |
jari |
159 |
if (isAntiAliasing) { |
2 |
26 Feb 07 |
jari |
160 |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF); |
2 |
26 Feb 07 |
jari |
161 |
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
2 |
26 Feb 07 |
jari |
162 |
} |
2 |
26 Feb 07 |
jari |
163 |
FontMetrics hfm = g.getFontMetrics(); |
2 |
26 Feb 07 |
jari |
164 |
int maxHeight = 0; |
2 |
26 Feb 07 |
jari |
165 |
String name; |
2 |
26 Feb 07 |
jari |
166 |
final int size = data.getFeaturesCount(); |
2 |
26 Feb 07 |
jari |
167 |
for (int feature = 0; feature < size; feature++) { |
2 |
26 Feb 07 |
jari |
168 |
name = data.getSampleName(feature); |
2 |
26 Feb 07 |
jari |
169 |
maxHeight = Math.max(maxHeight, hfm.stringWidth(name)); |
2 |
26 Feb 07 |
jari |
170 |
} |
2 |
26 Feb 07 |
jari |
171 |
|
2 |
26 Feb 07 |
jari |
// if(!this.isTracing) |
2 |
26 Feb 07 |
jari |
// contentWidth = this.data.getFeaturesCount() * this.elementWidth; |
2 |
26 Feb 07 |
jari |
// else |
2 |
26 Feb 07 |
jari |
// contentWidth = (this.data.getFeaturesCount() - 1) * (this.elementWidth + getSpacing()) + this.elementWidth ; |
2 |
26 Feb 07 |
jari |
176 |
|
2 |
26 Feb 07 |
jari |
177 |
setSize(contentWidth, maxHeight+10+this.RECT_HEIGHT+hfm.getHeight()); |
2 |
26 Feb 07 |
jari |
178 |
setPreferredSize(new Dimension(contentWidth, maxHeight+10+this.RECT_HEIGHT+hfm.getHeight())); |
2 |
26 Feb 07 |
jari |
179 |
} |
2 |
26 Feb 07 |
jari |
180 |
|
2 |
26 Feb 07 |
jari |
181 |
/** |
2 |
26 Feb 07 |
jari |
* Paints the header into specified graphics. |
2 |
26 Feb 07 |
jari |
183 |
*/ |
2 |
26 Feb 07 |
jari |
184 |
public void paint(Graphics g1D) { |
2 |
26 Feb 07 |
jari |
185 |
super.paint(g1D); |
2 |
26 Feb 07 |
jari |
186 |
if (this.data == null || this.data.getFeaturesCount() == 0) { |
2 |
26 Feb 07 |
jari |
187 |
return; |
2 |
26 Feb 07 |
jari |
188 |
} |
2 |
26 Feb 07 |
jari |
189 |
|
2 |
26 Feb 07 |
jari |
190 |
Graphics2D g = (Graphics2D)g1D; |
2 |
26 Feb 07 |
jari |
191 |
int width; |
2 |
26 Feb 07 |
jari |
192 |
if(!this.isTracing) |
2 |
26 Feb 07 |
jari |
193 |
width = this.data.getFeaturesCount() * this.elementWidth; |
2 |
26 Feb 07 |
jari |
194 |
else |
2 |
26 Feb 07 |
jari |
195 |
width = (this.data.getFeaturesCount() - 1) * (this.elementWidth + getSpacing()) + this.elementWidth ; |
2 |
26 Feb 07 |
jari |
196 |
if(useDoubleGradient) { |
2 |
26 Feb 07 |
jari |
197 |
g.drawImage(this.negColorImage, insets.left, 0, (int)(width/2f), RECT_HEIGHT, null); |
2 |
26 Feb 07 |
jari |
198 |
g.drawImage(this.posColorImage, (int)(width/2f)+insets.left, 0, (int)(width/2.0), RECT_HEIGHT, null); |
2 |
26 Feb 07 |
jari |
199 |
} else { |
2 |
26 Feb 07 |
jari |
200 |
g.drawImage(this.posColorImage, insets.left, 0, width, RECT_HEIGHT, null); |
2 |
26 Feb 07 |
jari |
201 |
} |
2 |
26 Feb 07 |
jari |
202 |
|
2 |
26 Feb 07 |
jari |
203 |
FontMetrics hfm = g.getFontMetrics(); |
2 |
26 Feb 07 |
jari |
204 |
int descent = hfm.getDescent(); |
2 |
26 Feb 07 |
jari |
205 |
int fHeight = hfm.getHeight(); |
2 |
26 Feb 07 |
jari |
206 |
|
2 |
26 Feb 07 |
jari |
207 |
g.setColor(Color.black); |
2 |
26 Feb 07 |
jari |
208 |
if (isAntiAliasing) { |
2 |
26 Feb 07 |
jari |
209 |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF); |
2 |
26 Feb 07 |
jari |
210 |
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
2 |
26 Feb 07 |
jari |
211 |
} |
2 |
26 Feb 07 |
jari |
212 |
int textWidth; |
2 |
26 Feb 07 |
jari |
213 |
g.drawString(String.valueOf(this.minValue), insets.left, RECT_HEIGHT+fHeight); |
2 |
26 Feb 07 |
jari |
214 |
textWidth = hfm.stringWidth(String.valueOf(midValue)); |
2 |
26 Feb 07 |
jari |
215 |
if(useDoubleGradient) |
2 |
26 Feb 07 |
jari |
216 |
g.drawString(String.valueOf(midValue), (int)(width/2f)-textWidth/2 + insets.left, RECT_HEIGHT+fHeight); |
2 |
26 Feb 07 |
jari |
217 |
textWidth = hfm.stringWidth(String.valueOf(this.maxValue)); |
2 |
26 Feb 07 |
jari |
218 |
g.drawString(String.valueOf(this.maxValue), (width-textWidth)+insets.left, RECT_HEIGHT+fHeight); |
2 |
26 Feb 07 |
jari |
219 |
|
2 |
26 Feb 07 |
jari |
220 |
drawColumnHeaders(g); |
2 |
26 Feb 07 |
jari |
221 |
} |
2 |
26 Feb 07 |
jari |
222 |
|
2 |
26 Feb 07 |
jari |
223 |
/** |
2 |
26 Feb 07 |
jari |
* Draws microarrays names. |
2 |
26 Feb 07 |
jari |
225 |
*/ |
2 |
26 Feb 07 |
jari |
226 |
private void drawColumnHeaders(Graphics2D g) { |
2 |
26 Feb 07 |
jari |
227 |
final int size = data.getFeaturesCount(); |
2 |
26 Feb 07 |
jari |
228 |
if (size == 0) { |
2 |
26 Feb 07 |
jari |
229 |
return; |
2 |
26 Feb 07 |
jari |
230 |
} |
2 |
26 Feb 07 |
jari |
231 |
FontMetrics hfm = g.getFontMetrics(); |
2 |
26 Feb 07 |
jari |
232 |
int descent = hfm.getDescent(); |
2 |
26 Feb 07 |
jari |
233 |
g.rotate(-Math.PI/2); |
2 |
26 Feb 07 |
jari |
234 |
String name; |
2 |
26 Feb 07 |
jari |
235 |
for (int feature = 0; feature < size; feature++) { |
2 |
26 Feb 07 |
jari |
236 |
name = data.getSampleName(feature); |
2 |
26 Feb 07 |
jari |
237 |
g.drawString(name, insets.bottom - getSize().height +5, insets.left + descent + (elementWidth+getSpacing())*feature + elementWidth/2); |
2 |
26 Feb 07 |
jari |
238 |
} |
2 |
26 Feb 07 |
jari |
239 |
g.rotate(Math.PI/2); |
2 |
26 Feb 07 |
jari |
240 |
} |
2 |
26 Feb 07 |
jari |
241 |
} |