001 /*
002 * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.plaf.synth;
027
028 import java.awt.*;
029 import java.awt.geom.AffineTransform;
030 import java.awt.event.*;
031 import javax.swing.*;
032 import javax.swing.event.*;
033 import javax.swing.plaf.*;
034 import javax.swing.plaf.basic.BasicProgressBarUI;
035 import java.beans.PropertyChangeListener;
036 import java.beans.PropertyChangeEvent;
037 import java.io.Serializable;
038 import sun.swing.plaf.synth.SynthUI;
039 import sun.swing.SwingUtilities2;
040
041 /**
042 * Synth's ProgressBarUI.
043 *
044 * @version 1.34, 05/05/07
045 * @author Joshua Outwater
046 */
047 class SynthProgressBarUI extends BasicProgressBarUI implements SynthUI,
048 PropertyChangeListener {
049 private SynthStyle style;
050
051 private int progressPadding;
052 private boolean paintOutsideClip;
053
054 public static ComponentUI createUI(JComponent x) {
055 return new SynthProgressBarUI();
056 }
057
058 protected void installListeners() {
059 super .installListeners();
060 progressBar.addPropertyChangeListener(this );
061 }
062
063 protected void uninstallListeners() {
064 super .uninstallListeners();
065 progressBar.removePropertyChangeListener(this );
066 }
067
068 protected void installDefaults() {
069 updateStyle(progressBar);
070 }
071
072 private void updateStyle(JProgressBar c) {
073 SynthContext context = getContext(c, ENABLED);
074 SynthStyle oldStyle = style;
075 style = SynthLookAndFeel.updateStyle(context, this );
076 if (style != oldStyle) {
077 setCellLength(style.getInt(context,
078 "ProgressBar.cellLength", 1));
079 setCellSpacing(style.getInt(context,
080 "ProgressBar.cellSpacing", 0));
081 progressPadding = style.getInt(context,
082 "ProgressBar.progressPadding", 0);
083 paintOutsideClip = style.getBoolean(context,
084 "ProgressBar.paintOutsideClip", false);
085 }
086 context.dispose();
087 }
088
089 protected void uninstallDefaults() {
090 SynthContext context = getContext(progressBar, ENABLED);
091
092 style.uninstallDefaults(context);
093 context.dispose();
094 style = null;
095 }
096
097 public SynthContext getContext(JComponent c) {
098 return getContext(c, getComponentState(c));
099 }
100
101 private SynthContext getContext(JComponent c, int state) {
102 return SynthContext.getContext(SynthContext.class, c,
103 SynthLookAndFeel.getRegion(c), style, state);
104 }
105
106 private Region getRegion(JComponent c) {
107 return SynthLookAndFeel.getRegion(c);
108 }
109
110 private int getComponentState(JComponent c) {
111 return SynthLookAndFeel.getComponentState(c);
112 }
113
114 public int getBaseline(JComponent c, int width, int height) {
115 super .getBaseline(c, width, height);
116 if (progressBar.isStringPainted()
117 && progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
118 SynthContext context = getContext(c);
119 Font font = context.getStyle().getFont(context);
120 FontMetrics metrics = progressBar.getFontMetrics(font);
121 context.dispose();
122 return (height - metrics.getAscent() - metrics.getDescent())
123 / 2 + metrics.getAscent();
124 }
125 return -1;
126 }
127
128 protected void setAnimationIndex(int newValue) {
129 if (paintOutsideClip) {
130 if (getAnimationIndex() == newValue) {
131 return;
132 }
133 super .setAnimationIndex(newValue);
134 progressBar.repaint();
135 } else {
136 super .setAnimationIndex(newValue);
137 }
138 }
139
140 public void update(Graphics g, JComponent c) {
141 SynthContext context = getContext(c);
142
143 SynthLookAndFeel.update(context, g);
144 context.getPainter().paintProgressBarBackground(context, g, 0,
145 0, c.getWidth(), c.getHeight(),
146 progressBar.getOrientation());
147 paint(context, g);
148 context.dispose();
149 }
150
151 public void paint(Graphics g, JComponent c) {
152 SynthContext context = getContext(c);
153
154 paint(context, g);
155 context.dispose();
156 }
157
158 protected void paint(SynthContext context, Graphics g) {
159 JProgressBar pBar = (JProgressBar) context.getComponent();
160 int x = 0, y = 0, width = 0, height = 0;
161 if (!pBar.isIndeterminate()) {
162 Insets pBarInsets = pBar.getInsets();
163 double percentComplete = pBar.getPercentComplete();
164 if (percentComplete != 0.0) {
165 if (pBar.getOrientation() == JProgressBar.HORIZONTAL) {
166 x = pBarInsets.left + progressPadding;
167 y = pBarInsets.top + progressPadding;
168 width = (int) (percentComplete * (pBar.getWidth() - (pBarInsets.left
169 + progressPadding + pBarInsets.right + progressPadding)));
170 height = pBar.getHeight()
171 - (pBarInsets.top + progressPadding
172 + pBarInsets.bottom + progressPadding);
173
174 if (!SynthLookAndFeel.isLeftToRight(pBar)) {
175 x = pBar.getWidth() - pBarInsets.right - width
176 - progressPadding;
177 }
178 } else { // JProgressBar.VERTICAL
179 x = pBarInsets.left + progressPadding;
180 width = pBar.getWidth()
181 - (pBarInsets.left + progressPadding
182 + pBarInsets.right + progressPadding);
183 height = (int) (percentComplete * (pBar.getHeight() - (pBarInsets.top
184 + progressPadding + pBarInsets.bottom + progressPadding)));
185 y = pBar.getHeight() - pBarInsets.bottom - height
186 - progressPadding;
187
188 // When the progress bar is vertical we always paint
189 // from bottom to top, not matter what the component
190 // orientation is.
191 }
192 }
193 } else {
194 boxRect = getBox(boxRect);
195 x = boxRect.x + progressPadding;
196 y = boxRect.y + progressPadding;
197 width = boxRect.width - progressPadding - progressPadding;
198 height = boxRect.height - progressPadding - progressPadding;
199 }
200 context.getPainter().paintProgressBarForeground(context, g, x,
201 y, width, height, pBar.getOrientation());
202
203 if (pBar.isStringPainted() && !pBar.isIndeterminate()) {
204 paintText(context, g, pBar.getString());
205 }
206 }
207
208 protected void paintText(SynthContext context, Graphics g,
209 String title) {
210 Font font = context.getStyle().getFont(context);
211 FontMetrics metrics = SwingUtilities2.getFontMetrics(
212 progressBar, g, font);
213
214 if (progressBar.isStringPainted()) {
215 String pBarString = progressBar.getString();
216 Rectangle bounds = progressBar.getBounds();
217 int strLength = context.getStyle()
218 .getGraphicsUtils(context).computeStringWidth(
219 context, font, metrics, pBarString);
220
221 // Calculate the bounds for the text.
222 Rectangle textRect = new Rectangle((bounds.width / 2)
223 - (strLength / 2), (bounds.height - (metrics
224 .getAscent() + metrics.getDescent())) / 2, 0, 0);
225
226 // Progress bar isn't tall enough for the font. Don't paint it.
227 if (textRect.y < 0) {
228 return;
229 }
230
231 // Paint the text.
232 SynthStyle style = context.getStyle();
233 g.setColor(style.getColor(context,
234 ColorType.TEXT_FOREGROUND));
235 g.setFont(style.getFont(context));
236 style.getGraphicsUtils(context).paintText(context, g,
237 title, textRect.x, textRect.y, -1);
238 }
239 }
240
241 public void paintBorder(SynthContext context, Graphics g, int x,
242 int y, int w, int h) {
243 context.getPainter().paintProgressBarBorder(context, g, x, y,
244 w, h, progressBar.getOrientation());
245 }
246
247 public void propertyChange(PropertyChangeEvent e) {
248 if (SynthLookAndFeel.shouldUpdateStyle(e)) {
249 updateStyle((JProgressBar) e.getSource());
250 }
251 }
252
253 public Dimension getPreferredSize(JComponent c) {
254 Dimension size;
255 Insets border = progressBar.getInsets();
256 FontMetrics fontSizer = progressBar.getFontMetrics(progressBar
257 .getFont());
258 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
259 size = new Dimension(getPreferredInnerHorizontal());
260 } else {
261 size = new Dimension(getPreferredInnerVertical());
262 }
263 // Ensure that the progress string will fit.
264 if (progressBar.isStringPainted()) {
265 String progString = progressBar.getString();
266 int stringHeight = fontSizer.getHeight()
267 + fontSizer.getDescent();
268 if (stringHeight > size.height) {
269 size.height = stringHeight;
270 }
271 // This is also for completeness.
272 int stringWidth = SwingUtilities2.stringWidth(progressBar,
273 fontSizer, progString);
274 if (stringWidth > size.width) {
275 size.width = stringWidth;
276 }
277 }
278
279 size.width += border.left + border.right;
280 size.height += border.top + border.bottom;
281
282 return size;
283 }
284 }
|