KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > SynthProgressBarUI


1 /*
2  * @(#)SynthProgressBarUI.java 1.23 04/04/02
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.synth;
9
10 import java.awt.*;
11 import java.awt.geom.AffineTransform JavaDoc;
12 import java.awt.event.*;
13 import javax.swing.*;
14 import javax.swing.event.*;
15 import javax.swing.plaf.*;
16 import javax.swing.plaf.basic.BasicProgressBarUI JavaDoc;
17 import java.beans.PropertyChangeListener JavaDoc;
18 import java.beans.PropertyChangeEvent JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import sun.swing.plaf.synth.SynthUI;
21 import com.sun.java.swing.SwingUtilities2;
22
23 /**
24  * Synth's ProgressBarUI.
25  *
26  * @version 1.23, 04/02/04
27  * @author Joshua Outwater
28  */

29 class SynthProgressBarUI extends BasicProgressBarUI JavaDoc implements SynthUI,
30         PropertyChangeListener JavaDoc {
31     private SynthStyle JavaDoc style;
32
33     private int progressPadding;
34
35     public static ComponentUI createUI(JComponent x) {
36         return new SynthProgressBarUI JavaDoc();
37     }
38
39     protected void installListeners() {
40         super.installListeners();
41         progressBar.addPropertyChangeListener(this);
42     }
43
44     protected void uninstallListeners() {
45         super.uninstallListeners();
46         progressBar.removePropertyChangeListener(this);
47     }
48
49     protected void installDefaults() {
50         updateStyle(progressBar);
51     }
52
53     private void updateStyle(JProgressBar c) {
54         SynthContext JavaDoc context = getContext(c, ENABLED);
55         SynthStyle JavaDoc oldStyle = style;
56         style = SynthLookAndFeel.updateStyle(context, this);
57         if (style != oldStyle) {
58             setCellLength(style.getInt(context, "ProgressBar.cellLength", 1));
59             setCellSpacing(style.getInt(context, "ProgressBar.cellSpacing", 0));
60             progressPadding = style.getInt(context,
61                     "ProgressBar.progressPadding", 0);
62         }
63         context.dispose();
64     }
65     
66     protected void uninstallDefaults() {
67         SynthContext JavaDoc context = getContext(progressBar, ENABLED);
68
69         style.uninstallDefaults(context);
70         context.dispose();
71         style = null;
72     }
73     
74     public SynthContext JavaDoc getContext(JComponent c) {
75         return getContext(c, getComponentState(c));
76     }
77
78     private SynthContext JavaDoc getContext(JComponent c, int state) {
79         return SynthContext.getContext(SynthContext JavaDoc.class, c,
80                             SynthLookAndFeel.getRegion(c), style, state);
81     }
82
83     private Region JavaDoc getRegion(JComponent c) {
84         return SynthLookAndFeel.getRegion(c);
85     }
86
87     private int getComponentState(JComponent c) {
88         return SynthLookAndFeel.getComponentState(c);
89     }
90
91     public void update(Graphics g, JComponent c) {
92         SynthContext JavaDoc context = getContext(c);
93
94         SynthLookAndFeel.update(context, g);
95         context.getPainter().paintProgressBarBackground(context,
96                           g, 0, 0, c.getWidth(), c.getHeight());
97         paint(context, g);
98         context.dispose();
99     }
100
101     public void paint(Graphics g, JComponent c) {
102         SynthContext JavaDoc context = getContext(c);
103
104         paint(context, g);
105         context.dispose();
106     }
107
108     protected void paint(SynthContext JavaDoc context, Graphics g) {
109         JProgressBar pBar = (JProgressBar)context.getComponent();
110         int x = 0, y = 0, width = 0, height = 0;
111         if (!pBar.isIndeterminate()) {
112             Insets pBarInsets = pBar.getInsets();
113             double percentComplete = pBar.getPercentComplete();
114             if (percentComplete != 0.0) {
115                 if (pBar.getOrientation() == JProgressBar.HORIZONTAL) {
116                     x = pBarInsets.left + progressPadding;
117                     y = pBarInsets.top + progressPadding;
118                     width = (int)(percentComplete * (pBar.getWidth()
119                             - (pBarInsets.left + progressPadding
120                              + pBarInsets.right + progressPadding)));
121                     height = pBar.getHeight()
122                             - (pBarInsets.top + progressPadding
123                              + pBarInsets.bottom + progressPadding);
124
125                     if (!SynthLookAndFeel.isLeftToRight(pBar)) {
126                         x = pBar.getWidth() - pBarInsets.right - width
127                                 - progressPadding;
128                     }
129                 } else { // JProgressBar.VERTICAL
130
x = pBarInsets.left + progressPadding;
131                     width = pBar.getWidth()
132                             - (pBarInsets.left + progressPadding
133                             + pBarInsets.right + progressPadding);
134                     height = (int)(percentComplete * (pBar.getHeight()
135                             - (pBarInsets.top + progressPadding
136                              + pBarInsets.bottom + progressPadding)));
137                     y = pBar.getHeight() - pBarInsets.bottom - height
138                             - progressPadding;
139
140                     // When the progress bar is vertical we always paint
141
// from bottom to top, not matter what the component
142
// orientation is.
143
}
144             }
145         } else {
146             boxRect = getBox(boxRect);
147             x = boxRect.x + progressPadding;
148             y = boxRect.y + progressPadding;
149             width = boxRect.width - progressPadding - progressPadding;
150             height = boxRect.height - progressPadding - progressPadding;
151         }
152         context.getPainter().paintProgressBarForeground(context, g,
153                 x, y, width, height, pBar.getOrientation());
154
155         if (pBar.isStringPainted() && !pBar.isIndeterminate()) {
156             paintText(context, g, pBar.getString());
157         }
158     }
159
160     protected void paintText(SynthContext JavaDoc context, Graphics g,
161             String JavaDoc title) {
162         Font font = context.getStyle().getFont(context);
163         FontMetrics metrics = SwingUtilities2.getFontMetrics(progressBar, g,
164                                                              font);
165
166         if (progressBar.isStringPainted()) {
167             String JavaDoc pBarString = progressBar.getString();
168             Rectangle bounds = progressBar.getBounds();
169             int strLength = context.getStyle().getGraphicsUtils(context).
170                 computeStringWidth(context, font, metrics, pBarString);
171
172             // Calculate the bounds for the text.
173
Rectangle textRect = new Rectangle(
174                 (bounds.width / 2) - (strLength / 2),
175                 (bounds.height -
176                     (metrics.getAscent() + metrics.getDescent())) / 2,
177                 0, 0);
178
179             // Progress bar isn't tall enough for the font. Don't paint it.
180
if (textRect.y < 0) {
181                 return;
182             }
183
184             // Paint the text.
185
SynthStyle JavaDoc style = context.getStyle();
186             g.setColor(style.getColor(context, ColorType.TEXT_FOREGROUND));
187             g.setFont(style.getFont(context));
188             style.getGraphicsUtils(context).paintText(context, g, title,
189                                                  textRect.x, textRect.y, -1);
190         }
191     }
192
193     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
194                             int y, int w, int h) {
195         context.getPainter().paintProgressBarBorder(context, g, x, y, w, h);
196     }
197
198     public void propertyChange(PropertyChangeEvent JavaDoc e) {
199         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
200             updateStyle((JProgressBar)e.getSource());
201         }
202     }
203     
204     public Dimension getPreferredSize(JComponent c) {
205         Dimension size;
206         Insets border = progressBar.getInsets();
207         FontMetrics fontSizer = progressBar.getFontMetrics(
208                                                   progressBar.getFont());
209         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
210             size = new Dimension(getPreferredInnerHorizontal());
211         } else {
212             size = new Dimension(getPreferredInnerVertical());
213         }
214         // Ensure that the progress string will fit.
215
if (progressBar.isStringPainted()) {
216             String JavaDoc progString = progressBar.getString();
217             int stringHeight = fontSizer.getHeight() +
218                     fontSizer.getDescent();
219             if (stringHeight > size.height) {
220                 size.height = stringHeight;
221             }
222             // This is also for completeness.
223
int stringWidth = SwingUtilities2.stringWidth(
224                                    progressBar, fontSizer, progString);
225             if (stringWidth > size.width) {
226                 size.width = stringWidth;
227             }
228         }
229
230         size.width += border.left + border.right;
231         size.height += border.top + border.bottom;
232         
233         return size;
234    }
235 }
236
Popular Tags