View Javadoc

1   
2   package org.telscenter.pas.ui.browser.mozswing;
3   
4   import java.awt.Component;
5   import java.net.MalformedURLException;
6   import java.net.URL;
7   import java.util.Vector;
8   
9   import org.mozilla.browser.IMozillaWindow;
10  import org.mozilla.browser.MozillaPanel;
11  import org.telscenter.pas.ui.browser.IBrowser;
12  import org.telscenter.pas.ui.browser.IBrowserListener;
13  import org.telscenter.pas.ui.browser.NavigationPolicy;
14  
15  /***
16   * The Mozilla swing browser implementation
17   * 
18   * @author aperritano
19   */
20  public class MozSwingBrowser implements IBrowser {
21  
22  	private MozillaPanel webBrowser ;
23  	private String browserType;
24  	protected String pageTitle;
25  	
26  	protected Vector listeners = new Vector();
27  	
28  	/***
29  	 * Contructor
30  	 * 
31  	 * @param webBrowser - webBrowser window
32  	 * @param browserType - the type of browser it is
33  	 */
34  	public MozSwingBrowser(MozillaPanel webBrowser,String browserType) {
35  		this.browserType = browserType;
36  		this.webBrowser = webBrowser;
37  		initListeners();
38  	}
39  	
40  	/***
41  	 * Starts the listeners
42  	 */
43  	protected void initListeners() {
44  //		this.webBrowser.addWebBrowserListener(new WebBrowserListener() {
45  //
46  //			public void documentCompleted(WebBrowserEvent event) {
47  //			}
48  //
49  //			public void downloadCompleted(WebBrowserEvent event) {				
50  //				BrowserEvent myEvent = new BrowserEvent(MozSwingBrowser.this, -1, null);
51  //				
52  //				// go backwards incase a listener removes itself
53  //				for(int i=listeners.size()-1; i>=0; i--){
54  //					((IBrowserListener) listeners.get(i)).downloadCompleted(myEvent);
55  //				}
56  //			}
57  //
58  //			public void downloadError(WebBrowserEvent event) {				
59  //			}
60  //
61  //			public void downloadProgress(WebBrowserEvent event) {
62  //			}
63  //
64  //			public void downloadStarted(WebBrowserEvent event) {
65  //			}
66  //
67  //			public void initializationCompleted(WebBrowserEvent event) {
68  //			}
69  //
70  //			public void statusTextChange(WebBrowserEvent event) {				
71  //			}
72  //
73  //			public void titleChange(WebBrowserEvent event) {
74  //				pageTitle = event.getData();
75  //			}});
76  		
77  	}
78  
79  	/***
80  	 * to the back
81  	 */
82  	public void back() {
83  		webBrowser.goBack();
84  	}
85  
86  	/***
87  	 * to the front
88  	 */
89  	public void forward() {
90  		webBrowser.goForward();
91  	}
92  
93  	/***
94  	 * is it ie, mozilla?
95  	 * 
96  	 * @return browserType - the type of browser
97  	 */
98  	public String getBrowserType() {
99  		return browserType;
100 	}
101 
102 	/***
103 	 * Gets the browser component
104 	 * 
105 	 * @return Component - the actucal componet to add to a layout
106 	 */
107 	public Component getComponent() {
108 		return (Component) webBrowser;
109 	}
110 
111 
112 	/***
113 	 * Gets the current url
114 	 * 
115 	 * @return URL - the current url
116 	 */
117 	public URL getUrl() {
118 		String url = webBrowser.getUrl();
119 		try {
120 			return new URL(url);
121 		} catch (MalformedURLException e) {
122 			e.printStackTrace();
123 		}
124 		return null;
125 	}
126 
127 	/***
128 	 * reloads the page
129 	 */
130 	public void refresh() {
131 		webBrowser.reload();
132 	}
133 
134 	/***
135 	 * Gets the html of the current page
136 	 * 
137 	 * @String - current html
138 	 */
139 	public String getContent() {
140 		return "not content";
141 		//return webBrowser.getContent();
142 	}
143 	
144 	/***
145 	 * Navigation policy
146 	 * 
147 	 * @return policy - the navigation policy for this browser
148 	 */
149 	public void setNavigationPolicy(NavigationPolicy policy)
150 			throws UnsupportedOperationException {
151 		throw new UnsupportedOperationException("MozSwing Browser does not yet support navigation policy");
152 	}
153 
154 	
155 	/***
156 	 * This method will add a monitor thread which will stop the browser after a cetain amount 
157 	 * of time.
158 	 */
159 	public void setUrl(URL url) {
160 		webBrowser.load(url.toString());
161 	}
162 
163 
164 	/***
165 	 * stop the current page loading 
166 	 */
167 	public void stop() {
168 		webBrowser.stop();
169 	}
170 
171 	/***
172 	 * Load a url with some postData
173 	 * 
174 	 * @param url - the url to load
175 	 * @param postData - data associated with the POST
176 	 */
177 	public void setUrl(URL url, String postData) {
178 		webBrowser.load(url.toString());
179 	}
180 
181 	/***
182 	 * Get the current page titl
183 	 * 
184 	 * @return String = page title
185 	 */
186 	public String getPageTitle() {
187 		return "no title for now";
188 	}
189 
190 	/***
191 	 * Adds a listener
192 	 * 
193 	 * @param listener - listener to add
194 	 */
195 	public void addBrowserListener(IBrowserListener listener) {
196 		listeners.add(listener);
197 	}
198 	
199 	/***
200 	 * removes a listener
201 	 * 
202 	 * @param listener - listener to remove
203 	 */
204 	public void removeBrowserListener(IBrowserListener listener){
205 		listeners.remove(listener);
206 	}
207 	
208 //	class CompleteMonitor extends Thread
209 //		implements WebBrowserListener {
210 //
211 //		boolean downloadCompleted = false;
212 //
213 //		public void run() {
214 //			try {
215 //				sleep(3000);
216 //			} catch (InterruptedException e) {
217 //				// TODO Auto-generated catch block
218 //				e.printStackTrace();
219 //			}
220 //			if(!downloadCompleted){
221 //				webBrowser.stop();
222 //				webBrowser.refresh();
223 //			}
224 //		}
225 //
226 //		// The webit implementation of jdic doesn't throw a
227 //		// document completed event
228 //		public void documentCompleted(WebBrowserEvent event) {}
229 //
230 //		public void downloadCompleted(WebBrowserEvent event) {
231 //			downloadCompleted = true;
232 //			webBrowser.removeWebBrowserListener(this);			
233 //		}
234 //		public void downloadError(WebBrowserEvent event) {}
235 //		public void downloadProgress(WebBrowserEvent event) {}
236 //		public void downloadStarted(WebBrowserEvent event) {}
237 //		public void initializationCompleted(WebBrowserEvent event) {}
238 //		public void statusTextChange(WebBrowserEvent event) {}
239 //		public void titleChange(WebBrowserEvent event) {}
240 //
241 //	}
242 
243 	/***
244 	 * Sets html as the current page
245 	 * 
246 	 * @param content - html to set
247 	 */
248 	public void setContent(String content) {
249 		webBrowser.loadHTML(content);
250 		
251 	}
252 }