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