FileDocCategorySizeDatePackage
Initializer.javaAPI DocAzureus 3.0.3.412146Fri Sep 07 12:58:28 BST 2007org.gudy.azureus2.ui.swt.mainwindow

Initializer

public class Initializer extends Object implements com.aelitis.azureus.ui.IUIIntializer, AzureusCoreListener
this class initiatize all GUI and Core components which are : 1. The SWT Thread 2. Images 3. The Splash Screen if needed 4. The GlobalManager 5. The Main Window in correct state or the Password window if needed.

Fields Summary
private static final LogIDs
LOGID
private AzureusCore
azureus_core
private org.gudy.azureus2.core3.global.GlobalManager
gm
private StartServer
startServer
private com.aelitis.azureus.core.util.CopyOnWriteList
listeners
private AEMonitor
listeners_mon
private String[]
args
private AESemaphore
semFilterLoader
private int
nbTasks
private int
currentTask
private int
currentPercent
Constructors Summary
public Initializer(AzureusCore _azureus_core, StartServer _server, String[] _args)

  
   
  
  		 		
  		 			
		 				  
     
    azureus_core	= _azureus_core;
    startServer 	= _server;
    args 			= _args;
    
    Thread filterLoaderThread = new AEThread("filter loader", true) {
			public void runSupport() {
				try {
					azureus_core.getIpFilterManager().getIPFilter();
				} finally {
					semFilterLoader.releaseForever();
				}
			}
		};
		filterLoaderThread.setPriority(Thread.MIN_PRIORITY);
		filterLoaderThread.start();

    try {
      SWTThread.createInstance(this);
    } catch(SWTThreadAlreadyInstanciatedException e) {
    	Debug.printStackTrace( e );
    }
  
Methods Summary
public voidaddListener(com.aelitis.azureus.ui.InitializerListener listener)

    try{
    	listeners_mon.enter();
    	
    	listeners.add(listener);
    }finally{
    	
    	listeners_mon.exit();
    }
  
public static booleanhandleStopRestart(boolean restart)

		UIFunctionsSWT functionsSWT = UIFunctionsManagerSWT.getUIFunctionsSWT();
		if (functionsSWT != null) {
			return functionsSWT.dispose(restart, true);
		}

		return false;
	
public static voidmain(java.lang.String[] args)

  	System.err.println("Shouldn't you be starting with org.gudy.azureus2.ui.swt.Main?");
 	AzureusCore		core = AzureusCoreFactory.create();

 	new Initializer( core, null,args);
  
private voidnextTask()

    currentTask++;
    currentPercent = 100 * currentTask / (nbTasks) ;
    //0% done of current task
    reportPercent(0);
  
private intoverallPercent(int taskPercent)

    //System.out.println("ST percent " + currentPercent + " / " + taskPercent + " : " + (currentPercent + (taskPercent / nbTasks)));
    return currentPercent + taskPercent / nbTasks;
  
public voidremoveListener(com.aelitis.azureus.ui.InitializerListener listener)

    try{
    	listeners_mon.enter();
		
    	listeners.remove(listener);
    }finally{
    	
    	listeners_mon.exit();
    }
  
public voidreportCurrentTask(AzureusCoreOperation operation, java.lang.String currentTask)

	  if ( operation.getOperationType() == AzureusCoreOperation.OP_INITIALISATION ){
		  reportCurrentTask( currentTask );
	  }
  
public voidreportCurrentTask(java.lang.String currentTaskString)

     try{
     	listeners_mon.enter();
     
	    Iterator iter = listeners.iterator();
	    while(iter.hasNext()) {
	    	try{
	    		InitializerListener listener = (InitializerListener) iter.next();
	      
	    		listener.reportCurrentTask(currentTaskString);
	    		
	    	}catch( Throwable e ){
	    		
	    		Debug.printStackTrace( e );
	    	}
	    }
    }finally{
    	
    	listeners_mon.exit();
    }
  
private voidreportCurrentTaskByKey(java.lang.String key)

    reportCurrentTask(MessageText.getString(key));
  
public voidreportPercent(AzureusCoreOperation operation, int percent)

	  if ( operation.getOperationType() == AzureusCoreOperation.OP_INITIALISATION ){
		  reportPercent( percent );
	  }
  
public voidreportPercent(int percent)

  	int overallPercent = overallPercent(percent);
    try{
    	listeners_mon.enter();
    
	    Iterator iter = listeners.iterator();
	    while(iter.hasNext()) {
	    	try{
	    		InitializerListener listener = (InitializerListener) iter.next();
	      
	    		listener.reportPercent(overallPercent);
	    		
	    	}catch( Throwable e ){
	    		
	    		Debug.printStackTrace( e );
	    	}
	    }

	    if (overallPercent > 100) {
	    	listeners.clear();
	    }
    }finally{
    	
    	listeners_mon.exit();
    }
  
