FileDocCategorySizeDatePackage
Initializer.javaAPI DocAzureus 3.0.3.411018Fri Sep 07 12:58:26 BST 2007com.aelitis.azureus.ui.swt

Initializer

public class Initializer extends Object implements com.aelitis.azureus.ui.IUIIntializer
author
TuxPaper
created
May 29, 2006

Fields Summary
private static StartServer
startServer
private final AzureusCore
core
protected static SplashWindow
splash
private final String[]
args
private com.aelitis.azureus.core.util.CopyOnWriteList
listeners
private AEMonitor
listeners_mon
private int
curPercent
Constructors Summary
public Initializer(AzureusCore core, boolean createSWTThreadAndRun, String[] args)
Main Initializer

param
core
param
args

		this.core = core;
		this.args = args;

		if (createSWTThreadAndRun) {
			try {
				SWTThread.createInstance(this);
			} catch (SWTThreadAlreadyInstanciatedException e) {
				Debug.printStackTrace(e);
			}
		} else {
			Constants.initialize(core);
			PlatformConfigMessenger.login(0);
		}
	
Methods Summary
public voidaddListener(com.aelitis.azureus.ui.InitializerListener listener)

		try {
			listeners_mon.enter();

			listeners.add(listener);
		} finally {

			listeners_mon.exit();
		}
	
public static voidmain(java.lang.String[] args)


	      
		if (System.getProperty("ui.temp") == null) {
			System.setProperty("ui.temp", "az3");
		}

		Main.main(args);
	
public voidremoveListener(com.aelitis.azureus.ui.InitializerListener listener)

		try {
			listeners_mon.enter();

			listeners.remove(listener);
		} finally {

			listeners_mon.exit();
		}
	
private voidreportCurrentTask(java.lang.String currentTaskString)

		try {
			listeners_mon.enter();

			Iterator iter = listeners.iterator();
			while (iter.hasNext()) {
				InitializerListener listener = (InitializerListener) iter.next();
				try {
					listener.reportCurrentTask(currentTaskString);
				} catch (Exception e) {
					// ignore
				}
			}
		} finally {

			listeners_mon.exit();
		}
	
private voidreportCurrentTaskByKey(java.lang.String key)

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

		if (curPercent > percent) {
			return;
		}

		curPercent = percent;
		try {
			listeners_mon.enter();

			Iterator iter = listeners.iterator();
			while (iter.hasNext()) {
				InitializerListener listener = (InitializerListener) iter.next();
				try {
					listener.reportPercent(percent);
				} catch (Exception e) {
					// ignore
				}
			}

			if (percent > 100) {
				listeners.clear();
			}
		} finally {

			listeners_mon.exit();
		}
	
public voidrun()

		// initialise the SWT locale util
		long startTime = SystemTime.getCurrentTime();

		new LocaleUtilSWT(core);

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

		UIConfigDefaultsSWT.initialize();

		UIConfigDefaultsSWTv3.initialize(core);

		ImageRepository.loadImagesForSplashWindow(display);

		ImageRepository.addPath("com/aelitis/azureus/ui/images/azureus.jpg",
				"azureus_splash");

		if (COConfigurationManager.getBooleanParameter("Show Splash")) {
			display.syncExec(new AERunnable() {
				public void runSupport() {
					splash = new SplashWindow(display, Initializer.this);
				}
			});
		}

		System.out.println("Locale Initializing took "
				+ (SystemTime.getCurrentTime() - startTime) + "ms");
		startTime = SystemTime.getCurrentTime();

		core.addListener(new AzureusCoreListener() {
			int fakePercent = 80;

			long startTime = SystemTime.getCurrentTime();

			String sLastTask;

			public void reportCurrentTask(AzureusCoreOperation op, String currentTask) {
				if (op.getOperationType() != AzureusCoreOperation.OP_INITIALISATION) {
					return;
				}

				Initializer.this.reportCurrentTask(currentTask);
				if (fakePercent > 0) {
					fakePercent--;
					Initializer.this.reportPercent(curPercent + 1);
				}

				if (sLastTask != null && !sLastTask.startsWith("Loading Torrent")) {
					long now = SystemTime.getCurrentTime();
					long diff = now - startTime;
					if (diff > 10 && diff < 1000 * 60 * 5) {
						System.out.println("   Core: " + diff + "ms for " + sLastTask);
					}
					startTime = SystemTime.getCurrentTime();
				}
				sLastTask = currentTask;
				//System.out.println(currentTask);
			}

			public void reportPercent(AzureusCoreOperation op, int percent) {
				if (op.getOperationType() != AzureusCoreOperation.OP_INITIALISATION) {
					return;
				}
				if (percent == 100) {
					long now = SystemTime.getCurrentTime();
					long diff = now - startTime;
					if (diff > 10 && diff < 1000 * 60 * 5) {
						System.out.println("   Core: " + diff + "ms for " + sLastTask);
					}
				}
				// TODO Auto-generated method stub
			}

		});

		core.addLifecycleListener(new AzureusCoreLifecycleAdapter() {
			private GlobalManager gm;

			public void componentCreated(AzureusCore core,
					AzureusCoreComponent component) {
				Initializer.this.reportPercent(curPercent + 1);
				if (component instanceof GlobalManager) {
					reportCurrentTaskByKey("splash.initializePlugins");

					gm = (GlobalManager) component;

					InitialisationFunctions.earlyInitialisation(core);
				}
			}

			// @see com.aelitis.azureus.core.AzureusCoreLifecycleAdapter#started(com.aelitis.azureus.core.AzureusCore)
			public void started(AzureusCore core) {
				InitialisationFunctions.lateInitialisation(core);
				if (gm == null) {
					return;
				}

				// Ensure colors initialized
				Colors.getInstance();
				
				Initializer.this.reportPercent(curPercent + 1);
				new UserAlerts(gm);

				reportCurrentTaskByKey("splash.initializeGui");

				Initializer.this.reportPercent(curPercent + 1);
				Cursors.init();

				Initializer.this.reportPercent(curPercent + 1);
				new MainWindow(core, Display.getDefault(), splash);

				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();

				//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 org.gudy.azureus2.ui.swt.mainwindow.Initializer.handleStopRestart(false);
			}

			public boolean restartRequested(final AzureusCore core) {
				return org.gudy.azureus2.ui.swt.mainwindow.Initializer.handleStopRestart(true);
			}

		});

		reportCurrentTaskByKey("splash.initializeCore");

		core.start();

		reportPercent(80);

		System.out.println("Core Initializing took "
				+ (SystemTime.getCurrentTime() - startTime) + "ms");
		startTime = SystemTime.getCurrentTime();

		reportCurrentTaskByKey("splash.initializeUIElements");

		// Ensure colors initialized
		Colors.getInstance();

		reportPercent(curPercent + 1);
		Alerts.init();

		reportPercent(curPercent + 1);
		ProgressWindow.register(core);

		reportPercent(curPercent + 1);
		new SWTNetworkSelection();

		reportPercent(curPercent + 1);
		new AuthenticatorWindow();

		reportPercent(curPercent + 1);
		new CertificateTrustWindow();

		reportPercent(90);

		InstallPluginWizard.register(core, display);

		System.out.println("GUI Initializing took "
				+ (SystemTime.getCurrentTime() - startTime) + "ms");
	
public voidstopIt(boolean isForRestart, boolean isCloseAreadyInProgress)

		if (core != null && !isCloseAreadyInProgress) {

			if (isForRestart) {

				core.checkRestartSupported();
			}
		}

		try {
			if (startServer != null) {
				startServer.stopIt();
			}

			//			Cursors.dispose();

			UIUpdaterFactory.getInstance().stopIt();

			Utils.execSWTThread(new AERunnable() {
				public void runSupport() {
					SWTThread.getInstance().terminate();
				}
			});

		} finally {

			if (core != null && !isCloseAreadyInProgress) {

				try {
					if (isForRestart) {

						core.restart();

					} else {

						long lStopStarted = System.currentTimeMillis();
						System.out.println("core.stop");
						core.stop();
						System.out.println("core.stop done in "
								+ (System.currentTimeMillis() - lStopStarted));
					}
				} catch (Throwable e) {

					// don't let any failure here cause the stop operation to fail

					Debug.out(e);
				}
			}
		}