|
1 /* |
|
2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.microedition.m2g; |
|
19 |
|
20 import java.io.IOException; |
|
21 |
|
22 import com.nokia.microedition.m2g.NativeError; |
|
23 import com.nokia.mj.impl.rt.support.Finalizer; |
|
24 import com.nokia.mj.impl.utils.Logger; |
|
25 |
|
26 |
|
27 /** |
|
28 * Base class |
|
29 */ |
|
30 abstract class M2GObject |
|
31 { |
|
32 //-------------------------------------------------- |
|
33 // STATIC CONSTANTS |
|
34 //-------------------------------------------------- |
|
35 public static final int INVALID_NATIVE_HANDLE = 0; |
|
36 // static final String ESWT_PACKAGE = "org.eclipse.swt.widgets.Display" ; |
|
37 //-------------------------------------------------- |
|
38 // VARIABLES |
|
39 //-------------------------------------------------- |
|
40 int iNativeHandle; |
|
41 M2GManager iManager; |
|
42 private Finalizer mFinalizer; |
|
43 |
|
44 //-------------------------------------------------- |
|
45 // METHODS |
|
46 //-------------------------------------------------- |
|
47 /** |
|
48 * Constructor. |
|
49 */ |
|
50 M2GObject() |
|
51 { |
|
52 this(INVALID_NATIVE_HANDLE); |
|
53 |
|
54 } |
|
55 |
|
56 /** |
|
57 * Constructor. |
|
58 * @param a native handle |
|
59 */ |
|
60 M2GObject(int aHandle) |
|
61 { |
|
62 |
|
63 iNativeHandle = aHandle; |
|
64 |
|
65 iManager = M2GManager.getInstance(); |
|
66 |
|
67 } |
|
68 |
|
69 /** |
|
70 * Checks handle validity. |
|
71 * @return true if valid |
|
72 */ |
|
73 boolean doCheckValidity() throws IOException |
|
74 { |
|
75 return isHandleValid(); |
|
76 } |
|
77 |
|
78 /** |
|
79 * Cleanup operations. |
|
80 */ |
|
81 abstract void doCleanup(); |
|
82 |
|
83 /** |
|
84 * Construct operations. Registration for finalization is done here. |
|
85 */ |
|
86 void doConstruct() |
|
87 { |
|
88 Logger.LOG(Logger.EJavaUI, Logger.EInfo, |
|
89 "doConstruct(), register for finalization: " |
|
90 + this.getClass().getName()); |
|
91 M2GManager.heuristicGC(); |
|
92 mFinalizer = new Finalizer() |
|
93 { |
|
94 public void finalizeImpl() |
|
95 { |
|
96 doFinalize(); |
|
97 } |
|
98 }; |
|
99 } |
|
100 |
|
101 /** |
|
102 * Create destroyer. |
|
103 */ |
|
104 void createDestroyer() |
|
105 { |
|
106 Logger.LOG(Logger.EJavaUI, Logger.EInfo, |
|
107 "createDestroyer(): " + this.getClass().getName()); |
|
108 |
|
109 } |
|
110 |
|
111 /** |
|
112 * Gets native handle |
|
113 * @return native handle |
|
114 */ |
|
115 int getHandle() |
|
116 { |
|
117 return iNativeHandle; |
|
118 } |
|
119 |
|
120 /** |
|
121 * Gets native proxy handle |
|
122 * @return native handle |
|
123 */ |
|
124 int getNativeSVGProxyHandle() |
|
125 { |
|
126 return iManager.getSVGProxyHandle(); |
|
127 } |
|
128 |
|
129 /** |
|
130 * Checks if native handle is valid. |
|
131 */ |
|
132 boolean isHandleValid() |
|
133 { |
|
134 M2GObject.checkNativeError(iNativeHandle); |
|
135 return M2GObject.checkHandle(iNativeHandle); |
|
136 } |
|
137 |
|
138 /** |
|
139 * Register an object |
|
140 * @param aObject Object |
|
141 */ |
|
142 void register(M2GObject aObject) |
|
143 { |
|
144 iManager.register(aObject); |
|
145 } |
|
146 |
|
147 /** |
|
148 * @see ObjectExtensions#registerForFinalization() |
|
149 */ |
|
150 private void doFinalize() |
|
151 { |
|
152 if (mFinalizer != null) |
|
153 { |
|
154 registeredFinalize(); |
|
155 mFinalizer = null; |
|
156 } |
|
157 } |
|
158 |
|
159 synchronized final void registeredFinalize() |
|
160 { |
|
161 Logger.LOG(Logger.EJavaUI, Logger.EInfo, |
|
162 "registeredFinalize() begin - doCleanup() is called"); |
|
163 doCleanup(); |
|
164 Logger.LOG(Logger.EJavaUI, Logger.EInfo, "registeredFinalize() end"); |
|
165 } |
|
166 |
|
167 /** |
|
168 * This method is called in Toolkit's destroyNotify call. |
|
169 * This will release convenient native resources. All native resource |
|
170 * will be deleted in registeredFinalize() method. |
|
171 */ |
|
172 synchronized final void release() |
|
173 { |
|
174 Logger.LOG(Logger.EJavaUI, Logger.EInfo, "release()"); |
|
175 } |
|
176 |
|
177 /** |
|
178 * Initializes native handles. |
|
179 */ |
|
180 void resetHandles() |
|
181 { |
|
182 iNativeHandle = INVALID_NATIVE_HANDLE; |
|
183 } |
|
184 |
|
185 /** |
|
186 * Sets a native handle. |
|
187 */ |
|
188 void setHandle(int aNativeHandle) |
|
189 { |
|
190 iNativeHandle = aNativeHandle; |
|
191 } |
|
192 |
|
193 /** |
|
194 * Unregister an object |
|
195 * @param aHandle Object's native handle |
|
196 */ |
|
197 void unregister(Integer aHandle) |
|
198 { |
|
199 iManager.unregister(aHandle); |
|
200 } |
|
201 |
|
202 //-------------------------------------------------- |
|
203 // STATIC METHODS |
|
204 //-------------------------------------------------- |
|
205 /** |
|
206 * Checks that the handle is valid. |
|
207 * @param a native handle |
|
208 * @return true if handle is valid. |
|
209 */ |
|
210 static boolean checkHandle(int aHandle) |
|
211 { |
|
212 return (aHandle > INVALID_NATIVE_HANDLE ? true : false); |
|
213 } |
|
214 |
|
215 /** |
|
216 * Checks for basic native error codes that maps to |
|
217 * standard Java exceptions and throws the exception if the error |
|
218 * code matches. Otherwise throws basic Error class. |
|
219 * @param aErrorCode Error code |
|
220 * @return Value passed in is returned if not an error |
|
221 * @throws Error if error |
|
222 */ |
|
223 static int checkNativeError(int aErrorCode) |
|
224 { |
|
225 return NativeError.check(aErrorCode); |
|
226 } |
|
227 } |
|
228 |