001: /*
002: * IndeterminateProgressBar.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Color;
025: import java.awt.EventQueue;
026: import java.awt.Graphics;
027:
028: import java.util.Timer;
029: import java.util.TimerTask;
030:
031: import javax.swing.JComponent;
032: import javax.swing.UIManager;
033:
034: /* ----------------------------------------------------------
035: * CVS NOTE: Changes to the CVS repository prior to the
036: * release of version 3.0.0beta1 has meant a
037: * resetting of CVS revision numbers.
038: * ----------------------------------------------------------
039: */
040:
041: /**
042: *
043: * @author Takis Diakoumis
044: * @version $Revision: 1.4 $
045: * @date $Date: 2006/05/14 06:56:07 $
046: */
047: public class IndeterminateProgressBar extends JComponent implements
048: Runnable {
049:
050: private Color scrollbarColour;
051:
052: private int scrollerWidth;
053: private int animationOffset;
054:
055: private Timer timer;
056: private TimerTask paintProgress;
057:
058: private boolean inProgress;
059: private boolean paintBorder;
060:
061: public IndeterminateProgressBar() {
062: this (true);
063: }
064:
065: public IndeterminateProgressBar(boolean paintBorder) {
066: inProgress = false;
067: scrollerWidth = 20;
068: this .paintBorder = paintBorder;
069:
070: animationOffset = scrollerWidth * -1;
071: scrollbarColour = UIManager.getColor("ProgressBar.foreground");
072: }
073:
074: public void run() {
075: animationOffset++;
076: repaint();
077: if (animationOffset >= 0) {
078: animationOffset = scrollerWidth * -1;
079: }
080: }
081:
082: public void stop() {
083: if (timer != null) {
084: timer.cancel();
085: }
086: inProgress = false;
087: repaint();
088: }
089:
090: public void start() {
091:
092: paintProgress = new TimerTask() {
093: public void run() {
094: EventQueue.invokeLater(IndeterminateProgressBar.this );
095: }
096: };
097:
098: timer = new Timer();
099: timer.schedule(paintProgress, 0, 25);
100: inProgress = true;
101: repaint();
102: }
103:
104: public void cleanup() {
105: if (timer != null) {
106: timer.cancel();
107: }
108: timer = null;
109: }
110:
111: public void paintComponent(Graphics g) {
112:
113: int width = getWidth();
114: int height = getHeight();
115:
116: int y1 = height - 2;
117: int y4 = height - 3;
118:
119: if (paintBorder) {
120: // draw the line border
121: g.setColor(scrollbarColour);
122: g.drawRect(0, 0, width - 2, height - 2);
123: width--;
124: }
125:
126: else {
127: // draw the default border
128: paintBorder(g);
129: y1 = height;
130: y4 = height - 1;
131: }
132:
133: if (!inProgress) {
134: return;
135: }
136:
137: // set the polygon points
138: int[] xPosns = { 0, 0, 0, 0 };
139: int[] yPosns = { y1, 1, 1, y1 };
140:
141: // constrain the clip
142: //g.setClip(1, 1, width - 3, y4);
143: g.setClip(0, 1, width, y4);
144:
145: g.setColor(scrollbarColour);
146:
147: for (int i = 0, k = width + scrollerWidth; i < k; i += scrollerWidth) {
148:
149: xPosns[0] = i + animationOffset;
150: xPosns[1] = xPosns[0] + (scrollerWidth / 2);
151: xPosns[2] = xPosns[0] + scrollerWidth;
152: xPosns[3] = xPosns[1];
153:
154: g.fillPolygon(xPosns, yPosns, 4);
155:
156: }
157:
158: }
159:
160: }
|