KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > browser > SimpleEnumerator


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.browser;
12
13 import org.eclipse.swt.internal.C;
14 import org.eclipse.swt.internal.mozilla.*;
15
16 class SimpleEnumerator {
17     XPCOMObject supports;
18     XPCOMObject simpleEnumerator;
19     int refCount = 0;
20     nsISupports[] values;
21     int index = 0;
22
23 public SimpleEnumerator (nsISupports[] values) {
24     this.values = values;
25     for (int i = 0; i < values.length; i++) {
26         values[i].AddRef ();
27     }
28     createCOMInterfaces ();
29 }
30
31 int AddRef () {
32     refCount++;
33     return refCount;
34 }
35
36 void createCOMInterfaces () {
37     /* Create each of the interfaces that this object implements */
38     supports = new XPCOMObject (new int[] {2, 0, 0}) {
39         public int /*long*/ method0 (int /*long*/[] args) {return queryInterface (args[0], args[1]);}
40         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
41         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
42     };
43
44     simpleEnumerator = new XPCOMObject (new int[] {2, 0, 0, 1, 1}) {
45         public int /*long*/ method0 (int /*long*/[] args) {return queryInterface (args[0], args[1]);}
46         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
47         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
48         public int /*long*/ method3 (int /*long*/[] args) {return HasMoreElements (args[0]);}
49         public int /*long*/ method4 (int /*long*/[] args) {return GetNext (args[0]);}
50     };
51 }
52
53 void disposeCOMInterfaces () {
54     if (supports != null) {
55         supports.dispose ();
56         supports = null;
57     }
58     if (simpleEnumerator != null) {
59         simpleEnumerator.dispose ();
60         simpleEnumerator = null;
61     }
62     if (values != null) {
63         for (int i = 0; i < values.length; i++) {
64             values[i].Release ();
65         }
66         values = null;
67     }
68 }
69
70 int /*long*/ getAddress () {
71     return simpleEnumerator.getAddress ();
72 }
73
74 int /*long*/ queryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
75     if (riid == 0 || ppvObject == 0) return XPCOM.NS_ERROR_NO_INTERFACE;
76     nsID guid = new nsID ();
77     XPCOM.memmove (guid, riid, nsID.sizeof);
78
79     if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
80         XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
81         AddRef ();
82         return XPCOM.NS_OK;
83     }
84     if (guid.Equals (nsISimpleEnumerator.NS_ISIMPLEENUMERATOR_IID)) {
85         XPCOM.memmove (ppvObject, new int /*long*/[] {simpleEnumerator.getAddress ()}, C.PTR_SIZEOF);
86         AddRef ();
87         return XPCOM.NS_OK;
88     }
89
90     XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
91     return XPCOM.NS_ERROR_NO_INTERFACE;
92 }
93
94 int Release () {
95     refCount--;
96     if (refCount == 0) disposeCOMInterfaces ();
97     return refCount;
98 }
99
100 int HasMoreElements (int /*long*/ _retval) {
101     boolean more = values != null && index < values.length;
102     XPCOM.memmove (_retval, new int[] {more ? 1 : 0}, 4); /*PRBool */
103     return XPCOM.NS_OK;
104 }
105     
106 int GetNext (int /*long*/ _retval) {
107     if (values == null || index == values.length) return XPCOM.NS_ERROR_UNEXPECTED;
108     nsISupports value = values[index++];
109     value.AddRef ();
110     XPCOM.memmove (_retval, new int /*long*/[] {value.getAddress ()}, C.PTR_SIZEOF);
111     return XPCOM.NS_OK;
112 }
113 }
114
115
Popular Tags