summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet305.java
blob: d90995af085f440c96eae6af97c091637864b83b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;

/*
 * Show row/column position of current selection in an Excel sheet.
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.ole.win32.*;
import org.eclipse.swt.widgets.*;

public class Snippet305 {
	static int SheetSelectionChange = 0x00000616;
	static String IID_AppEvents = "{00024413-0000-0000-C000-000000000046}";

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Excel Sheet Selection Example");
		shell.setLayout(new FillLayout());
		OleAutomation application;
		try {
			OleFrame frame = new OleFrame(shell, SWT.NONE);
			OleControlSite controlSite = new OleControlSite(frame, SWT.NONE, "Excel.Sheet");
			controlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
			
			OleAutomation excelSheet = new OleAutomation(controlSite);
			int[] dispIDs = excelSheet.getIDsOfNames(new String[] { "Application" });
			Variant pVarResult = excelSheet.getProperty(dispIDs[0]);
			application = pVarResult.getAutomation();
			pVarResult.dispose();
			excelSheet.dispose();
			
			OleListener listener = new OleListener() {
				public void handleEvent(OleEvent e) {
					// SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
					Variant[] args = e.arguments;
					// OleAutomation sheet = args[1].getAutomation(); // Excel.Sheet
					OleAutomation range = args[0].getAutomation(); // Excel.Range
					int[] dispIDs = range.getIDsOfNames(new String[] { "Row" });
					Variant pVarResult = range.getProperty(dispIDs[0]);
					int row = pVarResult.getInt();
					dispIDs = range.getIDsOfNames(new String[] { "Column" });
					pVarResult = range.getProperty(dispIDs[0]);
					int column = pVarResult.getInt();
					range.dispose();
					System.out.println("row=" + row + " column=" + column);
					for (int i = 0; i < args.length; i++) {
						args[i].dispose();
					}
				}
			};
			controlSite.addEventListener(application, IID_AppEvents, SheetSelectionChange, listener);
		} catch (SWTError e) {
			System.out.println("Unable to open activeX control");
			display.dispose();
			return;
		}
		shell.setSize(800, 600);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		if (application != null) application.dispose();
		display.dispose();
	}
}