001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: ProgressDialog.java,v $
023   Revision 1.10  2004/05/05 23:20:24  markl
024   comment block updates
025
026   Revision 1.9  2004/03/16 06:43:39  markl
027   LocaleManager method change
028
029   Revision 1.8  2004/01/23 00:06:28  markl
030   javadoc corrections
031
032   Revision 1.7  2003/01/19 09:39:38  markl
033   Added new constructors that take a parent dialog.
034
035   Revision 1.6  2001/03/20 00:54:56  markl
036   Fixed deprecated calls.
037
038   Revision 1.5  2001/03/12 09:56:56  markl
039   KLabel/KLabelArea changes.
040
041   Revision 1.4  2001/03/12 05:19:59  markl
042   Source code cleanup.
043
044   Revision 1.3  1999/04/19 06:06:32  markl
045   I18N changes, new constructor.
046
047   Revision 1.2  1999/01/10 03:22:17  markl
048   added GPL header & RCS tag
049   ----------------------------------------------------------------------------
050*/
051
052package kiwi.ui.dialog;
053
054import java.awt.*;
055import java.awt.event.*;
056import javax.swing.border.EmptyBorder;
057import javax.swing.*;
058
059import kiwi.ui.*;
060import kiwi.util.*;
061
062/** A simple dialog with a message and progress meter, for use when a
063  * non-interruptible, lengthy task is being performed. The dialog tracks the
064  * progress of a <code>Task</code>, which periodically notifies the dialog
065  * of the percentage of the task completed. 
066  *
067  * <p><center>
068  * <img src="snapshot/ProgressDialog.gif"><br>
069  * <i>An example ProgressDialog.</i>
070  * </center>
071  *
072  * @see kiwi.util.Task
073  *
074  * @author Mark Lindner
075  */
076
077public class ProgressDialog extends KDialog implements ProgressObserver
078  {
079  private JProgressBar bar;
080  private KLabel label, iconLabel;
081
082  /** Construct a new <code>ProgressDialog</code> with a default title.
083    *
084    * @param parent The parent window for this dialog.
085    * @param modal A flag specifying whether the dialog should be modal.
086    */
087  
088  public ProgressDialog(Frame parent, boolean modal)
089    {
090    this(parent, "", modal);
091    }
092
093  /** Construct a new <code>ProgressDialog</code> with a default title.
094    *
095    * @param parent The parent window for this dialog.
096    * @param modal A flag specifying whether the dialog should be modal.
097    *
098    * @since Kiwi 1.4
099    */
100  
101  public ProgressDialog(Dialog parent, boolean modal)
102    {
103    this(parent, "", modal);
104    }
105  
106  /** Construct a new <code>ProgressDialog</code>.
107    *
108    * @param parent The parent window for this dialog.
109    * @param title The title for the dialog window.
110    * @param modal A flag specifying whether the dialog should be modal.
111    */
112
113  public ProgressDialog(Frame parent, String title, boolean modal)
114    {
115    super(parent, title, modal);
116
117    _init();
118    }
119
120  /**
121   */
122
123  /** Construct a new <code>ProgressDialog</code>.
124    *
125    * @param parent The parent window for this dialog.
126    * @param title The title for the dialog window.
127    * @param modal A flag specifying whether the dialog should be modal.
128    *
129    * @since Kiwi 1.4
130    */
131
132  public ProgressDialog(Dialog parent, String title, boolean modal)
133    {
134    super(parent, title, modal);
135
136    _init();
137    }  
138
139  /*
140   */
141  
142  private void _init()
143    {
144    LocaleData loc = LocaleManager.getDefault().getLocaleData("KiwiDialogs");
145    
146    setResizable(false);
147    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
148
149    KPanel main = getMainContainer();
150
151    main.setLayout(new BorderLayout(5, 5));
152    main.setBorder(KiwiUtils.defaultBorder);
153
154    main.add("North", label
155             = new KLabel(loc.getMessage("kiwi.dialog.prompt.wait")));
156
157    KPanel p = new KPanel();
158    p.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
159
160    iconLabel = new KLabel(KiwiUtils.getResourceManager()
161                           .getIcon("ani-gear.gif"));
162    p.add(iconLabel);
163
164    bar = new JProgressBar();
165    bar.setMinimum(0);
166    bar.setMaximum(100);
167    bar.setValue(0);
168    bar.setOpaque(false);
169    p.add(bar);
170
171    main.add("Center", p);
172
173    if(getTitle().length() == 0)
174      setTitle(loc.getMessage("kiwi.dialog.title.progress"));
175
176    pack();
177    }
178
179  /** Implementation of the <code>ProgressObserver</code> interface. */
180
181  public void setProgress(int progress)
182    {
183    if(progress < 0) progress = 0;
184    else if(progress > 100) progress = 100;
185
186    bar.setValue(progress);
187    if(progress == 100)
188      {
189      KiwiUtils.paintImmediately(bar);
190      KiwiUtils.sleep(1);
191      setVisible(false);
192      }
193    }
194
195  /** Set the dialog's icon. The dialog's icon can be changed via a call to
196    * this method. Animated GIF images add a professional touch when used with
197    * <code>ProgressDialog</code>s.
198    *
199    * @param icon The new icon to use.
200    */
201
202  public void setIcon(Icon icon)
203    {
204    iconLabel.setIcon(icon);
205    }
206
207  /** Set the message for the dialog.
208    *
209    * @param message The message to display in the dialog.
210    */
211
212  public void setMessage(String message)
213    {
214    label.setText(message);
215    pack();
216    }
217
218  /** Track the progress of a task. Displays the dialog and tracks the progress
219    * of the task, updating the progress meter accordingly. When the task
220    * is completed, the dialog automatically disappears.
221    *
222    * @param task The <code>Task</code> to track; it should not be currently
223    * running, as the dialog will start the task itself.
224    */
225  
226  public void track(Task task)
227    {
228    bar.setValue(0);
229    task.addProgressObserver(this);
230    Thread t = new Thread(task);
231    t.start();
232    setVisible(true);
233    task.removeProgressObserver(this);
234    }
235
236  }
237
238/* end of source file */