Advertisement
moomoohk

Untitled

Apr 1st, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. public class Downloader implements Runnable //downloader part
  2.     {
  3.         private URL source;
  4.         private String dest;
  5.         private boolean indeterminate;
  6.         private JFrame frame;
  7.         private JProgressBar prog;
  8.         private String current;
  9.         private String ver;
  10.  
  11.         public Downloader(URL source, String dest, boolean indeterminate, String current, String ver)
  12.         {
  13.             this.source = source;
  14.             this.dest = dest;
  15.             this.indeterminate = indeterminate;
  16.             this.current=current;
  17.             this.ver=ver;
  18.         }
  19.  
  20.         public void run()
  21.         {
  22.             long filesize=0L;
  23.             if(!indeterminate)
  24.             {
  25.                 filesize=getFilesize(source.toString());
  26.                 System.out.println(filesize);
  27.             }
  28.             final long finalsize=filesize;
  29.  
  30.             EventQueue.invokeLater(new Runnable()
  31.             {
  32.                 public void run()
  33.                 {
  34.                     frame=new JFrame("Working...");
  35.                     frame.setLayout(new FlowLayout());
  36.                     JPanel panel=new JPanel(new FlowLayout());
  37.                     prog=new JProgressBar(0, (int)finalsize/1024);
  38.                     prog.setIndeterminate(indeterminate);
  39.                     panel.add(prog, "South");
  40.                     prog.setVisible(true);
  41.                     frame.add(panel, "South");
  42.                     frame.setSize(300, 100);
  43.                     panel.setSize(250, 100);
  44.                     frame.setLocationRelativeTo(null);
  45.                     frame.setVisible(true);
  46.                     prog.setStringPainted(true);
  47.                 }
  48.             });
  49.             try
  50.             {
  51.                 java.io.BufferedInputStream in = new java.io.BufferedInputStream(source.openStream());
  52.                 java.io.FileOutputStream fos = new java.io.FileOutputStream(dest);
  53.                 java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
  54.                 byte[] data = new byte[1024];
  55.                 int x=0, count=0;
  56.                 while((x=in.read(data,0,1024))>=0)
  57.                 {
  58.                     final long pos=fos.getChannel().position();
  59.                     if((!indeterminate)&&(count%3==0))
  60.                         EventQueue.invokeLater(new Runnable()
  61.                         {
  62.                             public void run()
  63.                             {
  64.                                 prog.setValue((int) (pos/1024));
  65.                                 prog.setString((pos/1024)+" KB/"+(finalsize/1024)+" KB");
  66.                             }
  67.                         });
  68.                     System.out.println("pos: "+fos.getChannel().position());
  69.                     bout.write(data,0,x);
  70.                 }
  71.                 bout.close();
  72.                 in.close();
  73.                 EventQueue.invokeLater(new Runnable()
  74.                 {
  75.                     public void run()
  76.                     {
  77.                         prog.setValue(prog.getMaximum());
  78.                         prog.setString(finalsize+" KB/"+finalsize+" KB");
  79.                     }
  80.                 });
  81.                 System.out.println("Done");
  82.             }
  83.             catch(UnknownHostException e)
  84.             {
  85.                 EventQueue.invokeLater(new Runnable()
  86.                 {
  87.                     public void run()
  88.                     {
  89.                         frame.setVisible(false);
  90.                     }
  91.                 });
  92.                 dialog("Can't connect to the internet", "Can't check if there's a new version.\n" +
  93.                         "Make sure you're connected to the internet and try again.", 10);
  94.             }
  95.             catch(IOException e)
  96.             {
  97.                 EventQueue.invokeLater(new Runnable()
  98.                 {
  99.                     public void run()
  100.                     {
  101.                         frame.setVisible(false);
  102.                     }
  103.                 });
  104.                 dialog("Complicated error", "Something to do with opening streams", 11);
  105.             }
  106.             EventQueue.invokeLater(new Runnable()
  107.             {
  108.                 public void run()
  109.                 {
  110.                     frame.setVisible(false);
  111.                     if(indeterminate)
  112.                         goOn(current, ver); //if there's a new version
  113.                     else
  114.                         EventQueue.invokeLater(new Runnable()
  115.                         {
  116.                             public void run()
  117.                             {
  118.                                 String message="Grame is up to date!";
  119.                                 JOptionPane p=new JOptionPane(message);
  120.                                 JDialog d =p.createDialog(new JFrame(), "Done!");
  121.                                 d.setSize(400, 150);
  122.                                 d.setVisible(true);
  123.                             }
  124.                         });
  125.                 }
  126.             });
  127.         }
  128.     }
  129. public static void goOn(String current, String ver) //skip down a bit
  130.     {
  131.         String link="";
  132.         try
  133.         {
  134.             link=new Scanner(new FileReader(new File("tmpFile"))).nextLine();
  135.         }
  136.         catch(NoSuchElementException e)
  137.         {
  138.             dialog("Something went wrong", "Run the updater again", 12);
  139.         }
  140.         catch (FileNotFoundException e)
  141.         {
  142.             dialog("Something went wrong", "Try running the updater again", 16);
  143.         }
  144.         final String finalLink=link;
  145.         if(link.equals(current))
  146.         {
  147.             String message="Your version ("+ver+") is up to date";
  148.             dialog("Good to go", message, 0);
  149.         }
  150.         int start=link.indexOf("%20")+3, end=link.indexOf(".zip");
  151.         final String newVer=link.substring(start, end);
  152.         if(newVer.compareTo(ver)<0)
  153.         {
  154.             String message="New: "+newVer+" ver: "+ver+"\n" +
  155.             " link: "+link+"\n" +
  156.             "(If you're seeing this, you screwed up lol)"; //only i can get this
  157.             dialog("Oh hey there", message, 0);
  158.         }
  159.         long filesize=getFilesize(link);
  160.         final JFrame f=new JFrame("Woohoo!"); //<=====THIS IS THE PROBLEM
  161.         Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
  162.         f.setLocation((screensize.width-f.getWidth())/3, (screensize.height - f.getHeight()) / 3);
  163.         f.setLayout(new FlowLayout());
  164.         f.setResizable(false);
  165.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  166.         f.setSize(new Dimension(424, 350));
  167.         JPanel changelogPanel = new JPanel(new FlowLayout());
  168.         JTextPane changelogPane = new JTextPane();
  169.         String changelog = getChanges();
  170.         System.out.println(changelog); //this prints fine
  171.         changelogPane.setText(changelog);
  172.         changelogPane.setEditable(false);
  173.         changelogPane.setOpaque(false);
  174.         JScrollPane scroller = new JScrollPane(changelogPane);
  175.         scroller.setPreferredSize(new Dimension(400, 150));
  176.         changelogPanel.add(scroller, "South");
  177.         JButton button1 = new JButton(" Download ("+(filesize/1024)+" KB)");
  178.         button1.addActionListener(new ActionListener()
  179.         {
  180.             public void actionPerformed(ActionEvent e)
  181.             {
  182.                 f.dispose();
  183.                 getNewVer(finalLink);
  184.             }
  185.         });
  186.         JButton button2 = new JButton(" Not now");
  187.         button2.addActionListener(new ActionListener()
  188.         {
  189.             public void actionPerformed(ActionEvent e)
  190.             {
  191.                 System.exit(0);
  192.             }
  193.         });
  194.         f.add(new JLabel("<HTML><center>There's a new version of Grame out!<BR> Get Grame v"+newVer+"? <BR><BR>What's new:</center></HTML>"), "North");
  195.         f.add(changelogPanel, "Center");
  196.         f.add(button2, "South");
  197.         f.add(button1, "South");
  198.         f.setVisible(true);
  199.         while(f.isVisible())
  200.         {
  201.         }
  202.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement