iA


Using JRex in Eclipse RCP

by Vijay Kiran

After successfully running JRex, I moved on to embed JRex in RCP application. RCP applications are developed using Eclipse Rich Client Platform. Including JRex in RCP is little tircky, since JRex is based on Swing/AWT. But Eclipse RCP provides SWT and AWT bridge, using which any AWT component can be embedded in SWT component.

First I’d to create a custom SWT component. Here’s the code for the same:

package com.vijaykiran.rcp.jrex;
 
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.mozilla.jrex.JRexFactory;
import org.mozilla.jrex.exception.JRexException;
import org.mozilla.jrex.window.JRexWindowManager;
 
public class JRexSWTComponent extends Composite {
 
	private JRexFactory myJRex;
 
	public JRexSWTComponent(Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new browserDisposeListener() {
		});
		try {
			this.setLayout(new GridLayout());
			this.setSize(450, 250);
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	private class browserDisposeListener implements
			org.eclipse.swt.events.DisposeListener {
		public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
			try {
				myJRex.shutdownEngine();
			} catch (Exception e2) {
				System.out.println("Couldn't properly dispose browser");
			}
		}
	}
 
	public void setupJRex() {
		System.setProperty("jrex.gre.path",
				"c:/jrex08092005/jrex_gre/org/mozilla/jrex/jrex_gre");
		myJRex = JRexFactory.getInstance();
 
		try {
			myJRex.startEngine();
 
			((JRexWindowManager) JRexFactory.getInstance().getImplInstance(
					JRexFactory.WINDOW_MANAGER))
					.create(JRexWindowManager.SINGLE_WINDOW_MODE);
 
		} catch (JRexException e) {
			System.out.println("jrex exception: " + e.getMessage());
			System.exit(0);
		}
 
	}
 
}

Once the component has been created, now it can be embedded in any RCP view.

package com.vijaykiran.rcp.jrex;
 
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.mozilla.jrex.event.window.WindowEventConstants;
import org.mozilla.jrex.log.JRexL;
import org.mozilla.jrex.navigation.WebNavigationConstants;
import org.mozilla.jrex.ui.JRexCanvas;
 
public class JRexView extends ViewPart {
	public static String ID = "com.vijaykiran.rcp.jrex.jrexview";
 
	public void createPartControl(Composite parent) {
 
		JRexSWTComponent browserComponent = new JRexSWTComponent(parent,
				SWT.EMBEDDED);
 
		browserComponent.setupJRex();
		String myURL = "http://www.google.com";
		showBrowser(browserComponent, myURL);
 
	}
 
	public void setFocus() {
		// TODO
 
	}
 
	private static void showBrowser(Composite parent, String myURL) {
 
		java.awt.Frame awtContainer = SWT_AWT.new_Frame(parent);
		awtContainer.setBackground(new java.awt.Color(255, 192, 0));
 
		JRexCanvas myBrowser = JRexCanvas
				.createBrowserComponent(WindowEventConstants.CHROME_DEFAULT);
		myBrowser.setVisible(true);
 
		awtContainer.add(myBrowser);
 
		// Now we browse to the specified web page!
 
		try {
			java.net.URI myURI = new java.net.URI(myURL);
			myBrowser.getNavigator().loadURI(myURI.toASCIIString(),
					WebNavigationConstants.LOAD_FLAGS_NONE, null, null, null);
		} catch (Exception uex) {
			if (JRexL.on)
				JRexL.printStackTrace(uex);
		}
	}
 
}

That’s it, now just run your RCP and you should be able to see RCP Application with a JRex browser view.



  • Christian van Rensen

    THANK YOU!
    this was very useful!