Merge pull request #1004 from marco-c/texteditor_listener_improvements

Some improvements for TextEditor listeners
This commit is contained in:
Myk Melez 2015-02-02 09:16:30 -08:00
Родитель 021d47edcf e070f981c4
Коммит 95cdc7a246
3 изменённых файлов: 23 добавлений и 41 удалений

Просмотреть файл

@ -3,28 +3,21 @@ package com.nokia.mid.ui;
import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Font;
import java.util.Hashtable; import java.util.Hashtable;
class TextEditorThread implements Runnable { class TextEditorThread extends Thread {
// We need a thread to be able to wake from js when there is an async js keyboard event. // We need a thread to be able to wake from js when there is an async js keyboard event.
native void sleep(); native TextEditor getNextDirtyEditor();
native int getNextDirtyEditor();
Hashtable listeners;
TextEditorThread() { TextEditorThread() {
listeners = new Hashtable(); setPriority(Thread.MAX_PRIORITY);
} }
public void run() { public void run() {
while (true) { while (true) {
sleep(); TextEditor dirty = getNextDirtyEditor();
int dirty = getNextDirtyEditor(); if (dirty.myListener != null) {
TextEditor t = (TextEditor)listeners.get("" + dirty); dirty.myListener.inputAction(dirty, TextEditorListener.ACTION_CONTENT_CHANGE);
t.myListener.inputAction(t, TextEditorListener.ACTION_CONTENT_CHANGE);
} }
} }
void register(int id, TextEditor t) {
listeners.put("" + id, t);
} }
} }
@ -35,22 +28,20 @@ public class TextEditor extends CanvasItem {
protected TextEditorListener myListener; protected TextEditorListener myListener;
private boolean multiline = false; private boolean multiline = false;
private int myId;
private static TextEditorThread textEditorThread; private static TextEditorThread textEditorThread;
private Font font = Font.getDefaultFont(); private Font font = Font.getDefaultFont();
protected TextEditor(String text, int maxSize, int constraints, int width, int height) { protected TextEditor(String text, int maxSize, int constraints, int width, int height) {
myId = init(text, maxSize, constraints, width, height); init(text, maxSize, constraints, width, height);
if (textEditorThread == null) { if (textEditorThread == null) {
textEditorThread = new TextEditorThread(); textEditorThread = new TextEditorThread();
Thread t = new Thread(textEditorThread); textEditorThread.start();
t.start();
} }
} }
// Initialize the native representation. // Initialize the native representation.
native private int init(String text, int maxSize, int constraints, int width, int height); native private void init(String text, int maxSize, int constraints, int width, int height);
// Creates a new TextEditor object with the given initial contents, maximum size in characters, constraints and editor size in pixels. // Creates a new TextEditor object with the given initial contents, maximum size in characters, constraints and editor size in pixels.
public static TextEditor createTextEditor(String text, int maxSize, int constraints, int width, int height) { public static TextEditor createTextEditor(String text, int maxSize, int constraints, int width, int height) {
@ -186,7 +177,6 @@ public class TextEditor extends CanvasItem {
// Sets a listener for content changes in this TextEditor, replacing any previous TextEditorListener. // Sets a listener for content changes in this TextEditor, replacing any previous TextEditorListener.
public void setTextEditorListener(TextEditorListener listener) { public void setTextEditorListener(TextEditorListener listener) {
myListener = listener; myListener = listener;
textEditorThread.register(myId, this);
} }
// Returns the multiline state of the TextEditor. // Returns the multiline state of the TextEditor.

Просмотреть файл

@ -42,7 +42,7 @@ module J2ME {
"com/ibm/oti/connection/file/FCOutputStream.openOffsetImpl.([BJ)I": YieldReason.Root, "com/ibm/oti/connection/file/FCOutputStream.openOffsetImpl.([BJ)I": YieldReason.Root,
"com/sun/midp/io/j2me/storage/RandomAccessStream.open.(Ljava/lang/String;I)I": YieldReason.Root, "com/sun/midp/io/j2me/storage/RandomAccessStream.open.(Ljava/lang/String;I)I": YieldReason.Root,
"javax/microedition/lcdui/ImageDataFactory.createImmutableImageDecodeImage.(Ljavax/microedition/lcdui/ImageData;[BII)V": YieldReason.Root, "javax/microedition/lcdui/ImageDataFactory.createImmutableImageDecodeImage.(Ljavax/microedition/lcdui/ImageData;[BII)V": YieldReason.Root,
"com/nokia/mid/ui/TextEditorThread.sleep.()V": YieldReason.Root, "com/nokia/mid/ui/TextEditorThread.getNextDirtyEditor.()Lcom/nokia/mid/ui/TextEditor;": YieldReason.Root,
"com/nokia/mid/ui/TextEditor.setFocus.(Z)V": YieldReason.Root, "com/nokia/mid/ui/TextEditor.setFocus.(Z)V": YieldReason.Root,
"com/nokia/mid/ui/VKVisibilityNotificationRunnable.sleepUntilVKVisibilityChange.()Z": YieldReason.Root, "com/nokia/mid/ui/VKVisibilityNotificationRunnable.sleepUntilVKVisibilityChange.()Z": YieldReason.Root,
"com/nokia/mid/s40/bg/BGUtils.waitUserInteraction.()V": YieldReason.Root, "com/nokia/mid/s40/bg/BGUtils.waitUserInteraction.()V": YieldReason.Root,

Просмотреть файл

@ -1494,15 +1494,15 @@ var currentlyFocusedTextEditor;
textEditorResolve = null, textEditorResolve = null,
dirtyEditors = []; dirtyEditors = [];
function wakeTextEditorThread(id) { function wakeTextEditorThread(textEditor) {
dirtyEditors.push(id); dirtyEditors.push(textEditor);
if (textEditorResolve) { if (textEditorResolve) {
textEditorResolve(); textEditorResolve();
textEditorResolve = null; textEditorResolve = null;
} }
} }
Native["com/nokia/mid/ui/TextEditor.init.(Ljava/lang/String;IIII)I"] = Native["com/nokia/mid/ui/TextEditor.init.(Ljava/lang/String;IIII)V"] =
function(text, maxSize, constraints, width, height) { function(text, maxSize, constraints, width, height) {
if (constraints != 0) { if (constraints != 0) {
console.warn("TextEditor.constraints not implemented"); console.warn("TextEditor.constraints not implemented");
@ -1542,9 +1542,8 @@ var currentlyFocusedTextEditor;
this.setCaretPosition(this.textEditor.getContentSize()); this.setCaretPosition(this.textEditor.getContentSize());
this.textEditor.oninput(function(e) { this.textEditor.oninput(function(e) {
wakeTextEditorThread(this.textEditorId); wakeTextEditorThread(this);
}.bind(this)); }.bind(this));
return textEditorId;
}; };
Native["com/nokia/mid/ui/CanvasItem.attachNativeImpl.()V"] = function() { Native["com/nokia/mid/ui/CanvasItem.attachNativeImpl.()V"] = function() {
@ -1719,25 +1718,18 @@ var currentlyFocusedTextEditor;
this.textEditor.setFont(font); this.textEditor.setFont(font);
}; };
Native["com/nokia/mid/ui/TextEditorThread.sleep.()V"] = function() { Native["com/nokia/mid/ui/TextEditorThread.getNextDirtyEditor.()Lcom/nokia/mid/ui/TextEditor;"] = function() {
asyncImpl("V", new Promise(function(resolve, reject) { if (dirtyEditors.length) {
if (!dirtyEditors.length) { return dirtyEditors.shift();
textEditorResolve = resolve; }
} else {
resolve(); asyncImpl("I", new Promise(function(resolve, reject) {
textEditorResolve = function() {
resolve(dirtyEditors.shift());
} }
})); }));
}; };
Native["com/nokia/mid/ui/TextEditorThread.getNextDirtyEditor.()I"] = function() {
if (!dirtyEditors.length) {
console.error("ERROR: getNextDirtyEditor called but no dirty editors");
return 0;
}
return dirtyEditors.shift();
};
var curDisplayableId = 0; var curDisplayableId = 0;
var nextMidpDisplayableId = 1; var nextMidpDisplayableId = 1;
var PLAIN = 0; var PLAIN = 0;