SWT browser widgets and local plugin files

Ok, getting very technical from my normal postings but I found this really difficult to track down so thought it was worth posting somewhere. I’ve creating an application inside the Eclipse framework and wanted to write some HTML and display inside the SWT browser widget. It’s pretty easy to do this with files elsewhere, you simply give the browser widget a URL. However, if you want to load stuff from a local project it turns out to be a bit less obvious (technically this applies to getting access to any files from within an Eclipse project at runtime). So… in great anticipation I’m sure – here’s the code:

Browser browser = new Browser(parent, SWT.NONE);
Bundle plugin = Activator.getDefault().getBundle(); // Where Activator is my org.eclipse.core.runtime.Plugin
IPath relativePagePath = new Path("html/index.html");
URL fileInPlugin = FileLocator.find(plugin, relativePagePath, null);
URL pageUrl = FileLocator.toFileURL(fileInPlugin);
browser.setUrl(pageUrl.toString());

Easy when you know how.

Tags: , , ,

  • Chris
    Hi Gareth, Thanks for this, it's just helped me to do exactly what I wanted to do in an RCP application - have it open with a welcome page with some static content and an iframe for the latest news etc.. However, when I export the application to create a download, the welcome page comes up but with all the images missing. Obviously they aren't part of index.html. Have you come across this? And, if so, do you know how to solve it? Any pointers you can offer me would be gratefully received as I don't have much time to get this done!
  • Chris
    No need to reply as I seem to have found it. Thought I'd post it here in case anybody else is wondering the same thing.

    The key was in the following post:
    http://dev.eclipse.org/newslists/news.eclipse.p...

    This meant I had to read in the initial HTML file and replace a marker with the actual base url within the plugin (which I had retrieved using the method above) and all went swimmingly! Thanks for posting this and giving me a clue in the first place!
  • Chris
    Oops. Spoke too soon. Worked when run inside Eclipse. Didn't once exported (even though the base URL looks right). Still looking for clues. Anybody?
  • Chris
    Hopefully my last comment on the matter.

    I was close but one extra step was needed. The feature that held my HTML needed to be unpacked (tick the box in feature.xml to say it should be unpacked when installed) - then the base tag worked.
  • Hi Chris, looks like you answered yourself several times before I got the chance :)

    Will be useful info for anyone else that has the same issue (and me if I need to do it) so thanks for posting up the answer.
blog comments powered by Disqus