KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > dnd > OleEnumFORMATETC


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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.dnd;
12
13 import org.eclipse.swt.internal.ole.win32.*;
14
15 final class OleEnumFORMATETC {
16
17     private COMObject iUnknown;
18     private COMObject iEnumFORMATETC;
19     
20     private int refCount;
21     private int index;
22     
23     private FORMATETC[] formats;
24     
25 OleEnumFORMATETC() {
26     
27     createCOMInterfaces();
28
29 }
30 int AddRef() {
31     refCount++;
32     return refCount;
33 }
34 private void createCOMInterfaces() {
35     // register each of the interfaces that this object implements
36
iUnknown = new COMObject(new int[] {2, 0, 0}){
37         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
38         public int method1(int[] args) {return AddRef();}
39         public int method2(int[] args) {return Release();}
40     };
41     iEnumFORMATETC = new COMObject(new int[] {2, 0, 0, 3, 1, 0, 1}){
42         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
43         public int method1(int[] args) {return AddRef();}
44         public int method2(int[] args) {return Release();}
45         public int method3(int[] args) {return Next(args[0], args[1], args[2]);}
46         public int method4(int[] args) {return Skip(args[0]);}
47         public int method5(int[] args) {return Reset();}
48         // method6 Clone - not implemented
49
};
50 }
51 private void disposeCOMInterfaces() {
52     
53     if (iUnknown != null)
54         iUnknown.dispose();
55     iUnknown = null;
56     
57     if (iEnumFORMATETC != null)
58         iEnumFORMATETC.dispose();
59     iEnumFORMATETC = null;
60 }
61 int getAddress() {
62     return iEnumFORMATETC.getAddress();
63 }
64 private FORMATETC[] getNextItems(int numItems){
65
66     if (formats == null || numItems < 1) return null;
67
68     int endIndex = index + numItems - 1;
69     if (endIndex > (formats.length - 1)) endIndex = formats.length - 1;
70     if (index > endIndex) return null;
71     
72     FORMATETC[] items = new FORMATETC[endIndex - index + 1];
73     for (int i = 0; i < items.length; i++){
74         items[i] = formats[index];
75         index++;
76     }
77
78     return items;
79 }
80 private int Next(int celt, int rgelt, int pceltFetched) {
81     /* Retrieves the next celt items in the enumeration sequence.
82        If there are fewer than the requested number of elements left in the sequence,
83        it retrieves the remaining elements.
84        The number of elements actually retrieved is returned through pceltFetched
85        (unless the caller passed in NULL for that parameter).
86     */

87
88     if (rgelt == 0) return COM.E_INVALIDARG;
89     if (pceltFetched == 0 && celt != 1) return COM.E_INVALIDARG;
90         
91     FORMATETC[] nextItems = getNextItems(celt);
92     if (nextItems != null) {
93         for (int i = 0; i < nextItems.length; i++) {
94             COM.MoveMemory(rgelt + i*FORMATETC.sizeof, nextItems[i], FORMATETC.sizeof);
95         }
96         
97         if (pceltFetched != 0)
98             COM.MoveMemory(pceltFetched, new int[] {nextItems.length}, 4);
99             
100         if (nextItems.length == celt) return COM.S_OK;
101             
102     } else {
103         if (pceltFetched != 0)
104             COM.MoveMemory(pceltFetched, new int[] {0}, 4);
105         COM.MoveMemory(rgelt, new FORMATETC(), FORMATETC.sizeof);
106             
107     }
108     return COM.S_FALSE;
109 }
110 private int QueryInterface(int riid, int ppvObject) {
111     
112     if (riid == 0 || ppvObject == 0) return COM.E_NOINTERFACE;
113     
114     GUID guid = new GUID();
115     COM.MoveMemory(guid, riid, GUID.sizeof);
116
117     if (COM.IsEqualGUID(guid, COM.IIDIUnknown)) {
118         COM.MoveMemory(ppvObject, new int[] {iUnknown.getAddress()}, 4);
119         AddRef();
120         return COM.S_OK;
121     }
122     if (COM.IsEqualGUID(guid, COM.IIDIEnumFORMATETC)) {
123         COM.MoveMemory(ppvObject, new int[] {iEnumFORMATETC.getAddress()}, 4);
124         AddRef();
125         return COM.S_OK;
126     }
127     COM.MoveMemory(ppvObject, new int[] {0}, 4);
128     return COM.E_NOINTERFACE;
129 }
130 int Release() {
131     refCount--;
132     
133     if (refCount == 0) {
134         disposeCOMInterfaces();
135         COM.CoFreeUnusedLibraries();
136     }
137     
138     return refCount;
139 }
140 private int Reset() {
141     //Resets the enumeration sequence to the beginning.
142
index = 0;
143     return COM.S_OK;
144 }
145 void setFormats(FORMATETC[] newFormats) {
146     formats = newFormats;
147     index = 0;
148 }
149 private int Skip(int celt) {
150     //Skips over the next specified number of elements in the enumeration sequence.
151
if (celt < 1 ) return COM.E_INVALIDARG;
152     
153     index += celt;
154     if (index > (formats.length - 1)){
155         index = formats.length - 1;
156         return COM.S_FALSE;
157     }
158     return COM.S_OK;
159 }
160 }
161
Popular Tags