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