Debian Codegraph @

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

raw
1 /*
2 * @(#)VsnetMetalProgressBarUI.java 6/21/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.metal.MetalProgressBarUI;
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 VsnetMetalProgressBarUI extends MetalProgressBarUI 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 VsnetMetalProgressBarUI();
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 super.paintDeterminate(g, c);
105
106 if (!(g instanceof Graphics2D)) {
107 return;
108 }
109
110 Color startColor = progressBar.getForeground();
111 Color endColor = VsnetUtils.getLighterColor(startColor, 0.9f);
112
113 Insets b = progressBar.getInsets(); // area for border
114 int barRectWidth = progressBar.getWidth() - (b.right + b.left);
115 int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
116
117 Graphics2D g2d = (Graphics2D) g;
118
119 // Paint the bouncing box.
120 boolean isVertical = c.getHeight() > c.getWidth();
121 if (delta > 0) {
122 boxRect = new Rectangle(0, 0, x, progressBar.getHeight() - 1);
123 JideSwingUtilities.fillNormalGradient(g2d, boxRect, endColor, startColor, isVertical);
124 }
125 else {
126 boxRect = new Rectangle(x, 0, progressBar.getWidth() - x, progressBar.getHeight() - 1);
127 JideSwingUtilities.fillNormalGradient(g2d, boxRect, startColor, endColor, isVertical);
128 }
129
130 // Deal with possible text painting
131 if (progressBar.isStringPainted()) {
132 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
133 paintString(g2d, b.left, b.top,
134 barRectWidth, barRectHeight,
135 boxRect.x, boxRect.width, b);
136 }
137 else {
138 paintString(g2d, b.left, b.top,
139 barRectWidth, barRectHeight,
140 boxRect.y, boxRect.height, b);
141 }
142 }
143 }
144
145 /**
146 * Paints the progress string.
147 *
148 * @param g Graphics used for drawing.
149 * @param x x location of bounding box
150 * @param y y location of bounding box
151 * @param width width of bounding box
152 * @param height height of bounding box
153 * @param fillStart start location, in x or y depending on orientation,
154 * of the filled portion of the progress bar.
155 * @param amountFull size of the fill region, either width or height
156 * depending upon orientation.
157 * @param b Insets of the progress bar.
158 */
159 private void paintString(Graphics g, int x, int y, int width, int height,
160 int fillStart, int amountFull, Insets b) {
161 if (!(g instanceof Graphics2D)) {
162 return;
163 }
164
165 Graphics2D g2 = (Graphics2D) g;
166 String progressString = progressBar.getString();
167 g2.setFont(progressBar.getFont());
168 Point renderLocation = getStringPlacement(g2, progressString,
169 x, y, width, height);
170 Rectangle oldClip = g2.getClipBounds();
171
172 if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
173 g2.setColor(getSelectionBackground());
174 JideSwingUtilities.drawString(progressBar, g2, progressString,
175 renderLocation.x, renderLocation.y);
176 g2.setColor(getSelectionForeground());
177 g2.clipRect(fillStart, y, amountFull, height);
178 JideSwingUtilities.drawString(progressBar, g2, progressString,
179 renderLocation.x, renderLocation.y);
180 }
181 else { // VERTICAL
182 g2.setColor(getSelectionBackground());
183 AffineTransform rotate =
184 AffineTransform.getRotateInstance(Math.PI / 2);
185 g2.setFont(progressBar.getFont().deriveFont(rotate));
186 renderLocation = getStringPlacement(g2, progressString,
187 x, y, width, height);
188 JideSwingUtilities.drawString(progressBar, g2, progressString,
189 renderLocation.x, renderLocation.y);
190 g2.setColor(getSelectionForeground());
191 g2.clipRect(x, fillStart, width, amountFull);
192 JideSwingUtilities.drawString(progressBar, g2, progressString,
193 renderLocation.x, renderLocation.y);
194 }
195 g2.setClip(oldClip);
196 }
197
198
199 public static void main(String[] args) {
200 try {
201 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
202 }
203 catch (Exception e) {
204 e.printStackTrace();
205 }
206 JProgressBar progressBar = new JProgressBar();
207 JProgressBar myProgressBar = new JProgressBar();
208 myProgressBar.setUI(new VsnetMetalProgressBarUI());
209 progressBar.setIndeterminate(true);
210 progressBar.setString("Percent");
211 progressBar.setStringPainted(true);
212 myProgressBar.setIndeterminate(true);
213 myProgressBar.setString("Percent");
214 myProgressBar.setStringPainted(true);
215 JPanel panel = new JPanel(new BorderLayout(5, 5));
216 panel.add(progressBar, BorderLayout.NORTH);
217 panel.add(myProgressBar, BorderLayout.SOUTH);
218 JOptionPane.showMessageDialog(null, panel, "ProgressBars made intutive - [email protected]", JOptionPane.INFORMATION_MESSAGE);
219 System.exit(0);
220 }
221 }