Search in sources :

Example 6 with TextEvent

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);
}
Also used : FocusListener(java.awt.event.FocusListener) TextListener(java.awt.event.TextListener) KeyListener(java.awt.event.KeyListener) ActionListener(java.awt.event.ActionListener) AdjustmentListener(java.awt.event.AdjustmentListener) MouseMotionListener(java.awt.event.MouseMotionListener) MouseWheelListener(java.awt.event.MouseWheelListener) DocumentListener(javax.swing.event.DocumentListener) ItemListener(java.awt.event.ItemListener) WindowListener(java.awt.event.WindowListener) MouseListener(java.awt.event.MouseListener) MenuListener(javax.swing.event.MenuListener) HierarchyBoundsListener(java.awt.event.HierarchyBoundsListener) TextArea(com.codename1.ui.TextArea) UITimer(com.codename1.ui.util.UITimer) ActionEvent(java.awt.event.ActionEvent) FocusEvent(java.awt.event.FocusEvent) KeyEvent(java.awt.event.KeyEvent) JTextComponent(javax.swing.text.JTextComponent) TextEvent(java.awt.event.TextEvent) TextListener(java.awt.event.TextListener) Point(java.awt.Point) ActionListener(java.awt.event.ActionListener) java.util(java.util) UITimer(com.codename1.ui.util.UITimer) KeyListener(java.awt.event.KeyListener) FocusListener(java.awt.event.FocusListener) java.awt(java.awt)

Example 7 with TextEvent

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);
    }
}
Also used : TextEvent(java.awt.event.TextEvent)

Example 8 with TextEvent

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();
}
Also used : TextEvent(java.awt.event.TextEvent) Document(javax.swing.text.Document)

Aggregations

TextEvent (java.awt.event.TextEvent)8 TextListener (java.awt.event.TextListener)5 ItemListener (java.awt.event.ItemListener)4 Color (java.awt.Color)3 Point (java.awt.Point)3 KeyEvent (java.awt.event.KeyEvent)3 MouseListener (java.awt.event.MouseListener)3 WindowListener (java.awt.event.WindowListener)3 TextArea (com.codename1.ui.TextArea)2 UITimer (com.codename1.ui.util.UITimer)2 java.awt (java.awt)2 Panel (java.awt.Panel)2 Rectangle (java.awt.Rectangle)2 TextField (java.awt.TextField)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 AdjustmentListener (java.awt.event.AdjustmentListener)2 FocusEvent (java.awt.event.FocusEvent)2 FocusListener (java.awt.event.FocusListener)2 HierarchyBoundsListener (java.awt.event.HierarchyBoundsListener)2