⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainwindow.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * Created on May 29, 2006 2:07:38 PM
 * Copyright (C) 2006 Aelitis, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * AELITIS, SAS au capital de 46,603.30 euros
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
 */
package com.aelitis.azureus.ui.swt.shells.main;

import java.io.File;
import java.lang.reflect.Method;
import java.util.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.*;

import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.download.DownloadManager;
import org.gudy.azureus2.core3.global.GlobalManager;
import org.gudy.azureus2.core3.global.GlobalManagerListener;
import org.gudy.azureus2.core3.global.GlobalMangerProgressListener;
import org.gudy.azureus2.core3.internat.MessageText;
import org.gudy.azureus2.core3.logging.LogEvent;
import org.gudy.azureus2.core3.logging.LogIDs;
import org.gudy.azureus2.core3.logging.Logger;
import org.gudy.azureus2.core3.torrent.TOTorrent;
import org.gudy.azureus2.core3.torrent.TOTorrentException;
import org.gudy.azureus2.core3.util.*;
import org.gudy.azureus2.ui.swt.*;
import org.gudy.azureus2.ui.swt.associations.AssociationChecker;
import org.gudy.azureus2.ui.swt.mainwindow.MainStatusBar;
import org.gudy.azureus2.ui.swt.mainwindow.SWTThread;
import org.gudy.azureus2.ui.swt.mainwindow.SplashWindow;
import org.gudy.azureus2.ui.swt.plugins.UISWTInstance;
import org.gudy.azureus2.ui.swt.pluginsimpl.UISWTInstanceImpl;
import org.gudy.azureus2.ui.swt.shells.MessageBoxShell;
import org.gudy.azureus2.ui.systray.SystemTraySWT;

import com.aelitis.azureus.core.AzureusCore;
import com.aelitis.azureus.core.messenger.*;
import com.aelitis.azureus.core.messenger.config.PlatformConfigMessenger;
import com.aelitis.azureus.core.messenger.config.PlatformRatingMessenger;
import com.aelitis.azureus.core.messenger.config.PlatformRatingMessenger.GetRatingReplyListener;
import com.aelitis.azureus.core.torrent.GlobalRatingUtils;
import com.aelitis.azureus.core.torrent.PlatformTorrentUtils;
import com.aelitis.azureus.ui.UIFunctionsManager;
import com.aelitis.azureus.ui.swt.Initializer;
import com.aelitis.azureus.ui.swt.UIFunctionsSWT;
import com.aelitis.azureus.ui.swt.browser.BrowserContext;
import com.aelitis.azureus.ui.swt.browser.listener.TorrentListener;
import com.aelitis.azureus.ui.swt.search.network.NetworkSearch;
import com.aelitis.azureus.ui.swt.skin.*;
import com.aelitis.azureus.ui.swt.skin.SWTSkinButtonUtility.ButtonListenerAdapter;
import com.aelitis.azureus.ui.swt.utils.*;
import com.aelitis.azureus.ui.swt.views.RecommendationsView;
import com.aelitis.azureus.ui.swt.views.skin.*;
import com.aelitis.azureus.util.Constants;

import org.gudy.azureus2.plugins.PluginEvent;

/**
 * @author TuxPaper
 * @created May 29, 2006
 *
 */
public class MainWindow implements SWTSkinTabSetListener
{

	private static final LogIDs LOGID = LogIDs.GUI;

	protected Shell shell;

	private final Display display;

	private final AzureusCore core;

	private SWTSkin skin;

	private org.gudy.azureus2.ui.swt.mainwindow.MainWindow oldMainWindow;

	private org.gudy.azureus2.ui.swt.mainwindow.MainMenu oldMainMenu;

	private MainMenu menu;

	private UISWTInstanceImpl uiSWTInstanceImpl;

	private UIFunctionsImpl uiFunctions;

	private SystemTraySWT systemTraySWT;

	private Map mapTrackUsage;

	private AEMonitor mapTrackUsage_mon = new AEMonitor("mapTrackUsage");

