001: package com.opensymphony.workflow.designer.swing;
002:
003: import java.awt.*;
004:
005: public final class Splash extends Window {
006:
007: private static final int DEFAULT_BAR_WIDTH = 100;
008: private static final int DEFAULT_BAR_HEIGHT = 10;
009: private static final int VPAD = 10;
010:
011: private final Image image;
012: private final boolean showProgress;
013: private final String text;
014:
015: private Color textColor;
016: private Rectangle progressBarBounds;
017: private int percent;
018:
019: public Splash(Window owner, Image image, String text,
020: boolean showProgress) {
021: super (owner);
022: this .image = image;
023: this .text = text;
024: this .percent = 0;
025: this .showProgress = showProgress;
026: setSize(image.getWidth(null), image.getHeight(null));
027: setProgressBarBounds(VPAD);
028: setForeground(Color.darkGray);
029: setBackground(Color.lightGray.brighter());
030: textColor = Color.black;
031: ScreenUtils.center(this );
032: }
033:
034: public void setProgressBarBounds(Rectangle r) {
035: progressBarBounds = new Rectangle(r);
036: }
037:
038: public void setProgressBarBounds(int bottomPad) {
039: setProgressBarBounds(defaultProgressBarBounds(bottomPad));
040: }
041:
042: private Rectangle defaultProgressBarBounds(int bottomPad) {
043: int x = (getWidth() - DEFAULT_BAR_WIDTH) / 2;
044: int y = getHeight() - DEFAULT_BAR_HEIGHT - bottomPad;
045: return new Rectangle(x, y, DEFAULT_BAR_WIDTH,
046: DEFAULT_BAR_HEIGHT);
047: }
048:
049: public void paint(Graphics g) {
050: boolean clipIsProgressRect = progressBarBounds.equals(g
051: .getClipBounds());
052:
053: if (image != null && (!showProgress || !clipIsProgressRect)) {
054: g.drawImage(image, 0, 0, this );
055: }
056: if (showProgress) {
057: int x = progressBarBounds.x;
058: int y = progressBarBounds.y;
059: int w = progressBarBounds.width;
060: int h = progressBarBounds.height;
061: int progressWidth = (w - 2) * percent / 100;
062: int progressHeight = h - 2;
063:
064: g.translate(x, y);
065: // Paint border
066: g.setColor(Color.gray);
067: g.drawLine(0, 0, w - 2, 0);
068: g.drawLine(0, 0, 0, h - 1);
069: g.setColor(Color.white);
070: g.drawLine(0, h - 1, w - 1, h - 1);
071: g.drawLine(w - 1, 0, w - 1, h - 1);
072: // Paint background
073: g.setColor(getBackground());
074: g.fillRect(1, 1, w - 2, progressHeight);
075: // Paint progress bar
076: g.setColor(getForeground());
077: g.fillRect(1, 1, progressWidth, progressHeight);
078: g.translate(-x, -y);
079:
080: if (!clipIsProgressRect) {
081: FontMetrics fm = getFontMetrics(g.getFont());
082: int textWidth = fm.stringWidth(text);
083: int textX = (getWidth() - textWidth) / 2;
084: g.setColor(textColor);
085: g.drawString(text, textX, progressBarBounds.y - VPAD
086: / 2);
087: }
088: }
089: }
090:
091: public void openSplash() {
092: setVisible(true);
093: }
094:
095: public void closeSplash() {
096: dispose();
097: }
098:
099: public void setProgress(int percent) {
100: if (!showProgress)
101: return;
102: this.percent = percent;
103: repaint(progressBarBounds.x, progressBarBounds.y,
104: progressBarBounds.width, progressBarBounds.height);
105: }
106: }
|