Sunday, June 22, 2008

Object Linking and Embedding (OLE) Java

Wooww !!

You can actually open files in their native application with the native application embedded within the applet (any frame).
Okay.. I wont talk much.. See for yourself. Do add the DLLs in c:\windows\system32.

Here is the code:

package test;
import java.applet.*;import java.awt.FileDialog;import java.io.File;
import javax.activation.FileDataSource;
import org.eclipse.swt.SWT;import org.eclipse.swt.events.FocusEvent;import org.eclipse.swt.events.FocusListener;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.events.MouseMoveListener;import org.eclipse.swt.ole.win32.OLE;import org.eclipse.swt.ole.win32.OleClientSite;import org.eclipse.swt.widgets.Control; public class ole extends Applet { org.eclipse.swt.widgets.Display display; org.eclipse.swt.widgets.Shell swtParent; java.awt.Canvas awtParent;
public void init () { Thread thread = new Thread (new Runnable () { public void run () { setLayout(new java.awt.GridLayout (1, 1)); awtParent = new java.awt.Canvas (); add (awtParent); display = new org.eclipse.swt.widgets.Display (); swtParent = org.eclipse.swt.awt.SWT_AWT.new_Shell (display, awtParent); swtParent.setLayout (new org.eclipse.swt.layout.FillLayout ()); org.eclipse.swt.ole.win32.OleFrame frame = new org.eclipse.swt.ole.win32.OleFrame (swtParent, org.eclipse.swt.SWT.NULL); final org.eclipse.swt.ole.win32.OleClientSite site; try { //if(fd.getText()!=null && fd.getText().length() !=0){ site = new org.eclipse.swt.ole.win32.OleClientSite(frame, org.eclipse.swt.SWT.NULL, "c:\\a.xls"); site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE); site.addMouseMoveListener(new MouseMoveListener(){
public void mouseMove(MouseEvent arg0) { site.forceFocus(); } }); site.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { System.out.println("client1 focus gained: " + e); site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE ); } public void focusLost(FocusEvent e) { System.out.println("client1 focus lost: " + e); site.deactivateInPlaceClient(); } }); //} } catch (org.eclipse.swt.SWTException e) { String str = "Create OleClientSite Error" + e.toString (); System.out.println (str); return; } setSize (800, 600);
validate (); while (swtParent != null && !swtParent.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } } }); thread.start (); try { thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }} public void stop (){ if (display != null && !display.isDisposed ()){ display.syncExec(new Runnable () { public void run () { if (swtParent != null && !swtParent.isDisposed ()) swtParent.dispose (); swtParent = null; display.dispose (); display = null; } }); remove (awtParent); awtParent = null; } } }

Sorry for the garbage . Be sure to include the jar files (eclipse plug-ins folder)

-Ranijth

About me

Yoohooo,

As this is my first blog, let me start by introducing myself. I am Ranjith. I have completed my B.E in Information Science from the National Institute of Engineering, Mysore (2007). I am currently a Development Engineer at Aditi Technologies, Bangalore. Just about to complete an year in the software industry (on July 2nd 2008). Wow !!! It has been a great experience here working with good people from whom I have learnt a lot.
I have some experience in Java/J2EE/Web services/Frameworks/APIs. I have used Hibernate/Axis/Quartz/Struts/Spring/Digester/Shale/Commons and some more. I will be posting my experiences here. Hope someone finds it useful.
I was posting all of my experiences in Aditi Java Forum but as it can only be accessed by people at Aditi, I thought of creating this blog.

Happy Blogger,
Ranjith