KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > browser > embedded > EmbeddedBrowserAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
19  * Web browser.
20  */

21 public class EmbeddedBrowserAdapter implements IBrowser, IBrowserCloseListener{
22     private EmbeddedBrowser browser;
23     // Thread to use in workbench mode on Windows
24
private UIThread2 secondThread;
25     class UIThread2 extends Thread JavaDoc {
26         
27         Display d;
28         
29         boolean runEventLoop = true;
30
31         public UIThread2() {
32             super();
33             setDaemon(true);
34             setName("Help Browser UI"); //$NON-NLS-1$
35
}
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 JavaDoc ie) {
51                 }
52             }
53             return d;
54         }
55         public void dispose() {
56             runEventLoop = false;
57         }
58     }
59     /**
60      * Adapter constructor.
61      */

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     /*
86      * @see IBrowser#displayURL(String)
87      */

88     public synchronized void displayURL(final String JavaDoc url) {
89         close();
90         if (getBrowserDisplay() == Display.getCurrent()) {
91             uiDisplayURL(url);
92         } else {
93             getBrowserDisplay().asyncExec(new Runnable JavaDoc() {
94                 public void run() {
95                     uiDisplayURL(url);
96                 }
97             });
98         }
99     }
100     /**
101      * Must be run on UI thread
102      *
103      * @param url
104      */

105     private void uiDisplayURL(final String JavaDoc url) {
106         getBrowser().displayUrl(url);
107     }
108
109     /*
110      * @see IBrowser#close()
111      */

112     public synchronized void close() {
113         if (getBrowserDisplay() == Display.getCurrent()) {
114             uiClose();
115         } else {
116             getBrowserDisplay().syncExec(new Runnable JavaDoc() {
117                 public void run() {
118                     uiClose();
119                 }
120             });
121         }
122     }
123     /*
124      * Must be run on UI thread
125      */

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     /**
136      *
137      */

138     private EmbeddedBrowser getBrowser() {
139         if (browser == null || browser.isDisposed()) {
140             browser = new EmbeddedBrowser();
141             browser.addCloseListener(this);
142         }
143         return browser;
144     }
145     /*
146      * @see IBrowser#isCloseSupported()
147      */

148     public boolean isCloseSupported() {
149         return true;
150     }
151     /*
152      * @see IBrowser#isSetLocationSupported()
153      */

154     public boolean isSetLocationSupported() {
155         return true;
156     }
157     /*
158      * @see IBrowser#isSetSizeSupported()
159      */

160     public boolean isSetSizeSupported() {
161         return true;
162     }
163     /*
164      * @see IBrowser#setLocation(int, int)
165      */

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 JavaDoc() {
171                 public void run() {
172                     uiSetLocation(x, y);
173                 }
174             });
175         }
176     }
177     /*
178      * Must be run on UI thread
179      */

180     private void uiSetLocation(int x, int y) {
181         getBrowser().setLocation(x, y);
182     }
183     /*
184      * @see IBrowser#setSize(int, int)
185      */

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 JavaDoc() {
191                 public void run() {
192                     uiSetSize(width, height);
193                 }
194             });
195         }
196     }
197     /*
198      * Must be run on UI thread
199      */

200     private void uiSetSize(int width, int height) {
201         getBrowser().setSize(width, height);
202     }
203 }
204
Popular Tags