1 11 package org.eclipse.help.ui.internal.browser.embedded; 12 import org.eclipse.core.runtime.Platform; 13 import org.eclipse.help.browser.IBrowser; 14 import org.eclipse.help.internal.base.BaseHelpSystem; 15 import org.eclipse.osgi.service.environment.Constants; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.widgets.Display; 18 21 public class EmbeddedBrowserAdapter implements IBrowser, IBrowserCloseListener{ 22 private EmbeddedBrowser browser; 23 private UIThread2 secondThread; 25 class UIThread2 extends Thread { 26 27 Display d; 28 29 boolean runEventLoop = true; 30 31 public UIThread2() { 32 super(); 33 setDaemon(true); 34 setName("Help Browser UI"); } 36 37 public void run() { 38 d = new Display(); 39 while (runEventLoop) { 40 if (!d.readAndDispatch()) { 41 d.sleep(); 42 } 43 } 44 d.dispose(); 45 } 46 public Display getDisplay() { 47 while (d == null && isAlive()) { 48 try { 49 sleep(40); 50 } catch (InterruptedException ie) { 51 } 52 } 53 return d; 54 } 55 public void dispose() { 56 runEventLoop = false; 57 } 58 } 59 62 public EmbeddedBrowserAdapter() { 63 } 64 public Display getBrowserDisplay() { 65 boolean useUIThread2 = BaseHelpSystem.getMode() == BaseHelpSystem.MODE_WORKBENCH 66 && Constants.OS_WIN32.equalsIgnoreCase(Platform.getOS()) 67 && !Constants.WS_WPF.equalsIgnoreCase(SWT.getPlatform()) ; 68 if (useUIThread2) { 69 if (secondThread == null) { 70 secondThread = new UIThread2(); 71 secondThread.start(); 72 } 73 return secondThread.getDisplay(); 74 } 75 return Display.getDefault(); 76 } 77 78 public void browserClosed() { 79 browser=null; 80 if(secondThread!=null){ 81 secondThread.dispose(); 82 secondThread = null; 83 } 84 } 85 88 public synchronized void displayURL(final String url) { 89 close(); 90 if (getBrowserDisplay() == Display.getCurrent()) { 91 uiDisplayURL(url); 92 } else { 93 getBrowserDisplay().asyncExec(new Runnable () { 94 public void run() { 95 uiDisplayURL(url); 96 } 97 }); 98 } 99 } 100 105 private void uiDisplayURL(final String url) { 106 getBrowser().displayUrl(url); 107 } 108 109 112 public synchronized void close() { 113 if (getBrowserDisplay() == Display.getCurrent()) { 114 uiClose(); 115 } else { 116 getBrowserDisplay().syncExec(new Runnable () { 117 public void run() { 118 uiClose(); 119 } 120 }); 121 } 122 } 123 126 private void uiClose() { 127 if (browser != null && !browser.isDisposed()){ 128 browser.close(); 129 } 130 if(secondThread!=null){ 131 secondThread.dispose(); 132 secondThread=null; 133 } 134 } 135 138 private EmbeddedBrowser getBrowser() { 139 if (browser == null || browser.isDisposed()) { 140 browser = new EmbeddedBrowser(); 141 browser.addCloseListener(this); 142 } 143 return browser; 144 } 145 148 public boolean isCloseSupported() { 149 return true; 150 } 151 154 public boolean isSetLocationSupported() { 155 return true; 156 } 157 160 public boolean isSetSizeSupported() { 161 return true; 162 } 163 166 public synchronized void setLocation(final int x, final int y) { 167 if (getBrowserDisplay() == Display.getCurrent()) { 168 uiSetLocation(x, y); 169 } else { 170 getBrowserDisplay().asyncExec(new Runnable () { 171 public void run() { 172 uiSetLocation(x, y); 173 } 174 }); 175 } 176 } 177 180 private void uiSetLocation(int x, int y) { 181 getBrowser().setLocation(x, y); 182 } 183 186 public synchronized void setSize(final int width, final int height) { 187 if (getBrowserDisplay() == Display.getCurrent()) { 188 uiSetSize(width, height); 189 } else { 190 getBrowserDisplay().asyncExec(new Runnable () { 191 public void run() { 192 uiSetSize(width, height); 193 } 194 }); 195 } 196 } 197 200 private void uiSetSize(int width, int height) { 201 getBrowser().setSize(width, height); 202 } 203 } 204 | Popular Tags |