Skip to content

Commit 2d67583

Browse files
authored
Merge pull request #2 from exceljava/jinx-2
Updates for Jinx 2.0
2 parents 971c624 + 8b23e82 commit 2d67583

File tree

19 files changed

+1704
-18
lines changed

19 files changed

+1704
-18
lines changed

examples/jinx-com4j-examples.xlsx

1.12 KB
Binary file not shown.

examples/pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.exceljava</groupId>
77
<artifactId>jinx-com4j-root</artifactId>
8-
<version>1.0-SNAPSHOT</version>
8+
<version>1.1.0</version>
99
<relativePath>..</relativePath>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>
@@ -16,6 +16,7 @@
1616
<plugin>
1717
<groupId>org.apache.maven.plugins</groupId>
1818
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>3.8.1</version>
1920
<configuration>
2021
<source>1.8</source>
2122
<target>1.8</target>
@@ -27,12 +28,12 @@
2728
<dependency>
2829
<groupId>com.exceljava</groupId>
2930
<artifactId>jinx-com4j</artifactId>
30-
<version>1.0-SNAPSHOT</version>
31+
<version>1.1.0</version>
3132
</dependency>
3233
<dependency>
3334
<groupId>com.exceljava</groupId>
3435
<artifactId>jinx</artifactId>
35-
<version>1.6.0</version>
36+
<version>[2.0.0,)</version>
3637
</dependency>
3738
</dependencies>
3839
</project>

examples/src/main/java/com/exceljava/com4j/examples/MacroFunctions.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
import com.exceljava.com4j.JinxBridge;
77
import com.exceljava.com4j.excel.*;
8-
import com4j.Com4jObject;
8+
import com.exceljava.jinx.ExcelReference;
9+
import com.exceljava.jinx.IUnknown;
10+
11+
import javax.swing.*;
12+
import java.awt.*;
913

1014
/**
1115
* Example macros that use com4j to call back into Excel
@@ -76,4 +80,32 @@ public void scrollbarExample() {
7680
// Set the cell value from the scrollbar value
7781
range.setValue(scrollbar.getValue());
7882
}
83+
84+
@ExcelMacro(
85+
value = "jinx.show_object_example",
86+
shortcut = "Ctrl+Shift+I"
87+
)
88+
public void showObject() throws HeadlessException {
89+
// Get the current selection
90+
_Application app = JinxBridge.getApplication(xl);
91+
Range selection = app.getSelection().queryInterface(Range.class);
92+
93+
// Ensure the cell is calculated
94+
selection.setFormula(selection.getFormula());
95+
selection.calculate();
96+
97+
// Get an ExcelReference corresponding to the selection
98+
IUnknown unk = JinxBridge.getIUnknown(selection);
99+
ExcelReference cell = xl.getReference(unk);
100+
101+
// Find the cached object for this cell
102+
Object cachedObject = xl.getCachedObject(cell);
103+
104+
// Popup a non-modal dialog with the string representation of the object
105+
String message = cachedObject != null ? cachedObject.toString() : "NULL";
106+
JOptionPane pane = new JOptionPane(message, JOptionPane.INFORMATION_MESSAGE);
107+
JDialog dialog = pane.createDialog("Java Object");
108+
dialog.setModal(false);
109+
dialog.setVisible(true);
110+
}
79111
}

jinx-com4j/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.exceljava</groupId>
77
<artifactId>jinx-com4j-root</artifactId>
8-
<version>1.0-SNAPSHOT</version>
8+
<version>1.1.0</version>
99
<relativePath>..</relativePath>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>
@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>com.exceljava</groupId>
2828
<artifactId>jinx</artifactId>
29-
<version>[1.0.0,)</version>
29+
<version>[2.0.0,)</version>
3030
</dependency>
3131
<!-- com4j -->
3232
<dependency>
@@ -47,6 +47,7 @@
4747
<plugin>
4848
<groupId>org.apache.maven.plugins</groupId>
4949
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>3.8.1</version>
5051
<configuration>
5152
<source>1.8</source>
5253
<target>1.8</target>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.exceljava.com4j;
2+
3+
import com.exceljava.jinx.IUnknown;
4+
import com4j.Com4jObject;
5+
6+
/**
7+
* Adaptor class that implements the IUnknown interface
8+
* wrapping a Com4jObject instance.
9+
*/
10+
class IUnknownAdaptor implements IUnknown {
11+
private Com4jObject obj;
12+
13+
public IUnknownAdaptor(Com4jObject obj) {
14+
this.obj = obj;
15+
}
16+
17+
private static <T extends Com4jObject> Class<T> adaptClass(Class<?> cls) {
18+
return (Class<T>)cls;
19+
}
20+
21+
@Override
22+
public <T> T queryInterface(Class<T> cls) {
23+
Com4jObject obj = this.obj;
24+
if (null != obj && cls.isAssignableFrom(Com4jObject.class)) {
25+
return obj.queryInterface(adaptClass(cls));
26+
}
27+
return null;
28+
}
29+
30+
@Override
31+
public long getPointer(boolean addRef) {
32+
if (addRef) {
33+
throw new UnsupportedOperationException();
34+
}
35+
36+
Com4jObject obj = this.obj;
37+
if (null != obj) {
38+
return obj.getIUnknownPointer();
39+
}
40+
41+
return 0;
42+
}
43+
44+
@Override
45+
public void close() {
46+
this.obj = null;
47+
}
48+
}
Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
package com.exceljava.com4j;
22

