Wednesday, 21 August 2013

HTML File in Eclipse RCP Application ViewPart

HTML File in Eclipse RCP Application ViewPart



This article is show how to view local html file in your eclipse rcp application.Here we use browser class to view html file.This coding will explain for after build the jar it will run the html file.So we get the file and convert into temporary file in our local machine then the file will converted to URI then displayed in browser.Here the temp directory is stored in String s="java.io.tmpdir".


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

public class CustomView extends ViewPart {
 


    public void createPartControl(Composite parent) {
     
        final Browser b = new Browser(parent, SWT.NONE); // Uses IE on MS Windows
   
        String s=System.getProperty("java.io.tmpdir");
try {

InputStream is = getClass().getResourceAsStream("pie.htm");

FileOutputStream outputStream = new FileOutputStream(new File(s+"/pie.html"));

int read = 0;
byte[] bytes = new byte[1024];

while ((read = is.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
File f=new File(s+"/pie.html");

f.deleteOnExit();
URL url = f.toURI().toURL();
   
        b.setUrl(url.toString());
        b.addProgressListener(new ProgressListener() {
            @Override
            public void completed(ProgressEvent event) {
                System.out.println("Page loaded");
                try {
               
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
             
            }
            @Override
            public void changed(ProgressEvent event) {
            }
        });
        outputStream.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

    }

 
    public void setFocus() {
    }
}

0 comments:

Post a Comment