001 /*
002 Copyright (C) 2003 Adam Olsen
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
006 the Free Software Foundation; either version 1, or (at your option)
007 any later version.
008
009 This program is distributed in the hope that it will be useful,
010 but WITHOUT ANY WARRANTY; without even the implied warranty of
011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012 GNU General Public License for more details.
013
014 You should have received a copy of the GNU General Public License
015 along with this program; if not, write to the Free Software
016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017 */
018
019 package com.valhalla.gui;
020
021 import javax.swing.*;
022 import javax.swing.event.*;
023 import java.awt.event.*;
024 import java.awt.*;
025
026 /**
027 * Displays a JDialog with a progress bar in it
028 *
029 * @author Adam Olsen
030 * @version 1.0
031 */
032 public class ProgressDialog extends JProgressBar
033 {
034 protected JButton cancelButton = new JButton( "Cancel" );
035 protected JDialog container = null;
036 protected JPanel mainPanel;
037 protected JPanel progressPanel = new JPanel();
038 protected JLabel messageLabel = new JLabel( "", SwingConstants.CENTER );
039 protected JPanel buttonPanel = new JPanel();
040
041 /**
042 * @param message the message to display
043 * @param min the minimum value of the progress bar
044 * @param max the maximum value of the progress bar
045 */
046 public ProgressDialog( Component parent, String message, int min, int max )
047 {
048 super( min, max );
049
050 if( parent instanceof Frame )
051 {
052 container = new JDialog( (Frame)parent );
053 }
054 else if( parent instanceof Dialog )
055 {
056 container = new JDialog( (Dialog)parent );
057 }
058 else container = new JDialog();
059
060 if( parent != null ) container.setLocationRelativeTo( parent );
061
062 messageLabel.setText( message );
063 container.setTitle( message );
064
065 mainPanel = (JPanel)container.getContentPane();
066 mainPanel.setLayout( new BorderLayout() );
067 mainPanel.add( messageLabel, BorderLayout.NORTH );
068
069 // JPanel progressPanel = new JPanel();
070 progressPanel.setLayout( new BoxLayout( progressPanel, BoxLayout.Y_AXIS ) );
071 progressPanel.add( this );
072 progressPanel.add( Box.createVerticalGlue() );
073 progressPanel.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) );
074
075 mainPanel.add( progressPanel, BorderLayout.CENTER );
076 mainPanel.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
077
078 buttonPanel.setLayout( new BoxLayout( buttonPanel, BoxLayout.X_AXIS ) );
079 buttonPanel.add( Box.createHorizontalGlue() );
080 buttonPanel.add( cancelButton );
081 buttonPanel.add( Box.createHorizontalGlue() );
082 setStringPainted( true );
083
084 mainPanel.add( buttonPanel, BorderLayout.SOUTH );
085 container.pack();
086
087 container.setSize( new Dimension( 300, 120 ) );
088 container.setLocationRelativeTo( null );
089 container.setVisible(true);
090 container.addWindowListener( new WindowAdapter() {
091 public void windowClosing( WindowEvent e ) { }
092 } );
093 }
094
095 public ProgressDialog()
096 {}
097
098 /**
099 * Returns the cancel button in the dialog
100 * @return the dialog's cancel button
101 */
102 public JButton getButton() { return cancelButton; }
103
104 /**
105 * Deletes the dialog
106 */
107 public void delete() { container.dispose(); }
108 public JDialog getDialog() { return container; }
109 }