KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import org.eclipse.swt.internal.ole.win32.*;
15 import org.eclipse.swt.*;
16
17 final class OlePropertyChangeSink {
18
19     private OleControlSite controlSite;
20     //private IUnknown objIUnknown;
21

22     private COMObject iUnknown;
23     private COMObject iPropertyNotifySink;
24
25     private int refCount;
26     
27     private int propertyCookie;
28
29     private OleEventTable eventTable;
30     
31 OlePropertyChangeSink(OleControlSite controlSite) {
32     
33     this.controlSite = controlSite;
34     
35     createCOMInterfaces();
36 }
37 void addListener(int propertyID, OleListener listener) {
38     if (listener == null) OLE.error (SWT.ERROR_NULL_ARGUMENT);
39     if (eventTable == null) eventTable = new OleEventTable ();
40     eventTable.hook(propertyID, listener);
41 }
42 int AddRef() {
43     refCount++;
44     return refCount;
45 }
46 void connect(IUnknown objIUnknown) {
47
48     // Set up property change notification sink
49
int[] ppvObject = new int[1];
50     if (objIUnknown.QueryInterface(COM.IIDIConnectionPointContainer, ppvObject) == COM.S_OK) {
51         IConnectionPointContainer cpc = new IConnectionPointContainer(ppvObject[0]);
52         if (cpc.FindConnectionPoint(COM.IIDIPropertyNotifySink, ppvObject) == COM.S_OK) {
53             IConnectionPoint cp = new IConnectionPoint(ppvObject[0]);
54             int[] cookie = new int[1];
55             if (cp.Advise(iPropertyNotifySink.getAddress(), cookie) == COM.S_OK) {
56                 propertyCookie = cookie[0];
57             }
58             cp.Release();
59         }
60         cpc.Release();
61     }
62 }
63 private void createCOMInterfaces() {
64     // register each of the interfaces that this object implements
65
iUnknown = new COMObject(new int[]{2, 0, 0}){
66         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
67         public int method1(int[] args) {return AddRef();}
68         public int method2(int[] args) {return Release();}
69     };
70     
71     iPropertyNotifySink = new COMObject(new int[]{2, 0, 0, 1, 1}){
72         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
73         public int method1(int[] args) {return AddRef();}
74         public int method2(int[] args) {return Release();}
75         public int method3(int[] args) {return OnChanged(args[0]);}
76         public int method4(int[] args) {return OnRequestEdit(args[0]);}
77     };
78 }
79 void disconnect(IUnknown objIUnknown) {
80
81     // disconnect property notification sink
82
if (propertyCookie != 0 && objIUnknown != null) {
83         int[] ppvObject = new int[1];
84         if (objIUnknown.QueryInterface(COM.IIDIConnectionPointContainer, ppvObject) == COM.S_OK) {
85             IConnectionPointContainer cpc = new IConnectionPointContainer(ppvObject[0]);
86             if (cpc.FindConnectionPoint(COM.IIDIPropertyNotifySink, ppvObject) == COM.S_OK) {
87                 IConnectionPoint cp = new IConnectionPoint(ppvObject[0]);
88                 if (cp.Unadvise(propertyCookie) == COM.S_OK) {
89                     propertyCookie = 0;
90                 }
91                 cp.Release();
92             }
93             cpc.Release();
94         }
95     }
96 }
97 private void disposeCOMInterfaces() {
98     if (iUnknown != null) iUnknown.dispose();
99     iUnknown = null;
100     if (iPropertyNotifySink != null) iPropertyNotifySink.dispose();
101     iPropertyNotifySink = null;
102 }
103 /**
104 * Notify listeners of an event.
105 * <p>
106 * This method notifies all listeners that an event
107 * has occurred.
108 *
109 * @param eventType the desired SWT event
110 * @param event the event data
111 *
112 * @exception IllegalArgumentException <ul>
113 * <li>ERROR_NULL_ARGUMENT when handler is null</li>
114 * </ul>
115 * @exception SWTException <ul>
116 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
117 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
118 * </ul>
119 */

120 private void notifyListener (int eventType, OleEvent event) {
121     if (event == null) OLE.error (SWT.ERROR_NULL_ARGUMENT);
122     if (eventTable == null) return;
123     event.type = eventType;
124     event.widget = controlSite;
125     eventTable.sendEvent (event);
126 }
127 private int OnChanged(int dispID) {
128     if (eventTable == null || !eventTable.hooks(dispID)) return COM.S_OK;
129     OleEvent event = new OleEvent();
130     event.detail = OLE.PROPERTY_CHANGED;
131     notifyListener(dispID,event);
132     return COM.S_OK;
133 }
134 private int OnRequestEdit(int dispID) {
135     if (eventTable == null || !eventTable.hooks(dispID)) return COM.S_OK;
136     OleEvent event = new OleEvent();
137     event.doit = true;
138     event.detail = OLE.PROPERTY_CHANGING;
139     notifyListener(dispID,event);
140     return (event.doit) ? COM.S_OK : COM.S_FALSE;
141 }
142 private int QueryInterface(int riid, int ppvObject) {
143     if (riid == 0 || ppvObject == 0)
144         return COM.E_INVALIDARG;
145     GUID guid = new GUID();
146     COM.MoveMemory(guid, riid, GUID.sizeof);
147     if (COM.IsEqualGUID(guid, COM.IIDIUnknown)) {
148         COM.MoveMemory(ppvObject, new int[] {iUnknown.getAddress()}, 4);
149         AddRef();
150         return COM.S_OK;
151     }
152     if (COM.IsEqualGUID(guid, COM.IIDIPropertyNotifySink)) {
153         COM.MoveMemory(ppvObject, new int[] {iPropertyNotifySink.getAddress()}, 4);
154         AddRef();
155         return COM.S_OK;
156     }
157     COM.MoveMemory(ppvObject, new int[] {0}, 4);
158     return COM.E_NOINTERFACE;
159 }
160 int Release() {
161     refCount--;
162     if (refCount == 0) {
163         disposeCOMInterfaces();
164     }
165     return refCount;
166 }
167 void removeListener(int propertyID, OleListener listener) {
168     if (listener == null) OLE.error (SWT.ERROR_NULL_ARGUMENT);
169     if (eventTable == null) return;
170     eventTable.unhook (propertyID, listener);
171 }
172 }
173
Popular Tags