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.

Advertisements

16 thoughts on “SWT browser widgets and local plugin files

  1. 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!

  2. 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!

  3. 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?

  4. 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!

  5. 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!

  6. 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?

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. Hi,I have a plugin in which the html files are bundled. I followed your steps and could open the html file through plugin jar. But there are some images embedded in those html files which are not showing up. I couldnt get the concept of packing and unpacking which you have told in your previous comment. Please suggest me how to get images in the html file.Thanks and Regards,Rupa

  14. Hi Rupa,Unfortunately I've not done this myself but it appears from Chris' comments above that he did. Sounds like you need to tick the option that unpacks your plugin when installed (in feature.xml). I assume this means that it unzip the installed JAR file rather than leaving it in the eclipse directory as-is. Good luck!Gareth

  15. Hi Gareth,Thank you very much for your response. But I could not find anything in my plugin.xml (Guessing that Chris' feature.xml is my plugin.xml) to unpack the jar during install. I guess the problem for not displaying the image in html files is that the html file is read to cache by toFileURL() method and the file contents are read and its not able to find the image there. I have to fix it somehow as the dead line is nearby. Anyways, please let me know if you find out any way to sort out this problem.Thanks,Rupa

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s