FileDocCategorySizeDatePackage
UpdateWindow.javaAPI DocAzureus 3.0.3.419930Fri Sep 21 14:13:58 BST 2007org.gudy.azureus2.ui.swt.update

UpdateWindow

public class UpdateWindow extends Object implements org.gudy.azureus2.plugins.utils.resourcedownloader.ResourceDownloaderListener
author
Olivier Chalouhi

Fields Summary
private UpdateMonitor
update_monitor
private org.gudy.azureus2.plugins.update.UpdateCheckInstance
check_instance
private int
check_type
Display
display
Shell
updateWindow
Table
table
org.gudy.azureus2.ui.swt.components.LinkArea
link_area
ProgressBar
progress
Label
status
Button
btnOk
Listener
lOk
Button
btnCancel
Listener
lCancel
boolean
hasMandatoryUpdates
boolean
restartRequired
private long
totalDownloadSize
private List
downloaders
private Iterator
iterDownloaders
private org.eclipse.swt.browser.Browser
browser
private static final int
COL_NAME
private static final int
COL_VERSION
private static final int
COL_SIZE
private Map
downloadersToData
Constructors Summary
public UpdateWindow(UpdateMonitor _update_monitor, com.aelitis.azureus.core.AzureusCore _azureus_core, org.gudy.azureus2.plugins.update.UpdateCheckInstance _check_instance)

  
  
   
  
			
  				
  		 
  
	update_monitor	= _update_monitor;
  	check_instance 	= _check_instance;
  	
  	check_type = check_instance.getType();
  	
  	check_instance.addDecisionListener(
	  		new UpdateManagerDecisionListener()
	  		{
	  			public Object
	  			decide(
	  				Update		update,
	  				int			decision_type,
	  				String		decision_name,
	  				String		decision_description,
	  				Object		decision_data )
	  			{
	  				if ( decision_type == UpdateManagerDecisionListener.DT_STRING_ARRAY_TO_STRING ){
	  					
	  					String[]	options = (String[])decision_data;
  					
	  					Shell	shell = updateWindow;
	  					
	  					if ( shell == null ){
	  						
	  						Debug.out( "Shell doesn't exist" );
	  						
	  						return( null );
	  					}
	  					
	  					StringListChooser chooser = new StringListChooser( shell );
	  					
	  					chooser.setTitle( decision_name );
	  					chooser.setText( decision_description );
	  					
	  					for (int i=0;i<options.length;i++){
	  						
	  						chooser.addOption( options[i] );
	  					}
	  					
	  					String	result = chooser.open();
	  					
	  					return( result );
	  				}
	  				
	  				return( null );
	  			}
	  		});
  	
    this.updateWindow = null;
    this.display = SWTThread.getInstance().getDisplay();
    
    Utils.execSWTThreadWithBool("UpdateWindow", new AERunnableBoolean() {
			public boolean runSupport() {
				buildWindow();
				return true;
			}
		});
  
Methods Summary
public voidaddUpdate(org.gudy.azureus2.plugins.update.Update update)

    if(display == null || display.isDisposed())
      return;
  
    if ( update.isMandatory()){
    	
    	hasMandatoryUpdates = true;
    }
    
    display.asyncExec(new AERunnable() {
      public void runSupport() {
        if(table == null || table.isDisposed())
          return;
        
        final TableItem item = new TableItem(table,SWT.NULL);
        item.setData(update);
        item.setText(COL_NAME,update.getName()==null?"Unknown":update.getName());  
        item.setText(COL_VERSION,update.getNewVersion()==null?"Unknown":update.getNewVersion());
        ResourceDownloader[] rds = update.getDownloaders();
        long totalLength = 0;
        for(int i = 0 ; i < rds.length ; i++) {
          try {
            totalLength += rds[i].getSize();
          } catch(Exception e) {
          }
        }                
        
        item.setText(COL_SIZE,DisplayFormatters.formatByteCountToBase10KBEtc(totalLength));                
        
        item.setChecked(true);
        
        	// select first entry
        
        if ( table.getItemCount() == 1 ){
        	
        	table.select(0);
        	
        	rowSelected();	// don't seem to be getting the selection event, do it explicitly
        }
        
        checkRestartNeeded();
        
        if( 	COConfigurationManager.getBooleanParameter("update.opendialog")|| 
        		check_instance.getType() != UpdateCheckInstance.UCI_UPDATE ) {
        	
        	show();
        	
        }else{
        	
        	if (MainWindow.getWindow() != null) {
        		MainWindow.getWindow().setUpdateNeeded(UpdateWindow.this);
        	}
        }
      }
    });
  
private voidappendDetails(java.lang.String text)

    if(display == null || display.isDisposed())
      return;
    
    display.asyncExec(new AERunnable(){
      public void runSupport() {
    	  link_area.addLine( text );
      }
    });  
  
public voidbuildWindow()

    if(display == null || display.isDisposed())
      return;

    Utils.waitForModals();
    
    //Do not use ~SWT.CLOSE cause on some linux/GTK platform it
    //forces the window to be only 200x200
    //catch close event instead, and never do it
		updateWindow = ShellFactory.createMainShell(SWT.DIALOG_TRIM | SWT.RESIZE);
    
    updateWindow.addListener(SWT.Close,new Listener() {
      public void handleEvent(Event e) {
        dispose();
      }
    });
    
    Utils.setShellIcon(updateWindow);
    
    String	res_prefix = "swt.";
    
    if ( check_type == UpdateCheckInstance.UCI_INSTALL ){
    	
    	res_prefix += "install.window";
    	
    }else if (check_type == UpdateCheckInstance.UCI_UNINSTALL ){
    	
    	res_prefix += "uninstall.window";
    	
    }else{
    	
    	res_prefix = "UpdateWindow";
    }
    
    Messages.setLanguageText(updateWindow, res_prefix + ".title");
    
    FormLayout layout = new FormLayout();
    try {
      layout.spacing = 5;
    } catch (NoSuchFieldError e) { /* Pre SWT 3.0 */ }
    layout.marginHeight = 10;
    layout.marginWidth = 10;
    FormData formData;
    updateWindow.setLayout(layout);
    
    Label lHeaderText = new Label(updateWindow,SWT.WRAP);
    Messages.setLanguageText(lHeaderText,res_prefix + ".header");
    formData = new FormData();
    formData.left = new FormAttachment(0,0);
    formData.right = new FormAttachment(100,0);
    formData.top = new FormAttachment(0,0);
    lHeaderText.setLayoutData(formData);
    
    SashForm sash = new SashForm(updateWindow,SWT.VERTICAL);
       
    table = new Table(sash,SWT.CHECK | SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
    String[] names = {"name" , "version" , "size"};
    int[] sizes = {350,100,100};
    for(int i = 0 ; i < names.length ; i++) {
      TableColumn column = new TableColumn(table, i == 0 ? SWT.LEFT : SWT.RIGHT);
      Messages.setLanguageText(column,"UpdateWindow.columns." + names[i]);
      column.setWidth(sizes[i]);
    }
    table.setHeaderVisible(true);    

    table.addListener(SWT.Selection,new Listener() {
      public void handleEvent(Event e) {
      	rowSelected();
      }
    });
    
    Composite cInfoArea = new Composite(sash, SWT.NONE);
    cInfoArea.setLayout(new FormLayout());
    
    link_area = new LinkArea(cInfoArea);
    FormData fd = new FormData();
    fd.top = new FormAttachment(0, 0);
    fd.bottom = new FormAttachment(100, 0);
    fd.right = new FormAttachment(100, 0);
    fd.left = new FormAttachment(0, 0);
    link_area.getComponent().setLayoutData(fd);
    
    try {
    	browser = new Browser(cInfoArea, SWT.BORDER);
      fd = new FormData();
      fd.top = new FormAttachment(0, 0);
      fd.bottom = new FormAttachment(100, 0);
      fd.right = new FormAttachment(100, 0);
      fd.left = new FormAttachment(0, 0);
      browser.setLayoutData(fd);
    } catch (Throwable t) {
    }

    progress = new ProgressBar(updateWindow,SWT.NULL);
    progress.setMinimum(0);
    progress.setMaximum(100);
    progress.setSelection(0);
    
    status = new Label(updateWindow,SWT.NULL);
    
 
    Composite cButtons = new Composite(updateWindow, SWT.NONE);
		FillLayout fl = new FillLayout(SWT.HORIZONTAL);
		fl.spacing = 3;
		cButtons.setLayout(fl);
    
    btnOk = new Button(cButtons,SWT.PUSH);
    Messages.setLanguageText(btnOk,res_prefix + ".ok" );
    
    updateWindow.setDefaultButton( btnOk );
    lOk = new Listener() {
      public void handleEvent(Event e) {
        update();
      }
    };
    
    btnOk.addListener(SWT.Selection, lOk);
    btnOk.setEnabled( false );
    
    btnCancel = new Button(cButtons,SWT.PUSH);
    
    Messages.setLanguageText(btnCancel,"UpdateWindow.cancel");
    
    lCancel = new Listener() {
	      public void handleEvent(Event e) {
	        dispose();
	       	check_instance.cancel();
	      }
	   };
    btnCancel.addListener(SWT.Selection,lCancel);
    
    updateWindow.addListener(SWT.Traverse, new Listener() {	
		public void handleEvent(Event e) {
			if ( e.character == SWT.ESC){
			      dispose();
			      check_instance.cancel();			
			 }
		}
    });
    
    formData = new FormData();
    formData.left = new FormAttachment(0,0);
    formData.right = new FormAttachment(100,0);
    formData.top = new FormAttachment(lHeaderText);
    formData.bottom = new FormAttachment(progress);
    sash.setLayoutData(formData);
    
    formData = new FormData();
    formData.left = new FormAttachment(0,0);
    formData.right = new FormAttachment(100,0);
    formData.bottom = new FormAttachment(status);
    progress.setLayoutData(formData);
    
    formData = new FormData();
    formData.left = new FormAttachment(0,0);
    formData.right = new FormAttachment(100,0);
    formData.bottom = new FormAttachment(cButtons);
    status.setLayoutData(formData);
    
    formData = new FormData();
    formData.right = new FormAttachment(100,0);
    formData.bottom = new FormAttachment(100,0);
    cButtons.setLayoutData(formData);
    
    sash.setWeights(new int[] { 25, 75 });
    
    updateWindow.setSize(600,450);
    //updateWindow.open();
  
private voidcheckMandatory()

    TableItem[] items = table.getItems();
    for(int i = 0 ; i < items.length ; i++) {
      Update update = (Update) items[i].getData();
      if(update.isMandatory()) items[i].setChecked(true);
    }
  
private voidcheckRestartNeeded()

  
    restartRequired = false;
    boolean	restartMaybeRequired = false;
    TableItem[] items = table.getItems();
    for(int i = 0 ; i < items.length ; i++) {
      if(! items[i].getChecked()) continue;
      Update update = (Update) items[i].getData();
      int required = update.getRestartRequired();
      if((required == Update.RESTART_REQUIRED_MAYBE)){
      	restartMaybeRequired = true;
      }else if ( required == Update.RESTART_REQUIRED_YES ){
      	restartRequired = true;
      }
    }
    if(restartRequired) {
        status.setText(MessageText.getString("UpdateWindow.status.restartNeeded"));
    }else if(restartMaybeRequired) {
        status.setText(MessageText.getString("UpdateWindow.status.restartMaybeNeeded"));
    }else{
      status.setText("");
    }
  
public booleancompleted(org.gudy.azureus2.plugins.utils.resourcedownloader.ResourceDownloader downloader, java.io.InputStream data)

    downloadersToData.put(downloader,data);
    downloader.removeListener(this);
    setProgressSelection(0);
    nextUpdate();
    return true;
  
public voiddispose()

  	Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
		    updateWindow.dispose();
		    MainWindow window = MainWindow.getWindow();
		    if (window != null) {
		    	MainWindow.getWindow().setUpdateNeeded(null);
		    }
			}
		});
  
public voidfailed(org.gudy.azureus2.plugins.utils.resourcedownloader.ResourceDownloader downloader, org.gudy.azureus2.plugins.utils.resourcedownloader.ResourceDownloaderException e)

    downloader.removeListener(this);
    setStatusText(MessageText.getString("UpdateWindow.status.failed"));
    
    String	msg = downloader.getName() + " : " + e;
    
    if ( e.getCause() != null ){
    	
    	msg += " [" + e.getCause() + "]";
    }
    
    appendDetails(msg);
  
private voidfinishUpdate(boolean restartNow)

    //When completing, remove the link in mainWindow :
  	if (MainWindow.getWindow() != null) {
  		MainWindow.getWindow().setUpdateNeeded(null);
  	}
    
  	boolean bDisposeUpdateWindow = true;
    //If restart is required, then restart
    if( restartRequired && restartNow) {
    	// this HAS to be done this way around else the restart inherits
    	// the 6880 port listen. However, this is a general problem....
    	UIFunctionsSWT uiFunctions = UIFunctionsManagerSWT.getUIFunctionsSWT();
    	if (uiFunctions != null && uiFunctions.dispose(true, false)) {
   			bDisposeUpdateWindow = false;
			}
    }else if ( hasMandatoryUpdates && !restartRequired ){
    	
    		// run a further update check as we can immediately install non-mandatory updates now
    	
    	update_monitor.requestRecheck();
    }

    if (bDisposeUpdateWindow) {
      updateWindow.dispose();
    }
  
public ShellgetShell()

	  return( updateWindow );
  
protected booleanisDisposed()

  	return( display == null || display.isDisposed() || updateWindow == null || updateWindow.isDisposed());
  
private voidnextUpdate()

    if(iterDownloaders.hasNext()) {
      ResourceDownloader downloader = (ResourceDownloader) iterDownloaders.next();
      downloader.addListener(this);
      downloader.asyncDownload();
    } else {
      switchToRestart();      
    }
  
public voidreportActivity(org.gudy.azureus2.plugins.utils.resourcedownloader.ResourceDownloader downloader, java.lang.String activity)

    setStatusText(activity.trim());
    appendDetails(activity);
  
public voidreportAmountComplete(org.gudy.azureus2.plugins.utils.resourcedownloader.ResourceDownloader downloader, long amount)

  
  
public voidreportPercentComplete(org.gudy.azureus2.plugins.utils.resourcedownloader.ResourceDownloader downloader, int percentage)

    setProgressSelection(percentage);   
  
protected voidrowSelected()

    checkMandatory();
    checkRestartNeeded();
    TableItem[] items = table.getSelection();
    if(items.length == 0) return;
    Update update = (Update) items[0].getData();
    
    String desciptionURL = update.getDesciptionURL();
    if (desciptionURL != null && browser != null) {
    	browser.setUrl(desciptionURL);
    	browser.setVisible(true);
    	link_area.getComponent().setVisible(false);
    } else {
    	if (browser != null) {
    		browser.setVisible(false);
    	}
    	link_area.getComponent().setVisible(true);
    
      String[] descriptions = update.getDescription();
      
      link_area.reset();
      
      link_area.setRelativeURLBase( update.getRelativeURLBase());
      
      for(int i = 0 ; i < descriptions.length ; i++) {
  
      	link_area.addLine( descriptions[i] );
      }
    }
  
private voidsetProgressSelection(int percent)

    if(display == null || display.isDisposed())
      return;
    
    display.asyncExec(new AERunnable() {
      public void runSupport() {
        if(progress != null && !progress.isDisposed())
        progress.setSelection(percent);
      }
    });
  
private voidsetStatusText(java.lang.String text)

    if(display == null || display.isDisposed())
      return;
    
    display.asyncExec(new AERunnable(){
      public void runSupport() {
        if(status != null && !status.isDisposed())
          status.setText(text);
      }
    });  
  
public voidshow()

    if(updateWindow == null || updateWindow.isDisposed()) {
      return;
    }
    
    Utils.centreWindow( updateWindow );
    updateWindow.open();
    updateWindow.forceActive();       
  
private voidswitchToRestart()

    if(display == null || display.isDisposed())
      return;
    
    display.asyncExec(new AERunnable(){
      public void runSupport() {
      	checkRestartNeeded();	// gotta recheck coz a maybe might have got to yes
        progress.setSelection(100);
        status.setText(MessageText.getString("UpdateWindow.status.done"));
        btnOk.removeListener(SWT.Selection,lOk);
        btnOk.setEnabled(true);
        btnOk.addListener(SWT.Selection,new Listener() {
          public void handleEvent(Event e) {
            finishUpdate(true);
          }
        });
        if(restartRequired) {
          Messages.setLanguageText(btnOk,"UpdateWindow.restart");
          btnCancel.removeListener(SWT.Selection,lCancel);
          Messages.setLanguageText(btnCancel,"UpdateWindow.restartLater");
          btnCancel.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
              finishUpdate(false);
            }
          });
          updateWindow.layout();
        } else {
          Messages.setLanguageText(btnOk,"UpdateWindow.close");
          btnCancel.setEnabled(false);
          updateWindow.layout();
        }
      }
    });
  
private voidupdate()

    btnOk.setEnabled(false);    
    Messages.setLanguageText(btnCancel,"UpdateWindow.cancel");
    table.setEnabled(false);
    
    link_area.reset();
    if (browser != null) {
    	browser.setVisible(false);
    }
  	link_area.getComponent().setVisible(true);
    
    TableItem[] items = table.getItems();
    
    totalDownloadSize = 0;   
    downloaders = new ArrayList();
    
    for(int i = 0 ; i < items.length ; i++) {
      if(! items[i].getChecked()) continue;
      
      Update update = (Update) items[i].getData();
      ResourceDownloader[] rds = update.getDownloaders();
      for(int j = 0 ; j < rds.length ; j++) {
        downloaders.add(rds[j]);        
        try {
          totalDownloadSize += rds[j].getSize();
        } catch (Exception e) {
          link_area.addLine(MessageText.getString("UpdateWindow.no_size") + rds[j].getName());
        }        
      }
    }
    downloadersToData = new HashMap();
    iterDownloaders = downloaders.iterator();
    nextUpdate();
  
protected voidupdateAdditionComplete()

    if(display == null || display.isDisposed())
        return;
    
      display.asyncExec(new AERunnable() {
        public void runSupport() {
          if(btnOk == null || btnOk.isDisposed())
            return;
          
          btnOk.setEnabled(true);
        }
      });