Skip to content

Commit 4eb1454

Browse files
committed
Add way to get back from a Com4jObject to an IUknown
And add example showing how to use that the get the cached object at a specific cell.
1 parent 93869e7 commit 4eb1454

File tree

6 files changed

+96
-3
lines changed

6 files changed

+96
-3
lines changed

examples/jinx-com4j-examples.xlsx

1.12 KB
Binary file not shown.

examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<dependency>
3434
<groupId>com.exceljava</groupId>
3535
<artifactId>jinx</artifactId>
36-
<version>2.0.0-beta1</version>
36+
<version>2.0.0-beta2</version>
3737
</dependency>
3838
</dependencies>
3939
</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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>com.exceljava</groupId>
2828
<artifactId>jinx</artifactId>
29-
<version>2.0.0-beta1</version>
29+
<version>2.0.0-beta2</version>
3030
</dependency>
3131
<!-- com4j -->
3232
<dependency>
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+
}

jinx-com4j/src/main/java/com/exceljava/com4j/JinxBridge.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ public static _Application getApplication(ExcelAddIn xl) {
3131
return xl.getExcelApplication(_Application.class);
3232
}
3333

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+
3447
/**
3548
* Converts IUnknown to any Com4jObject type.
3649
*

0 commit comments

Comments
 (0)