public voidrun()

  	try{
  		String uiMode = UISwitcherUtil.openSwitcherWindow(false);
			if (uiMode.equals("az3")) {
				try {
					final Class az3Class = Class.forName("com.aelitis.azureus.ui.swt.Initializer");

					final Constructor constructor = az3Class.getConstructor(new Class[] {
						AzureusCore.class,
						Boolean.TYPE,
						String[].class
					});

					IUIIntializer initializer = (IUIIntializer) constructor.newInstance(new Object[] {
						azureus_core,
						new Boolean(false),
						args
					});

					initializer.run();
					return;
				} catch (Throwable t) {
					// use print stack trace because we don't want to introduce logger
					t.printStackTrace();
					// Ignore and use AZ2
				}
			}
			// else AZ2UI
  		
  		
  		// initialise the SWT locale util
	  	
	    new LocaleUtilSWT( azureus_core );

			Display display = SWTThread.getInstance().getDisplay();

			UIConfigDefaultsSWT.initialize();
			
	    //The splash window, if displayed will need some images. 
	    ImageRepository.loadImagesForSplashWindow(display);
	    
	    //Splash Window is not always shown
	    if (COConfigurationManager.getBooleanParameter("Show Splash", true)) {
	      SplashWindow.create(display,this);
	    }
	    	    
	    setNbTasks(6);
	    
	    nextTask(); 
	    reportCurrentTaskByKey("splash.firstMessageNoI18N");
	    
	    Alerts.init();
	    
	    final ArrayList logEvents = new ArrayList();
	    ILogEventListener logListener = null;
	    if (COConfigurationManager.getBooleanParameter("Open Console", false)) {
	    	logListener = new ILogEventListener() {
					public void log(LogEvent event) {
						logEvents.add(event);
					}
	    	};
	    	Logger.addListener(logListener);
	    }
	    final ILogEventListener finalLogListener = logListener;

	    ProgressWindow.register( azureus_core );
	    
	    new SWTNetworkSelection();
	    
	    new AuthenticatorWindow();
	    
	    new CertificateTrustWindow();

	    InstallPluginWizard.register( azureus_core, display );
	    
	    nextTask(); 	    
	    reportCurrentTaskByKey("splash.loadingImages");
	    
	    ImageRepository.loadImages(display);
	    
	    nextTask();
	    reportCurrentTaskByKey("splash.initializeGM");
	    
	    azureus_core.addListener( this );
	    
	    azureus_core.addLifecycleListener(new AzureusCoreLifecycleAdapter() {
				public void componentCreated(AzureusCore core, AzureusCoreComponent comp) {
					if (comp instanceof GlobalManager) {

						gm = (GlobalManager) comp;

						nextTask();
						reportCurrentTask(MessageText.getString("splash.initializePlugins"));
					}
				}

				public void started(AzureusCore core) {
					if (gm == null)
						return;

					new UserAlerts(gm);

					nextTask();
					IpFilterManager ipFilterManager = azureus_core.getIpFilterManager();
					if (ipFilterManager != null) {
						String s = MessageText.getString("splash.loadIpFilters");
  					do {
  						reportCurrentTask(s);
  						s += ".";
  					} while (!semFilterLoader.reserve(3000));
					}
					
					nextTask();
					reportCurrentTaskByKey("splash.initializeGui");

					new Colors();

					Cursors.init();

					new MainWindow(core, Initializer.this,
							logEvents);

					if (finalLogListener != null)
						Logger.removeListener(finalLogListener);

					nextTask();

					reportCurrentTaskByKey("splash.openViews");

					SWTUpdateChecker.initialize();

					PreUpdateChecker.initialize( core, COConfigurationManager.getStringParameter("ui"));

					UpdateMonitor.getSingleton(core); // setup the update monitor

					//Tell listeners that all is initialized :
					Alerts.initComplete();

					// 7th task: just for triggering a > 100% completion
					nextTask();

					//Finally, open torrents if any.
					for (int i = 0; i < args.length; i++) {

						try {
							TorrentOpener.openTorrent(args[i]);

						} catch (Throwable e) {

							Debug.printStackTrace(e);
						}
					}
				}

				public void stopping(AzureusCore core) {
					Alerts.stopInitiated();
				}

				public void stopped(AzureusCore core) {
				}

				public boolean syncInvokeRequired() {
					return (true);
				}

				public boolean stopRequested(AzureusCore _core)

				throws AzureusCoreException {
					return (handleStopRestart(false));
				}

				public boolean restartRequested(final AzureusCore core) {
					return (handleStopRestart(true));
				}
			});
	    
	    azureus_core.start();

  	}catch( Throwable e ){
  		Logger.log(new LogEvent(LOGID, "Initialization fails:", e));
  	} 
  
private voidsetNbTasks(int _nbTasks)

  
      
    currentTask = 0;
    nbTasks = _nbTasks;
  
public voidstopIt(boolean for_restart, boolean close_already_in_progress)

    if ( azureus_core != null && !close_already_in_progress ){

    	if ( for_restart ){

    		azureus_core.checkRestartSupported();
    	}
    }
    
  	try{
	    if ( startServer != null ){
	    
	    	startServer.stopIt();
	    }
	    
	    Cursors.dispose();
	    
	    SWTThread.getInstance().terminate();  
	    
	}finally{
	  	
	    if ( azureus_core != null && !close_already_in_progress ){

	    	try{
		    	if ( for_restart ){
		    			
		    		azureus_core.restart();
		    			
		    	}else{
		    			
		    		azureus_core.stop();
		    	}
	    	}catch( Throwable e ){
	    		
	    			// don't let any failure here cause the stop operation to fail
	    		
	    		Debug.out( e );
	    	}
	    }
	}