Debian Codegraph @

src/com/jidesoft/plaf/vsnet/VsnetProgressBarUI.java

raw
1 /*
2 * @(#)VsnetProgressBarUI.java 6/5/2005
3 *
4 * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
5 */
6 package com.jidesoft.plaf.vsnet;
7
8 import com.jidesoft.plaf.UIDefaultsLookup;
9 import com.jidesoft.swing.JideSwingUtilities;
10
11 import javax.swing.*;
12 import javax.swing.plaf.ComponentUI;
13 import javax.swing.plaf.basic.BasicProgressBarUI;
14 import java.awt.*;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17 import java.awt.geom.AffineTransform;
18
19 /**
20 * A better ProgressBarUI for indeterminate progress bar.
21 * <p/>
22 * v * <b>Credit:</b> This implementation is based on work from Santhosh Kumar - [email protected].
23 */
24
25 public class VsnetProgressBarUI extends BasicProgressBarUI implements ActionListener {
26 /**
27 * Interval (in ms) between repaints of the indeterminate progress bar.
28 * The value of this method is set
29 * (every time the progress bar changes to indeterminate mode)
30 * using the
31 * "ProgressBar.repaintInterval" key in the defaults table.
32 */
33 private int repaintInterval;
34
35 private int x = 0, y = 0, delta = +1;
36 private Timer timer = null;
37
38
39 public static ComponentUI createUI(JComponent x) {
40 return new VsnetProgressBarUI();
41 }
42
43 @Override
44 protected void installDefaults() {
45 super.installDefaults();
46 initRepaintInterval(); //initialize repaint interval
47 }
48
49 @Override
50 protected void startAnimationTimer() {
51 if (timer == null) {
52 timer = new Timer(getRepaintInterval() / 20, this);
53 }
54 x = y = 0;
55 delta = 1;
56 timer.start();
57 }
58
59 @Override
60 protected void stopAnimationTimer() {
61 if (timer != null) {
62 timer.stop();
63 }
64 }
65
66 public void actionPerformed(ActionEvent ae) {
67 // style1
68 if (x == 0)
69 delta = +1;
70 else if (x == progressBar.getWidth())
71 delta = -1;
72 x += delta;
73
74 progressBar.repaint();
75 }
76
77 /**
78 * Returns the desired number of milliseconds between repaints.
79 * This value is meaningful
80 * only if the progress bar is in indeterminate mode.
81 * The repaint interval determines how often the
82 * default animation thread's timer is fired.
83 * It's also used by the default indeterminate progress bar
84 * painting code when determining
85 * how far to move the bouncing box per frame.
86 * The repaint interval is specified by
87 * the "ProgressBar.repaintInterval" UI default.
88 *
89 * @return the repaint interval, in milliseconds
90 */
91 protected int getRepaintInterval() {
92 return repaintInterval;
93 }
94
95 private int initRepaintInterval() {
96 repaintInterval = UIDefaultsLookup.getInt("ProgressBar.repaintInterval");
97 return repaintInterval;
98 }
99
100 private Rectangle boxRect;
101
102 @Override
103 public void paintIndeterminate(Graphics g, JComponent c) {
104 if (!(g instanceof Graphics2D)) {
105 return;
106 }
107
108 Color startColor = progressBar.getForeground();
109 Color endColor = VsnetUtils.getLighterColor(startColor, 0.9f);
110
111 Insets b = progressBar.getInsets(); // area for border
112 int barRectWidth = progressBar.getWidth() - (b.right + b.left);
113 int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
114
115 Graphics2D g2d = (Graphics2D) g;
116
117 // Paint the bouncing box.
118 boolean isVertical = c.getHeight() > c.getWidth();
119 if (delta > 0) {
120 boxRect = new Rectangle(0, 0, x, progressBar.getHeight() - 1);
121 JideSwingUtilities.fillNormalGradient(g2d, boxRect, endColor, startColor, isVertical);
122 }
123 else {
124 boxRect = new Rectangle(x, 0, progressBar.getWidth() - x, progressBar.getHeight() - 1);
125 JideSwingUtilities.fillNormalGradient(g2d, boxRect, startColor, endColor, isVertical);
126 }
127
128 // Deal with possible text painting
129 if (progressBar.isStringPainted()) {
130 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
131 paintString(g2d, b.left, b.top,
132 barRectWidth, barRectHeight,
133 boxRect.x, boxRect.width, b);
134 }
135 else {
136 paintString(g2d, b.left, b.top,
137 barRectWidth, barRectHeight,
138 boxRect.y, boxRect.height, b);
139 }
140 }
141 }
142
143 /**
144 * Paints the progress string.
145 *
146 * @param g Graphics used for drawing.
147 * @param x x location of bounding box
148 * @param y y location of bounding box
149 * @param width width of bounding box
150 * @param height height of bounding box
151 * @param fillStart start location, in x or y depending on orientation,
152 * of the filled portion of the progress bar.
153 * @param amountFull size of the fill region, either width or height
154 * depending upon orientation.
155 * @param b Insets of the progress bar.
156 */
157 private void paintString(Graphics g, int x, int y, int width, int height,
158 int fillStart, int amountFull, Insets b) {
159 if (!(g instanceof Graphics2D)) {
160 return;
161 }
162
163 Graphics2D g2 = (Graphics2D) g;
164 String progressString = progressBar.getString();
165 g2.setFont(progressBar.getFont());
166 Point renderLocation = getStringPlacement(g2, progressString,
167 x, y, width, height);
168 Rectangle oldClip = g2.getClipBounds();
169
170 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
171 g2.setColor(getSelectionBackground());
172 JideSwingUtilities.drawString(progressBar, g2, progressString,
173 renderLocation.x, renderLocation.y);
174 g2.setColor(getSelectionForeground());
175 g2.clipRect(fillStart, y, amountFull, height);
176 JideSwingUtilities.drawString(progressBar, g2, progressString,
177 renderLocation.x, renderLocation.y);
178 }
179 else { // VERTICAL
180 g2.setColor(getSelectionBackground());
181 AffineTransform rotate =
182 AffineTransform.getRotateInstance(Math.PI / 2);
183 g2.setFont(progressBar.getFont().deriveFont(rotate));
184 renderLocation = getStringPlacement(g2, progressString,
185 x, y, width, height);
186 JideSwingUtilities.drawString(progressBar, g2, progressString,
187 renderLocation.x, renderLocation.y);
188 g2.setColor(getSelectionForeground());
189 g2.clipRect(x, fillStart, width, amountFull);
190 JideSwingUtilities.drawString(progressBar, g2, progressString,
191 renderLocation.x, renderLocation.y);
192 }
193 g2.setClip(oldClip);
194 }
195 }