You might want to use the Swing control with the AWT / SWT bridge . I used this to embed OpenOffice into a SWT application:
package ooswtviewer; import java.awt.Panel; import com.sun.star.awt.XView; import com.sun.star.beans.Property; import com.sun.star.beans.UnknownPropertyException; import com.sun.star.beans.XPropertySet; import com.sun.star.comp.beans.Frame; import com.sun.star.comp.beans.NoConnectionException; import com.sun.star.comp.beans.OOoBean; import com.sun.star.comp.beans.OfficeDocument; import com.sun.star.drawing.XDrawView; import com.sun.star.frame.XController; import com.sun.star.frame.XDesktop; import com.sun.star.frame.XFrame; import com.sun.star.frame.XFramesSupplier; import com.sun.star.frame.XLayoutManager; import com.sun.star.frame.XModel; import com.sun.star.lang.WrappedTargetException; import com.sun.star.ui.XUIElement; import com.sun.star.uno.Any; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XInterface; import com.sun.star.view.XViewSettingsSupplier; public class OOoSwtViewer extends Panel { private static final String RESOURCE_TOOLBAR_TEXTOBJECTBAR = "private:resource/toolbar/textobjectbar"; private static final String RESOURCE_TOOLBAR_STANDARDBAR = "private:resource/toolbar/standardbar"; private static final String RESOURCE_MENUBAR = "private:resource/menubar/menubar"; private static final long serialVersionUID = -1408623115735065822L; private OOoBean aBean; public OOoSwtViewer() { super(); aBean = new OOoBean(); setLayout(new java.awt.BorderLayout()); add(aBean, java.awt.BorderLayout.CENTER); aBean.setAllBarsVisible (false); } public XPropertySet getXPropertySet () { return getXPropertySet (getFrame ()); } public XPropertySet getXPropertySet (Object o) { return (XPropertySet)UnoRuntime.queryInterface (XPropertySet.class, o); } public Frame getFrame () { try { return aBean.getFrame (); } catch (NoConnectionException e) { throw new OOException ("Error getting frame from bean", e); } } public XLayoutManager getXLayoutManager () { try { return (XLayoutManager)UnoRuntime.queryInterface (XLayoutManager.class, getXPropertySet ().getPropertyValue ("LayoutManager")); } catch (Exception e) { throw new OOException ("Error getting LayoutManager from bean properties", e); } } public void setMenuBarVisible (boolean visible) { if (visible) getXLayoutManager ().showElement (RESOURCE_MENUBAR); else getXLayoutManager ().hideElement (RESOURCE_MENUBAR); } public void setStandardBarVisible (boolean visible) { if (visible) getXLayoutManager ().showElement (RESOURCE_TOOLBAR_STANDARDBAR); else getXLayoutManager ().hideElement (RESOURCE_TOOLBAR_STANDARDBAR); } public void setTextObjectBarVisible (boolean visible) { if (visible) getXLayoutManager ().showElement (RESOURCE_TOOLBAR_TEXTOBJECTBAR); else getXLayoutManager ().hideElement (RESOURCE_TOOLBAR_TEXTOBJECTBAR); } private Thread loadThread; private Exception loadException; public void setDocument(final String url) { loadThread = new Thread () { public void run() { try { aBean.loadFromURL(url, null); aBean.aquireSystemWindow(); setTextObjectBarVisible (false);
You can use the control as follows:
package ooswtviewer; import java.awt.BorderLayout; import java.awt.Frame; import java.awt.Panel; import java.io.File; import javax.swing.JRootPane; import org.eclipse.swt.SWT; import org.eclipse.swt.awt.SWT_AWT; import org.eclipse.swt.events.DisposeEvent; import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class OOoSwtSnippet { public static void main(String[] args) { OOoSwtSnippet obj = new OOoSwtSnippet (); try { obj.run (args); } catch (Exception e) { e.printStackTrace (); } } public void run (String[] args) throws Exception { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Composite composite = new Composite(shell, SWT.NO_BACKGROUND | SWT.EMBEDDED); System.setProperty("sun.awt.noerasebackground", "true"); Frame frame = SWT_AWT.new_Frame(composite); Panel panel = new Panel(new BorderLayout()) { public void update(java.awt.Graphics g) { paint(g); } }; frame.add(panel); JRootPane root = new JRootPane(); panel.add(root); java.awt.Container contentPane = root.getContentPane(); shell.setSize(800, 600); final OOoSwtViewer viewer = new OOoSwtViewer(); contentPane.add(viewer);
OOException - RuntimeException exception:
package ooswtviewer; public class OOException extends RuntimeException { public OOException () { super (); } public OOException (String message, Throwable cause) { super (message, cause); } public OOException (String message) { super (message); } public OOException (Throwable cause) { super (cause); } }
Aaron Digulla Nov 06 '08 at 15:33 2008-11-06 15:33
source share