33
import com.exceljava.jinx.ExcelAddIn;
4+
import com.exceljava.jinx.ExcelArgumentConverter;
5+
import com.exceljava.jinx.IUnknown;
46
import com.exceljava.com4j.excel._Application;
57
import com4j.COM4J;
68
import com4j.Com4jObject;
7-
import com4j.ComThread;
8-
import com4j.ROT;
9+
910

1011
/**
1112
* Bridge between the Jinx add-in and com4j.
1213
* Used for obtaining com4j COM wrappers from code running in Excel using Jinx.
1314
*/
1415
public class JinxBridge {
15-
16-
private static final ThreadLocal<_Application> xlApp = new ThreadLocal<_Application>() {
17-
public _Application initialValue() {
18-
return null;
19-
}
20-
};
21-
2216
/**
2317
* Gets the Excel Application object for the current Excel process.
2418
*
@@ -34,7 +28,37 @@ public _Application initialValue() {
3428
* @return An Excel Application instance.
3529
*/
3630
public static _Application getApplication(ExcelAddIn xl) {
37-
Com4jObject unk = COM4J.wrapSta(Com4jObject.class, xl.getExcelApplication());
38-
return unk.queryInterface(_Application.class);
31+
return xl.getExcelApplication(_Application.class);
32+
}
33+
34+
/**
35+
* Return an IUnknown instance that wraps a Com4jObject.
36+
*
37+
* This can be used for passing Com4jObjects back to Jinx
38+
* methods requiring an IUnknown instance.
39+
*
40+
* @param object COM object to be wrapped as an IUnknown.
41+
* @return Instance implementing IUnknown.
42+
*/
43+
public static IUnknown getIUnknown(Com4jObject object) {
44+
return new IUnknownAdaptor(object);
45+
}
46+
47+
/**
48+
* Converts IUnknown to any Com4jObject type.
49+
*
50+
* This allows Com4jObjects to be used as method arguments where
51+
* an IUnknown would be passed by Jinx. For example, in the
52+
* ribbon actions.
53+
*
54+
* @param unk IUnknown instance.
55+
* @param cls Class of type to cast to.
56+
* @param <T> Type to cast to.
57+
* @return IUnknown instance cast to a Com4jObject instance.
58+
*/
59+
@ExcelArgumentConverter
60+
public static <T extends Com4jObject> T convertIUnknown(IUnknown unk, Class<T> cls) {
61+
Com4jObject obj = COM4J.wrapSta(Com4jObject.class, unk.getPointer(true));
62+
return obj.queryInterface(cls);
3963
}
4064
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.exceljava.com4j.excel ;
2+
3+
import com4j.*;
4+
5+
@IID("{00020400-0000-0000-C000-000000000046}")
6+
public interface Author extends Com4jObject {
7+
// Methods:
8+
/**
9+
* <p>
10+
* Getter method for the COM property "Application"
11+
* </p>
12+
*/
13+
14+
@DISPID(148)
15+
@PropGet
16+
com.exceljava.com4j.excel._Application getApplication();
17+
18+
19+
/**
20+
* <p>
21+
* Getter method for the COM property "Creator"
22+
* </p>
23+
*/
24+
25+
@DISPID(149)
26+
@PropGet
27+
com.exceljava.com4j.excel.XlCreator getCreator();
28+
29+
30+
/**
31+
* <p>
32+
* Getter method for the COM property "Parent"
33+
* </p>
34+
*/
35+
36+
@DISPID(150)
37+
@PropGet
38+
com4j.Com4jObject getParent();
39+
40+
41+
/**
42+
* <p>
43+
* Getter method for the COM property "Name"
44+
* </p>
45+
*/
46+
47+
@DISPID(110)
48+
@PropGet
49+
java.lang.String getName();
50+
51+
52+
/**
53+
* <p>
54+
* Getter method for the COM property "ProviderID"
55+
* </p>
56+
*/
57+
58+
@DISPID(3286)
59+
@PropGet
60+
java.lang.String getProviderID();
61+
62+
63+
/**
64+
* <p>
65+
* Getter method for the COM property "UserID"
66+
* </p>
67+
*/
68+
69+
@DISPID(3287)
70+
@PropGet
71+
java.lang.String getUserID();
72+
73+
74+
// Properties:
75+
}

0 commit comments

Comments
 (0)