使用SWT-OLE相关面板的实现
解决在客户端中内嵌面板,使用相关系统程序打开各类文件,在面板中呈现
import static com.gildata.ui.widgets.WidgetUtil.GridLayout;
import java.io.File;
import org.apache.commons.lang.StringUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Composite;
/**
* @date 2017年9月8日 下午1:16:30
* @author gongml
* @desc ActiveX Composite to open all kinds of files
*/
public class OLEComposite extends Composite{
private OleClientSite clientSite;
private OleFrame oleFrame;
private String fileName=null;
public OLEComposite(Composite parent, String filename) {
super(parent, SWT.NONE);
parent.setLayout(GridLayout(1));
this.fileName=filename;
Composite composite=new Composite(parent, SWT.NONE);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
composite.setLayout(GridLayout(1));
oleFrame = new OleFrame(composite, SWT.NONE);
oleFrame.setLayoutData(new GridData(GridData.FILL_BOTH));
oleFrame.setLayout(GridLayout(1));
fileOpen(fileName);
}
private void disposeClient() {
if (clientSite != null)
clientSite.dispose();
clientSite = null;
}
private void fileOpen(String fileName) {
if (StringUtils.isEmpty(fileName)) {
return;
}
disposeClient();
// try opening a .doc file using Word
if (clientSite == null) {
int index = fileName.lastIndexOf('.');
if (index != -1) {
String fileExtension = fileName.substring(index + 1);
if (fileExtension.equalsIgnoreCase("doc") ||
fileExtension.equalsIgnoreCase("docx") ||
fileExtension.equalsIgnoreCase("rtf") ||
fileExtension.equalsIgnoreCase("txt")) {
try {
clientSite = new OleClientSite(oleFrame, SWT.NONE, "Word.Document", new File(fileName));
clientSite.setLayoutData(new GridData(GridData.FILL_BOTH));
} catch (SWTException error2) {
disposeClient();
}
}
}
}
// try opening a xls/xlsx file with Excel
if (clientSite == null) {
int index = fileName.lastIndexOf('.');
if (index != -1) {
String fileExtension = fileName.substring(index + 1);
if (fileExtension.equalsIgnoreCase("xls") ||
fileExtension.equalsIgnoreCase("xlsx")) {
try {
clientSite = new OleClientSite(oleFrame, SWT.NONE, "Excel.Sheet", new File(fileName));
clientSite.setLayoutData(new GridData(GridData.FILL_BOTH));
} catch (SWTException error2) {
disposeClient();
}
}
}
}
// try opening a media file with MPlayer
if (clientSite == null) {
int index = fileName.lastIndexOf('.');
if (index != -1) {
String fileExtension = fileName.substring(index + 1);
if (fileExtension.equalsIgnoreCase("mpa")) {
try {
clientSite = new OleClientSite(oleFrame, SWT.NONE, "MPlayer", new File(fileName));
clientSite.setLayoutData(new GridData(GridData.FILL_BOTH));
} catch (SWTException error2) {
disposeClient();
}
}
}
}
// try opening with wmv, mpg, mpeg, avi, asf, wav with WMPlayer
if (clientSite == null) {
int index = fileName.lastIndexOf('.');
if (index != -1) {
String fileExtension = fileName.substring(index + 1);
if (fileExtension.equalsIgnoreCase("wmv")
|| fileExtension.equalsIgnoreCase("mpg")
|| fileExtension.equalsIgnoreCase("mpeg")
|| fileExtension.equalsIgnoreCase("avi")
|| fileExtension.equalsIgnoreCase("asf")
|| fileExtension.equalsIgnoreCase("wav")) {
try {
clientSite = new OleClientSite(oleFrame, SWT.NONE, "WMPlayer.OCX");
clientSite.setLayoutData(new GridData(GridData.FILL_BOTH));
clientSite.setLayout(GridLayout(1));
OleAutomation player = new OleAutomation(clientSite);
int playURL[] = player.getIDsOfNames(new String[] { "URL" });
if (playURL != null) {
boolean suceeded = player.setProperty(playURL[0], new Variant(fileName));
if (!suceeded)
disposeClient();
} else {
disposeClient();
}
player.dispose();
} catch (SWTException error2) {
disposeClient();
}
}
}
}
// try opening a PDF file with Acrobat reader
if (clientSite == null) {
int index = fileName.lastIndexOf('.');
if (index != -1) {
String fileExtension = fileName.substring(index + 1);
if (fileExtension.equalsIgnoreCase("pdf")) {
try {
clientSite = new OleClientSite(oleFrame, SWT.NONE, "PDF.PdfCtrl.5");
clientSite.setLayoutData(new GridData(GridData.FILL_BOTH));
clientSite.setLayout(GridLayout(1));
clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
OleAutomation pdf = new OleAutomation (clientSite);
int loadFile[] = pdf.getIDsOfNames (new String [] {"LoadFile"});
if (loadFile != null) {
Variant result = pdf.invoke(loadFile[0], new Variant[] {new Variant(fileName)});
if (result == null)
disposeClient();
else
result.dispose();
} else {
disposeClient();
}
pdf.dispose();
} catch (SWTException error2) {
disposeClient();
}
}
}
}
// try opening with Explorer
if (clientSite == null) {
try {
clientSite = new OleClientSite(oleFrame, SWT.NONE, "Shell.Explorer");
clientSite.setLayoutData(new GridData(GridData.FILL_BOTH));
clientSite.setLayout(GridLayout(1));
OleAutomation explorer = new OleAutomation(clientSite);
int[] navigate = explorer.getIDsOfNames(new String[]{"Navigate"});
if (navigate != null) {
Variant result = explorer.invoke(navigate[0], new Variant[] {new Variant(fileName)});
if (result == null)
disposeClient();
else
result.dispose();
} else {
disposeClient();
}
explorer.dispose();
} catch (SWTException error2) {
disposeClient();
}
}
if (clientSite != null){
clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
}
}
}