KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > gui > ProgressBar


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.upgrade.gui;
25
26 import java.awt.*;
27 import java.util.Stack JavaDoc;
28
29 public class ProgressBar extends Component
30 {
31
32     protected Dimension size;
33     protected int progress;
34     protected int depth;
35
36     public ProgressBar()
37     {
38         size = new Dimension(250, 16);
39         progress = 0;
40         depth = 1;
41     }
42
43     protected void drawProgress(Graphics g, int i, int j)
44     {
45         if(g == null)
46         {
47             return;
48         }
49         int k = size.width - 2 * depth;
50         int l = (int)(((double)k / 100D) * (double)i);
51         int i1 = (int)(((double)k / 100D) * (double)j);
52         if(g != null)
53         {
54             if(i1 < l)
55             {
56                 g.setColor(Color.white);
57                 g.fillRect(depth + i1, depth, l - i1, size.height - 2 * depth);
58             } else
59             {
60                 g.setColor(Color.blue);
61                 g.fillRect(depth + l, depth, i1 - l, size.height - 2 * depth);
62             }
63         }
64     }
65
66     protected void drawProgressBorder(Graphics g)
67     {
68         size = getSize();
69         if(size.width == 0 || size.height == 0)
70         {
71             Stack JavaDoc stack = new Stack JavaDoc();
72             Container container = getParent();
73             Dimension dimension = null;
74             if(container != null)
75             {
76                 dimension = container.getSize();
77             }
78             while(container != null && (dimension.width == 0 || dimension.height == 0))
79             {
80                 stack.push(container);
81                 container = container.getParent();
82                 if(container != null)
83                 {
84                     dimension = container.getSize();
85                 }
86             }
87             if(container == null)
88             {
89                 container = (Container)stack.pop();
90             }
91             if(container != null)
92             {
93                 container.validate();
94             }
95             return;
96         }
97         Color color = getBackground();
98         Color color1 = Color.white;
99         Color color2 = Color.black;
100         int i = 150 / depth;
101         for(int j = 0; j < depth; j++)
102         {
103             g.setColor(color1);
104             g.drawLine(j, size.height - (j + 1), j, j);
105             g.drawLine(j, j, size.width - (j + 1), j);
106             g.setColor(color2);
107             g.drawLine(j + 1, size.height - (j + 1), size.width - (j + 1), size.height - (j + 1));
108             g.drawLine(size.width - (j + 1), size.height - (j + 1), size.width - (j + 1), j + 1);
109             color1 = new Color(color1.getRed() - i, color1.getGreen() - i, color1.getBlue() - i);
110             color2 = new Color(color2.getRed() + i, color2.getGreen() + i, color2.getBlue() + i);
111         }
112
113         g.setColor(Color.white);
114         g.fillRect(depth, depth, size.width - 2 * depth, size.height - 2 * depth);
115     }
116
117     public int getDepth()
118     {
119         return depth;
120     }
121
122     public Dimension getMinimumSize()
123     {
124         return getPreferredSize();
125     }
126
127     public Dimension getPreferredSize()
128     {
129         Container container = getParent();
130         if(container == null)
131         {
132             return size;
133         }
134         Dimension dimension = container.getSize();
135         if(dimension.width == 0 || dimension.height == 0)
136         {
137             return size;
138         } else
139         {
140             Insets insets = container.getInsets();
141             return new Dimension(dimension.width - insets.left - insets.right, size.height);
142         }
143     }
144
145     public int getProgress()
146     {
147         return progress;
148     }
149
150     public void paint(Graphics g)
151     {
152         Dimension dimension = getSize();
153         g.setClip(0, 0, dimension.width, dimension.height);
154         drawProgressBorder(g);
155         drawProgress(g, 0, progress);
156     }
157
158     public void setDepth(int i)
159     {
160         depth = i;
161     }
162
163     public void setProgress(int i)
164     {
165         Graphics g = getGraphics();
166         if(g != null)
167         {
168             Dimension dimension = getSize();
169             g.setClip(0, 0, dimension.width, dimension.height);
170         }
171         drawProgress(g, progress, i);
172         progress = i;
173     }
174
175     public void setSize(Dimension dimension)
176     {
177         super.setSize(dimension);
178         size = dimension;
179     }
180 }
181
Popular Tags