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 java.awt.BorderLayout;
022    import java.awt.Container;
023    import java.awt.Dialog;
024    import java.awt.Dimension;
025    import java.awt.Frame;
026    import java.awt.event.ActionEvent;
027    import java.awt.event.ActionListener;
028    
029    import javax.swing.BorderFactory;
030    import javax.swing.Box;
031    import javax.swing.BoxLayout;
032    import javax.swing.JButton;
033    import javax.swing.JDialog;
034    import javax.swing.JFrame;
035    import javax.swing.JPanel;
036    import javax.swing.JProgressBar;
037    
038    /**
039     * WaitDialog This is just a frame with a JLabel in it and no buttons used for
040     * displaying "Please wait..." dialogs and the like
041     *
042     * @author Adam Olsen
043     * @version 1.0
044     *
045     */
046    
047    public class WaitDialog extends JDialog {
048        private Container parent;
049    
050        private String title;
051    
052        private JButton cancel = new JButton("Cancel");
053    
054        private WaitDialogListener listener = null;
055    
056        /**
057         * Default constructor
058         *
059         * @param title
060         *            the message to be displayed in the window's titlebar
061         * @param string
062         *            the message to be displayed in the main window
063         */
064        public WaitDialog(Frame parent, WaitDialogListener listener, String title) {
065            super(parent, title);
066            this.parent = parent;
067            this.listener = listener;
068            this.title = title;
069            initComponents();
070        }
071    
072        public WaitDialog(Dialog parent, WaitDialogListener listener, String title) {
073            super(parent, title);
074            this.parent = parent;
075            this.listener = listener;
076            this.title = title;
077            initComponents();
078        }
079    
080        public WaitDialog(String title){
081            setTitle(title);
082            this.title = title;
083            initComponents();
084        }
085    
086        public void setWaitListener( WaitDialogListener listener )
087        {
088            this.listener = listener;
089            if(listener != null) cancel.setEnabled(true);
090        }
091    
092        private void initComponents() {
093            JPanel panel = (JPanel) getContentPane();
094            panel.setLayout(new BorderLayout(5, 5));
095            panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
096    
097            JProgressBar bar = new JProgressBar();
098            bar.setPreferredSize(new Dimension(200, 20));
099            bar.setIndeterminate(true);
100            bar.setStringPainted(true);
101            bar.setString(title);
102    
103            if (listener == null)
104                cancel.setEnabled(false);
105    
106            panel.add(bar, BorderLayout.NORTH);
107    
108            JPanel buttons = new JPanel();
109            buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
110            buttons.add(Box.createHorizontalGlue());
111            buttons.add(cancel);
112            buttons.add(Box.createHorizontalGlue());
113            panel.add(buttons, BorderLayout.SOUTH);
114            cancel.addActionListener(new ActionListener() {
115                public void actionPerformed(ActionEvent e) {
116                    listener.cancel();
117                    dispose();
118                }
119            });
120    
121            pack();
122            setLocationRelativeTo(parent);
123    
124            setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
125        }
126    }