Skip to content

Commit

Permalink
Merge pull request #2 from exceljava/jinx-2
Browse files Browse the repository at this point in the history
Updates for Jinx 2.0
  • Loading branch information
tonyroberts authored Nov 13, 2019
2 parents 971c624 + 8b23e82 commit 2d67583
Show file tree
Hide file tree
Showing 19 changed files with 1,704 additions and 18 deletions.
Binary file modified examples/jinx-com4j-examples.xlsx
Binary file not shown.
7 changes: 4 additions & 3 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.exceljava</groupId>
<artifactId>jinx-com4j-root</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0</version>
<relativePath>..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand All @@ -16,6 +16,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand All @@ -27,12 +28,12 @@
<dependency>
<groupId>com.exceljava</groupId>
<artifactId>jinx-com4j</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.exceljava</groupId>
<artifactId>jinx</artifactId>
<version>1.6.0</version>
<version>[2.0.0,)</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import com.exceljava.com4j.JinxBridge;
import com.exceljava.com4j.excel.*;
import com4j.Com4jObject;
import com.exceljava.jinx.ExcelReference;
import com.exceljava.jinx.IUnknown;

import javax.swing.*;
import java.awt.*;

/**
* Example macros that use com4j to call back into Excel
Expand Down Expand Up @@ -76,4 +80,32 @@ public void scrollbarExample() {
// Set the cell value from the scrollbar value
range.setValue(scrollbar.getValue());
}

@ExcelMacro(
value = "jinx.show_object_example",
shortcut = "Ctrl+Shift+I"
)
public void showObject() throws HeadlessException {
// Get the current selection
_Application app = JinxBridge.getApplication(xl);
Range selection = app.getSelection().queryInterface(Range.class);

// Ensure the cell is calculated
selection.setFormula(selection.getFormula());
selection.calculate();

// Get an ExcelReference corresponding to the selection
IUnknown unk = JinxBridge.getIUnknown(selection);
ExcelReference cell = xl.getReference(unk);

// Find the cached object for this cell
Object cachedObject = xl.getCachedObject(cell);

// Popup a non-modal dialog with the string representation of the object
String message = cachedObject != null ? cachedObject.toString() : "NULL";
JOptionPane pane = new JOptionPane(message, JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog("Java Object");
dialog.setModal(false);
dialog.setVisible(true);
}
}
5 changes: 3 additions & 2 deletions jinx-com4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.exceljava</groupId>
<artifactId>jinx-com4j-root</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0</version>
<relativePath>..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand All @@ -26,7 +26,7 @@
<dependency>
<groupId>com.exceljava</groupId>
<artifactId>jinx</artifactId>
<version>[1.0.0,)</version>
<version>[2.0.0,)</version>
</dependency>
<!-- com4j -->
<dependency>
Expand All @@ -47,6 +47,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand Down
48 changes: 48 additions & 0 deletions jinx-com4j/src/main/java/com/exceljava/com4j/IUnknownAdaptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.exceljava.com4j;

import com.exceljava.jinx.IUnknown;
import com4j.Com4jObject;

/**
* Adaptor class that implements the IUnknown interface
* wrapping a Com4jObject instance.
*/
class IUnknownAdaptor implements IUnknown {
private Com4jObject obj;

public IUnknownAdaptor(Com4jObject obj) {
this.obj = obj;
}

private static <T extends Com4jObject> Class<T> adaptClass(Class<?> cls) {
return (Class<T>)cls;
}

@Override
public <T> T queryInterface(Class<T> cls) {
Com4jObject obj = this.obj;
if (null != obj && cls.isAssignableFrom(Com4jObject.class)) {
return obj.queryInterface(adaptClass(cls));
}
return null;
}

@Override
public long getPointer(boolean addRef) {
if (addRef) {
throw new UnsupportedOperationException();
}

Com4jObject obj = this.obj;
if (null != obj) {
return obj.getIUnknownPointer();
}

return 0;
}

@Override
public void close() {
this.obj = null;
}
}
46 changes: 35 additions & 11 deletions jinx-com4j/src/main/java/com/exceljava/com4j/JinxBridge.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package com.exceljava.com4j;

import com.exceljava.jinx.ExcelAddIn;
import com.exceljava.jinx.ExcelArgumentConverter;
import com.exceljava.jinx.IUnknown;
import com.exceljava.com4j.excel._Application;
import com4j.COM4J;
import com4j.Com4jObject;
import com4j.ComThread;
import com4j.ROT;


/**
* Bridge between the Jinx add-in and com4j.
* Used for obtaining com4j COM wrappers from code running in Excel using Jinx.
*/
public class JinxBridge {

private static final ThreadLocal<_Application> xlApp = new ThreadLocal<_Application>() {
public _Application initialValue() {
return null;
}
};

/**
* Gets the Excel Application object for the current Excel process.
*
Expand All @@ -34,7 +28,37 @@ public _Application initialValue() {
* @return An Excel Application instance.
*/
public static _Application getApplication(ExcelAddIn xl) {
Com4jObject unk = COM4J.wrapSta(Com4jObject.class, xl.getExcelApplication());
return unk.queryInterface(_Application.class);
return xl.getExcelApplication(_Application.class);
}

/**
* Return an IUnknown instance that wraps a Com4jObject.
*
* This can be used for passing Com4jObjects back to Jinx
* methods requiring an IUnknown instance.
*
* @param object COM object to be wrapped as an IUnknown.
* @return Instance implementing IUnknown.
*/
public static IUnknown getIUnknown(Com4jObject object) {
return new IUnknownAdaptor(object);
}

/**
* Converts IUnknown to any Com4jObject type.
*
* This allows Com4jObjects to be used as method arguments where
* an IUnknown would be passed by Jinx. For example, in the
* ribbon actions.
*
* @param unk IUnknown instance.
* @param cls Class of type to cast to.
* @param <T> Type to cast to.
* @return IUnknown instance cast to a Com4jObject instance.
*/
@ExcelArgumentConverter
public static <T extends Com4jObject> T convertIUnknown(IUnknown unk, Class<T> cls) {
Com4jObject obj = COM4J.wrapSta(Com4jObject.class, unk.getPointer(true));
return obj.queryInterface(cls);
}
}
75 changes: 75 additions & 0 deletions jinx-com4j/src/main/java/com/exceljava/com4j/excel/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.exceljava.com4j.excel ;

import com4j.*;

@IID("{00020400-0000-0000-C000-000000000046}")
public interface Author extends Com4jObject {
// Methods:
/**
* <p>
* Getter method for the COM property "Application"
* </p>
*/

@DISPID(148)
@PropGet
com.exceljava.com4j.excel._Application getApplication();


/**
* <p>
* Getter method for the COM property "Creator"
* </p>
*/

@DISPID(149)
@PropGet
com.exceljava.com4j.excel.XlCreator getCreator();


/**
* <p>
* Getter method for the COM property "Parent"
* </p>
*/

@DISPID(150)
@PropGet
com4j.Com4jObject getParent();


/**
* <p>
* Getter method for the COM property "Name"
* </p>
*/

@DISPID(110)
@PropGet
java.lang.String getName();


/**
* <p>
* Getter method for the COM property "ProviderID"
* </p>
*/

@DISPID(3286)
@PropGet
java.lang.String getProviderID();


/**
* <p>
* Getter method for the COM property "UserID"
* </p>
*/

@DISPID(3287)
@PropGet
java.lang.String getUserID();


// Properties:
}
Loading

0 comments on commit 2d67583

Please sign in to comment.