001: /*
002: * @(#)VsnetProgressBarUI.java 6/5/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.basic.BasicProgressBarUI;
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 VsnetProgressBarUI extends BasicProgressBarUI implements
026: 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 VsnetProgressBarUI();
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: if (!(g instanceof Graphics2D)) {
106: return;
107: }
108:
109: Color startColor = progressBar.getForeground();
110: Color endColor = VsnetUtils.getLighterColor(startColor, 0.9f);
111:
112: Insets b = progressBar.getInsets(); // area for border
113: int barRectWidth = progressBar.getWidth() - (b.right + b.left);
114: int barRectHeight = progressBar.getHeight()
115: - (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,
123: progressBar.getHeight() - 1);
124: JideSwingUtilities.fillNormalGradient(g2d, boxRect,
125: endColor, startColor, isVertical);
126: } else {
127: boxRect = new Rectangle(x, 0, progressBar.getWidth() - x,
128: progressBar.getHeight() - 1);
129: JideSwingUtilities.fillNormalGradient(g2d, boxRect,
130: startColor, endColor, isVertical);
131: }
132:
133: // Deal with possible text painting
134: if (progressBar.isStringPainted()) {
135: if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
136: paintString(g2d, b.left, b.top, barRectWidth,
137: barRectHeight, boxRect.x, boxRect.width, b);
138: } else {
139: paintString(g2d, b.left, b.top, barRectWidth,
140: barRectHeight, 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,
160: int height, 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,
175: progressString, renderLocation.x, renderLocation.y);
176: g2.setColor(getSelectionForeground());
177: g2.clipRect(fillStart, y, amountFull, height);
178: JideSwingUtilities.drawString(progressBar, g2,
179: progressString, renderLocation.x, renderLocation.y);
180: } else { // VERTICAL
181: g2.setColor(getSelectionBackground());
182: AffineTransform rotate = AffineTransform
183: .getRotateInstance(Math.PI / 2);
184: g2.setFont(progressBar.getFont().deriveFont(rotate));
185: renderLocation = getStringPlacement(g2, progressString, x,
186: y, width, height);
187: JideSwingUtilities.drawString(progressBar, g2,
188: progressString, renderLocation.x, renderLocation.y);
189: g2.setColor(getSelectionForeground());
190: g2.clipRect(x, fillStart, width, amountFull);
191: JideSwingUtilities.drawString(progressBar, g2,
192: progressString, renderLocation.x, renderLocation.y);
193: }
194: g2.setClip(oldClip);
195: }
196: }
|