001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.client.internal;
014:
015: import java.net.URL;
016: import java.awt.image.ImageObserver;
017: import java.awt.Color;
018: import java.awt.Image;
019: import java.awt.Frame;
020: import java.awt.Graphics;
021: import java.awt.MediaTracker;
022: import java.awt.Toolkit;
023: import java.awt.Cursor;
024: import java.awt.Component;
025: import java.awt.Dimension;
026: import javax.swing.JProgressBar;
027:
028: final public class LoadingProgress {
029: JProgressBar progressBar;
030: Frame frame;
031: ImageObserver observer;
032: Image image;
033: Component canvas;
034:
035: int maxValue;
036:
037: /** this method creates the progress bar*/
038: public void start(int startValue, int maxValue) {
039: this .maxValue = maxValue;
040: frame = new Frame() {
041: private static final long serialVersionUID = 1L;
042:
043: public void paint(Graphics g) {
044: super .paint(g);
045: g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
046: }
047: };
048: observer = new ImageObserver() {
049: public boolean imageUpdate(Image img, int flags, int x,
050: int y, int w, int h) {
051: if ((flags & ALLBITS) != 0) {
052: canvas.repaint();
053: }
054: return (flags & (ALLBITS | ABORT | ERROR)) == 0;
055: }
056: };
057: frame.setBackground(new Color(255, 255, 204));
058: canvas = new Component() {
059: private static final long serialVersionUID = 1L;
060:
061: public void paint(Graphics g) {
062: g.drawImage(image, 0, 0, observer);
063: }
064: };
065: Toolkit toolkit = Toolkit.getDefaultToolkit();
066: URL url = LoadingProgress.class
067: .getResource("/org/rapla/gui/images/tafel.png");
068:
069: int width = 108;
070: int height = 46;
071: image = toolkit.createImage(url);
072: frame.setResizable(false);
073: frame.setLayout(null);
074: frame.add(canvas);
075: canvas.setBounds(125 - width / 2, 50 - height / 2, width,
076: height);
077: try {
078: // If run on jdk < 1.4 this will throw a MethodNotFoundException
079: //frame.setUndecorated(true);
080: Frame.class.getMethod("setUndecorated",
081: new Class[] { boolean.class }).invoke(frame,
082: new Object[] { new Boolean(true) });
083: } catch (Exception ex) {
084: }
085: Dimension screenSize = Toolkit.getDefaultToolkit()
086: .getScreenSize();
087: frame.setBounds(screenSize.width / 2 - 125,
088: screenSize.height / 2 - 60, 250, 120);
089: frame.validate();
090: MediaTracker mt = new MediaTracker(frame);
091: mt.addImage(image, 0);
092: try {
093: mt.waitForID(0);
094: } catch (InterruptedException e) {
095: }
096: frame.setVisible(true);
097: frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
098: progressBar = new JProgressBar(0, maxValue);
099: progressBar.setValue(startValue);
100: progressBar.setBounds(50, 80, 150, 15);
101: frame.add(progressBar);
102: progressBar.repaint();
103: }
104:
105: public void advance() {
106: int oldValue = progressBar.getValue();
107: if (oldValue < maxValue)
108: progressBar.setValue(oldValue + 1);
109: progressBar.repaint();
110: }
111:
112: public void close() {
113: frame.setCursor(Cursor
114: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
115: frame.dispose();
116: }
117: }
|