001package jmri.util.swing;
002
003import java.awt.BorderLayout;
004import java.awt.Dimension;
005import javax.swing.BorderFactory;
006import javax.swing.JDialog;
007import javax.swing.JFrame;
008import javax.swing.JProgressBar;
009
010/**
011 * Creates a simple counting progress bar.
012 * <p>
013 * After constructing one, call start() to display it.
014 * Then call count(..) to update the progress count, and
015 * finish() when the operation is done.
016 * If the initial maxCount is less than zero,
017 * an indeterminate bar is displayed.
018 *
019 * @author   Mark Underwood Copyright (C) 2011
020 * @author   Bob Jacobsen   Copyright (C) 2023
021 *
022 */
023public class CountingBusyDialog extends JDialog {
024
025    JFrame frame;
026    JProgressBar pbar;
027    int maxCount;
028
029    public CountingBusyDialog(JFrame frame, String title, boolean modal, int maxCount) {
030        super(frame, title, modal);
031        this.frame = frame;
032        this.maxCount = maxCount;
033        initComponents();
034    }
035
036    public void initComponents() {
037
038        setLocationRelativeTo(frame);
039        setPreferredSize(new Dimension(200, 100));
040        setMinimumSize(new Dimension(200, 100));
041        setLayout(new BorderLayout(10, 10));
042
043         if (maxCount < 0) {
044            pbar = new JProgressBar();
045            pbar.setIndeterminate(true);
046        } else {
047            pbar = new JProgressBar(0, maxCount);
048        }
049        pbar.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
050        //pbar.setBorderPainted(true);
051        this.add(pbar, BorderLayout.CENTER);
052    }
053
054    public void start() {
055        this.pack();
056        this.setVisible(true);
057        this.getContentPane().paintAll(pbar.getGraphics());
058    }
059
060    public void count(int now) {
061        pbar.setValue(now);
062    }
063
064    public void finish() {
065        this.dispose();
066
067    }
068
069    // Unused, for now.  Commented out to avoid the compiler warning.
070    //private static final Logger log = LoggerFactory.getLogger(VSDecoderPane.class);
071}