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

📄 mainstatusbar.java

📁 JAVA多torrent下载程序P2P源码 This is a stripped down version of Manfred Duchrows Programmer s Freind class
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	}

	protected class updateStatusChanger {
		UpdateCheckInstance instance;

		int check_num = 0;

		boolean active;

		protected updateStatusChanger(UpdateCheckInstance _instance) {
			instance = _instance;

			try {
				this_mon.enter();

				update_stack.add(this);

				instance.addListener(new UpdateCheckInstanceListener() {
					public void cancelled(UpdateCheckInstance instance) {
						deactivate();
					}

					public void complete(UpdateCheckInstance instance) {
						deactivate();
					}
				});

				UpdateChecker[] checkers = instance.getCheckers();

				UpdateCheckerListener listener = new UpdateCheckerListener() {
					public void cancelled(UpdateChecker checker) {
						// we don't count a cancellation as progress step
					}

					public void completed(UpdateChecker checker) {
						setNextCheck();
					}

					public void failed(UpdateChecker checker) {
						setNextCheck();
					}

				};
				for (int i = 0; i < checkers.length; i++) {
					checkers[i].addListener(listener);
				}

				activate();

			} finally {

				this_mon.exit();
			}

		}

		protected UpdateCheckInstance getInstance() {
			return (instance);
		}

		private void activate() {
			try {
				this_mon.enter();

				active = true;

				switchStatusToUpdate();

				setNbChecks(instance.getCheckers().length);

			} finally {

				this_mon.exit();
			}
		}

		private void deactivate() {
			try {
				this_mon.enter();

				active = false;

				for (int i = 0; i < update_stack.size(); i++) {

					if (update_stack.get(i) == this) {

						update_stack.remove(i);

						break;
					}
				}
				if (update_stack.size() == 0) {

					switchStatusToText();

				} else {

					((updateStatusChanger) update_stack.get(update_stack.size() - 1))
							.activate();
				}

			} finally {

				this_mon.exit();
			}
		}

		private void setNbChecks(final int nbChecks) {
			if (display != null && !display.isDisposed())
				Utils.execSWTThread(new AERunnable() {
					public void runSupport() {
						if (statusUpdateProgressBar == null
								|| statusUpdateProgressBar.isDisposed())
							return;
						statusUpdateProgressBar.setMinimum(0);
						statusUpdateProgressBar.setMaximum(nbChecks);
						statusUpdateProgressBar.setSelection(check_num);
					}
				});
		}

		private void setNextCheck() {
			if (display != null && !display.isDisposed())
				Utils.execSWTThread(new AERunnable() {
					public void runSupport() {
						if (statusUpdateProgressBar == null
								|| statusUpdateProgressBar.isDisposed())
							return;

						check_num++;

						if (active) {
							statusUpdateProgressBar.setSelection(check_num);
						}
					}
				});
		}

		private void switchStatusToUpdate() {
			if (display != null && !display.isDisposed())
				Utils.execSWTThread(new AERunnable() {
					public void runSupport() {
						if (statusArea == null || statusArea.isDisposed()) {
							return;
						}

						String name = instance.getName();

						if (MessageText.keyExists(name)) {

							name = MessageText.getString(name);
						}

						statusUpdateLabel.setText(name);

						layoutStatusArea.topControl = statusUpdate;
						statusArea.layout();
					}
				});
		}

		private void switchStatusToText() {
			if (display != null && !display.isDisposed())
				Utils.execSWTThread(new AERunnable() {
					public void runSupport() {
						if (statusArea == null || statusArea.isDisposed()) {
							return;
						}
						layoutStatusArea.topControl = statusText;
						statusArea.layout();
					}
				});
		}
	}

	protected void showUpdateProgressWindow() {
		try {
			this_mon.enter();

			UpdateCheckInstance[] instances = new UpdateCheckInstance[update_stack
					.size()];

			for (int i = 0; i < instances.length; i++) {

				instances[i] = ((updateStatusChanger) update_stack.get(i))
						.getInstance();
			}

			UpdateProgressWindow.show(instances, statusBar.getShell());

		} finally {

			this_mon.exit();
		}
	}

	/**
	 */
	public void refreshStatusBar() {
		if (ipBlocked.isDisposed()) {
			return;
		}
		
		// IP Filter Status Section
		IpFilter ip_filter = azureusCore.getIpFilterManager().getIPFilter();

		ipBlocked.setText("IPs: "
				+ numberFormat.format(ip_filter.getNbRanges())
				+ " - "
				+ numberFormat.format(ip_filter.getNbIpsBlockedAndLoggable())
				+ "/"
				+ numberFormat.format(ip_filter.getNbBannedIps())
				+ "/"
				+ numberFormat.format(azureusCore.getIpFilterManager().getBadIps()
						.getNbBadIps()));
		ipBlocked.setToolTipText(MessageText.getString(
				"MainWindow.IPs.tooltip",
				new String[] { DisplayFormatters.formatDateShort(ip_filter.getLastUpdateTime())
				}));

		// SR status section

		long ratio = (1000 * overall_stats.getUploadedBytes() / (overall_stats
				.getDownloadedBytes() + 1));

		int sr_status;

		if (ratio < 500) {

			sr_status = 0;

		} else if (ratio < 900) {

			sr_status = 1;

		} else {

			sr_status = 2;
		}

		if (sr_status != last_sr_status) {

			String imgID;

			switch (sr_status) {
				case 2:
					imgID = "greenled";
					break;

				case 1:
					imgID = "yellowled";
					break;

				default:
					imgID = "redled";
					break;
			}

			srStatus.setImage(ImageRepository.getImage(imgID));

			last_sr_status = sr_status;
		}

		if (ratio != last_sr_ratio) {

			String tooltipID;

			switch (sr_status) {
				case 2:
					tooltipID = "MainWindow.sr.status.tooltip.ok";
					break;

				case 1:
					tooltipID = "MainWindow.sr.status.tooltip.poor";
					break;

				default:
					tooltipID = "MainWindow.sr.status.tooltip.bad";
					break;
			}

			String ratio_str = "";

			String partial = "" + ratio % 1000;

			while (partial.length() < 3) {

				partial = "0" + partial;
			}

			ratio_str = (ratio / 1000) + "." + partial;

			srStatus.setToolTipText(MessageText.getString(tooltipID,
					new String[] { ratio_str }));

			last_sr_ratio = ratio;
		}

		// NAT status Section

		int nat_status = connection_manager.getNATStatus();

		if (lastNATstatus != nat_status) {
			String imgID;
			String tooltipID;
			String statusID;

			switch (nat_status) {
				case ConnectionManager.NAT_UNKNOWN:
					imgID = "grayled";
					tooltipID = "MainWindow.nat.status.tooltip.unknown";
					statusID = "MainWindow.nat.status.unknown";
					break;

				case ConnectionManager.NAT_OK:
					imgID = "greenled";
					tooltipID = "MainWindow.nat.status.tooltip.ok";
					statusID = "MainWindow.nat.status.ok";
					break;

				case ConnectionManager.NAT_PROBABLY_OK:
					imgID = "yellowled";
					tooltipID = "MainWindow.nat.status.tooltip.probok";
					statusID = "MainWindow.nat.status.probok";
					break;

				default:
					imgID = "redled";
					tooltipID = "MainWindow.nat.status.tooltip.bad";
					statusID = "MainWindow.nat.status.bad";
					break;
			}

			natStatus.setImage(ImageRepository.getImage(imgID));
			natStatus.setToolTipText(MessageText.getString(tooltipID));
			natStatus.setText(MessageText.getString(statusID));
			lastNATstatus = nat_status;
		}

		// DHT Status Section
		int dht_status = (dhtPlugin == null) ? DHTPlugin.STATUS_DISABLED 	: dhtPlugin.getStatus();
		long dht_count = -1;
		//boolean	reachable = false;
		if (dht_status == DHTPlugin.STATUS_RUNNING) {
			DHT[] dhts = dhtPlugin.getDHTs();

			//reachable = dhts.length > 0 && dhts[0].getTransport().isReachable();
			
			//if ( reachable ){
				dht_count = dhts[0].getControl().getStats().getEstimatedDHTSize();
			//}
		}

		if (lastDHTstatus != dht_status || lastDHTcount != dht_count) {
			Image img = ImageRepository.getImage("sb_count");
			switch (dht_status) {
				case DHTPlugin.STATUS_RUNNING:
					
					dhtStatus.setToolTipText(MessageText.getString("MainWindow.dht.status.tooltip"));
					dhtStatus.setText(MessageText.getString("MainWindow.dht.status.users").replaceAll("%1", numberFormat.format(dht_count)));
					
					/*
					if ( reachable ){
						dhtStatus.setImage(ImageRepository.getImage("greenled"));
						dhtStatus.setToolTipText(MessageText
								.getString("MainWindow.dht.status.tooltip"));
						dhtStatus.setText(MessageText.getString("MainWindow.dht.status.users").replaceAll("%1", numberFormat.format(dht_count)));
					} else {
						dhtStatus.setImage(ImageRepository.getImage("yellowled"));
						dhtStatus.setToolTipText(MessageText
								.getString("MainWindow.dht.status.unreachabletooltip"));
						dhtStatus.setText(MessageText
								.getString("MainWindow.dht.status.unreachable"));
					}
					*/
					break;

				case DHTPlugin.STATUS_DISABLED:
					//dhtStatus.setImage(ImageRepository.getImage("grayled"));
					dhtStatus.setText(MessageText.getString("MainWindow.dht.status.disabled"));
					break;

				case DHTPlugin.STATUS_INITALISING:
					//dhtStatus.setImage(ImageRepository.getImage("yellowled"));
					dhtStatus.setText(MessageText.getString("MainWindow.dht.status.initializing"));
					break;

				case DHTPlugin.STATUS_FAILED:
					//dhtStatus.setImage(ImageRepository.getImage("redled"));
					dhtStatus.setText(MessageText.getString("MainWindow.dht.status.failed"));
					break;

				default:
					img = null;
					break;
			}

			dhtStatus.setImage(img);
			lastDHTstatus = dht_status;
			lastDHTcount = dht_count;
		}

		// UL/DL Status Sections

		int dl_limit = NetworkManager.getMaxDownloadRateBPS() / 1024;

		GlobalManagerStats stats = globalManager.getStats();

		statusDown.setText((dl_limit == 0 ? "" : "[" + dl_limit + "K] ")
				+ DisplayFormatters.formatDataProtByteCountToKiBEtcPerSec(stats
						.getDataReceiveRate(), stats.getProtocolReceiveRate()));

		boolean auto_up = COConfigurationManager
				.getBooleanParameter(TransferSpeedValidator
						.getActiveAutoUploadParameter(globalManager))
				&& TransferSpeedValidator.isAutoUploadAvailable(azureusCore);

		int ul_limit_norm = NetworkManager.getMaxUploadRateBPSNormal() / 1024;

		String seeding_only;
		if (NetworkManager.isSeedingOnlyUploadRate()) {
			int ul_limit_seed = NetworkManager.getMaxUploadRateBPSSeedingOnly() / 1024;
			if (ul_limit_seed == 0) {
				seeding_only = "+" + Constants.INFINITY_STRING + "K";
			} else {
				int diff = ul_limit_seed - ul_limit_norm;
				seeding_only = (diff >= 0 ? "+" : "") + diff + "K";
			}
		} else {
			seeding_only = "";
		}

		statusUp.setText((ul_limit_norm == 0 ? "" : "[" + ul_limit_norm + "K"
				+ seeding_only + "]")
				+ (auto_up ? "* " : " ")
				+ DisplayFormatters.formatDataProtByteCountToKiBEtcPerSec(stats
						.getDataSendRate(), stats.getProtocolSendRate()));

		// End of Status Sections
		statusBar.layout();
	}

	/**
	 * @param string
	 */
	public void setDebugInfo(String string) {
		if (!statusText.isDisposed())
			statusText.setToolTipText(string);
	}

	/**
	 * CLabel that shrinks to fit text after a specific period of time.
	 * Makes textual changes less jumpy
	 * 
	 * @author TuxPaper
	 * @created Mar 21, 2006
	 *
	 */
	private class CLabelPadding extends CLabel {
		private int lastWidth = 0;

		private long widthSetOn = 0;

		private static final int KEEPWIDTHFOR_MS = 30 * 1000;

		/**
		 * Default Constructor
		 * 
		 * @param parent
		 * @param style
		 */
		public CLabelPadding(Composite parent, int style) {
			super(parent, style | SWT.CENTER);

			GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER
					| GridData.VERTICAL_ALIGN_FILL);
			setLayoutData(gridData);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.swt.custom.CLabel#computeSize(int, int, boolean)
		 */
		public Point computeSize(int wHint, int hHint, boolean changed) {
			if (!isVisible()) {
				return (new Point(0, 0));
			}
			Point pt = super.computeSize(wHint, hHint, changed);
			pt.x += 4;

			long now = System.currentTimeMillis();
			if (lastWidth > pt.x && now - widthSetOn < KEEPWIDTHFOR_MS) {
				pt.x = lastWidth;
			} else {
				if (lastWidth != pt.x)
					lastWidth = pt.x;
				widthSetOn = now;
			}

			return pt;
		}
	}

}

⌨️ 快捷键说明

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