FileDocCategorySizeDatePackage
FileDownloadWindow.javaAPI DocAzureus 3.0.3.410029Mon Sep 10 11:08:48 BST 2007org.gudy.azureus2.ui.swt

FileDownloadWindow

public class FileDownloadWindow extends Object implements org.gudy.azureus2.core3.torrentdownloader.TorrentDownloaderCallBackInterface
author
Olivier

Fields Summary
com.aelitis.azureus.core.AzureusCore
azureus_core
Display
display
Shell
shell
ProgressBar
progressBar
Label
status
Button
retry
Button
cancel
org.gudy.azureus2.core3.torrentdownloader.TorrentDownloader
downloader
org.gudy.azureus2.core3.torrentdownloader.TorrentDownloaderCallBackInterface
listener
Constructors Summary
public FileDownloadWindow(com.aelitis.azureus.core.AzureusCore _azureus_core, Shell parent, String url, String referrer)
Create a file download window. Add torrent when done downloading

param
_azureus_core
param
parent
param
url
param
referrer

  	this(_azureus_core, parent, url, referrer, null);
	
public FileDownloadWindow(com.aelitis.azureus.core.AzureusCore _azureus_core, Shell parent, String url, String referrer, org.gudy.azureus2.core3.torrentdownloader.TorrentDownloaderCallBackInterface listener)
Create a file download window. If no listener is supplied, torrent will be added when download is complete. If a listener is supplied, caller handles it

param
_azureus_core
param
parent
param
url
param
referrer
param
listener

		Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
		  	init(_azureus_core, parent, url, referrer, listener);
			}
		});
	
Methods Summary
public voidTorrentDownloaderEvent(int state, org.gudy.azureus2.core3.torrentdownloader.TorrentDownloader inf)

  	if (listener != null)
  		listener.TorrentDownloaderEvent(state, inf);
    update();    
  
private voidfixupSize()

  	shell.update();
    shell.layout(true, true);
  	Point p = shell.computeSize(600, SWT.DEFAULT );
  	
  	if ( !shell.getSize().equals(p)){
  		
  		p.y += 15;
        shell.setSize( p );
        
        Utils.centreWindow( shell );
        
        shell.layout();
        
  	}
  
private voidinit(com.aelitis.azureus.core.AzureusCore _azureus_core, Shell parent, java.lang.String url, java.lang.String referrer, org.gudy.azureus2.core3.torrentdownloader.TorrentDownloaderCallBackInterface listener)

  	azureus_core	= _azureus_core;
  	
  	this.listener = listener;
  	
    String dirName = null;
    if(COConfigurationManager.getBooleanParameter("Save Torrent Files")) {
      try {
        dirName = COConfigurationManager.getDirectoryParameter("General_sDefaultTorrent_Directory");
      } catch(Exception egnore) {}
    }
    if(dirName == null) {
      DirectoryDialog dd = new DirectoryDialog(parent, SWT.NULL);
      dd.setText(MessageText.getString("fileDownloadWindow.saveTorrentIn"));
      dirName = dd.open();
    }
    if(dirName == null) return;
    
    this.display = parent.getDisplay();
    this.shell = ShellFactory.createShell(parent, SWT.DIALOG_TRIM);
    shell.setText(MessageText.getString("fileDownloadWindow.title"));
    Utils.setShellIcon(shell);

    final FormLayout formLayout = new FormLayout();
    formLayout.marginHeight = 5;
    formLayout.marginWidth = 5;
    formLayout.spacing = 5;
    shell.setLayout(formLayout);    
    FormData data;
    
    
    Label lDownloading = new Label(shell, SWT.NONE);      
    lDownloading.setText(MessageText.getString("fileDownloadWindow.downloading"));
    
    
    Label lLocation = new Label(shell, SWT.WRAP);
    data = new FormData();
    data.top= new FormAttachment(0, 0);
    data.left = new FormAttachment(lDownloading);
    data.right = new FormAttachment(100,0);
    lLocation.setLayoutData(data);
        
    String shortUrl = url;
    	// truncate any url parameters for display. This has the benefit of hiding additional uninteresting
    	// parameters added to urls to control the download process (e.g. "&pause_on_error" for magnet downloads")
    int	amp_pos = shortUrl.indexOf('&");
    if ( amp_pos != -1 ){
    	shortUrl = shortUrl.substring(0,amp_pos+1) + "...";
    }
    lLocation.setText(shortUrl.replaceAll("&", "&&"));
    lLocation.setToolTipText(url.replaceAll("&", "&&" ));
    
    
    progressBar = new ProgressBar(shell, SWT.NONE);
    progressBar.setMinimum(0);
    progressBar.setMaximum(100);
    progressBar.setSelection(0);
    
    data = new FormData();
    data.top = new FormAttachment(lLocation);
    data.left = new FormAttachment(0,0);
    data.right = new FormAttachment(100,0);
    progressBar.setLayoutData(data);
    
    Label lStatus = new Label(shell, SWT.NONE);
    lStatus.setText(MessageText.getString("fileDownloadWindow.status"));
    
    data = new FormData();
    data.top = new FormAttachment(progressBar);
    data.left = new FormAttachment(0,0);
    lStatus.setLayoutData(data);
    
    status = new Label(shell, SWT.WRAP);    
    
    data = new FormData();
    data.top = new FormAttachment(progressBar);
    data.left = new FormAttachment(lStatus);
    data.right = new FormAttachment(100,0);
    status.setLayoutData(data);    
    
    retry = new Button(shell,SWT.PUSH);
        
    retry.setEnabled(false);
    retry.setText(MessageText.getString("fileDownloadWindow.retry"));
    final String _dirName = dirName;
    retry.addListener(SWT.Selection,new Listener() {
      public void handleEvent(Event e) {
        retry.setEnabled(false);
        status.setText("");
        downloader.cancel();       
        downloader = 
        	TorrentDownloaderFactory.create(
        			FileDownloadWindow.this,
					url,
					referrer,
					_dirName);
        downloader.start();
      }
    });        
    
    cancel = new Button(shell, SWT.PUSH);    
    cancel.setText(MessageText.getString("Button.cancel"));
    cancel.addListener(SWT.Selection,new Listener() {
      public void handleEvent(Event e) {
        downloader.cancel();
        if (!shell.isDisposed())
        	shell.dispose();
      }
    });
    
    data = new FormData();
    data.top = new FormAttachment(status);
    data.right = new FormAttachment(cancel);
    data.width = 100;        
    retry.setLayoutData(data);
    
    data = new FormData();
    data.top = new FormAttachment(status);
    data.right = new FormAttachment(100,0);
    data.width = 100;
    cancel.setLayoutData(data);
        
    shell.setDefaultButton( retry );
    
	shell.addListener(SWT.Traverse, new Listener() {
 		
		public void handleEvent(Event e) {
			
			if ( e.character == SWT.ESC){
												
			    downloader.cancel();
			    
	        if (!shell.isDisposed())
	        	shell.dispose();
			}
		}
	});
	
	fixupSize();
	
    shell.open();
  	fixupSize();
    
    downloader = TorrentDownloaderFactory.create(this,url,referrer,dirName);
    downloader.start();
  
private voidupdate()

    if(display != null && ! display.isDisposed()) {
      display.asyncExec(new AERunnable() {
        public void runSupport() {
          int percentDone = downloader.getPercentDone();
          if(progressBar != null && !progressBar.isDisposed()) {
            progressBar.setSelection(percentDone);
          }
          int state = downloader.getDownloadState();
          String stateText;
          switch(state) {
          	case TorrentDownloader.STATE_CANCELLED :
              if (!shell.isDisposed())
              	shell.dispose();
              return;
            case TorrentDownloader.STATE_DOWNLOADING :
              stateText = MessageText.getString("fileDownloadWindow.state_downloading") + ": " + downloader.getStatus();
              break;
            case TorrentDownloader.STATE_ERROR :
            	if (!progressBar.isDisposed()) {
            		progressBar.setSelection(100);
            	}
              stateText = MessageText.getString("fileDownloadWindow.state_error") + downloader.getError();
              break;
            default :
              stateText = "";
          }
          if(status != null && ! status.isDisposed()) {
            status.setText(stateText);
            status.setToolTipText(stateText);
          }
          
          if(state == TorrentDownloader.STATE_FINISHED) {
            //If the Shell has been disposed, then don't process the torrent.
            if(shell != null && ! shell.isDisposed()) {
              shell.dispose();
              if (listener == null)
              	TorrentOpener.openTorrent(downloader.getFile().getAbsolutePath());
            }
          }
   
          if ( !shell.isDisposed()){
          	
          	if(state == TorrentDownloader.STATE_ERROR) {
          		if(! retry.isDisposed())
          			retry.setEnabled(true); 
          	}
            fixupSize();
        	}
        }
      });
    }