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: ProgressBar.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.2 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2006/02/23 21:00:04 $ |
2 |
26 Feb 07 |
jari |
* $Author: caliente $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.util.awt; |
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.Container; |
2 |
26 Feb 07 |
jari |
16 |
import java.awt.Dimension; |
2 |
26 Feb 07 |
jari |
17 |
import java.awt.Font; |
2 |
26 Feb 07 |
jari |
18 |
import java.awt.GradientPaint; |
2 |
26 Feb 07 |
jari |
19 |
import java.awt.Graphics; |
2 |
26 Feb 07 |
jari |
20 |
import java.awt.Graphics2D; |
2 |
26 Feb 07 |
jari |
21 |
import java.awt.GridBagLayout; |
2 |
26 Feb 07 |
jari |
22 |
import java.awt.RenderingHints; |
2 |
26 Feb 07 |
jari |
23 |
import java.awt.Toolkit; |
2 |
26 Feb 07 |
jari |
24 |
import java.awt.event.WindowAdapter; |
2 |
26 Feb 07 |
jari |
25 |
import java.awt.event.WindowEvent; |
2 |
26 Feb 07 |
jari |
26 |
|
2 |
26 Feb 07 |
jari |
27 |
import javax.swing.JDialog; |
2 |
26 Feb 07 |
jari |
28 |
import javax.swing.JFrame; |
2 |
26 Feb 07 |
jari |
29 |
|
2 |
26 Feb 07 |
jari |
30 |
public class ProgressBar extends JDialog { |
2 |
26 Feb 07 |
jari |
31 |
private JFrame parent; |
2 |
26 Feb 07 |
jari |
32 |
private String title; |
2 |
26 Feb 07 |
jari |
33 |
private Color barColor1, barColor2, textColor; |
2 |
26 Feb 07 |
jari |
34 |
private int units = 0, value = 0; |
2 |
26 Feb 07 |
jari |
35 |
private Font progressBarFont; |
2 |
26 Feb 07 |
jari |
36 |
private ProgressBarCanvas canvas; |
2 |
26 Feb 07 |
jari |
37 |
private Container contentPane; |
2 |
26 Feb 07 |
jari |
38 |
private GBA gba; |
2 |
26 Feb 07 |
jari |
39 |
|
2 |
26 Feb 07 |
jari |
40 |
public ProgressBar(JFrame parent, String title, Color barColor1, Color barColor2, Color textColor, int units) { |
2 |
26 Feb 07 |
jari |
41 |
super(parent, title, false); |
2 |
26 Feb 07 |
jari |
42 |
|
2 |
26 Feb 07 |
jari |
43 |
this.parent = parent; |
2 |
26 Feb 07 |
jari |
44 |
this.title = title; |
2 |
26 Feb 07 |
jari |
45 |
this.barColor1 = barColor1; |
2 |
26 Feb 07 |
jari |
46 |
this.barColor2 = barColor2; |
2 |
26 Feb 07 |
jari |
47 |
this.textColor = textColor; |
2 |
26 Feb 07 |
jari |
48 |
this.units = units; |
2 |
26 Feb 07 |
jari |
49 |
progressBarFont = new Font("monospaced", Font.BOLD, 20); |
2 |
26 Feb 07 |
jari |
50 |
contentPane = getContentPane(); |
2 |
26 Feb 07 |
jari |
51 |
gba = new GBA(); |
2 |
26 Feb 07 |
jari |
52 |
|
2 |
26 Feb 07 |
jari |
53 |
addWindowListener(new WindowAdapter() { |
2 |
26 Feb 07 |
jari |
54 |
public void windowClosing(WindowEvent e) { |
2 |
26 Feb 07 |
jari |
55 |
hide(); |
2 |
26 Feb 07 |
jari |
56 |
} |
2 |
26 Feb 07 |
jari |
57 |
}); |
2 |
26 Feb 07 |
jari |
58 |
|
2 |
26 Feb 07 |
jari |
59 |
canvas = new ProgressBarCanvas(200, 50, 5, 5, 5, 5); |
2 |
26 Feb 07 |
jari |
60 |
contentPane.setLayout(new GridBagLayout()); |
2 |
26 Feb 07 |
jari |
61 |
gba.add(contentPane, canvas, 0, 0, 1, 1, 1, 1, GBA.B, GBA.C); |
2 |
26 Feb 07 |
jari |
62 |
|
2 |
26 Feb 07 |
jari |
63 |
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
2 |
26 Feb 07 |
jari |
64 |
Dimension barSize = getSize(); |
2 |
26 Feb 07 |
jari |
65 |
setLocation((screenSize.width - barSize.width) / 2, (screenSize.height - barSize.height) / 2); |
2 |
26 Feb 07 |
jari |
66 |
setResizable(true); |
2 |
26 Feb 07 |
jari |
67 |
pack(); |
2 |
26 Feb 07 |
jari |
68 |
show(); |
2 |
26 Feb 07 |
jari |
69 |
requestFocus(); |
2 |
26 Feb 07 |
jari |
70 |
} |
2 |
26 Feb 07 |
jari |
71 |
|
2 |
26 Feb 07 |
jari |
72 |
public ProgressBar(JFrame parent, String title, int units) { |
2 |
26 Feb 07 |
jari |
73 |
this(parent, title, Color.green, Color.green, Color.black, units); |
2 |
26 Feb 07 |
jari |
74 |
} |
2 |
26 Feb 07 |
jari |
75 |
|
2 |
26 Feb 07 |
jari |
76 |
public void drawProgressBar() { |
2 |
26 Feb 07 |
jari |
77 |
if (value < units) setVisible(true); |
2 |
26 Feb 07 |
jari |
78 |
} |
2 |
26 Feb 07 |
jari |
79 |
|
2 |
26 Feb 07 |
jari |
80 |
public void increment(int increment) { |
2 |
26 Feb 07 |
jari |
81 |
if (value < units) { |
2 |
26 Feb 07 |
jari |
82 |
value = value + increment; |
2 |
26 Feb 07 |
jari |
83 |
|
2 |
26 Feb 07 |
jari |
84 |
if ((value * 100 / units) != ((value - increment) * 100 / units)) { |
2 |
26 Feb 07 |
jari |
85 |
canvas.paint(canvas.getGraphics()); |
2 |
26 Feb 07 |
jari |
86 |
} |
2 |
26 Feb 07 |
jari |
87 |
} |
2 |
26 Feb 07 |
jari |
88 |
if (value >= units) { |
2 |
26 Feb 07 |
jari |
89 |
complete(); |
2 |
26 Feb 07 |
jari |
90 |
} |
2 |
26 Feb 07 |
jari |
91 |
} |
2 |
26 Feb 07 |
jari |
92 |
|
2 |
26 Feb 07 |
jari |
93 |
public void complete() { |
2 |
26 Feb 07 |
jari |
94 |
hide(); |
2 |
26 Feb 07 |
jari |
95 |
dispose(); |
2 |
26 Feb 07 |
jari |
96 |
} |
2 |
26 Feb 07 |
jari |
97 |
|
2 |
26 Feb 07 |
jari |
98 |
private class ProgressBarCanvas extends Drawable { |
2 |
26 Feb 07 |
jari |
99 |
private int width, height; |
2 |
26 Feb 07 |
jari |
100 |
private int preXSpacing, postXSpacing, preYSpacing, postYSpacing; |
2 |
26 Feb 07 |
jari |
101 |
|
2 |
26 Feb 07 |
jari |
102 |
public ProgressBarCanvas(int width, int height, int preXSpacing, int postXSpacing, int preYSpacing, int postYSpacing) { |
2 |
26 Feb 07 |
jari |
103 |
super(0, preXSpacing + width + postXSpacing, 0, preYSpacing + height + postYSpacing); |
2 |
26 Feb 07 |
jari |
104 |
|
2 |
26 Feb 07 |
jari |
105 |
this.width = width; |
2 |
26 Feb 07 |
jari |
106 |
this.height = height; |
2 |
26 Feb 07 |
jari |
107 |
this.preXSpacing = preXSpacing; |
2 |
26 Feb 07 |
jari |
108 |
this.postXSpacing = postXSpacing; |
2 |
26 Feb 07 |
jari |
109 |
this.preYSpacing = preYSpacing; |
2 |
26 Feb 07 |
jari |
110 |
this.postYSpacing = postYSpacing; |
2 |
26 Feb 07 |
jari |
111 |
|
2 |
26 Feb 07 |
jari |
112 |
setPreferredSize(new Dimension(preXSpacing + width + postXSpacing, preYSpacing + height + postYSpacing)); |
2 |
26 Feb 07 |
jari |
113 |
setBackground(Color.white); |
2 |
26 Feb 07 |
jari |
114 |
} |
2 |
26 Feb 07 |
jari |
115 |
|
2 |
26 Feb 07 |
jari |
116 |
public void controlPaint(Graphics g1D) { |
2 |
26 Feb 07 |
jari |
117 |
Graphics2D g = (Graphics2D) g1D; |
2 |
26 Feb 07 |
jari |
118 |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); |
2 |
26 Feb 07 |
jari |
119 |
g.setPaint(new GradientPaint(0, 0, barColor1, width, 0, barColor2)); |
2 |
26 Feb 07 |
jari |
120 |
g.fillRect(preXSpacing, preYSpacing, (value * width / units), height); |
2 |
26 Feb 07 |
jari |
121 |
drawRect(g, preXSpacing, preYSpacing, width, height, textColor); |
2 |
26 Feb 07 |
jari |
122 |
drawString(g, "" + (value * 100 / units) + "%", 90, 36, textColor, progressBarFont); |
2 |
26 Feb 07 |
jari |
123 |
} |
2 |
26 Feb 07 |
jari |
124 |
} |
2 |
26 Feb 07 |
jari |
125 |
} |