	private long lCurrentTrackTime = 0;

	private boolean disposedOrDisposing;

	public static void main(String args[]) {
		Initializer.main(new String[0]);
		//org.gudy.azureus2.ui.swt.Main.main(args);
	}

	/**
	 * 
	 */
	public MainWindow(AzureusCore core, Display display, final SplashWindow splash) {
		this.core = core;
		this.display = display;
		disposedOrDisposing = false;

		Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
				createWindow(splash);
				if (splash != null) {
					splash.closeSplash();
				}
			}
		});

		// When a download is added, check for new meta data and
		// un-"wait state" the rating
		// TODO: smart refreshing of meta data ("Refresh On" attribute)
		core.getGlobalManager().addListener(new GlobalManagerListener() {

			public void seedingStatusChanged(boolean seeding_only_mode) {
			}

			public void downloadManagerRemoved(DownloadManager dm) {
			}

			public void downloadManagerAdded(final DownloadManager dm) {
				// must be in a new thread because we don't want to block
				// initilization or any other add listeners
				AEThread thread = new AEThread("v3.mw.dmAdded", true) {
					public void runSupport() {
						downloadAdded(dm);
					}
				};
				thread.start();
			}

			public void destroyed() {
			}

			public void destroyInitiated() {
			}

		});
	}

	private void downloadAdded(DownloadManager dm) {
		final TOTorrent torrent = dm.getTorrent();
		if (torrent == null) {
			return;
		}

		String hash = null;
		try {
			hash = torrent.getHashWrapper().toBase32String();
		} catch (TOTorrentException e) {
			Debug.out(e);
		}

		// Show a popup when user adds a download
		if (skin != null && PlatformTorrentUtils.isContent(torrent)) {
			Utils.execSWTThread(new AERunnable() {
				public void runSupport() {
					SWTSkinTabSet tabSetMain = skin.getTabSet("maintabs");
					if (tabSetMain != null
							&& !tabSetMain.getActiveTab().getSkinObjectID().equals(
									"maintabs.home")) {
						Display current = Display.getCurrent();
						// checking focusControl for null doesn't really work
						// Preferably, we'd check to see if the app has the OS' focus
						// and not display the popup when it doesn't
						if (current != null && current.getFocusControl() != null
								&& !MessageBoxShell.isOpen()) {
							MessageBoxShell.open(
									shell,
									MessageText.getString("HomeReminder.title"),
									MessageText.getString(
											"HomeReminder.text",
											new String[] { PlatformTorrentUtils.getContentTitle(torrent)
											}), new String[] { MessageText.getString("Button.ok")
									}, 0, "downloadinhome",
									MessageText.getString("MessageBoxWindow.nomoreprompting"),
									false, 15000);
						}
					}
				}
			});
		}
		final String fHash = hash;

		if (PlatformTorrentUtils.getUserRating(torrent) == -2) {
			PlatformTorrentUtils.setUserRating(torrent, -1);
			PlatformRatingMessenger.getUserRating(
					new String[] { PlatformRatingMessenger.RATE_TYPE_CONTENT
					}, new String[] { hash
					}, 5000, new GetRatingReplyListener() {
						public void replyReceived(String replyType,
								PlatformRatingMessenger.GetRatingReply reply) {
							if (replyType.equals(PlatformMessenger.REPLY_RESULT)) {
								long rating = reply.getRatingValue(fHash,
										PlatformRatingMessenger.RATE_TYPE_CONTENT);
								if (rating >= -1) {
									PlatformTorrentUtils.setUserRating(torrent, (int) rating);
								}
							}

						}

						public void messageSent() {
						}
					});
		}

		long now = SystemTime.getCurrentTime();
		long mdRefreshOn = PlatformTorrentUtils.getMetaDataRefreshOn(torrent);
		if (mdRefreshOn < now) {
			PlatformTorrentUtils.log("addDM, update MD NOW");
			PlatformTorrentUtils.updateMetaData(torrent, 5000);
		} else {
			PlatformTorrentUtils.log("addDM, update MD on " + new Date(mdRefreshOn));
			SimpleTimer.addEvent("Update MD", mdRefreshOn, new TimerEventPerformer() {
				public void perform(TimerEvent event) {
					PlatformTorrentUtils.updateMetaData(torrent, 15000);
				}
			});
		}

		long grRefreshOn = GlobalRatingUtils.getRefreshOn(torrent);
		if (grRefreshOn <= now) {
			GlobalRatingUtils.updateFromPlatform(torrent, 5000);
		} else {
			SimpleTimer.addEvent("Update G.Rating", grRefreshOn,
					new TimerEventPerformer() {
						public void perform(TimerEvent event) {
							GlobalRatingUtils.updateFromPlatform(torrent, 15000);
						}
					});
		}
	}

	/**
	 * @param splash 
	 * 
	 */
	protected void createWindow(SplashWindow splash) {
		long startTime = SystemTime.getCurrentTime();

		uiFunctions = new UIFunctionsImpl(this);
		UIFunctionsManager.setUIFunctions(uiFunctions);

		Utils.disposeComposite(shell);

		splash.reportPercent(60);

		// XXX Temporary.  We'll use our own images
		ImageRepository.loadImagesForSplashWindow(display);
		ImageRepository.loadImages(display);

		shell = new Shell(display, SWT.SHELL_TRIM);
		shell.setData("class", this);
		shell.setText("Azureus");
		Utils.setShellIcon(shell);
		Utils.linkShellMetricsToConfig(shell, "window");

		setupUsageTracker();

		splash.reportPercent(70);

		skin = SWTSkinFactory.getInstance();

		initSkinListeners();

		// attach the UI to plugins
		// Must be done before initializing views, since plugins may register
		// table columns and other objects
		uiSWTInstanceImpl = new UISWTInstanceImpl(core);
		uiSWTInstanceImpl.init();

		skin.initialize(shell, "main.shell");

		splash.reportPercent(80);

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

		menu = new MainMenu(skin, shell);

		if (org.gudy.azureus2.core3.util.Constants.isOSX) {
			try {

				Class ehancerClass = Class.forName("org.gudy.azureus2.ui.swt.osx.CarbonUIEnhancer");

				Method method = ehancerClass.getMethod("registerToolbarToggle",
						new Class[] { Shell.class
						});
				method.invoke(null, new Object[] { shell
				});

			} catch (Exception e) {
				Debug.printStackTrace(e);
			}

			Listener toggleListener = new Listener() {
				public void handleEvent(Event event) {
					boolean bVisible = (event.type == SWT.Expand);
					menu.setVisibility("SearchBar.visible", "searchbar", bVisible);
				}
			};
			shell.addListener(SWT.Expand, toggleListener);
			shell.addListener(SWT.Collapse, toggleListener);
		}

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

		splash.reportPercent(90);

		skin.layout();

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

		try {
			Utils.createTorrentDropTarget(shell, true);
		} catch (Throwable e) {
			Logger.log(new LogEvent(LOGID, "Drag and Drop not available", e));
		}

		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				dispose(false, false);
			}
		});

		shell.addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent event) {
				if (systemTraySWT != null
						&& COConfigurationManager.getBooleanParameter("Enable System Tray")
						&& COConfigurationManager.getBooleanParameter("Close To Tray")) {

					minimizeToTray(event);
				} else {
					event.doit = dispose(false, false);
				}
			}

			public void shellIconified(ShellEvent event) {
				if (systemTraySWT != null
						&& COConfigurationManager.getBooleanParameter("Enable System Tray")
						&& COConfigurationManager.getBooleanParameter("Minimize To Tray")) {

					minimizeToTray(event);
				}
			}

		});

		shell.addListener(SWT.Deiconify, new Listener() {
			public void handleEvent(Event e) {
				if (Constants.isOSX
						&& COConfigurationManager.getBooleanParameter("Password enabled")) {
					e.doit = false;
					shell.setVisible(false);
					PasswordWindow.showPasswordWindow(display);
				}
			}
		});

		initWidgets();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -