|
1 /* |
|
2 * Copyright (c) 2009 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: Receives callbacks from the native side. |
|
15 * |
|
16 */ |
|
17 |
|
18 // PACKAGE |
|
19 package com.nokia.mid.ui.impl; |
|
20 |
|
21 // IMPORTS |
|
22 import com.nokia.mid.ui.TextEditorListener; |
|
23 import com.nokia.mid.ui.TextEditor; |
|
24 import com.nokia.mj.impl.rt.legacy.NativeError; |
|
25 import com.nokia.mj.impl.rt.legacy.ToolkitInvoker; |
|
26 import com.nokia.mj.impl.rt.support.Finalizer; |
|
27 |
|
28 |
|
29 // CLASS DESCRIPTION |
|
30 /** |
|
31 * Receives callbacks from the native side. |
|
32 */ |
|
33 public class TextEditorListenerImpl |
|
34 { |
|
35 // Data |
|
36 private final TextEditor iEditor; |
|
37 private final TextEditorListener iListener; |
|
38 private final Object iToolkit; |
|
39 private final ToolkitInvoker iToolkitInvoker; |
|
40 private int iHandle; |
|
41 private int iActions; |
|
42 private Finalizer mFinalizer; |
|
43 |
|
44 /** |
|
45 * Constructor. |
|
46 * |
|
47 * @param aEditor |
|
48 * The editor to which this listener is associated to. ' @param |
|
49 * aListener The listener to which this implementation is |
|
50 * associated to. Actions are delivered to this object. |
|
51 */ |
|
52 public TextEditorListenerImpl( |
|
53 TextEditor aEditor, |
|
54 TextEditorListener aListener) |
|
55 { |
|
56 iEditor = aEditor; |
|
57 iListener = aListener; |
|
58 |
|
59 // Toolkit invoker is needed for accessing javax.microedition.lcdui |
|
60 // package |
|
61 iToolkitInvoker = ToolkitInvoker.getToolkitInvoker(); |
|
62 iToolkit = iToolkitInvoker.getToolkit(); |
|
63 |
|
64 int handle = 0; |
|
65 synchronized (iToolkit) |
|
66 { |
|
67 // Create native peer object for this Java object. |
|
68 handle = |
|
69 _createNativePeer(iToolkitInvoker.toolkitGetHandle(iToolkit)); |
|
70 } |
|
71 |
|
72 // Check if construction failed and throw out of memory error. |
|
73 if (handle <= NativeError.KErrNone) |
|
74 { |
|
75 throw new OutOfMemoryError(); |
|
76 } |
|
77 |
|
78 // Construction was successful. Store handle and register for |
|
79 // finalization. |
|
80 iHandle = handle; |
|
81 mFinalizer = new Finalizer() |
|
82 { |
|
83 public void finalizeImpl() |
|
84 { |
|
85 registeredFinalize(); |
|
86 } |
|
87 }; |
|
88 } |
|
89 |
|
90 /** |
|
91 * This method is called when an action occurs in the native side. |
|
92 * |
|
93 * The actions are simply delegated to the associated |
|
94 * <code>TextEditorListener</code> object which provids the actual |
|
95 * functionality. |
|
96 * |
|
97 * @param aActions |
|
98 * The actions that occured. |
|
99 * @return The status from the associated listener. |
|
100 */ |
|
101 public void inputAction(int aActions) |
|
102 { |
|
103 // JDEBUG( "TextEditorListenerImpl.inputAction() + aActions=" + aActions |
|
104 // ); |
|
105 |
|
106 try |
|
107 { |
|
108 iListener.inputAction(iEditor, aActions); |
|
109 } |
|
110 catch (Throwable t) |
|
111 { |
|
112 // JDEBUG( |
|
113 // "TextEditorListenerImpl.inputAction(), exception in callback" ); |
|
114 |
|
115 // Ignored intentionally. |
|
116 |
|
117 t.printStackTrace(); |
|
118 } |
|
119 |
|
120 // JDEBUG( "TextEditorListenerImpl.inputAction() -" ); |
|
121 } |
|
122 |
|
123 /** |
|
124 * Returns the handle to the native side peer object. |
|
125 * |
|
126 * @return The handle to the native side peer object. |
|
127 */ |
|
128 public int getHandle() |
|
129 { |
|
130 return iHandle; |
|
131 } |
|
132 |
|
133 /** |
|
134 * Disposes the Landmark native peer objecct, if the handles are valid. |
|
135 * Invalid (negative) handles indicate that their creation failed in the |
|
136 * first place. |
|
137 */ |
|
138 final void registeredFinalize() |
|
139 { |
|
140 // JDEBUG( "TextEditorListenerImpl.registeredFinalize() +" ); |
|
141 |
|
142 synchronized (iToolkit) |
|
143 { |
|
144 if (iHandle > 0) |
|
145 { |
|
146 _dispose(iToolkitInvoker.toolkitGetHandle(iToolkit), iHandle); |
|
147 |
|
148 iHandle = 0; |
|
149 } |
|
150 } |
|
151 |
|
152 // JDEBUG( "TextEditorListenerImpl.registeredFinalize() -" ); |
|
153 } |
|
154 |
|
155 // Native methods |
|
156 |
|
157 /** |
|
158 * Creates the native side peer object for this object. |
|
159 * |
|
160 * @param aToolkitHandle |
|
161 * A handle to the LCDUI toolkit. |
|
162 * @return A handle to the the native side peer object or a system-wide |
|
163 * error code. |
|
164 */ |
|
165 private native int _createNativePeer(int aToolkitHandle); |
|
166 |
|
167 /** |
|
168 * Disposes the native side peer object. |
|
169 * |
|
170 * @param aToolkitHandle |
|
171 * A handle to the LCDUI toolkit. |
|
172 * @param aNativePeerHandle |
|
173 * A handle to the native side peer object. |
|
174 */ |
|
175 private native void _dispose(int aToolkitHandle, int aNativePeerHandle); |
|
176 } |
|
177 |
|
178 // End of file |