001    /* JAPI - (Yet another (hopefully) useful) Java API
002     *
003     * Copyright (C) 2004-2006 Christian Hujer
004     *
005     * This program 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 program is distributed in the hope that it will be useful, but
011     * 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 program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018     * 02111-1307, USA.
019     */
020    
021    package net.sf.japi.swing;
022    
023    import java.awt.BorderLayout;
024    import static java.awt.Color.BLACK;
025    import java.awt.Component;
026    import java.awt.GridLayout;
027    import static javax.swing.BorderFactory.createEmptyBorder;
028    import javax.swing.JDialog;
029    import javax.swing.JFrame;
030    import javax.swing.JLabel;
031    import javax.swing.JPanel;
032    import javax.swing.JProgressBar;
033    import static javax.swing.SwingConstants.CENTER;
034    
035    /** ProgressDisplay handles a popup dialog for the mainview
036     * which displays a process progressBar.
037     * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
038     */
039    public final class ProgressDisplay extends JDialog implements Progress {
040    
041        /** Serial Version UID. */
042        private static final long serialVersionUID = 1L;
043    
044        /** The progress progressBar.
045         * @serial include
046         */
047        private final JProgressBar progressBar;  // the progress progressBar
048    
049        /** The text label.
050         * @serial include
051         */
052        private final JLabel label;
053    
054        /** Create a ProgressDisplay.
055         * @param parent Frame to display dialog on
056         * @param title  Title string for progress dialog
057         * @param max    initial maximum of progress points
058         * @param text   the initial label text
059         */
060        public ProgressDisplay(final JFrame parent, final String title, final int max, final String text) {
061            super(parent, title, false);
062            setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); // can't close
063    
064            final JPanel panel = new JPanel(new GridLayout(2, 1));
065            panel.setBorder(createEmptyBorder(20, 15, 30, 15));
066            label = new JLabel(text);
067            label.setForeground(BLACK);
068            panel.add(label);
069    
070            progressBar = new JProgressBar(0, max);
071            progressBar.setValue(0);
072            progressBar.setStringPainted(true);
073            progressBar.setAlignmentY(CENTER);
074            panel.add(progressBar);
075    
076            getContentPane().setLayout(new BorderLayout());
077            getContentPane().add(panel);
078            setSize(300, 140);
079            setLocationRelativeTo(parent);
080    
081            setVisible(true);
082        }
083    
084        /** {@inheritDoc} */
085        public void finished() {
086            setVisible(false);
087            removeAll();
088            dispose();
089        }
090    
091        /** {@inheritDoc} */
092        public void setValue(final int value) {
093            final int max = progressBar.getMaximum();
094            progressBar.setValue(value > max ? max : value);
095        }
096    
097        /** {@inheritDoc} */
098        public void setLabel(final String msg, final int max) {
099            label.setText(msg);
100            progressBar.setMaximum(max);
101            progressBar.setValue(0);
102        }
103    
104        /** {@inheritDoc} */
105        public Component getParentComponent() {
106            return this;
107        }
108    
109    } // class ProgressDisplay