1 11 package org.eclipse.ui.internal.intro.impl.presentations; 12 13 import java.io.PrintWriter ; 14 import java.io.StringWriter ; 15 import java.util.Map ; 16 import java.util.Properties ; 17 18 import org.eclipse.core.runtime.IRegistryChangeEvent; 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.jface.action.IToolBarManager; 21 import org.eclipse.jface.action.Separator; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.browser.Browser; 24 import org.eclipse.swt.browser.LocationAdapter; 25 import org.eclipse.swt.browser.LocationEvent; 26 import org.eclipse.swt.browser.ProgressEvent; 27 import org.eclipse.swt.browser.ProgressListener; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Event; 30 import org.eclipse.swt.widgets.Listener; 31 import org.eclipse.ui.IActionBars; 32 import org.eclipse.ui.IMemento; 33 import org.eclipse.ui.IPropertyListener; 34 import org.eclipse.ui.actions.ActionFactory; 35 import org.eclipse.ui.internal.intro.impl.IIntroConstants; 36 import org.eclipse.ui.internal.intro.impl.IntroPlugin; 37 import org.eclipse.ui.internal.intro.impl.Messages; 38 import org.eclipse.ui.internal.intro.impl.html.HTMLElement; 39 import org.eclipse.ui.internal.intro.impl.html.IIntroHTMLConstants; 40 import org.eclipse.ui.internal.intro.impl.html.IntroHTMLGenerator; 41 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPage; 42 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPartImplementation; 43 import org.eclipse.ui.internal.intro.impl.model.History; 44 import org.eclipse.ui.internal.intro.impl.model.IntroContentProvider; 45 import org.eclipse.ui.internal.intro.impl.model.IntroHomePage; 46 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot; 47 import org.eclipse.ui.internal.intro.impl.model.loader.ContentProviderManager; 48 import org.eclipse.ui.internal.intro.impl.model.loader.IntroContentParser; 49 import org.eclipse.ui.internal.intro.impl.model.util.ModelUtil; 50 import org.eclipse.ui.internal.intro.impl.util.Log; 51 import org.eclipse.ui.internal.intro.impl.util.Util; 52 import org.eclipse.ui.intro.config.IIntroContentProvider; 53 import org.eclipse.ui.intro.config.IIntroContentProviderSite; 54 import org.eclipse.ui.intro.config.IIntroXHTMLContentProvider; 55 import org.eclipse.ui.intro.config.IntroConfigurer; 56 import org.w3c.dom.Document ; 57 import org.w3c.dom.Element ; 58 import org.w3c.dom.Node ; 59 import org.w3c.dom.NodeList ; 60 61 public class BrowserIntroPartImplementation extends 62 AbstractIntroPartImplementation implements IPropertyListener, 63 IIntroContentProviderSite { 64 65 66 protected Browser browser = null; 68 69 private IntroHTMLGenerator htmlGenerator = null; 71 72 protected BrowserIntroPartLocationListener urlListener = new BrowserIntroPartLocationListener( 73 this); 74 75 private boolean isFinishedLoading; 77 78 protected void updateNavigationActionsState() { 79 if (getModel().isDynamic()) { 80 forwardAction.setEnabled(history.canNavigateForward()); 81 backAction.setEnabled(history.canNavigateBackward()); 82 return; 83 } 84 85 forwardAction.setEnabled(browser.isForwardEnabled()); 87 backAction.setEnabled(browser.isBackEnabled()); 88 } 89 90 91 94 public void createPartControl(Composite parent) { 95 long start = 0; 96 if (Log.logPerformance) 97 start = System.currentTimeMillis(); 98 99 browser = new Browser(parent, SWT.NONE); 100 101 browser.addLocationListener(urlListener); 105 106 browser.addProgressListener(new ProgressListener() { 110 111 public void changed(ProgressEvent event) { 112 } 114 115 public void completed(ProgressEvent event) { 116 urlListener.flagEndOfNavigation(); 117 urlListener.flagEndOfFrameNavigation(); 118 urlListener.flagRemovedTempUrl(); 119 if (!getModel().isDynamic()) 120 updateNavigationActionsState(); 121 } 122 }); 123 124 browser.addListener(SWT.MenuDetect, new Listener() { 126 127 public void handleEvent(Event event) { 128 if (IntroPlugin.getDefault().isDebugging()) 129 event.doit = true; 130 else 131 event.doit = false; 132 } 133 }); 134 135 if (Log.logPerformance) 138 Util.logPerformanceTime("creating a new Browser() took:", start); 140 addToolBarActions(); 141 142 if (!getModel().hasValidConfig()) { 143 browser.setText(Messages.Browser_invalidConfig); 144 return; 145 } 146 147 if (getModel().isDynamic()) 149 handleDynamicIntro(); 150 else 151 handleStaticIntro(); 152 } 153 154 155 156 private void handleDynamicIntro() { 157 IntroHomePage homePage = getModel().getHomePage(); 158 String cachedPage = getCachedCurrentPage(); 160 if (cachedPage != null) { 161 if (History.isURL(cachedPage)) { 163 boolean success = browser.setUrl(cachedPage); 165 if (!success) { 166 Log.error("Unable to set the following ULR in browser: " + cachedPage, null); 168 return; 169 } 170 history.updateHistory(cachedPage); 171 } else { 172 getModel().setCurrentPageId(cachedPage, false); 174 history.updateHistory(getModel().getCurrentPage()); 176 } 177 178 } else { 179 history.updateHistory(homePage); 183 } 184 187 getModel().addPropertyListener(this); 190 } 191 192 193 201 private boolean generateDynamicContentForPage(AbstractIntroPage page) { 202 String content = null; 203 204 if (page.isXHTMLPage()) 205 content = generateXHTMLPage(page, this); 206 else { 207 HTMLElement html = getHTMLGenerator().generateHTMLforPage(page, 208 this); 209 if (html != null) { 210 IntroModelRoot root = getModel(); 211 if (root!=null) { 212 Map props = root.getTheme()!=null?root.getTheme().getProperties():null; 213 if (props!=null) { 214 String value = (String )props.get("standardSupport"); String doctype=null; 216 if ("strict".equalsIgnoreCase(value)) doctype = generateDoctype(true); 218 else if ("loose".equalsIgnoreCase(value)) doctype = generateDoctype(false); 220 if (doctype!=null) 221 content = doctype+html.toString(); 222 } 223 } 224 if (content==null) 225 content = html.toString(); 226 } 227 } 228 229 230 if (content == null) { 231 Log.error("Error generating HTML content for page", null); return false; 234 } 235 236 boolean success = false; 238 if (browser != null) { 239 long start = 0; 240 if (Log.logPerformance) 241 start = System.currentTimeMillis(); 242 browser.addLocationListener(new LocationAdapter() { 243 public void changed(LocationEvent event) { 244 if (event.top) { 245 isFinishedLoading = true; 246 } 247 } 248 }); 249 success = browser.setText(content); 250 if (Log.logPerformance) 251 Util 252 .logPerformanceTime("setText() on the browser took:", start); 254 if (!success) 255 Log.error("Unable to set HTML on the browser", null); } 257 258 259 if (IntroPlugin.getDefault().isDebugging()) { 261 String printHtml = Platform 262 .getDebugOption("org.eclipse.ui.intro/trace/printHTML"); if (printHtml != null && printHtml.equalsIgnoreCase("true")) { System.out.println(content); 265 } 266 } 267 return success; 268 } 269 270 284 public String generateXHTMLPage(AbstractIntroPage page, 285 IIntroContentProviderSite site) { 286 Document dom = page.getResolvedDocument(); 288 NodeList nodes = dom.getElementsByTagNameNS("*", IntroContentProvider.TAG_CONTENT_PROVIDER); 290 Node [] contentProviderElements = ModelUtil.getArray(nodes); 292 293 resolveDynamicContent(page, site); 295 String content = IntroContentParser.convertToString(dom); 296 297 reinjectDynamicContent(dom, contentProviderElements); 299 return content; 300 } 301 302 private String generateDoctype(boolean strict) { 303 StringWriter swriter = new StringWriter (); 304 PrintWriter writer = new PrintWriter (swriter); 305 if (strict) { 306 writer.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\""); writer.println("\t\t\t\"http://www.w3.org/TR/html4/strict.dtd\">"); } 309 else { 310 writer.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\""); writer.println("\t\t\t\"http://www.w3.org/TR/html4/loose.dtd\">"); } 313 writer.close(); 314 return swriter.toString(); 315 } 316 317 322 private Document resolveDynamicContent(AbstractIntroPage page, 323 IIntroContentProviderSite site) { 324 Document dom = page.getResolvedDocument(); 325 326 NodeList contentProviders = dom.getElementsByTagNameNS("*", IntroContentProvider.TAG_CONTENT_PROVIDER); 329 330 Node [] nodes = ModelUtil.getArray(contentProviders); 332 for (int i = 0; i < nodes.length; i++) { 333 Element contentProviderElement = (Element) nodes[i]; 334 IntroContentProvider provider = new IntroContentProvider( 335 contentProviderElement, page.getBundle()); 336 provider.setParent(page); 337 IIntroXHTMLContentProvider providerClass = (IIntroXHTMLContentProvider) ContentProviderManager 340 .getInst().getContentProvider(provider); 341 if (providerClass == null) 342 providerClass = (IIntroXHTMLContentProvider) ContentProviderManager 344 .getInst().createContentProvider(provider, site); 345 346 if (providerClass != null) { 347 Properties att = new Properties (); 351 att.setProperty(IIntroHTMLConstants.ATTRIBUTE_ID, provider 352 .getId()); 353 Element contentDiv = ModelUtil.createElement(dom, 354 ModelUtil.TAG_DIV, att); 355 providerClass.createContent(provider.getId(), contentDiv); 356 357 contentProviderElement.getParentNode().replaceChild(contentDiv, 358 contentProviderElement); 359 } else { 360 } 365 } 366 return dom; 367 } 368 369 370 private void reinjectDynamicContent(Document dom, 371 Node [] contentProviderElements) { 372 for (int i = 0; i < contentProviderElements.length; i++) { 373 Element contentProviderElement = (Element) contentProviderElements[i]; 376 Element contentProviderDiv = ModelUtil.getElementById(dom, 377 contentProviderElement 378 .getAttribute(IIntroHTMLConstants.ATTRIBUTE_ID), 379 ModelUtil.TAG_DIV); 380 contentProviderDiv.getParentNode().replaceChild( 381 contentProviderElement, contentProviderDiv); 382 } 383 } 384 385 386 387 392 private IntroHTMLGenerator getHTMLGenerator() { 393 if (htmlGenerator == null) 394 htmlGenerator = new IntroHTMLGenerator(); 395 396 return htmlGenerator; 397 } 398 399 protected void addToolBarActions() { 400 IActionBars actionBars = getIntroPart().getIntroSite().getActionBars(); 402 IToolBarManager toolBarManager = actionBars.getToolBarManager(); 403 actionBars.setGlobalActionHandler(ActionFactory.FORWARD.getId(), 404 forwardAction); 405 actionBars.setGlobalActionHandler(ActionFactory.BACK.getId(), 406 backAction); 407 toolBarManager.add(new Separator(IntroConfigurer.TB_ADDITIONS)); 408 toolBarManager.add(homeAction); 409 toolBarManager.add(backAction); 410 toolBarManager.add(forwardAction); 411 toolBarManager.update(true); 412 actionBars.updateActionBars(); 413 updateNavigationActionsState(); 414 } 415 416 public void dynamicStandbyStateChanged(boolean standby, 417 boolean isStandbyPartNeeded) { 418 419 if (isStandbyPartNeeded) 420 return; 422 423 if (history.currentLocationIsUrl()) 424 return; 427 428 429 430 IntroHomePage homePage = getModel().getHomePage(); 433 IntroHomePage standbyPage = getModel().getStandbyPage(); 434 if (standbyPage == null) 435 standbyPage = homePage; 436 437 if (standby) { 438 generateDynamicContentForPage(standbyPage); 439 } else { 440 if (getModel().getCurrentPage().equals(standbyPage.getId())) 444 getModel().setCurrentPageId(getModel().getHomePage().getId()); 445 generateDynamicContentForPage(getModel().getCurrentPage()); 446 } 447 } 448 449 450 451 458 public void propertyChanged(Object source, int propId) { 459 if (propId == IntroModelRoot.CURRENT_PAGE_PROPERTY_ID) { 460 String pageId = getModel().getCurrentPageId(); 461 if (pageId == null || pageId.equals("")) return; 464 updateContent(); 466 } 467 } 468 469 public void setFocus() { 470 browser.setFocus(); 471 } 472 473 public void dispose() { 474 browser.dispose(); 475 } 476 477 480 protected void updateContent() { 481 generateDynamicContentForPage(getModel().getCurrentPage()); 482 } 483 484 488 public void reflow(IIntroContentProvider provider, boolean incremental) { 489 updateContent(); 490 } 491 492 498 protected void saveCurrentPage(IMemento memento) { 499 if (memento == null) 500 return; 501 if (browser != null && browser.getUrl() != null 505 && browser.getUrl().length() > 0 506 && !(browser.getUrl().equals("about:blank")) && !(browser.getUrl().equals("file:///"))) { 509 String currentURL = browser.getUrl(); 510 if (currentURL != null) { 511 memento.putString(IIntroConstants.MEMENTO_CURRENT_PAGE_ATT, 512 currentURL); 513 } 514 } else { 515 super.saveCurrentPage(memento); 516 } 517 } 518 519 520 525 public boolean navigateBackward() { 526 boolean success = false; 527 if (getModel().isDynamic()) { 528 if (history.canNavigateBackward()) { 530 history.navigateHistoryBackward(); 531 urlListener.flagStartOfNavigation(); 533 if (history.currentLocationIsUrl()) { 534 success = browser.setUrl(history.getCurrentLocationAsUrl()); 535 } else { 536 AbstractIntroPage page = history.getCurrentLocationAsPage(); 541 getModel().setCurrentPageId(page.getId(), false); 542 success = generateDynamicContentForPage(page); 543 } 544 } else 545 success = false; 546 updateNavigationActionsState(); 548 } else 549 success = browser.back(); 551 552 return success; 553 } 554 555 556 561 public boolean navigateForward() { 562 boolean success = false; 563 if (getModel().isDynamic()) { 564 if (history.canNavigateForward()) { 566 history.navigateHistoryForward(); 567 urlListener.flagStartOfNavigation(); 569 if (history.currentLocationIsUrl()) { 570 success = browser.setUrl(history.getCurrentLocationAsUrl()); 571 } else { 572 AbstractIntroPage page = history.getCurrentLocationAsPage(); 573 getModel().setCurrentPageId(page.getId(), false); 574 success = generateDynamicContentForPage(page); 575 } 576 } else 577 success = false; 578 updateNavigationActionsState(); 580 } else 581 success = browser.forward(); 583 584 return success; 585 } 586 587 592 public boolean navigateHome() { 593 IntroHomePage rootPage = getModel().getHomePage(); 596 boolean success = false; 597 if (getModel().isDynamic()) { 598 if (history.currentLocationIsUrl()) 604 generateDynamicContentForPage(rootPage); 605 606 success = getModel().setCurrentPageId(rootPage.getId()); 607 updateHistory(rootPage); 608 609 } else { 610 String location = rootPage.getUrl(); 611 success = browser.setUrl(location); 612 updateHistory(location); 613 } 614 615 return success; 616 } 617 618 619 620 625 protected void handleRegistryChanged(IRegistryChangeEvent event) { 626 if (getModel().isDynamic()) { 627 htmlGenerator = null; 629 getModel().addPropertyListener(this); 632 getModel().firePropertyChange( 633 IntroModelRoot.CURRENT_PAGE_PROPERTY_ID); 634 } 635 } 636 637 638 protected void doStandbyStateChanged(boolean standby, 639 boolean isStandbyPartNeeded) { 640 if (isStandbyPartNeeded | standby) { 643 homeAction.setEnabled(false); 644 forwardAction.setEnabled(false); 645 backAction.setEnabled(false); 646 } else { 647 homeAction.setEnabled(true); 648 updateNavigationActionsState(); 649 } 650 651 if (getModel().isDynamic()) 652 dynamicStandbyStateChanged(standby, isStandbyPartNeeded); 653 else 654 staticStandbyStateChanged(standby); 655 } 656 657 658 659 private void handleStaticIntro() { 661 String url = getCachedCurrentPage(); 665 if (!History.isURL(url)) 666 url = getModel().getHomePage().getUrl(); 668 669 if (url == null) { 670 Log.error("Url is null; no content to display in browser", null); return; 673 } 674 boolean success = browser.setUrl(url); 676 if (!success) { 677 Log.error("Unable to set the following ULR in browser: " + url, null); 679 return; 680 } 681 } 682 683 public void staticStandbyStateChanged(boolean standby) { 684 IntroHomePage homePage = getModel().getHomePage(); 685 IntroHomePage standbyPage = getModel().getStandbyPage(); 686 if (standbyPage == null) 687 standbyPage = homePage; 688 689 if (standby) 690 browser.setUrl(standbyPage.getUrl()); 691 else 692 browser.setUrl(homePage.getUrl()); 693 } 694 695 696 public Browser getBrowser() { 697 return browser; 698 } 699 700 703 public boolean isFinishedLoading() { 704 return isFinishedLoading; 705 } 706 } 707 | Popular Tags |