    001    package cpw.mods.fml.relauncher;
    002
    003    import java.awt.Dialog.ModalityType;
    004    import java.awt.Dimension;
    005    import java.awt.event.WindowAdapter;
    006    import java.awt.event.WindowEvent;
    007    import java.beans.PropertyChangeEvent;
    008    import java.beans.PropertyChangeListener;
    009
    010    import javax.swing.Box;
    011    import javax.swing.JDialog;
    012    import javax.swing.JFrame;
    013    import javax.swing.JLabel;
    014    import javax.swing.JOptionPane;
    015    import javax.swing.JProgressBar;
    016
    017    import cpw.mods.fml.common.FMLLog;
    018
    019    public class Downloader extends JOptionPane implements IDownloadDisplay
    020    {
    021        private JDialog container;
    022        private JLabel currentActivity;
    023        private JProgressBar progress;
    024        boolean stopIt;
    025        Thread pokeThread;
    026
    027        private Box makeProgressPanel()
    028        {
    029            Box box = Box.createVerticalBox();
    030            box.add(Box.createRigidArea(new Dimension(0,10)));
    031            JLabel welcomeLabel = new JLabel("<html><b><font size='+1'>FML is setting up your minecraft environment</font></b></html>");
    032            box.add(welcomeLabel);
    033            welcomeLabel.setAlignmentY(LEFT_ALIGNMENT);
    034            welcomeLabel = new JLabel("<html>Please wait, FML has some tasks to do before you can play</html>");
    035            welcomeLabel.setAlignmentY(LEFT_ALIGNMENT);
    036            box.add(welcomeLabel);
    037            box.add(Box.createRigidArea(new Dimension(0,10)));
    038            currentActivity = new JLabel("Currently doing ...");
    039            box.add(currentActivity);
    040            box.add(Box.createRigidArea(new Dimension(0,10)));
    041            progress = new JProgressBar(0, 100);
    042            progress.setStringPainted(true);
    043            box.add(progress);
    044            box.add(Box.createRigidArea(new Dimension(0,30)));
    045            return box;
    046        }
    047
    048        public JDialog makeDialog()
    049        {
    050            setMessageType(JOptionPane.INFORMATION_MESSAGE);
    051            setMessage(makeProgressPanel());
    052            setOptions(new Object[] { "Stop" });
    053            addPropertyChangeListener(new PropertyChangeListener()
    054            {
    055                @Override
    056                public void propertyChange(PropertyChangeEvent evt)
    057                {
    058                    if (evt.getSource() == Downloader.this && evt.getPropertyName()==VALUE_PROPERTY)
    059                    {
    060                        requestClose("This will stop minecraft from launching\nAre you sure you want to do this?");
    061                    }
    062                }
    063            });
    064            container = new JDialog(null, "Hello", ModalityType.MODELESS);
    065            container.setResizable(false);
    066            container.setLocationRelativeTo(null);
    067            container.add(this);
    068            this.updateUI();
    069            container.pack();
    070            container.setMinimumSize(container.getPreferredSize());
    071            container.setVisible(true);
    072            container.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    073            container.addWindowListener(new WindowAdapter()
    074            {
    075                @Override
    076                public void windowClosing(WindowEvent e)
    077                {
    078                    requestClose("Closing this window will stop minecraft from launching\nAre you sure you wish to do this?");
    079                }
    080            });
    081            return container;
    082        }
    083        protected void requestClose(String message)
    084        {
    085            int shouldClose = JOptionPane.showConfirmDialog(container, message, "Are you sure you want to stop?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
    086            if (shouldClose == JOptionPane.YES_OPTION)
    087            {
    088                container.dispose();
    089            }
    090            stopIt = true;
    091            if (pokeThread != null)
    092            {
    093                pokeThread.interrupt();
    094            }
    095        }
    096
    097        public void updateProgressString(String progressUpdate, Object... data)
    098        {
    099            FMLLog.finest(progressUpdate, data);
    100            if (currentActivity!=null)
    101            {
    102                currentActivity.setText(String.format(progressUpdate,data));
    103            }
    104        }
    105
    106        public void resetProgress(int sizeGuess)
    107        {
    108            if (progress!=null)
    109            {
    110                progress.getModel().setRangeProperties(0, 0, 0, sizeGuess, false);
    111            }
    112        }
    113
    114        public void updateProgress(int fullLength)
    115        {
    116            if (progress!=null)
    117            {
    118                progress.getModel().setValue(fullLength);
    119            }
    120        }
    121
    122        public void makeHeadless()
    123        {
    124            container = null;
    125            progress = null;
    126            currentActivity = null;
    127        }
    128
    129        @Override
    130        public void setPokeThread(Thread currentThread)
    131        {
    132            this.pokeThread = currentThread;
    133        }
    134
    135        @Override
    136        public boolean shouldStopIt()
    137        {
    138            return stopIt;
    139        }
    140    }