sourceforge 1386454 now supporting return values in event handlers. also added visio example of this
This commit is contained in:
85
samples/com/jacob/samples/visio/VisioApp.java
Normal file
85
samples/com/jacob/samples/visio/VisioApp.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package com.jacob.samples.visio;
|
||||
|
||||
import com.jacob.com.*;
|
||||
import com.jacob.activeX.*;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Created as part of sourceforge 1386454 to demonstrate returning values in event handlers
|
||||
* @author miles@rowansoftware.net
|
||||
*
|
||||
* This class represents the visio app itself
|
||||
*/
|
||||
public class VisioApp extends ActiveXComponent {
|
||||
|
||||
|
||||
public VisioApp() throws VisioException {
|
||||
super("Visio.Application");
|
||||
setVisible(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a DispatchEvents boject to register o as a listener
|
||||
* @param o
|
||||
*/
|
||||
public void addEventListener(VisioEventListener o) {
|
||||
DispatchEvents events = new DispatchEvents(this, o);
|
||||
if (events == null){
|
||||
System.out.println("You should never get null back when creating a DispatchEvents object");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void open(File f) throws VisioException {
|
||||
try {
|
||||
ActiveXComponent documents = new ActiveXComponent(getProperty("Documents").toDispatch());
|
||||
Variant[] args = new Variant[1];
|
||||
args[0] = new Variant(f.getPath());
|
||||
documents.invoke("Open",args);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new VisioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void save() throws VisioException {
|
||||
try {
|
||||
ActiveXComponent document = new ActiveXComponent(getProperty("ActiveDocument").toDispatch());
|
||||
document.invoke("Save");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new VisioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* terminates visio
|
||||
*/
|
||||
public void quit() {
|
||||
System.out.println("Received quit()");
|
||||
// there can't be any open documents for this to work
|
||||
// you'll get a visio error if you don't close them
|
||||
ActiveXComponent document = new ActiveXComponent(getProperty("ActiveDocument").toDispatch());
|
||||
document.invoke("Close");
|
||||
invoke("Quit");
|
||||
}
|
||||
|
||||
public void export(File f) throws VisioException {
|
||||
try {
|
||||
ActiveXComponent document = new ActiveXComponent(getProperty("ActivePage").toDispatch());
|
||||
Variant[] args = new Variant[1];
|
||||
args[0] = new Variant(f.getPath());
|
||||
document.invoke("Export",args);
|
||||
} catch (Exception e) {
|
||||
throw new VisioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setVisible(boolean b) throws VisioException {
|
||||
try {
|
||||
setProperty("Visible",new Variant(b));
|
||||
} catch (Exception e) {
|
||||
throw new VisioException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
120
samples/com/jacob/samples/visio/VisioAppFacade.java
Normal file
120
samples/com/jacob/samples/visio/VisioAppFacade.java
Normal file
@@ -0,0 +1,120 @@
|
||||
package com.jacob.samples.visio;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Created as part of sourceforge 1386454 to demonstrate returning values in event handlers
|
||||
* @author miles@rowansoftware.net
|
||||
*
|
||||
* This singleton isolates the demo app from the Visio instance object so that
|
||||
* you can't try and send messages to a dead Visio instance after quit() has been
|
||||
* called. Direct consumption of VisioApp would mean you could quit but would
|
||||
* still have a handle to the no longer connected application proxy
|
||||
*
|
||||
*/
|
||||
public class VisioAppFacade {
|
||||
|
||||
|
||||
private VisioApp app;
|
||||
private static VisioAppFacade instance;
|
||||
|
||||
public static final String IMAGE_EXT = ".jpg";
|
||||
public static final String VISIO_EXT = ".vsd";
|
||||
public static final int BUFFER_SIZE = 2048;
|
||||
|
||||
private VisioAppFacade() throws VisioException {
|
||||
this.app = new VisioApp();
|
||||
app.addEventListener(new VisioEventAdapter(app));
|
||||
}
|
||||
|
||||
public static VisioAppFacade getInstance() throws VisioException {
|
||||
if (instance == null) {
|
||||
instance = new VisioAppFacade();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public byte[] createPreview(byte[] visioData) throws VisioException {
|
||||
byte[] preview;
|
||||
File tmpFile;
|
||||
try {
|
||||
tmpFile = getTempVisioFile();
|
||||
OutputStream out = new FileOutputStream(tmpFile);
|
||||
out.write(visioData);
|
||||
out.close();
|
||||
} catch (IOException ioe) {
|
||||
throw new VisioException(ioe);
|
||||
}
|
||||
preview = createPreview(tmpFile);
|
||||
tmpFile.delete();
|
||||
return preview;
|
||||
}
|
||||
|
||||
public byte[] createPreview(File visioFile) throws VisioException {
|
||||
try {
|
||||
File imageFile;
|
||||
imageFile = getTempImageFile();
|
||||
app.open(visioFile);
|
||||
app.export(imageFile);
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
FileInputStream fin = new FileInputStream(imageFile);
|
||||
copy(fin, bout);
|
||||
fin.close();
|
||||
imageFile.delete();
|
||||
bout.close();
|
||||
return bout.toByteArray();
|
||||
} catch (IOException ioe) {
|
||||
throw new VisioException(ioe);
|
||||
}
|
||||
}
|
||||
|
||||
private void copy(InputStream in, OutputStream out) throws IOException {
|
||||
byte[] buff = new byte[BUFFER_SIZE];
|
||||
int read;
|
||||
do {
|
||||
read = in.read(buff);
|
||||
if (read > 0) {
|
||||
out.write(buff,0,read);
|
||||
}
|
||||
} while (read > 0);
|
||||
}
|
||||
|
||||
public byte[] createPreview(InputStream in) throws VisioException {
|
||||
byte[] preview;
|
||||
//byte[] buff = new byte[2048];
|
||||
//int read = 0;
|
||||
OutputStream out;
|
||||
File tmpFile;
|
||||
|
||||
try {
|
||||
tmpFile = getTempVisioFile();
|
||||
out = new FileOutputStream(tmpFile);
|
||||
copy(in, out);
|
||||
out.close();
|
||||
} catch (IOException ioe) {
|
||||
throw new VisioException(ioe);
|
||||
}
|
||||
|
||||
preview = createPreview(tmpFile);
|
||||
tmpFile.delete();
|
||||
return preview;
|
||||
}
|
||||
|
||||
public void editDiagram(File f) throws VisioException {
|
||||
app.open(f);
|
||||
app.setVisible(true);
|
||||
}
|
||||
|
||||
private File getTempVisioFile() throws IOException {
|
||||
return File.createTempFile("java",VISIO_EXT);
|
||||
}
|
||||
|
||||
private File getTempImageFile() throws IOException {
|
||||
return File.createTempFile("java",IMAGE_EXT);
|
||||
}
|
||||
|
||||
public void quit() {
|
||||
app.quit();
|
||||
instance = null;
|
||||
}
|
||||
}
|
||||
181
samples/com/jacob/samples/visio/VisioDemo.java
Normal file
181
samples/com/jacob/samples/visio/VisioDemo.java
Normal file
@@ -0,0 +1,181 @@
|
||||
package com.jacob.samples.visio;
|
||||
import javax.swing.*;
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
|
||||
import com.jacob.com.ComThread;
|
||||
|
||||
import java.io.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
/**
|
||||
* Created as part of sourceforge 1386454 to demonstrate returning values in event handlers
|
||||
* @author miles@rowansoftware.net
|
||||
*
|
||||
* This file contains the main() that runs the demo
|
||||
*
|
||||
* This can be run in Eclipse with options
|
||||
* <pre>
|
||||
* -Djava.library.path=d:/jacob/release -Dcom.jacob.autogc=false
|
||||
* -Dcom.jacob.debug=false
|
||||
* </pre>
|
||||
*/
|
||||
public class VisioDemo extends JFrame implements ActionListener, WindowListener {
|
||||
|
||||
/**
|
||||
* Totally dummy value to make Eclipse quit complaining
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
JButton chooseButton;
|
||||
JButton openButton;
|
||||
JPanel buttons;
|
||||
|
||||
ImageIcon theImage;
|
||||
JLabel theLabel; // the icon on the page is actually this button's icon
|
||||
|
||||
File selectedFile;
|
||||
/** everyone should get this through getVisio() */
|
||||
private VisioAppFacade visioProxy = null;
|
||||
|
||||
// put this up here so it remembers where we were on the last choose
|
||||
JFileChooser chooser = null;
|
||||
|
||||
|
||||
public class VisioFileFilter extends FileFilter {
|
||||
public boolean accept(File f) {
|
||||
if (f.isDirectory()){
|
||||
return true;
|
||||
} else {
|
||||
return (f.getName().toUpperCase().endsWith(".VSD"));
|
||||
}
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return "Visio Drawings";
|
||||
}
|
||||
}
|
||||
|
||||
public VisioDemo() {
|
||||
super("Visio in Swing POC");
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
buttons = new JPanel();
|
||||
getContentPane().setLayout(new BorderLayout());
|
||||
chooseButton = new JButton("Choose file to display");
|
||||
openButton = new JButton("Open file chosen file in Visio");
|
||||
chooseButton.addActionListener(this);
|
||||
openButton.addActionListener(this);
|
||||
buttons.add(chooseButton);
|
||||
buttons.add(openButton);
|
||||
getContentPane().add(buttons, BorderLayout.SOUTH);
|
||||
theLabel = new JLabel("");
|
||||
getContentPane().add(theLabel, BorderLayout.CENTER);
|
||||
addWindowListener(this);
|
||||
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
||||
setSize(640,480);
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
SwingUtilities.invokeLater(new Runnable(){
|
||||
public void run(){
|
||||
ComThread.InitSTA();
|
||||
VisioDemo poc = new VisioDemo();
|
||||
ComThread.Release();
|
||||
if (poc == null){
|
||||
System.out.println("poc== null? That should never happen!");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (e.getSource() == chooseButton) {
|
||||
pickFile();
|
||||
} else if (e.getSource() == openButton) {
|
||||
try {
|
||||
openFile();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
} else {
|
||||
System.out.println("Awesome!");
|
||||
}
|
||||
}
|
||||
|
||||
private void pickFile() {
|
||||
try {
|
||||
chooser = new JFileChooser();
|
||||
// comment this out if you want it to always go to myDocuments
|
||||
chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
|
||||
chooser.setFileFilter(new VisioFileFilter());
|
||||
int returnVal = chooser.showOpenDialog(this);
|
||||
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
||||
selectedFile = chooser.getSelectedFile();
|
||||
showSelectedFilePreview();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* use this private method instead of initializing on boot up so that instance
|
||||
* and all listeners are created in this thread (event thread) rather than root thread
|
||||
* @return
|
||||
*/
|
||||
private VisioAppFacade getVisio(){
|
||||
if (visioProxy == null){
|
||||
try {
|
||||
visioProxy = VisioAppFacade.getInstance();
|
||||
} catch (VisioException ve){
|
||||
System.out.println("ailed to openFile()");
|
||||
ve.printStackTrace();
|
||||
}
|
||||
}
|
||||
return visioProxy;
|
||||
}
|
||||
private void showSelectedFilePreview() throws VisioException {
|
||||
if (selectedFile != null) {
|
||||
byte[] image = getVisio().createPreview(selectedFile);
|
||||
theImage = new ImageIcon(image);
|
||||
theLabel.setIcon(theImage);
|
||||
}
|
||||
}
|
||||
|
||||
private void openFile() throws VisioException {
|
||||
try {
|
||||
getVisio().editDiagram(selectedFile);
|
||||
showSelectedFilePreview();
|
||||
} catch (VisioException ve){
|
||||
System.out.println("ailed to openFile()");
|
||||
ve.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void windowActivated(WindowEvent e) {
|
||||
}
|
||||
|
||||
public void windowClosed(WindowEvent e) {
|
||||
System.out.println("WINDOW CLOSED");
|
||||
if (visioProxy != null){
|
||||
visioProxy.quit();
|
||||
}
|
||||
}
|
||||
public void windowClosing(WindowEvent e){
|
||||
}
|
||||
|
||||
public void windowDeactivated(WindowEvent e){
|
||||
}
|
||||
|
||||
public void windowDeiconified(WindowEvent e){
|
||||
}
|
||||
|
||||
public void windowIconified(WindowEvent e){
|
||||
System.out.println("Fooboo");
|
||||
}
|
||||
public void windowOpened(WindowEvent e){
|
||||
}
|
||||
}
|
||||
59
samples/com/jacob/samples/visio/VisioEventAdapter.java
Normal file
59
samples/com/jacob/samples/visio/VisioEventAdapter.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package com.jacob.samples.visio;
|
||||
import com.jacob.com.*;
|
||||
|
||||
/**
|
||||
* Created as part of sourceforge 1386454 to demonstrate returning values in event handlers
|
||||
* @author miles@rowansoftware.net
|
||||
*
|
||||
* You can subclass this class and only implement the methods you're interested in
|
||||
*/
|
||||
public class VisioEventAdapter implements VisioEventListener {
|
||||
|
||||
VisioApp app = null;
|
||||
|
||||
public VisioEventAdapter(VisioApp pApp){
|
||||
app = pApp;
|
||||
System.out.println("Event listener constructed");
|
||||
}
|
||||
|
||||
public void BeforeQuit(Variant[] args){ }
|
||||
|
||||
public void DocumentChanged(Variant[] args){
|
||||
System.out.println("documentChanged()");
|
||||
}
|
||||
|
||||
public void DocumentCloseCanceled(Variant[] args){ }
|
||||
|
||||
public void DocumentCreated(Variant[] args){ }
|
||||
|
||||
public void DocumentOpened(Variant[] args){
|
||||
System.out.println("DocumentOpened()");
|
||||
}
|
||||
|
||||
public void DocumentSaved(Variant[] args){ }
|
||||
|
||||
public void DocumentSavedAs(Variant[] args){ }
|
||||
|
||||
public Variant QueryCancelDocumentClose(Variant[] args){
|
||||
System.out.println("QueryCancelDocumentClose()");
|
||||
return new Variant(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* we don't actually let it quit. We block it so
|
||||
* that we don't have to relaunch when we look at a new document
|
||||
*/
|
||||
public Variant QueryCancelQuit(Variant[] args) {
|
||||
// these may throw VisioException
|
||||
System.out.println("Saving document, hiding and telling visio not to quit");
|
||||
try {
|
||||
app.save();
|
||||
app.setVisible(false);
|
||||
} catch (VisioException ve){
|
||||
System.out.println("ailed to openFile()");
|
||||
ve.printStackTrace();
|
||||
}
|
||||
return new Variant(true);
|
||||
}
|
||||
}
|
||||
|
||||
33
samples/com/jacob/samples/visio/VisioEventListener.java
Normal file
33
samples/com/jacob/samples/visio/VisioEventListener.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.jacob.samples.visio;
|
||||
import com.jacob.com.*;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Created as part of sourceforge 1386454 to demonstrate returning values in event handlers
|
||||
* @author miles@rowansoftware.net
|
||||
*
|
||||
* There are many more Visio events available. See the Microsoft
|
||||
* Office SDK documentation. To receive an event, add a method to this interface
|
||||
* whose name matches the event name and has only one parameter, Variant[].
|
||||
* The JACOB library will use reflection to call that method when an event is received.
|
||||
*/
|
||||
public interface VisioEventListener {
|
||||
|
||||
public void BeforeQuit(Variant[] args);
|
||||
|
||||
public void DocumentChanged(Variant[] args);
|
||||
|
||||
public void DocumentCloseCanceled(Variant[] args);
|
||||
|
||||
public void DocumentCreated(Variant[] args);
|
||||
|
||||
public void DocumentOpened(Variant[] args);
|
||||
|
||||
public void DocumentSaved(Variant[] args);
|
||||
|
||||
public void DocumentSavedAs(Variant[] args);
|
||||
|
||||
public Variant QueryCancelQuit(Variant[] args);
|
||||
}
|
||||
|
||||
22
samples/com/jacob/samples/visio/VisioException.java
Normal file
22
samples/com/jacob/samples/visio/VisioException.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.jacob.samples.visio;
|
||||
|
||||
/**
|
||||
* Created as part of sourceforge 1386454 to demonstrate returning values in event handlers
|
||||
* @author miles@rowansoftware.net
|
||||
*
|
||||
* This extends runtime exception so that we can be sloppy and not put catch blocks everywhere
|
||||
*/
|
||||
public class VisioException extends Exception {
|
||||
/**
|
||||
* Totally dummy value to make Eclipse quit complaining
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public VisioException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public VisioException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user