001: /*
002: * Copyright 2005 Patrick Gotthardt
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.pagosoft.plaf;
017:
018: import javax.swing.*;
019: import javax.swing.plaf.*;
020: import javax.swing.plaf.basic.*;
021: import java.awt.*;
022: import java.awt.geom.*;
023:
024: public class PgsProgressBarUI extends BasicProgressBarUI {
025: private Rectangle boxRect;
026:
027: public static ComponentUI createUI(JComponent x) {
028: return new PgsProgressBarUI();
029: }
030:
031: public void paint(Graphics g, JComponent c) {
032: JProgressBar prog = (JProgressBar) c;
033: Graphics2D gfx = (Graphics2D) g;
034: Dimension size = c.getSize();
035: if (true) {
036: gfx.setColor(c.getBackground());
037: } else if (prog.getOrientation() == JProgressBar.HORIZONTAL) {
038: gfx
039: .setPaint(new GradientPaint(
040: 0,
041: 0,
042: UIManager
043: .getColor("ProgressBar.gradientStart"),
044: 0,
045: size.height / 2,
046: UIManager
047: .getColor("ProgressBar.gradientEnd"),
048: true));
049: } else {
050: gfx
051: .setPaint(new GradientPaint(
052: 0,
053: 0,
054: UIManager
055: .getColor("ProgressBar.gradientStart"),
056: size.width / 2,
057: 0,
058: UIManager
059: .getColor("ProgressBar.gradientEnd"),
060: true));
061: }
062: gfx.fill(new Rectangle(0, 0, size.width, size.height));
063: super .paint(g, c);
064: }
065:
066: /**
067: * All purpose paint method that should do the right thing for almost
068: * all linear, determinate progress bars. By setting a few values in
069: * the defaults
070: * table, things should work just fine to paint your progress bar.
071: * Naturally, override this if you are making a circular or
072: * semi-circular progress bar.
073: *
074: * @see #paintIndeterminate
075: * @since 1.4
076: */
077: protected void paintDeterminate(Graphics g, JComponent c) {
078: if (!(g instanceof Graphics2D)) {
079: return;
080: }
081:
082: Insets b = progressBar.getInsets(); // area for border
083: int barRectWidth = progressBar.getWidth() - (b.right + b.left);
084: int barRectHeight = progressBar.getHeight()
085: - (b.top + b.bottom);
086:
087: // amount of progress to draw
088: int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
089:
090: Graphics2D g2 = (Graphics2D) g;
091: g2.setColor(progressBar.getForeground());
092:
093: if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
094: if (PgsUtils.isLeftToRight(c)) {
095: if (progressBar.isEnabled()) {
096: g2
097: .setPaint(new GradientPaint(
098: 0,
099: 0,
100: UIManager
101: .getColor("ProgressBar.innerGradientStart"),
102: 0,
103: (barRectHeight / 2),
104: UIManager
105: .getColor("ProgressBar.innerGradientEnd"),
106: true));
107: } else {
108: g2
109: .setPaint(new GradientPaint(
110: 0,
111: 0,
112: UIManager
113: .getColor("ProgressBar.innerDisabledGradientStart"),
114: 0,
115: (barRectHeight / 2),
116: UIManager
117: .getColor("ProgressBar.innerDisabledGradientEnd"),
118: true));
119: }
120: g2.fillRect(b.left + 2, b.top + 2, amountFull + b.left
121: - b.right - 4, barRectHeight - 4);
122: g2
123: .setColor(UIManager
124: .getColor(progressBar.isEnabled() ? "ProgressBar.innerBorderColor"
125: : "ProgressBar.innerDisabledBorderColor"));
126: g2.drawRect(b.left + 1, b.top + 1, amountFull + b.left
127: - b.right - 3, barRectHeight - 3);
128: } else {
129: if (progressBar.isEnabled()) {
130: g2
131: .setPaint(new GradientPaint(
132: 0,
133: 0,
134: UIManager
135: .getColor("ProgressBar.innerGradientStart"),
136: 0,
137: (barRectHeight / 2),
138: UIManager
139: .getColor("ProgressBar.innerGradientEnd"),
140: true));
141: } else {
142: g2
143: .setPaint(new GradientPaint(
144: 0,
145: 0,
146: UIManager
147: .getColor("ProgressBar.innerDisabledGradientStart"),
148: 0,
149: (barRectHeight / 2),
150: UIManager
151: .getColor("ProgressBar.innerDisabledGradientEnd"),
152: true));
153: }
154: g2.fillRect(b.left + barRectWidth - amountFull + 2,
155: b.top + 2, amountFull + b.left - b.right - 4,
156: barRectHeight - 4);
157: g2
158: .setColor(UIManager
159: .getColor(progressBar.isEnabled() ? "ProgressBar.innerBorderColor"
160: : "ProgressBar.innerDisabledBorderColor"));
161: g2.drawRect((b.left + barRectWidth - amountFull) - 1,
162: b.top + 1, amountFull + b.left - b.right - 3,
163: barRectHeight - 3);
164: }
165:
166: } else { // VERTICAL
167: if (progressBar.isEnabled()) {
168: g2
169: .setPaint(new GradientPaint(
170: 0,
171: 0,
172: UIManager
173: .getColor("ProgressBar.innerGradientStart"),
174: (barRectWidth / 2),
175: 0,
176: UIManager
177: .getColor("ProgressBar.innerGradientEnd"),
178: true));
179: } else {
180: g2
181: .setPaint(new GradientPaint(
182: 0,
183: 0,
184: UIManager
185: .getColor("ProgressBar.innerDisabledGradientStart"),
186: (barRectWidth / 2),
187: 0,
188: UIManager
189: .getColor("ProgressBar.innerDisabledGradientEnd"),
190: true));
191: }
192: g2.fillRect(b.left + 2, b.top + barRectHeight - b.bottom
193: - amountFull - 3, barRectWidth - b.left - 3,
194: amountFull + 2);
195: g2
196: .setColor(UIManager
197: .getColor(progressBar.isEnabled() ? "ProgressBar.innerBorderColor"
198: : "ProgressBar.innerDisabledBorderColor"));
199: g2.drawRect(b.left + 1, b.top + barRectHeight - b.bottom
200: - amountFull - 4, barRectWidth - b.left - 2,
201: amountFull + 3);
202: }
203:
204: // Deal with possible text painting
205: if (progressBar.isStringPainted()) {
206: paintString(g, b.left, b.top, barRectWidth, barRectHeight,
207: amountFull, b);
208: }
209: }
210:
211: /**
212: * All purpose paint method that should do the right thing for all
213: * linear bouncing-box progress bars.
214: * Override this if you are making another kind of
215: * progress bar.
216: *
217: * @see #paintDeterminate
218: * @since 1.4
219: */
220: protected void paintIndeterminate(Graphics g, JComponent c) {
221: if (!(g instanceof Graphics2D)) {
222: return;
223: }
224:
225: Insets b = progressBar.getInsets(); // area for border
226: int barRectWidth = progressBar.getWidth() - (b.right + b.left);
227: int barRectHeight = progressBar.getHeight()
228: - (b.top + b.bottom);
229:
230: Graphics2D g2 = (Graphics2D) g;
231:
232: // Paint the bouncing box.
233: boxRect = getBox(boxRect);
234: if (boxRect != null) {
235: if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
236: g2
237: .setPaint(new GradientPaint(
238: 0,
239: 0,
240: UIManager
241: .getColor("ProgressBar.innerGradientStart"),
242: 0,
243: (barRectHeight / 2),
244: UIManager
245: .getColor("ProgressBar.innerGradientEnd"),
246: true));
247: g2.fillRect(boxRect.x + b.left + 2, boxRect.y + 2,
248: boxRect.width + b.left - b.right - 4,
249: boxRect.height - 4);
250: g2.setColor(UIManager
251: .getColor("ProgressBar.innerBorderColor"));
252: g2.drawRect(boxRect.x + b.left + 1, boxRect.y + 1,
253: boxRect.width + b.left - b.right - 3,
254: boxRect.height - 3);
255: } else {
256: g2
257: .setPaint(new GradientPaint(
258: 0,
259: 0,
260: UIManager
261: .getColor("ProgressBar.innerGradientStart"),
262: (barRectWidth / 2),
263: 0,
264: UIManager
265: .getColor("ProgressBar.innerGradientEnd"),
266: true));
267: g2.fillRect(boxRect.x + b.left + 1, boxRect.y + b.top
268: + 1, boxRect.width + b.left - b.right - 4,
269: boxRect.height - 2 - b.bottom);
270: g2.setColor(UIManager
271: .getColor("ProgressBar.innerBorderColor"));
272: g2.drawRect(boxRect.x + b.left, boxRect.y + b.top,
273: boxRect.width + b.left - b.right - 3,
274: boxRect.height - 1 - b.bottom);
275: }
276: }
277:
278: // Deal with possible text painting
279: if (progressBar.isStringPainted()) {
280: if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
281: paintString(g2, b.left, b.top, barRectWidth,
282: barRectHeight, boxRect.x, boxRect.width, b);
283: } else {
284: paintString(g2, b.left, b.top, barRectWidth,
285: barRectHeight, boxRect.y, boxRect.height, b);
286: }
287: }
288: }
289:
290: /**
291: * Paints the progress string.
292: *
293: * @param g Graphics used for drawing.
294: * @param x x location of bounding box
295: * @param y y location of bounding box
296: * @param width width of bounding box
297: * @param height height of bounding box
298: * @param fillStart start location, in x or y depending on orientation,
299: * of the filled portion of the progress bar.
300: * @param amountFull size of the fill region, either width or height
301: * depending upon orientation.
302: * @param b Insets of the progress bar.
303: */
304: private void paintString(Graphics g, int x, int y, int width,
305: int height, int fillStart, int amountFull, Insets b) {
306: if (!(g instanceof Graphics2D)) {
307: return;
308: }
309:
310: PgsUtils.installAntialiasing(g);
311:
312: Graphics2D g2 = (Graphics2D) g;
313: String progressString = progressBar.getString();
314: g2.setFont(progressBar.getFont());
315: Point renderLocation = getStringPlacement(g2, progressString,
316: x, y, width, height);
317: Rectangle oldClip = g2.getClipBounds();
318:
319: if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
320: g2.setColor(getSelectionBackground());
321: g2.drawString(progressString, renderLocation.x,
322: renderLocation.y);
323: g2.setColor(getSelectionForeground());
324: g2.clipRect(fillStart, y, amountFull, height);
325: g.drawString(progressString, renderLocation.x,
326: renderLocation.y);
327: } else { // VERTICAL
328: g2.setColor(getSelectionBackground());
329: AffineTransform rotate = AffineTransform
330: .getRotateInstance(Math.PI / 2);
331: g2.setFont(progressBar.getFont().deriveFont(rotate));
332: renderLocation = getStringPlacement(g2, progressString, x,
333: y, width, height);
334: g2.drawString(progressString, renderLocation.x,
335: renderLocation.y);
336: g2.setColor(getSelectionForeground());
337: g2.clipRect(x, fillStart, width, amountFull);
338: g2.drawString(progressString, renderLocation.x,
339: renderLocation.y);
340: }
341: g2.setClip(oldClip);
342:
343: PgsUtils.uninstallAntialiasing(g);
344: }
345: }
|