001: /*
002: * @(#)VsnetMetalProgressBarUI.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:
011: import javax.swing.*;
012: import javax.swing.plaf.ComponentUI;
013: import javax.swing.plaf.metal.MetalProgressBarUI;
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: * v * <b>Credit:</b> This implementation is based on work from Santhosh Kumar - santhosh@in.fiorano.com.
023: */
024:
025: public class VsnetMetalProgressBarUI extends MetalProgressBarUI
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 VsnetMetalProgressBarUI();
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 .paintDeterminate(g, c);
106:
107: if (!(g instanceof Graphics2D)) {
108: return;
109: }
110:
111: Color startColor = progressBar.getForeground();
112: Color endColor = VsnetUtils.getLighterColor(startColor, 0.9f);
113:
114: Insets b = progressBar.getInsets(); // area for border
115: int barRectWidth = progressBar.getWidth() - (b.right + b.left);
116: int barRectHeight = progressBar.getHeight()
117: - (b.top + b.bottom);
118:
119: Graphics2D g2d = (Graphics2D) g;
120:
121: // Paint the bouncing box.
122: boolean isVertical = c.getHeight() > c.getWidth();
123: if (delta > 0) {
124: boxRect = new Rectangle(0, 0, x,
125: progressBar.getHeight() - 1);
126: JideSwingUtilities.fillNormalGradient(g2d, boxRect,
127: endColor, startColor, isVertical);
128: } else {
129: boxRect = new Rectangle(x, 0, progressBar.getWidth() - x,
130: progressBar.getHeight() - 1);
131: JideSwingUtilities.fillNormalGradient(g2d, boxRect,
132: startColor, endColor, isVertical);
133: }
134:
135: // Deal with possible text painting
136: if (progressBar.isStringPainted()) {
137: if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
138: paintString(g2d, b.left, b.top, barRectWidth,
139: barRectHeight, boxRect.x, boxRect.width, b);
140: } else {
141: paintString(g2d, b.left, b.top, barRectWidth,
142: barRectHeight, boxRect.y, boxRect.height, b);
143: }
144: }
145: }
146:
147: /**
148: * Paints the progress string.
149: *
150: * @param g Graphics used for drawing.
151: * @param x x location of bounding box
152: * @param y y location of bounding box
153: * @param width width of bounding box
154: * @param height height of bounding box
155: * @param fillStart start location, in x or y depending on orientation,
156: * of the filled portion of the progress bar.
157: * @param amountFull size of the fill region, either width or height
158: * depending upon orientation.
159: * @param b Insets of the progress bar.
160: */
161: private void paintString(Graphics g, int x, int y, int width,
162: int height, int fillStart, int amountFull, Insets b) {
163: if (!(g instanceof Graphics2D)) {
164: return;
165: }
166:
167: Graphics2D g2 = (Graphics2D) g;
168: String progressString = progressBar.getString();
169: g2.setFont(progressBar.getFont());
170: Point renderLocation = getStringPlacement(g2, progressString,
171: x, y, width, height);
172: Rectangle oldClip = g2.getClipBounds();
173:
174: if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
175: g2.setColor(getSelectionBackground());
176: JideSwingUtilities.drawString(progressBar, g2,
177: progressString, renderLocation.x, renderLocation.y);
178: g2.setColor(getSelectionForeground());
179: g2.clipRect(fillStart, y, amountFull, height);
180: JideSwingUtilities.drawString(progressBar, g2,
181: progressString, renderLocation.x, renderLocation.y);
182: } else { // VERTICAL
183: g2.setColor(getSelectionBackground());
184: AffineTransform rotate = AffineTransform
185: .getRotateInstance(Math.PI / 2);
186: g2.setFont(progressBar.getFont().deriveFont(rotate));
187: renderLocation = getStringPlacement(g2, progressString, x,
188: y, width, height);
189: JideSwingUtilities.drawString(progressBar, g2,
190: progressString, renderLocation.x, renderLocation.y);
191: g2.setColor(getSelectionForeground());
192: g2.clipRect(x, fillStart, width, amountFull);
193: JideSwingUtilities.drawString(progressBar, g2,
194: progressString, renderLocation.x, renderLocation.y);
195: }
196: g2.setClip(oldClip);
197: }
198:
199: public static void main(String[] args) {
200: try {
201: UIManager.setLookAndFeel(UIManager
202: .getSystemLookAndFeelClassName());
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,
219: "ProgressBars made intutive - santhosh@in.fiorano.com",
220: JOptionPane.INFORMATION_MESSAGE);
221: System.exit(0);
222: }
223: }
|