use of java.awt.event.TextEvent in project CodenameOne by codenameone.
the class JavaSEPort method editStringLegacy.
public void editStringLegacy(final Component cmp, int maxSize, int constraint, String text, int keyCode) {
checkEDT();
java.awt.Component awtTf;
if (cmp instanceof com.codename1.ui.TextField) {
java.awt.TextField t = new java.awt.TextField();
awtTf = t;
t.setSelectionEnd(0);
t.setSelectionStart(0);
} else {
java.awt.TextArea t = new java.awt.TextArea("", 0, 0, java.awt.TextArea.SCROLLBARS_NONE);
;
awtTf = t;
t.setSelectionEnd(0);
t.setSelectionStart(0);
}
final java.awt.Component tf = awtTf;
if (keyCode > 0) {
text += ((char) keyCode);
setText(tf, text);
setCaretPosition(tf, text.length());
((com.codename1.ui.TextField) cmp).setText(getText(tf));
} else {
setText(tf, text);
}
canvas.add(tf);
if (getSkin() != null) {
tf.setBounds((int) ((cmp.getAbsoluteX() + getScreenCoordinates().x + canvas.x) * zoomLevel), (int) ((cmp.getAbsoluteY() + getScreenCoordinates().y + canvas.y) * zoomLevel), (int) (cmp.getWidth() * zoomLevel), (int) (cmp.getHeight() * zoomLevel));
java.awt.Font f = font(cmp.getStyle().getFont().getNativeFont());
tf.setFont(f.deriveFont(f.getSize2D() * zoomLevel));
} else {
tf.setBounds(cmp.getAbsoluteX(), cmp.getAbsoluteY(), cmp.getWidth(), cmp.getHeight());
tf.setFont(font(cmp.getStyle().getFont().getNativeFont()));
}
setCaretPosition(tf, getText(tf).length());
tf.requestFocus();
class Listener implements ActionListener, FocusListener, KeyListener, TextListener, Runnable {
public synchronized void run() {
while (tf.getParent() != null) {
try {
wait(20);
} catch (InterruptedException ex) {
}
}
}
public void actionPerformed(ActionEvent e) {
String txt = getText(tf);
if (testRecorder != null) {
testRecorder.editTextFieldCompleted(cmp, txt);
}
Display.getInstance().onEditingComplete(cmp, txt);
if (tf instanceof java.awt.TextField) {
((java.awt.TextField) tf).removeActionListener(this);
}
((TextComponent) tf).removeTextListener(this);
tf.removeFocusListener(this);
canvas.remove(tf);
synchronized (this) {
notify();
}
canvas.repaint();
}
public void focusGained(FocusEvent e) {
setCaretPosition(tf, getText(tf).length());
}
public void focusLost(FocusEvent e) {
actionPerformed(null);
}
public void keyTyped(KeyEvent e) {
String t = getText(tf);
if (t.length() >= ((TextArea) cmp).getMaxSize()) {
e.consume();
}
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (tf instanceof java.awt.TextField) {
actionPerformed(null);
} else {
if (getCaretPosition(tf) >= getText(tf).length() - 1) {
actionPerformed(null);
}
}
return;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
if (tf instanceof java.awt.TextField) {
actionPerformed(null);
} else {
if (getCaretPosition(tf) <= 2) {
actionPerformed(null);
}
}
return;
}
}
public void textValueChanged(TextEvent e) {
if (cmp instanceof com.codename1.ui.TextField) {
((com.codename1.ui.TextField) cmp).setText(getText(tf));
}
}
}
;
final Listener l = new Listener();
if (tf instanceof java.awt.TextField) {
((java.awt.TextField) tf).addActionListener(l);
}
((TextComponent) tf).addTextListener(l);
tf.addKeyListener(l);
tf.addFocusListener(l);
if (simulateAndroidKeyboard) {
java.util.Timer t = new java.util.Timer();
TimerTask tt = new TimerTask() {
@Override
public void run() {
if (!Display.getInstance().isEdt()) {
Display.getInstance().callSerially(this);
return;
}
if (tf.getParent() != null) {
final int height = getScreenCoordinates().height;
JavaSEPort.this.sizeChanged(getScreenCoordinates().width, height / 2);
new UITimer(new Runnable() {
public void run() {
if (tf.getParent() != null) {
new UITimer(this).schedule(100, false, Display.getInstance().getCurrent());
} else {
JavaSEPort.this.sizeChanged(getScreenCoordinates().width, height);
}
}
}).schedule(100, false, Display.getInstance().getCurrent());
}
}
};
t.schedule(tt, 300);
}
Display.getInstance().invokeAndBlock(l);
}
use of java.awt.event.TextEvent in project jdk8u_jdk by JetBrains.
the class XTextFieldPeer method setXAWTTextField.
private void setXAWTTextField(String txt) {
text = txt;
if (xtext != null) {
// JTextField.setText() posts two different events (remove & insert).
// Since we make no differences between text events,
// the document listener has to be disabled while
// JTextField.setText() is called.
xtext.getDocument().removeDocumentListener(xtext);
xtext.setText(txt);
if (firstChangeSkipped) {
postEvent(new TextEvent(target, TextEvent.TEXT_VALUE_CHANGED));
}
xtext.getDocument().addDocumentListener(xtext);
xtext.setCaretPosition(0);
}
}
use of java.awt.event.TextEvent in project jdk8u_jdk by JetBrains.
the class LWTextAreaPeer method replaceRange.
@Override
public void replaceRange(final String text, final int start, final int end) {
synchronized (getDelegateLock()) {
// JTextArea.replaceRange() posts two different events.
// Since we make no differences between text events,
// the document listener has to be disabled while
// JTextArea.replaceRange() is called.
final Document document = getTextComponent().getDocument();
document.removeDocumentListener(this);
getTextComponent().replaceRange(text, start, end);
revalidate();
postEvent(new TextEvent(getTarget(), TextEvent.TEXT_VALUE_CHANGED));
document.addDocumentListener(this);
}
repaintPeer();
}
Aggregations