KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > swing > Splash


1 package com.opensymphony.workflow.designer.swing;
2
3 import java.awt.*;
4
5 public final class Splash extends Window
6 {
7
8   private static final int DEFAULT_BAR_WIDTH = 100;
9   private static final int DEFAULT_BAR_HEIGHT = 10;
10   private static final int VPAD = 10;
11
12   private final Image image;
13   private final boolean showProgress;
14   private final String JavaDoc text;
15
16   private Color textColor;
17   private Rectangle progressBarBounds;
18   private int percent;
19
20   public Splash(Window owner, Image image, String JavaDoc text, boolean showProgress)
21   {
22     super(owner);
23     this.image = image;
24     this.text = text;
25     this.percent = 0;
26     this.showProgress = showProgress;
27     setSize(image.getWidth(null), image.getHeight(null));
28     setProgressBarBounds(VPAD);
29     setForeground(Color.darkGray);
30     setBackground(Color.lightGray.brighter());
31     textColor = Color.black;
32     ScreenUtils.center(this);
33   }
34
35   public void setProgressBarBounds(Rectangle r)
36   {
37     progressBarBounds = new Rectangle(r);
38   }
39
40   public void setProgressBarBounds(int bottomPad)
41   {
42     setProgressBarBounds(defaultProgressBarBounds(bottomPad));
43   }
44
45   private Rectangle defaultProgressBarBounds(int bottomPad)
46   {
47     int x = (getWidth() - DEFAULT_BAR_WIDTH) / 2;
48     int y = getHeight() - DEFAULT_BAR_HEIGHT - bottomPad;
49     return new Rectangle(x, y, DEFAULT_BAR_WIDTH, DEFAULT_BAR_HEIGHT);
50   }
51
52   public void paint(Graphics g)
53   {
54     boolean clipIsProgressRect = progressBarBounds.equals(g.getClipBounds());
55
56     if(image != null && (!showProgress || !clipIsProgressRect))
57     {
58       g.drawImage(image, 0, 0, this);
59     }
60     if(showProgress)
61     {
62       int x = progressBarBounds.x;
63       int y = progressBarBounds.y;
64       int w = progressBarBounds.width;
65       int h = progressBarBounds.height;
66       int progressWidth = (w - 2) * percent / 100;
67       int progressHeight = h - 2;
68
69       g.translate(x, y);
70       // Paint border
71
g.setColor(Color.gray);
72       g.drawLine(0, 0, w - 2, 0);
73       g.drawLine(0, 0, 0, h - 1);
74       g.setColor(Color.white);
75       g.drawLine(0, h - 1, w - 1, h - 1);
76       g.drawLine(w - 1, 0, w - 1, h - 1);
77       // Paint background
78
g.setColor(getBackground());
79       g.fillRect(1, 1, w - 2, progressHeight);
80       // Paint progress bar
81
g.setColor(getForeground());
82       g.fillRect(1, 1, progressWidth, progressHeight);
83       g.translate(-x, -y);
84
85       if(!clipIsProgressRect)
86       {
87         FontMetrics fm = getFontMetrics(g.getFont());
88         int textWidth = fm.stringWidth(text);
89         int textX = (getWidth() - textWidth) / 2;
90         g.setColor(textColor);
91         g.drawString(text, textX, progressBarBounds.y - VPAD / 2);
92       }
93     }
94   }
95
96   public void openSplash()
97   {
98     setVisible(true);
99   }
100
101   public void closeSplash()
102   {
103     dispose();
104   }
105
106   public void setProgress(int percent)
107   {
108     if(!showProgress)
109       return;
110     this.percent = percent;
111     repaint(progressBarBounds.x, progressBarBounds.y, progressBarBounds.width, progressBarBounds.height);
112   }
113 }
Popular Tags