001: /*
002: * @(#)VsnetWindowsProgressBarUI.java 6/21/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.plaf.vsnet;
007:
008: import com.jidesoft.plaf.UIDefaultsLookup;
009: import com.jidesoft.swing.JideSwingUtilities;
010: import com.sun.java.swing.plaf.windows.WindowsProgressBarUI;
011:
012: import javax.swing.*;
013: import javax.swing.plaf.ComponentUI;
014: import java.awt.*;
015: import java.awt.event.ActionEvent;
016: import java.awt.event.ActionListener;
017: import java.awt.geom.AffineTransform;
018:
019: /**
020: * A better ProgressBarUI for indeterminate progress bar.
021: * <p/>
022: * <b>Credit:</b> This implementation is based on work from Santhosh Kumar - santhosh@in.fiorano.com.
023: */
024:
025: public class VsnetWindowsProgressBarUI extends WindowsProgressBarUI
026: implements ActionListener {
027: /**
028: * Interval (in ms) between repaints of the indeterminate progress bar.
029: * The value of this method is set
030: * (every time the progress bar changes to indeterminate mode)
031: * using the
032: * "ProgressBar.repaintInterval" key in the defaults table.
033: */
034: private int repaintInterval;
035:
036: private int x = 0, y = 0, delta = +1;
037: private Timer timer = null;
038:
039: public static ComponentUI createUI(JComponent x) {
040: return new VsnetWindowsProgressBarUI();
041: }
042:
043: @Override
044: protected void installDefaults() {
045: super .installDefaults();
046: initRepaintInterval(); //initialize repaint interval
047: }
048:
049: @Override
050: protected void startAnimationTimer() {
051: if (timer == null) {
052: timer = new Timer(getRepaintInterval() / 20, this );
053: }
054: x = y = 0;
055: delta = 1;
056: timer.start();
057: }
058:
059: @Override
060: protected void stopAnimationTimer() {
061: if (timer != null) {
062: timer.stop();
063: }
064: }
065:
066: public void actionPerformed(ActionEvent ae) {
067: // style1
068: if (x == 0)
069: delta = +1;
070: else if (x == progressBar.getWidth())
071: delta = -1;
072: x += delta;
073:
074: progressBar.repaint();
075: }
076:
077: /**
078: * Returns the desired number of milliseconds between repaints.
079: * This value is meaningful
080: * only if the progress bar is in indeterminate mode.
081: * The repaint interval determines how often the
082: * default animation thread's timer is fired.
083: * It's also used by the default indeterminate progress bar
084: * painting code when determining
085: * how far to move the bouncing box per frame.
086: * The repaint interval is specified by
087: * the "ProgressBar.repaintInterval" UI default.
088: *
089: * @return the repaint interval, in milliseconds
090: */
091: protected int getRepaintInterval() {
092: return repaintInterval;
093: }
094:
095: private int initRepaintInterval() {
096: repaintInterval = UIDefaultsLookup
097: .getInt("ProgressBar.repaintInterval");
098: return repaintInterval;
099: }
100:
101: private Rectangle boxRect;
102:
103: @Override
104: public void paintIndeterminate(Graphics g, JComponent c) {
105: super .paintIndeterminate(g, c);
106: Color startColor = progressBar.getForeground();
107: Color endColor = VsnetUtils.getLighterColor(startColor, 0.9f);
108:
109: if (!(g instanceof Graphics2D)) {
110: return;
111: }
112:
113: boolean vertical = (progressBar.getOrientation() == JProgressBar.VERTICAL);
114:
115: Insets b = progressBar.getInsets(); // area for border
116: b.top = 2;
117: b.left = 2;
118: b.right = 2;
119: b.bottom = 2;
120: int barRectWidth = progressBar.getWidth() - (b.right + b.left);
121: int barRectHeight = progressBar.getHeight()
122: - (b.top + b.bottom);
123: g.setColor(progressBar.getBackground());
124: g.fillRect(b.left, b.top, barRectWidth, barRectHeight);
125:
126: Graphics2D g2d = (Graphics2D) g;
127:
128: // Paint the bouncing box.
129: if (delta > 0) {
130: boxRect = new Rectangle(b.left, b.top, x, barRectHeight);
131: JideSwingUtilities.fillNormalGradient(g2d, boxRect,
132: endColor, startColor, vertical);
133: } else {
134: boxRect = new Rectangle(x, b.top, barRectWidth - x,
135: barRectHeight);
136: JideSwingUtilities.fillNormalGradient(g2d, boxRect,
137: startColor, endColor, vertical);
138: }
139:
140: // Deal with possible text painting
141: if (progressBar.isStringPainted()) {
142: if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
143: paintString(g2d, b.left, b.top, barRectWidth,
144: barRectHeight, boxRect.x, boxRect.width, b);
145: } else {
146: paintString(g2d, b.left, b.top, barRectWidth,
147: barRectHeight, boxRect.y, boxRect.height, b);
148: }
149: }
150: }
151:
152: /**
153: * Paints the progress string.
154: *
155: * @param g Graphics used for drawing.
156: * @param x x location of bounding box
157: * @param y y location of bounding box
158: * @param width width of bounding box
159: * @param height height of bounding box
160: * @param fillStart start location, in x or y depending on orientation,
161: * of the filled portion of the progress bar.
162: * @param amountFull size of the fill region, either width or height
163: * depending upon orientation.
164: * @param b Insets of the progress bar.
165: */
166: private void paintString(Graphics g, int x, int y, int width,
167: int height, int fillStart, int amountFull, Insets b) {
168: if (!(g instanceof Graphics2D)) {
169: return;
170: }
171:
172: Graphics2D g2 = (Graphics2D) g;
173: String progressString = progressBar.getString();
174: g2.setFont(progressBar.getFont());
175: Point renderLocation = getStringPlacement(g2, progressString,
176: x, y, width, height);
177: Rectangle oldClip = g2.getClipBounds();
178:
179: if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
180: g2.setColor(getSelectionBackground());
181: JideSwingUtilities.drawString(progressBar, g2,
182: progressString, renderLocation.x, renderLocation.y);
183: g2.setColor(getSelectionForeground());
184: g2.clipRect(fillStart, y, amountFull, height);
185: JideSwingUtilities.drawString(progressBar, g2,
186: progressString, renderLocation.x, renderLocation.y);
187: } else { // VERTICAL
188: g2.setColor(getSelectionBackground());
189: AffineTransform rotate = AffineTransform
190: .getRotateInstance(Math.PI / 2);
191: g2.setFont(progressBar.getFont().deriveFont(rotate));
192: renderLocation = getStringPlacement(g2, progressString, x,
193: y, width, height);
194: JideSwingUtilities.drawString(progressBar, g2,
195: progressString, renderLocation.x, renderLocation.y);
196: g2.setColor(getSelectionForeground());
197: g2.clipRect(x, fillStart, width, amountFull);
198: JideSwingUtilities.drawString(progressBar, g2,
199: progressString, renderLocation.x, renderLocation.y);
200: }
201: g2.setClip(oldClip);
202: }
203:
204: public static void main(String[] args) {
205: try {
206: UIManager.setLookAndFeel(UIManager
207: .getSystemLookAndFeelClassName());
208: } catch (Exception e) {
209: e.printStackTrace();
210: }
211: JProgressBar progressBar = new JProgressBar();
212: JProgressBar myProgressBar = new JProgressBar();
213: myProgressBar.setUI(new VsnetWindowsProgressBarUI());
214: progressBar.setIndeterminate(true);
215: progressBar.setString("Percent");
216: progressBar.setStringPainted(true);
217: myProgressBar.setIndeterminate(true);
218: myProgressBar.setString("Percent");
219: myProgressBar.setStringPainted(true);
220: JPanel panel = new JPanel(new BorderLayout(5, 5));
221: panel.add(progressBar, BorderLayout.NORTH);
222: panel.add(myProgressBar, BorderLayout.SOUTH);
223: JOptionPane.showMessageDialog(null, panel,
224: "ProgressBars made intutive - santhosh@in.fiorano.com",
225: JOptionPane.INFORMATION_MESSAGE);
226: System.exit(0);
227: }
228: }
|