FileDocCategorySizeDatePackage
DisplayListener.javaAPI DocAzureus 3.0.3.47858Wed Jul 18 10:58:24 BST 2007com.aelitis.azureus.ui.swt.browser.listener

DisplayListener

public class DisplayListener extends com.aelitis.azureus.ui.swt.browser.msg.AbstractMessageListener

Fields Summary
public static final String
DEFAULT_LISTENER_ID
public static final String
OP_COPY_TO_CLIPBOARD
public static final String
OP_OPEN_URL
public static final String
OP_RESET_URL
public static final String
OP_SEND_EMAIL
public static final String
OP_IRC_SUPPORT
public static final String
OP_BRING_TO_FRONT
public static final String
OP_SWITCH_TO_TAB
private org.eclipse.swt.browser.Browser
browser
Constructors Summary
public DisplayListener(String id, org.eclipse.swt.browser.Browser browser)


	     
		super(id);
		this.browser = browser;
	
public DisplayListener(org.eclipse.swt.browser.Browser browser)

		this(DEFAULT_LISTENER_ID, browser);
	
Methods Summary
private voidbringToFront()

		final UIFunctions functions = UIFunctionsManager.getUIFunctions();
		if (functions != null) {
			functions.bringToFront();
		}
	
private voidcopyToClipboard(java.lang.String text)

		if (browser == null || browser.isDisposed()) {
			return;
		}
		final Clipboard cb = new Clipboard(browser.getDisplay());
		TextTransfer textTransfer = TextTransfer.getInstance();
		cb.setContents(new Object[] {
			text
		}, new Transfer[] {
			textTransfer
		});
		cb.dispose();
	
public voidhandleMessage(com.aelitis.azureus.ui.swt.browser.msg.BrowserMessage message)

		String opid = message.getOperationId();

		if (OP_COPY_TO_CLIPBOARD.equals(opid)) {
			Map decodedMap = message.getDecodedMap();
			copyToClipboard(MapUtils.getMapString(decodedMap, "text", ""));
		} else if (OP_OPEN_URL.equals(opid)) {
			Map decodedMap = message.getDecodedMap();
			String target = MapUtils.getMapString(decodedMap, "target", null);
			if (target == null && !decodedMap.containsKey("width")) {
				launchUrl(MapUtils.getMapString(decodedMap, "url", null));
			} else {
				message.setCompleteDelayed(true);
				showBrowser(MapUtils.getMapString(decodedMap, "url", null),
						target, MapUtils.getMapInt(decodedMap, "width", 0),
						MapUtils.getMapInt(decodedMap, "height", 0),
						MapUtils.getMapBoolean(decodedMap, "resizable", false),
						message);
			}
		} else if (OP_RESET_URL.equals(opid)) {
			resetURL();
		} else if (OP_SEND_EMAIL.equals(opid)) {
			Map decodedMap = message.getDecodedMap();

			String to = MapUtils.getMapString(decodedMap, "to", "");
			String subject = MapUtils.getMapString(decodedMap, "subject", "");

			String body = MapUtils.getMapString(decodedMap, "body", null);

			sendEmail(to, subject, body);

		} else if (OP_IRC_SUPPORT.equals(opid)) {
			Map decodedMap = message.getDecodedMap();
			openIrc(null, MapUtils.getMapString(decodedMap, "channel", ""),
					MapUtils.getMapString(decodedMap, "user", ""));
		} else if (OP_BRING_TO_FRONT.equals(opid)) {
			bringToFront();
		} else if (OP_SWITCH_TO_TAB.equals(opid)) {
			Map decodedMap = message.getDecodedMap();
			switchToTab(MapUtils.getMapString(decodedMap, "target", ""));
		} else {
			throw new IllegalArgumentException("Unknown operation: " + opid);
		}
	
private voidlaunchUrl(java.lang.String url)

		if (url.startsWith("http://") || url.startsWith("https://")
				|| url.startsWith("mailto:")) {
			Utils.launch(url);
		}
	
private voidopenIrc(java.lang.String server, java.lang.String channel, java.lang.String alias)

		try {
			PluginManager pluginManager = PluginInitializer.getDefaultInterface().getPluginManager();
			PluginInterface piChat = pluginManager.getPluginInterfaceByID("azplugins");
			UIManager manager = piChat.getUIManager();
			manager.addUIListener(new UIManagerListener() {
				public void UIDetached(UIInstance instance) {
				}

				public void UIAttached(UIInstance instance) {
					if (instance instanceof UISWTInstance) {
						try {
							debug("Opening IRC channel " + channel + " on " + server
									+ " for user " + alias);
							UISWTInstance swtInstance = (UISWTInstance) instance;
							UISWTView[] openViews = swtInstance.getOpenViews(UISWTInstance.VIEW_MAIN);
							for (int i = 0; i < openViews.length; i++) {
								UISWTView view = openViews[i];
								// if only there was a way to tell if it was our IRC
								view.closeView();
							}

							swtInstance.openView(UISWTInstance.VIEW_MAIN, "IRC",
									new String[] {
										server,
										channel,
										alias
									});
						} catch (Exception e) {
							debug("Failure opening IRC channel " + channel + " on " + server,
									e);
						}
					}
				}
			});
		} catch (Exception e) {
			debug("Failure opening IRC channel " + channel + " on " + server, e);
		}
	
private voidresetURL()

		if (browser == null || browser.isDisposed()) {
			return;
		}

		String sURL = (String) browser.getData("StartURL");
		System.out.println("reset " + sURL);
		if (sURL != null && sURL.length() > 0) {
			if (sURL.indexOf('?") > 0) {
				browser.setUrl(sURL + "&rnd=" + Math.random());
			} else {
				browser.setUrl(sURL + "?rnd=" + Math.random());
			}
		}
	
private voidsendEmail(java.lang.String to, java.lang.String subject, java.lang.String body)

		String url = "mailto:" + to + "?subject=" + UrlUtils.encode(subject);

		if (body != null) {
			url = url + "&body=" + UrlUtils.encode(body);
		}
		Utils.launch(url);
	
private voidshowBrowser(java.lang.String url, java.lang.String target, int w, int h, boolean allowResize, com.aelitis.azureus.ui.swt.browser.msg.BrowserMessage message)

		final UIFunctions functions = UIFunctionsManager.getUIFunctions();
		if (functions == null) {
			AEThread thread = new AEThread("show browser " + url) {
				public void runSupport() {
					final Display display = Display.getDefault();
					display.asyncExec(new AERunnable() {
						public void runSupport() {
							BrowserWindow window = new BrowserWindow(
									display.getActiveShell(), url, w, h, allowResize, false);
							window.waitUntilClosed();
							message.complete(false, true, null);
						}
					});
				}
			};
			thread.run();
			return;
		}

		AEThread thread = new AEThread("show browser " + url) {
			public void runSupport() {
				functions.viewURL(url, target, w, h, allowResize, false);
				message.complete(false, true, null);
			}
		};
		thread.run();
	
private voidswitchToTab(java.lang.String tabID)

param
string
since
3.0.0.7

		SWTSkin skin = SWTSkinFactory.getInstance();
		SWTSkinTabSet tabSet = skin.getTabSet(SkinConstants.TABSET_MAIN);
		if (tabSet != null) {
			tabSet.setActiveTab("maintabs." + tabID);
		}