Created
August 16, 2009 17:58
-
-
Save jwill/168690 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Groovy source file | |
* Created 27 August 2007 | |
* @author jwilliams | |
*/ | |
package com.fintech.fos.client.ui | |
import java.awt.AlphaComposite | |
import java.awt.Color | |
import java.awt.Composite | |
import java.awt.Font | |
import java.awt.FontMetrics | |
import java.awt.GradientPaint | |
import java.awt.Graphics | |
import java.awt.Graphics2D | |
import java.awt.LinearGradientPaint | |
import java.awt.Paint | |
import java.awt.Rectangle | |
import java.awt.RenderingHints | |
import java.awt.event.ComponentAdapter | |
import java.awt.event.ComponentEvent | |
import java.awt.event.KeyListener | |
import java.awt.event.MouseListener | |
import java.awt.event.MouseMotionListener | |
import javax.swing.JComponent | |
//Code adapted from Filthy Rich Clients sample code | |
public class ProgressGlassPane extends JComponent{ | |
def tasksToComplete = 0, tasksCompleted = 0 | |
private static final int BAR_WIDTH = 200; | |
private static final int BAR_HEIGHT = 10; | |
private static final Color TEXT_COLOR = new Color(0x333333); | |
private static final Color BORDER_COLOR = new Color(0x333333); | |
private static final float[] GRADIENT_FRACTIONS = [0.0f, 0.499f, 0.5f, 1.0f] | |
private static final Color[] GRADIENT_COLORS = [Color.GRAY, Color.DARK_GRAY, Color.BLACK, Color.GRAY] | |
private static final Color GRADIENT_COLOR2 = Color.WHITE; | |
private static final Color GRADIENT_COLOR1 = Color.GRAY; | |
private String message = "Connecting to schedulers/database..."; | |
private int progress = 0; | |
// Adapters to capture input while the glass pane is active | |
def keyAdapter = [ | |
keyPressed: {evt -> }, | |
keyReleased: {evt -> }, | |
keyTyped: {evt -> } | |
] as KeyListener | |
def mouseAdapter = [ | |
mouseClicked: {evt -> }, | |
mouseEntered: {evt -> }, | |
mouseExited: {evt -> }, | |
mousePressed: {evt -> }, | |
mouseReleased: {evt -> }, | |
] as MouseListener | |
def mouseMotionAdapter = [ | |
mouseDragged: {evt -> }, | |
mouseMoved: {evt -> } | |
] as MouseMotionListener | |
public ProgressGlassPane() { | |
addMouseListener(mouseAdapter) | |
addMouseMotionListener(mouseMotionAdapter) | |
addKeyListener(keyAdapter) | |
setFocusTraversalKeysEnabled(false) | |
} | |
public ProgressGlassPane(tasks) { | |
tasksToComplete = tasks | |
this() | |
} | |
public void increment() { | |
tasksCompleted ++ | |
println "progress: "+ (int)(tasksCompleted/tasksToComplete*100) | |
setProgress((tasksCompleted/tasksToComplete * 100).intValue()) | |
} | |
public void setProgress(progress) { | |
def oldProgress = this.progress | |
this.progress = progress | |
// compute the damaged area | |
def metrics = getGraphics().getFontMetrics(getFont()) | |
int w = (int) (BAR_WIDTH * ((float) oldProgress / 100.0f)) | |
int x = w + (getHeight() - BAR_WIDTH) / 2 | |
int y = (getHeight() - BAR_HEIGHT) / 2 | |
y += metrics.getDescent() / 2 | |
w = (int) (BAR_WIDTH * ((float) progress / 100.0f)) | |
int h = BAR_HEIGHT | |
repaint((int)x, y.intValue(), (int)w, (int)h) | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
//println "painting component" | |
// enables anti-aliasing | |
Graphics2D g2 = (Graphics2D) g; | |
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | |
RenderingHints.VALUE_ANTIALIAS_ON); | |
// gets the current clipping area | |
Rectangle clip = g.getClipBounds(); | |
// sets a 65% translucent composite | |
AlphaComposite alpha = AlphaComposite.SrcOver.derive(0.65f); | |
Composite composite = g2.getComposite(); | |
g2.setComposite(alpha); | |
// fills the background | |
g2.setColor(getBackground()); | |
g2.fillRect((int)clip.x, (int)clip.y, (int)clip.width, (int)clip.height); | |
// centers the progress bar on screen | |
FontMetrics metrics = g.getFontMetrics(); | |
int x = (getWidth() - BAR_WIDTH) / 2; | |
int y = (getHeight() - BAR_HEIGHT - metrics.getDescent()) / 2; | |
// draws the text | |
g2.setColor(TEXT_COLOR); | |
g2.drawString(message, x, y); | |
// goes to the position of the progress bar | |
y += metrics.getDescent(); | |
// computes the size of the progress indicator | |
int w = (int) (BAR_WIDTH * ((float) progress / 100.0f)); | |
int h = BAR_HEIGHT; | |
// draws the content of the progress bar | |
Paint paint = g2.getPaint(); | |
// bar's background | |
Paint gradient = new GradientPaint(x, y, GRADIENT_COLOR1, | |
x, y + h, GRADIENT_COLOR2); | |
g2.setPaint(gradient); | |
g2.fillRect(x, y, BAR_WIDTH, BAR_HEIGHT); | |
// actual progress | |
gradient = new LinearGradientPaint(x, y, x, y + h, | |
GRADIENT_FRACTIONS, GRADIENT_COLORS); | |
g2.setPaint(gradient); | |
g2.fillRect(x, y, w, h); | |
g2.setPaint(paint); | |
// draws the progress bar border | |
g2.drawRect(x, y, BAR_WIDTH, BAR_HEIGHT); | |
g2.setComposite(composite); | |
} | |
} | |
class GlassPaneComponentAdapter extends ComponentAdapter { | |
public void componentShown(ComponentEvent evt) { | |
requestFocusInWindow() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment