KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > ole > win32 > OleEventSink


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.ole.win32;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.internal.ole.win32.*;
15
16 final class OleEventSink
17 {
18     private OleControlSite widget;
19     
20     private COMObject iDispatch;
21     private int refCount;
22
23     private IUnknown objIUnknown;
24     private int eventCookie;
25     private GUID eventGuid;
26
27     private OleEventTable eventTable;
28     
29 OleEventSink(OleControlSite widget, int iUnknown, GUID riid) {
30
31     this.widget = widget;
32     this.eventGuid = riid;
33     this.objIUnknown = new IUnknown(iUnknown);
34     
35     createCOMInterfaces();
36 }
37
38 void connect () {
39     int[] ppvObject = new int[1];
40     if (objIUnknown.QueryInterface(COM.IIDIConnectionPointContainer, ppvObject) == COM.S_OK) {
41         IConnectionPointContainer cpc = new IConnectionPointContainer(ppvObject[0]);
42         int[] ppCP = new int[1];
43         if (cpc.FindConnectionPoint(eventGuid, ppCP) == COM.S_OK) {
44             IConnectionPoint cp = new IConnectionPoint(ppCP[0]);
45             int[] pCookie = new int[1];
46             if (cp.Advise(iDispatch.getAddress(), pCookie) == COM.S_OK)
47                 eventCookie = pCookie[0];
48             cp.Release();
49         }
50         cpc.Release();
51     }
52 }
53 void addListener(int eventID, OleListener listener) {
54     if (listener == null) OLE.error (SWT.ERROR_NULL_ARGUMENT);
55     if (eventTable == null) eventTable = new OleEventTable ();
56     eventTable.hook(eventID, listener);
57 }
58 int AddRef() {
59     refCount++;
60     return refCount;
61 }
62 private void createCOMInterfaces() {
63     iDispatch = new COMObject(new int[]{2, 0, 0, 1, 3, 4, 8}){
64         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
65         public int method1(int[] args) {return AddRef();}
66         public int method2(int[] args) {return Release();}
67         // method3 GetTypeInfoCount - not implemented
68
// method4 GetTypeInfo - not implemented
69
// method5 GetIDsOfNames - not implemented
70
public int method6(int[] args) {return Invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);}
71     };
72 }
73 void disconnect() {
74     // disconnect event sink
75
if (eventCookie != 0 && objIUnknown != null) {
76         int[] ppvObject = new int[1];
77         if (objIUnknown.QueryInterface(COM.IIDIConnectionPointContainer, ppvObject) == COM.S_OK) {
78             IConnectionPointContainer cpc = new IConnectionPointContainer(ppvObject[0]);
79             if (cpc.FindConnectionPoint(eventGuid, ppvObject) == COM.S_OK) {
80                 IConnectionPoint cp = new IConnectionPoint(ppvObject[0]);
81                 if (cp.Unadvise(eventCookie) == COM.S_OK) {
82                     eventCookie = 0;
83                 }
84                 cp.Release();
85             }
86             cpc.Release();
87         }
88     }
89 }
90 private void disposeCOMInterfaces() {
91     if (iDispatch != null)
92         iDispatch.dispose();
93     iDispatch = null;
94     
95 }
96 private int Invoke(int dispIdMember, int riid, int lcid, int dwFlags, int pDispParams, int pVarResult, int pExcepInfo, int pArgErr)
97 {
98     if (eventTable == null || !eventTable.hooks(dispIdMember)) return COM.S_OK;
99     
100     // Construct an array of the parameters that are passed in
101
// Note: parameters are passed in reverse order - here we will correct the order
102
Variant[] eventInfo = null;
103     if (pDispParams != 0) {
104         DISPPARAMS dispParams = new DISPPARAMS();
105         COM.MoveMemory(dispParams, pDispParams, DISPPARAMS.sizeof);
106         eventInfo = new Variant[dispParams.cArgs];
107         int size = Variant.sizeof;
108         int offset = (dispParams.cArgs - 1) * size;
109         
110         for (int j = 0; j < dispParams.cArgs; j++){
111             eventInfo[j] = new Variant();
112             eventInfo[j].setData(dispParams.rgvarg + offset);
113             offset = offset - size;
114         }
115     }
116         
117     OleEvent event = new OleEvent();
118     event.arguments = eventInfo;
119     notifyListener(dispIdMember,event);
120     return COM.S_OK;
121 }
122 /**
123 * Notify listeners of an event.
124 * <p>
125 * This method notifies all listeners that an event
126 * has occurred.
127 *
128 * @param eventType the desired SWT event
129 * @param event the event data
130 *
131 * @exception IllegalArgumentException <ul>
132 * <li>ERROR_NULL_ARGUMENT when handler is null</li>
133 * </ul>
134 * @exception SWTException <ul>
135 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
136 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
137 * </ul>
138 */

139 private void notifyListener (int eventType, OleEvent event) {
140     if (event == null) OLE.error (SWT.ERROR_NULL_ARGUMENT);
141     if (eventTable == null) return;
142     event.type = eventType;
143     event.widget = widget;
144     eventTable.sendEvent (event);
145 }
146 private int QueryInterface(int riid, int ppvObject) {
147
148     if (riid == 0 || ppvObject == 0)
149         return COM.E_INVALIDARG;
150     GUID guid = new GUID();
151     COM.MoveMemory(guid, riid, GUID.sizeof);
152
153     if ( COM.IsEqualGUID(guid, COM.IIDIUnknown) || COM.IsEqualGUID(guid, COM.IIDIDispatch) ||
154             COM.IsEqualGUID(guid, eventGuid)) {
155         COM.MoveMemory(ppvObject, new int[] {iDispatch.getAddress()}, 4);
156         AddRef();
157         return OLE.S_OK;
158     }
159
160     COM.MoveMemory(ppvObject, new int[] {0}, 4);
161     return COM.E_NOINTERFACE;
162 }
163 int Release() {
164     refCount--;
165     if (refCount == 0) {
166         disposeCOMInterfaces();
167     }
168     
169     return refCount;
170 }
171 void removeListener(int eventID, OleListener listener) {
172     if (listener == null) OLE.error (SWT.ERROR_NULL_ARGUMENT);
173     if (eventTable == null) return;
174     eventTable.unhook (eventID, listener);
175 }
176 boolean hasListeners() {
177     return eventTable.hasEntries();
178 }
179 }
180
Popular Tags