1 11 package org.eclipse.help.ui.internal.views; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import java.util.StringTokenizer ; 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 QUERY = "BrowserPartQuery:"; private final static String HIGHLIGHT_ON = "highlight-on"; 55 private ReusableHelpPart parent; 56 57 private Browser browser; 58 59 private String id; 60 61 private int lastProgress = -1; 62 63 private String 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 statusURL; 76 77 private String 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://")) { try { 89 BaseHelpSystem.getHelpBrowser(true).displayURL( 90 event.location); 91 } catch (Exception exc) { 92 } 93 } 94 } 95 96 public void changed(LocationEvent event) { 97 String url = event.location; 98 boolean isResult = url.indexOf("resultof")!=-1; 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); 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 value = executeQuery("document.title"); BrowserPart.this.title = value != null ? value : "N/A"; } 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) statusURL = event.text; 152 } 153 }); 154 browser.addOpenWindowListener(new OpenWindowListener() { 155 public void open(WindowEvent event) { 156 if (statusURL != null) { 157 try { 158 String relativeURL = BaseHelpSystem.unresolve(new URL ( 159 statusURL)); 160 if (BrowserPart.this.parent.isHelpResource(relativeURL)) { 161 BrowserPart.this.parent 162 .showExternalURL(relativeURL); 163 event.required = true; 164 } 165 } catch (MalformedURLException e) { 166 HelpUIPlugin.logError("Malformed URL: " + statusURL, e); } 168 } 169 } 170 }); 171 contributeToToolBar(tbm); 172 } 173 174 private String executeQuery(String domValue) { 175 String query = "window.status=\"" + QUERY + "\"+" + domValue + ";"; boolean status = browser.execute(query); 177 if (status) { 178 return (String ) browser.getData("query"); } 180 return null; 181 } 182 183 private boolean processQuery(String text) { 184 if (text.startsWith(QUERY)) { 185 browser.setData("query", text.substring(QUERY.length())); 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 () { 196 public void run() { 197 try { 198 parent.showExternalURL(BaseHelpSystem 199 .unresolve(new URL (url))); 200 } catch (MalformedURLException e) { 201 HelpUIPlugin.logError("Malformed URL: " + statusURL, e); } 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 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()+");"); } 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); tbm.insertBefore("back", syncTocAction); tbm.insertBefore("back", bookmarkAction); tbm.insertBefore("back", highlightAction); tbm.insertBefore("back", new Separator()); printAction = new Action(ActionFactory.PRINT.getId()) { 246 public void run() { 247 doPrint(); 248 } 249 }; 250 } 251 252 257 public void init(ReusableHelpPart parent, String id, IMemento memento) { 258 this.parent = parent; 259 this.id = id; 260 if (memento != null) { 261 String href = memento.getString("BrowserPart.url"); if (href != null) 263 showURL(BaseHelpSystem.resolve(href, "/help/ntopic").toString()); } 265 } 266 267 public String getId() { 268 return id; 269 } 270 271 276 public Control getControl() { 277 return browser; 278 } 279 280 285 public void setVisible(boolean visible) { 286 if (browser != null) { 287 browser.setVisible(visible); 288 } 289 } 290 291 296 public void setFocus() { 297 if (browser != null) 298 browser.setFocus(); 299 } 300 301 public void showURL(String 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();"); } 316 317 private void doSyncToc() { 318 String href = BaseHelpSystem.unresolve(this.url); 319 int ix = href.indexOf("?resultof="); 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 href = BaseHelpSystem.unresolve(this.url); 333 syncTocAction.setEnabled(parent.isHelpResource(href)); 334 } 335 336 private boolean redirectLink(final String url) { 337 if (url.indexOf("/topic/") != -1) { if (url.indexOf("noframes") == -1) { return true; 342 } 343 } else if (url.indexOf("livehelp/?pluginID=")>0) { processLiveAction(url); 345 return true; 346 } 347 return false; 348 } 349 350 private void processLiveAction(String url) { 351 Preferences prefs = HelpBasePlugin.getDefault().getPluginPreferences(); 352 if (!"true".equalsIgnoreCase(prefs.getString("activeHelp"))) { return; 354 } 355 356 String query = null; 357 try { 358 URL u = new URL (url); 359 query = u.getQuery(); 360 } catch (MalformedURLException mue) { 361 } 362 if (query == null) 363 return; 364 StringTokenizer st = new StringTokenizer (query, "=&"); if (st.countTokens() < 6) { 366 return; 367 } 368 st.nextToken(); 369 String pluginId = URLCoder.decode(st.nextToken()); 370 st.nextToken(); 371 String className = URLCoder.decode(st.nextToken()); 372 st.nextToken(); 373 String arg = URLCoder.decode(st.nextToken()); 374 if (pluginId == null || className == null || arg == null) 375 return; 376 BaseHelpSystem.runLiveHelp(pluginId, className, arg); 377 } 378 379 384 385 public boolean fillContextMenu(IMenuManager manager) { 386 return false; 387 } 388 389 394 public boolean hasFocusControl(Control control) { 395 return browser.equals(control); 396 } 397 398 public IAction getGlobalAction(String 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 href = BaseHelpSystem.unresolve(url); 414 memento.putString("BrowserPart.url", href); } 416 } 417 } 418 | Popular Tags |