Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Downloader implements Runnable //downloader part
- {
- private URL source;
- private String dest;
- private boolean indeterminate;
- private JFrame frame;
- private JProgressBar prog;
- private String current;
- private String ver;
- public Downloader(URL source, String dest, boolean indeterminate, String current, String ver)
- {
- this.source = source;
- this.dest = dest;
- this.indeterminate = indeterminate;
- this.current=current;
- this.ver=ver;
- }
- public void run()
- {
- long filesize=0L;
- if(!indeterminate)
- {
- filesize=getFilesize(source.toString());
- System.out.println(filesize);
- }
- final long finalsize=filesize;
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- frame=new JFrame("Working...");
- frame.setLayout(new FlowLayout());
- JPanel panel=new JPanel(new FlowLayout());
- prog=new JProgressBar(0, (int)finalsize/1024);
- prog.setIndeterminate(indeterminate);
- panel.add(prog, "South");
- prog.setVisible(true);
- frame.add(panel, "South");
- frame.setSize(300, 100);
- panel.setSize(250, 100);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- prog.setStringPainted(true);
- }
- });
- try
- {
- java.io.BufferedInputStream in = new java.io.BufferedInputStream(source.openStream());
- java.io.FileOutputStream fos = new java.io.FileOutputStream(dest);
- java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
- byte[] data = new byte[1024];
- int x=0, count=0;
- while((x=in.read(data,0,1024))>=0)
- {
- final long pos=fos.getChannel().position();
- if((!indeterminate)&&(count%3==0))
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- prog.setValue((int) (pos/1024));
- prog.setString((pos/1024)+" KB/"+(finalsize/1024)+" KB");
- }
- });
- System.out.println("pos: "+fos.getChannel().position());
- bout.write(data,0,x);
- }
- bout.close();
- in.close();
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- prog.setValue(prog.getMaximum());
- prog.setString(finalsize+" KB/"+finalsize+" KB");
- }
- });
- System.out.println("Done");
- }
- catch(UnknownHostException e)
- {
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- frame.setVisible(false);
- }
- });
- dialog("Can't connect to the internet", "Can't check if there's a new version.\n" +
- "Make sure you're connected to the internet and try again.", 10);
- }
- catch(IOException e)
- {
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- frame.setVisible(false);
- }
- });
- dialog("Complicated error", "Something to do with opening streams", 11);
- }
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- frame.setVisible(false);
- if(indeterminate)
- goOn(current, ver); //if there's a new version
- else
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- String message="Grame is up to date!";
- JOptionPane p=new JOptionPane(message);
- JDialog d =p.createDialog(new JFrame(), "Done!");
- d.setSize(400, 150);
- d.setVisible(true);
- }
- });
- }
- });
- }
- }
- public static void goOn(String current, String ver) //skip down a bit
- {
- String link="";
- try
- {
- link=new Scanner(new FileReader(new File("tmpFile"))).nextLine();
- }
- catch(NoSuchElementException e)
- {
- dialog("Something went wrong", "Run the updater again", 12);
- }
- catch (FileNotFoundException e)
- {
- dialog("Something went wrong", "Try running the updater again", 16);
- }
- final String finalLink=link;
- if(link.equals(current))
- {
- String message="Your version ("+ver+") is up to date";
- dialog("Good to go", message, 0);
- }
- int start=link.indexOf("%20")+3, end=link.indexOf(".zip");
- final String newVer=link.substring(start, end);
- if(newVer.compareTo(ver)<0)
- {
- String message="New: "+newVer+" ver: "+ver+"\n" +
- " link: "+link+"\n" +
- "(If you're seeing this, you screwed up lol)"; //only i can get this
- dialog("Oh hey there", message, 0);
- }
- long filesize=getFilesize(link);
- final JFrame f=new JFrame("Woohoo!"); //<=====THIS IS THE PROBLEM
- Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
- f.setLocation((screensize.width-f.getWidth())/3, (screensize.height - f.getHeight()) / 3);
- f.setLayout(new FlowLayout());
- f.setResizable(false);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setSize(new Dimension(424, 350));
- JPanel changelogPanel = new JPanel(new FlowLayout());
- JTextPane changelogPane = new JTextPane();
- String changelog = getChanges();
- System.out.println(changelog); //this prints fine
- changelogPane.setText(changelog);
- changelogPane.setEditable(false);
- changelogPane.setOpaque(false);
- JScrollPane scroller = new JScrollPane(changelogPane);
- scroller.setPreferredSize(new Dimension(400, 150));
- changelogPanel.add(scroller, "South");
- JButton button1 = new JButton(" Download ("+(filesize/1024)+" KB)");
- button1.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- f.dispose();
- getNewVer(finalLink);
- }
- });
- JButton button2 = new JButton(" Not now");
- button2.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- System.exit(0);
- }
- });
- 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");
- f.add(changelogPanel, "Center");
- f.add(button2, "South");
- f.add(button1, "South");
- f.setVisible(true);
- while(f.isVisible())
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement