Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » {SWT Shell} How to open pdf file with the SWT shell ?(How to open pdf file with the SWT shell ?)
icon8.gif  {SWT Shell} How to open pdf file with the SWT shell ? [message #1749211] Sat, 03 December 2016 06:54 Go to next message
Sumit Fataniya is currently offline Sumit FataniyaFriend
Messages: 12
Registered: July 2016
Junior Member
Currently, given below code file opening via default application,
This file open externally but Now I want to this .pdf file open within the SWT shell how to do to this?

public class FileDialogOpen {
Display d; Shell s;
FileDialogOpen() {
d = new Display();
s = new Shell(d);
s.setLayout(new GridLayout(1, false));
s.setSize(400, 400);

s.setText("A MessageBox Example");
Menu m = new Menu(s, SWT.BAR);
final MenuItem file = new MenuItem(m, SWT.CASCADE);
file.setText("&File");
final Menu filemenu = new Menu(s, SWT.DROP_DOWN);
file.setMenu(filemenu);
final MenuItem openItem = new MenuItem(filemenu, SWT.PUSH);
class Open implements SelectionListener {
public void widgetSelected(SelectionEvent event) {
FileDialog fd = new FileDialog(s, SWT.OPEN);
fd.setText("Open");
String[] filterExt = { "*.pdf", "*.docx", ".xlsx", "*.doc",
"All Files(*).*" };

fd.setFilterExtensions(filterExt);
String path = fd.open();

if (path != null) {
File file = new File(path);
if (file.getName().endsWith(".pdf")) {
Program.launch(path);
}
if (file.getName().endsWith(".doc")) {
Program.launch(path);
}
if (file.getName().endsWith(".xlsx")) {
Program.launch(path);
}
}

}
}
openItem.addSelectionListener(new Open());

s.setMenuBar(m);
s.open();
d.dispose();
}
public static void main(String[] argv) {
new FileDialogOpen();
}

Re: {SWT Shell} How to open pdf file with the SWT shell ? [message #1749466 is a reply to message #1749211] Wed, 07 December 2016 05:33 Go to previous message
Sumit Fataniya is currently offline Sumit FataniyaFriend
Messages: 12
Registered: July 2016
Junior Member
This is possible for .pdf,.doc file etc.

Referance Link: http://alvinalexander.com/java/jwarehouse/eclipse/org.eclipse.swt.examples.ole.win32/src/org/eclipse/swt/examples/ole/win32/OLEExample.java.shtml

I think, is might be useful.
This is given blow code:

import java.io.*;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.ole.win32.*;
import org.eclipse.swt.widgets.*;

/**
* OLEExample is an example that uses <code>org.eclipse.swt
* libraries to implement a simple SWT window that can host different Active X
* controls.
*
* @since 3.3
*/
public class OLEExample {

OleClientSite clientSite;
OleFrame oleFrame;
Button closeButton;

public static void main(String[] args) {
Display display = new Display();
OLEExample example = new OLEExample();
example.open(display);
display.dispose();
}

/**
* Create a file Exit menu item
*/
void addFileMenu(OleFrame frame) {
final Shell shell = frame.getShell();
Menu menuBar = shell.getMenuBar();
if (menuBar == null) {
menuBar = new Menu(shell, SWT.BAR);
shell.setMenuBar(menuBar);
}
MenuItem fileMenu = new MenuItem(menuBar, SWT.CASCADE);
fileMenu.setText("&File");
Menu menuFile = new Menu(fileMenu);
fileMenu.setMenu(menuFile);
frame.setFileMenus(new MenuItem[] { fileMenu });

MenuItem menuFileExit = new MenuItem(menuFile, SWT.CASCADE);
menuFileExit.setText("Exit");
menuFileExit.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.dispose();
}
});
}

void disposeClient() {
if (clientSite != null)
clientSite.dispose();
clientSite = null;
}

/**
* Prompt the user for a file and try to open it with some known ActiveX controls.
*/
void fileOpen() {
Shell shell = oleFrame.getShell();
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
String fileName = dialog.open();
if (fileName == null) 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("rtf") ||
fileExtension.equalsIgnoreCase("txt")) {
try {
clientSite = new OleClientSite(oleFrame, SWT.NONE, "Word.Document", new File(fileName));
} catch (SWTException error2) {
disposeClient();
}
}
}
}

// try opening a xls file with Excel
if (clientSite == null) {
int index = fileName.lastIndexOf('.');
if (index != -1) {
String fileExtension = fileName.substring(index + 1);
if (fileExtension.equalsIgnoreCase("xls")) {
try {
clientSite = new OleClientSite(oleFrame, SWT.NONE, "Excel.Sheet", new File(fileName));
} 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));
} 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");
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.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");
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);
}
}

void newClientSite(String progID) {
disposeClient();
try {
clientSite = new OleClientSite(oleFrame, SWT.NONE, progID);
} catch (SWTException error) {

}
if (clientSite != null)
clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
}

public void open(Display display) {
Shell shell = new Shell(display);
shell.setText("OLE Example");
shell.setLayout(new FillLayout());

Composite parent = new Composite(shell, SWT.NONE);
parent.setLayout(new GridLayout(4, true));

Composite buttons = new Composite(parent, SWT.NONE);
buttons.setLayout(new GridLayout());
GridData gridData = new GridData(SWT.BEGINNING, SWT.FILL, false, false);
buttons.setLayoutData(gridData);

Composite displayArea = new Composite(parent, SWT.BORDER);
displayArea.setLayout(new FillLayout());
displayArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));

Button excelButton = new Button(buttons, SWT.RADIO);
excelButton.setText("New Excel Sheet");
excelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (((Button) e.widget).getSelection())
newClientSite("Excel.Sheet");
}
});
Button mediaPlayerButton = new Button(buttons, SWT.RADIO);
mediaPlayerButton.setText("New MPlayer");
mediaPlayerButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (((Button) e.widget).getSelection())
newClientSite("MPlayer");
}
});
Button powerPointButton = new Button(buttons, SWT.RADIO);
powerPointButton.setText("New PowerPoint Slide");
powerPointButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (((Button) e.widget).getSelection())
newClientSite("PowerPoint.Slide");
}
});
Button wordButton = new Button(buttons, SWT.RADIO);
wordButton.setText("New Word Document");
wordButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (((Button) e.widget).getSelection())
newClientSite("Word.Document");
}
});
new Label(buttons, SWT.NONE);
Button openButton = new Button(buttons, SWT.RADIO);
openButton.setText("Open file...");
openButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (((Button) e.widget).getSelection())
fileOpen();
}
});
new Label(buttons, SWT.NONE);
closeButton = new Button(buttons, SWT.RADIO);
closeButton.setText("Close file");
closeButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (((Button) e.widget).getSelection())
disposeClient();
}
});
closeButton.setSelection(true);

oleFrame = new OleFrame(displayArea, SWT.NONE);
addFileMenu(oleFrame);

shell.setSize(800, 600);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
Previous Topic:SWT on Neon not rendering some window controls fully with GTK3
Next Topic:Generation of SWT based GUIs/Wizards
Goto Forum:
  


Current Time: Mon Dec 11 09:42:08 GMT 2017

Powered by FUDForum. Page generated in 0.01518 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software