KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > views > BrowserPart


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.views;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.Preferences;
19 import org.eclipse.help.internal.base.BaseHelpSystem;
20 import org.eclipse.help.internal.base.HelpBasePlugin;
21 import org.eclipse.help.internal.base.util.LinkUtil;
22 import org.eclipse.help.internal.util.URLCoder;
23 import org.eclipse.help.ui.internal.HelpUIPlugin;
24 import org.eclipse.help.ui.internal.HelpUIResources;
25 import org.eclipse.help.ui.internal.IHelpUIConstants;
26 import org.eclipse.help.ui.internal.Messages;
27 import org.eclipse.jface.action.Action;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.action.IMenuManager;
30 import org.eclipse.jface.action.IStatusLineManager;
31 import org.eclipse.jface.action.IToolBarManager;
32 import org.eclipse.jface.action.Separator;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.browser.Browser;
35 import org.eclipse.swt.browser.LocationEvent;
36 import org.eclipse.swt.browser.LocationListener;
37 import org.eclipse.swt.browser.OpenWindowListener;
38 import org.eclipse.swt.browser.ProgressEvent;
39 import org.eclipse.swt.browser.ProgressListener;
40 import org.eclipse.swt.browser.StatusTextEvent;
41 import org.eclipse.swt.browser.StatusTextListener;
42 import org.eclipse.swt.browser.WindowEvent;
43 import org.eclipse.swt.custom.BusyIndicator;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.ui.IMemento;
47 import org.eclipse.ui.actions.ActionFactory;
48 import org.eclipse.ui.forms.AbstractFormPart;
49 import org.eclipse.ui.forms.widgets.FormToolkit;
50
51 public class BrowserPart extends AbstractFormPart implements IHelpPart {
52     private final static String JavaDoc QUERY = "BrowserPartQuery:"; //$NON-NLS-1$
53
private final static String JavaDoc HIGHLIGHT_ON = "highlight-on"; //$NON-NLS-1$
54

55     private ReusableHelpPart parent;
56
57     private Browser browser;
58
59     private String JavaDoc id;
60
61     private int lastProgress = -1;
62
63     private String JavaDoc url;
64
65     private Action showExternalAction;
66
67     private Action syncTocAction;
68     
69     private Action highlightAction;
70
71     private Action bookmarkAction;
72
73     private Action printAction;
74
75     private String JavaDoc statusURL;
76
77     private String JavaDoc title;
78
79     public BrowserPart(final Composite parent, FormToolkit toolkit,
80             final IToolBarManager tbm) {
81         browser = new Browser(parent, SWT.NULL);
82         browser.addLocationListener(new LocationListener() {
83             public void changing(LocationEvent event) {
84                 if (redirectLink(event.location))
85                     event.doit = false;
86                 if (!event.doit && event.location != null
87                         && event.location.startsWith("https://")) { //$NON-NLS-1$
88
try {
89                         BaseHelpSystem.getHelpBrowser(true).displayURL(
90                                 event.location);
91                     } catch (Exception JavaDoc exc) {
92                     }
93                 }
94             }
95
96             public void changed(LocationEvent event) {
97                 String JavaDoc url = event.location;
98                 boolean isResult = url.indexOf("resultof")!=-1; //$NON-NLS-1$
99
BrowserPart.this.parent.browserChanged(url);
100                 BrowserPart.this.url = url;
101                 updateSyncTocAction();
102                 BrowserPart.this.highlightAction.setEnabled(isResult);
103             }
104         });
105         browser.addProgressListener(new ProgressListener() {
106             public void changed(ProgressEvent e) {
107                 if (e.current == e.total)
108                     return;
109                 IStatusLineManager slm = BrowserPart.this.parent
110                         .getStatusLineManager();
111                 IProgressMonitor monitor = slm != null ? slm
112                         .getProgressMonitor() : null;
113                 if (lastProgress == -1) {
114                     lastProgress = 0;
115                     if (monitor != null) {
116                         monitor.beginTask("", e.total); //$NON-NLS-1$
117
slm.setCancelEnabled(true);
118                     }
119                 } else if (monitor != null && monitor.isCanceled()) {
120                     browser.stop();
121                     return;
122                 }
123                 if (monitor != null)
124                     monitor.worked(e.current - lastProgress);
125                 lastProgress = e.current;
126             }
127
128             public void completed(ProgressEvent e) {
129                 IStatusLineManager slm = BrowserPart.this.parent
130                         .getStatusLineManager();
131                 IProgressMonitor monitor = slm != null ? slm
132                         .getProgressMonitor() : null;
133                 if (monitor != null) {
134                     slm.setCancelEnabled(false);
135                     monitor.done();
136                 }
137                 lastProgress = -1;
138                 String JavaDoc value = executeQuery("document.title"); //$NON-NLS-1$
139
BrowserPart.this.title = value != null ? value : "N/A"; //$NON-NLS-1$
140
}
141         });
142         browser.addStatusTextListener(new StatusTextListener() {
143             public void changed(StatusTextEvent event) {
144                 if (processQuery(event.text))
145                     return;
146                 IStatusLineManager statusLine = BrowserPart.this.parent
147                         .getStatusLineManager();
148                 if (statusLine != null)
149                     statusLine.setMessage(event.text);
150                 if (event.text.indexOf("://") != -1) //$NON-NLS-1$
151
statusURL = event.text;
152             }
153         });
154         browser.addOpenWindowListener(new OpenWindowListener() {
155             public void open(WindowEvent event) {
156                 if (statusURL != null) {
157                     try {
158                         String JavaDoc relativeURL = BaseHelpSystem.unresolve(new URL JavaDoc(
159                                 statusURL));
160                         if (BrowserPart.this.parent.isHelpResource(relativeURL)) {
161                             BrowserPart.this.parent
162                                     .showExternalURL(relativeURL);
163                             event.required = true;
164                         }
165                     } catch (MalformedURLException JavaDoc e) {
166                         HelpUIPlugin.logError("Malformed URL: " + statusURL, e); //$NON-NLS-1$
167
}
168                 }
169             }
170         });
171         contributeToToolBar(tbm);
172     }
173
174     private String JavaDoc executeQuery(String JavaDoc domValue) {
175         String JavaDoc query = "window.status=\"" + QUERY + "\"+" + domValue + ";"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
176
boolean status = browser.execute(query);
177         if (status) {
178             return (String JavaDoc) browser.getData("query"); //$NON-NLS-1$
179
}
180         return null;
181     }
182
183     private boolean processQuery(String JavaDoc text) {
184         if (text.startsWith(QUERY)) {
185             browser.setData("query", text.substring(QUERY.length())); //$NON-NLS-1$
186
return true;
187         }
188         return false;
189     }
190
191     private void contributeToToolBar(IToolBarManager tbm) {
192         boolean highlight = HelpBasePlugin.getDefault().getPluginPreferences().getBoolean(HIGHLIGHT_ON);
193         showExternalAction = new Action() {
194             public void run() {
195                 BusyIndicator.showWhile(browser.getDisplay(), new Runnable JavaDoc() {
196                     public void run() {
197                         try {
198                             parent.showExternalURL(BaseHelpSystem
199                                     .unresolve(new URL JavaDoc(url)));
200                         } catch (MalformedURLException JavaDoc e) {
201                             HelpUIPlugin.logError("Malformed URL: " + statusURL, e); //$NON-NLS-1$
202
}
203                     }
204                 });
205             }
206         };
207         showExternalAction
208                 .setToolTipText(Messages.BrowserPart_showExternalTooltip);
209         showExternalAction.setImageDescriptor(HelpUIResources
210                 .getImageDescriptor(IHelpUIConstants.IMAGE_NW));
211         syncTocAction = new Action() {
212             public void run() {
213                 doSyncToc();
214             }
215         };
216         syncTocAction.setToolTipText(Messages.BrowserPart_syncTocTooltip);
217         syncTocAction.setImageDescriptor(HelpUIResources
218                 .getImageDescriptor(IHelpUIConstants.IMAGE_SYNC_TOC));
219         syncTocAction.setEnabled(false);
220         bookmarkAction = new Action() {
221             public void run() {
222                 String JavaDoc href = LinkUtil.stripParams(BaseHelpSystem.unresolve(url));
223                 BaseHelpSystem.getBookmarkManager().addBookmark(href, title);
224             }
225         };
226         bookmarkAction.setToolTipText(Messages.BrowserPart_bookmarkTooltip);
227         bookmarkAction.setImageDescriptor(HelpUIResources
228                 .getImageDescriptor(IHelpUIConstants.IMAGE_ADD_BOOKMARK));
229         highlightAction = new Action() {
230             public void run() {
231                 HelpBasePlugin.getDefault().getPluginPreferences().setValue(HIGHLIGHT_ON, highlightAction.isChecked());
232                 if (browser.getUrl().indexOf("resultof")!=-1) browser.execute("setHighlight(" +highlightAction.isChecked()+");"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
233
}
234         };
235         highlightAction.setChecked(highlight);
236         highlightAction.setToolTipText(Messages.BrowserPart_highlightTooltip);
237         highlightAction.setImageDescriptor(HelpUIResources
238                 .getImageDescriptor(IHelpUIConstants.IMAGE_HIGHLIGHT));
239             
240         tbm.insertBefore("back", showExternalAction); //$NON-NLS-1$
241
tbm.insertBefore("back", syncTocAction); //$NON-NLS-1$
242
tbm.insertBefore("back", bookmarkAction); //$NON-NLS-1$
243
tbm.insertBefore("back", highlightAction); //$NON-NLS-1$
244
tbm.insertBefore("back", new Separator()); //$NON-NLS-1$
245
printAction = new Action(ActionFactory.PRINT.getId()) {
246             public void run() {
247                 doPrint();
248             }
249         };
250     }
251
252     /*
253      * (non-Javadoc)
254      *
255      * @see org.eclipse.help.ui.internal.views.IHelpPart#init(org.eclipse.help.ui.internal.views.NewReusableHelpPart)
256      */

257     public void init(ReusableHelpPart parent, String JavaDoc id, IMemento memento) {
258         this.parent = parent;
259         this.id = id;
260         if (memento != null) {
261             String JavaDoc href = memento.getString("BrowserPart.url"); //$NON-NLS-1$
262
if (href != null)
263                 showURL(BaseHelpSystem.resolve(href, "/help/ntopic").toString()); //$NON-NLS-1$
264
}
265     }
266
267     public String JavaDoc getId() {
268         return id;
269     }
270
271     /*
272      * (non-Javadoc)
273      *
274      * @see org.eclipse.help.ui.internal.views.IHelpPart#getControl()
275      */

276     public Control getControl() {
277         return browser;
278     }
279
280     /*
281      * (non-Javadoc)
282      *
283      * @see org.eclipse.help.ui.internal.views.IHelpPart#setVisible(boolean)
284      */

285     public void setVisible(boolean visible) {
286         if (browser != null) {
287             browser.setVisible(visible);
288         }
289     }
290
291     /*
292      * (non-Javadoc)
293      *
294      * @see org.eclipse.ui.forms.IFormPart#setFocus()
295      */

296     public void setFocus() {
297         if (browser != null)
298             browser.setFocus();
299     }
300
301     public void showURL(String JavaDoc url) {
302         if (browser != null && url != null) {
303             browser.setUrl(url);
304         }
305     }
306
307     public void stop() {
308         if (browser != null && !browser.isDisposed()) {
309             browser.stop();
310         }
311     }
312
313     private void doPrint() {
314         browser.execute("window.print();"); //$NON-NLS-1$
315
}
316
317     private void doSyncToc() {
318         String JavaDoc href = BaseHelpSystem.unresolve(this.url);
319         int ix = href.indexOf("?resultof="); //$NON-NLS-1$
320
if (ix >= 0) {
321             href = href.substring(0, ix);
322         }
323         parent.showPage(IHelpUIConstants.HV_ALL_TOPICS_PAGE);
324         AllTopicsPart part = (AllTopicsPart) parent
325                 .findPart(IHelpUIConstants.HV_TOPIC_TREE);
326         if (part != null) {
327             part.selectReveal(href);
328         }
329     }
330
331     private void updateSyncTocAction() {
332         String JavaDoc href = BaseHelpSystem.unresolve(this.url);
333         syncTocAction.setEnabled(parent.isHelpResource(href));
334     }
335
336     private boolean redirectLink(final String JavaDoc url) {
337         if (url.indexOf("/topic/") != -1) { //$NON-NLS-1$
338
if (url.indexOf("noframes") == -1) { //$NON-NLS-1$
339
// char sep = url.lastIndexOf('?') != -1 ? '&' : '?';
340
// String newURL = url + sep + "noframes=true"; //$NON-NLS-1$
341
return true;
342             }
343         } else if (url.indexOf("livehelp/?pluginID=")>0) { //$NON-NLS-1$
344
processLiveAction(url);
345             return true;
346         }
347         return false;
348     }
349
350     private void processLiveAction(String JavaDoc url) {
351         Preferences prefs = HelpBasePlugin.getDefault().getPluginPreferences();
352         if (!"true".equalsIgnoreCase(prefs.getString("activeHelp"))) { //$NON-NLS-1$ //$NON-NLS-2$
353
return;
354         }
355
356         String JavaDoc query = null;
357         try {
358             URL JavaDoc u = new URL JavaDoc(url);
359             query = u.getQuery();
360         } catch (MalformedURLException JavaDoc mue) {
361         }
362         if (query == null)
363             return;
364         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(query, "=&"); //$NON-NLS-1$
365
if (st.countTokens() < 6) {
366             return;
367         }
368         st.nextToken();
369         String JavaDoc pluginId = URLCoder.decode(st.nextToken());
370         st.nextToken();
371         String JavaDoc className = URLCoder.decode(st.nextToken());
372         st.nextToken();
373         String JavaDoc arg = URLCoder.decode(st.nextToken());
374         if (pluginId == null || className == null || arg == null)
375             return;
376         BaseHelpSystem.runLiveHelp(pluginId, className, arg);
377     }
378
379     /*
380      * (non-Javadoc)
381      *
382      * @see org.eclipse.help.ui.internal.views.IHelpPart#fillContextMenu(org.eclipse.jface.action.IMenuManager)
383      */

384
385     public boolean fillContextMenu(IMenuManager manager) {
386         return false;
387     }
388
389     /*
390      * (non-Javadoc)
391      *
392      * @see org.eclipse.help.ui.internal.views.IHelpPart#hasFocusControl(org.eclipse.swt.widgets.Control)
393      */

394     public boolean hasFocusControl(Control control) {
395         return browser.equals(control);
396     }
397
398     public IAction getGlobalAction(String JavaDoc id) {
399         if (id.equals(ActionFactory.PRINT.getId()))
400             return printAction;
401         return null;
402     }
403
404     public void toggleRoleFilter() {
405     }
406
407     public void refilter() {
408         showURL(this.url);
409     }
410
411     public void saveState(IMemento memento) {
412         if (url != null) {
413             String JavaDoc href = BaseHelpSystem.unresolve(url);
414             memento.putString("BrowserPart.url", href); //$NON-NLS-1$
415
}
416     }
417 }
418
Popular Tags