Search in sources :

Example 21 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class AndroidGraphics method drawLabelComponent.

public void drawLabelComponent(int cmpX, int cmpY, int cmpHeight, int cmpWidth, Style style, String text, Bitmap icon, Bitmap stateIcon, int preserveSpaceForState, int gap, boolean rtl, boolean isOppositeSide, int textPosition, int stringWidth, boolean isTickerRunning, int tickerShiftText, boolean endsWith3Points, int valign) {
    int clipX = getClipX();
    int clipY = getClipY();
    int clipW = getClipWidth();
    int clipH = getClipHeight();
    Font cn1Font = style.getFont();
    Object nativeFont = cn1Font.getNativeFont();
    impl.setNativeFont(this, nativeFont);
    setColor(style.getFgColor());
    canvas.save();
    applyTransform();
    int iconWidth = 0;
    int iconHeight = 0;
    if (icon != null) {
        iconWidth = icon.getWidth();
        iconHeight = icon.getHeight();
    }
    int textDecoration = style.getTextDecoration();
    int stateIconSize = 0;
    int stateIconYPosition = 0;
    int leftPadding = style.getPaddingLeft(rtl);
    int rightPadding = style.getPaddingRight(rtl);
    int topPadding = style.getPaddingTop();
    int bottomPadding = style.getPaddingBottom();
    int fontHeight = 0;
    if (text == null) {
        text = "";
    }
    if (text.length() > 0) {
        fontHeight = cn1Font.getHeight();
    }
    if (stateIcon != null) {
        stateIconSize = stateIcon.getWidth();
        stateIconYPosition = cmpY + topPadding + (cmpHeight - topPadding - bottomPadding) / 2 - stateIconSize / 2;
        int tX = cmpX;
        if (isOppositeSide) {
            if (rtl) {
                tX += leftPadding;
            } else {
                tX = tX + cmpWidth - leftPadding - stateIconSize;
            }
            cmpWidth -= leftPadding - stateIconSize;
        } else {
            preserveSpaceForState = stateIconSize + gap;
            if (rtl) {
                tX = tX + cmpWidth - leftPadding - stateIconSize;
            } else {
                tX += leftPadding;
            }
        }
        drawImage(stateIcon, tX, stateIconYPosition);
    }
    // default for bottom left alignment
    int x = cmpX + leftPadding + preserveSpaceForState;
    int y = cmpY + topPadding;
    int align = reverseAlignForBidi(rtl, style.getAlignment());
    int textPos = reverseAlignForBidi(rtl, textPosition);
    // set initial x,y position according to the alignment and textPosition
    switch(align) {
        case Component.LEFT:
            switch(textPos) {
                case Label.LEFT:
                case Label.RIGHT:
                    y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? iconHeight : 0), fontHeight))) / 2;
                    break;
                case Label.BOTTOM:
                case Label.TOP:
                    y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? iconHeight + gap : 0) + fontHeight)) / 2;
                    break;
            }
            break;
        case Component.CENTER:
            switch(textPos) {
                case Label.LEFT:
                case Label.RIGHT:
                    x = x + (cmpWidth - (preserveSpaceForState + leftPadding + rightPadding + ((icon != null) ? iconWidth + gap : 0) + stringWidth)) / 2;
                    x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
                    y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? iconHeight : 0), fontHeight))) / 2;
                    break;
                case Label.BOTTOM:
                case Label.TOP:
                    x = x + (cmpWidth - (preserveSpaceForState + leftPadding + rightPadding + Math.max(((icon != null) ? iconWidth + gap : 0), stringWidth))) / 2;
                    x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
                    y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? iconHeight + gap : 0) + fontHeight)) / 2;
                    break;
            }
            break;
        case Component.RIGHT:
            switch(textPos) {
                case Label.LEFT:
                case Label.RIGHT:
                    x = cmpX + cmpWidth - rightPadding - (((icon != null) ? (iconWidth + gap) : 0) + stringWidth);
                    if (rtl) {
                        x = Math.max(x - preserveSpaceForState, cmpX + leftPadding);
                    } else {
                        x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
                    }
                    y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? iconHeight : 0), fontHeight))) / 2;
                    break;
                case Label.BOTTOM:
                case Label.TOP:
                    x = cmpX + cmpWidth - rightPadding - (Math.max(((icon != null) ? (iconWidth) : 0), stringWidth));
                    x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
                    y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? iconHeight + gap : 0) + fontHeight)) / 2;
                    break;
            }
            break;
        default:
            break;
    }
    int textSpaceW = cmpWidth - rightPadding - leftPadding;
    if (icon != null && (textPos == Label.RIGHT || textPos == Label.LEFT)) {
        textSpaceW = textSpaceW - iconWidth;
    }
    if (stateIcon != null) {
        textSpaceW = textSpaceW - stateIconSize;
    } else {
        textSpaceW = textSpaceW - preserveSpaceForState;
    }
    if (icon == null) {
        // no icon only string
        drawLabelString(nativeFont, text, x, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, stringWidth, fontHeight);
    } else {
        int strWidth = stringWidth;
        int iconStringWGap;
        int iconStringHGap;
        switch(textPos) {
            case Label.LEFT:
                if (iconHeight > fontHeight) {
                    iconStringHGap = (iconHeight - fontHeight) / 2;
                    strWidth = drawLabelStringValign(nativeFont, text, x, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, strWidth, iconStringHGap, iconHeight, fontHeight, valign);
                    drawImage(icon, x + strWidth + gap, y);
                } else {
                    iconStringHGap = (fontHeight - iconHeight) / 2;
                    strWidth = drawLabelString(nativeFont, text, x, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, strWidth, fontHeight);
                    drawImage(icon, x + strWidth + gap, y + iconStringHGap);
                }
                break;
            case Label.RIGHT:
                if (iconHeight > fontHeight) {
                    iconStringHGap = (iconHeight - fontHeight) / 2;
                    drawImage(icon, x, y);
                    drawLabelStringValign(nativeFont, text, x + iconWidth + gap, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, iconWidth, iconStringHGap, iconHeight, fontHeight, valign);
                } else {
                    iconStringHGap = (fontHeight - iconHeight) / 2;
                    drawImage(icon, x, y + iconStringHGap);
                    drawLabelString(nativeFont, text, x + iconWidth + gap, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, iconWidth, fontHeight);
                }
                break;
            case Label.BOTTOM:
                // center align the smaller
                if (iconWidth > strWidth) {
                    iconStringWGap = (iconWidth - strWidth) / 2;
                    drawImage(icon, x, y);
                    drawLabelString(nativeFont, text, x + iconStringWGap, y + iconHeight + gap, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, iconWidth, fontHeight);
                } else {
                    iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
                    drawImage(icon, x + iconStringWGap, y);
                    drawLabelString(nativeFont, text, x, y + iconHeight + gap, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, iconWidth, fontHeight);
                }
                break;
            case Label.TOP:
                // center align the smaller
                if (iconWidth > strWidth) {
                    iconStringWGap = (iconWidth - strWidth) / 2;
                    drawLabelString(nativeFont, text, x + iconStringWGap, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, iconWidth, fontHeight);
                    drawImage(icon, x, y + fontHeight + gap);
                } else {
                    iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
                    drawLabelString(nativeFont, text, x, y, textSpaceW, isTickerRunning, tickerShiftText, textDecoration, rtl, endsWith3Points, iconWidth, fontHeight);
                    drawImage(icon, x + iconStringWGap, y + fontHeight + gap);
                }
                break;
        }
    }
    unapplyTransform();
    canvas.restore();
    setClip(clipX, clipY, clipW, clipH);
}
Also used : Paint(android.graphics.Paint) Font(com.codename1.ui.Font)

Example 22 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class Paint method getTextWidths.

public void getTextWidths(String text, float[] widths) {
    Font f = getTypeface();
    if (f != null) {
        char[] chars = text.toCharArray();
        int clen = chars.length;
        int wlen = widths.length;
        for (int i = 0; i < clen && i < wlen; i++) {
            widths[i] = f.charWidth(chars[i]);
        }
    } else {
        throw new RuntimeException("Faild to get cn1 font");
    }
}
Also used : Font(com.codename1.ui.Font)

Example 23 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class JavaSEPort method editString.

/**
 * @inheritDoc
 */
public void editString(final Component cmp, int maxSize, int constraint, String text, int keyCode) {
    if (scrollWheeling) {
        return;
    }
    if (System.getProperty("TextCompatMode") != null) {
        editStringLegacy(cmp, maxSize, constraint, text, keyCode);
        return;
    }
    // a workaround to fix an issue where the previous Text Component wasn't removed properly.
    java.awt.Component[] cmps = canvas.getComponents();
    for (int i = 0; i < cmps.length; i++) {
        java.awt.Component cmp1 = cmps[i];
        if (cmp1 instanceof JScrollPane || cmp1 instanceof javax.swing.text.JTextComponent) {
            canvas.remove(cmp1);
        }
    }
    checkEDT();
    javax.swing.text.JTextComponent swingT;
    if (((com.codename1.ui.TextArea) cmp).isSingleLineTextArea()) {
        JTextComponent t;
        if (isDesktop() && (constraint & TextArea.PASSWORD) == TextArea.PASSWORD) {
            t = new JPasswordField() {

                public void repaint(long tm, int x, int y, int width, int height) {
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            cmp.repaint();
                        }
                    });
                }
            };
        } else {
            t = new JTextField() {

                public void repaint(long tm, int x, int y, int width, int height) {
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            cmp.repaint();
                        }
                    });
                }
            };
        /*
                ((JTextField)t).addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (cmp instanceof com.codename1.ui.TextField) {
                            final com.codename1.ui.TextField tf = (com.codename1.ui.TextField)cmp;
                            if (tf.getDoneListener() != null) {
                                Display.getInstance().callSerially(new Runnable() {
                                    public void run() {
                                        if (tf.getDoneListener() != null) {
                                            tf.fireDoneEvent();
                                        }
                                    }
                                });
                            }
                        }
                    }
                    
                });
                */
        }
        swingT = t;
        textCmp = swingT;
    } else {
        final com.codename1.ui.TextArea ta = (com.codename1.ui.TextArea) cmp;
        JTextArea t = new JTextArea(ta.getLines(), ta.getColumns()) {

            public void repaint(long tm, int x, int y, int width, int height) {
                int marginTop = cmp.getSelectedStyle().getPadding(Component.TOP);
                int marginLeft = cmp.getSelectedStyle().getPadding(Component.LEFT);
                int marginRight = cmp.getSelectedStyle().getPadding(Component.RIGHT);
                int marginBottom = cmp.getSelectedStyle().getPadding(Component.BOTTOM);
                Rectangle bounds;
                if (getSkin() != null) {
                    bounds = new Rectangle((int) ((cmp.getAbsoluteX() + cmp.getScrollX() + getScreenCoordinates().x + canvas.x + marginLeft) * zoomLevel), (int) ((cmp.getAbsoluteY() + cmp.getScrollY() + getScreenCoordinates().y + canvas.y + marginTop) * zoomLevel), (int) ((cmp.getWidth() - marginLeft - marginRight) * zoomLevel), (int) ((cmp.getHeight() - marginTop - marginBottom) * zoomLevel));
                } else {
                    bounds = new Rectangle(cmp.getAbsoluteX() + cmp.getScrollX() + marginLeft, cmp.getAbsoluteY() + cmp.getScrollY() + marginTop, cmp.getWidth() - marginRight - marginLeft, cmp.getHeight() - marginTop - marginBottom);
                }
                if (textCmp != null && !textCmp.getBounds().equals(bounds)) {
                    textCmp.setBounds(bounds);
                }
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        cmp.repaint();
                    }
                });
            }
        };
        t.setWrapStyleWord(true);
        t.setLineWrap(true);
        swingT = t;
        JScrollPane pane = new JScrollPane(swingT);
        pane.setBorder(null);
        pane.setOpaque(false);
        pane.getViewport().setOpaque(false);
        pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        textCmp = pane;
    }
    DefaultCaret caret = (DefaultCaret) swingT.getCaret();
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    swingT.setFocusTraversalKeysEnabled(false);
    TextEditUtil.setCurrentEditComponent(cmp);
    final javax.swing.text.JTextComponent txt = swingT;
    txt.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_TAB) {
                TextEditUtil.editNextTextArea();
            }
        }
    });
    swingT.setBorder(null);
    swingT.setOpaque(false);
    swingT.setForeground(new Color(cmp.getUnselectedStyle().getFgColor()));
    final javax.swing.text.JTextComponent tf = swingT;
    if (keyCode > 0) {
        text += ((char) keyCode);
        setText(tf, text);
        setCaretPosition(tf, text.length());
        if (cmp instanceof com.codename1.ui.TextField) {
            ((com.codename1.ui.TextField) cmp).setText(getText(tf));
        }
    } else {
        setText(tf, text);
    }
    textCmp.setBorder(null);
    textCmp.setOpaque(false);
    canvas.add(textCmp);
    int marginTop = cmp.getSelectedStyle().getPadding(Component.TOP);
    int marginLeft = cmp.getSelectedStyle().getPadding(Component.LEFT);
    int marginRight = cmp.getSelectedStyle().getPadding(Component.RIGHT);
    int marginBottom = cmp.getSelectedStyle().getPadding(Component.BOTTOM);
    if (getSkin() != null) {
        textCmp.setBounds((int) ((cmp.getAbsoluteX() + cmp.getScrollX() + getScreenCoordinates().x + canvas.x + marginLeft) * zoomLevel), (int) ((cmp.getAbsoluteY() + cmp.getScrollY() + getScreenCoordinates().y + canvas.y + marginTop) * zoomLevel), (int) ((cmp.getWidth() - marginLeft - marginRight) * zoomLevel), (int) ((cmp.getHeight() - marginTop - marginBottom) * zoomLevel));
        java.awt.Font f = font(cmp.getStyle().getFont().getNativeFont());
        tf.setFont(f.deriveFont(f.getSize2D() * zoomLevel));
    } else {
        textCmp.setBounds(cmp.getAbsoluteX() + cmp.getScrollX() + marginLeft, cmp.getAbsoluteY() + cmp.getScrollY() + marginTop, cmp.getWidth() - marginRight - marginLeft, cmp.getHeight() - marginTop - marginBottom);
        tf.setFont(font(cmp.getStyle().getFont().getNativeFont()));
    }
    setCaretPosition(tf, getText(tf).length());
    tf.requestFocus();
    tf.setSelectionStart(0);
    tf.setSelectionEnd(0);
    class Listener implements ActionListener, FocusListener, KeyListener, TextListener, Runnable, DocumentListener {

        public synchronized void run() {
            while (textCmp.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 (e != null && cmp instanceof com.codename1.ui.TextField) {
                final com.codename1.ui.TextField cn1Tf = (com.codename1.ui.TextField) cmp;
                if (cmp != null && cn1Tf.getDoneListener() != null) {
                    cn1Tf.fireDoneEvent();
                }
            }
            if (tf instanceof JTextField) {
                ((JTextField) tf).removeActionListener(this);
            }
            ((JTextComponent) tf).getDocument().removeDocumentListener(this);
            tf.removeFocusListener(this);
            canvas.remove(textCmp);
            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 JTextField) {
                    actionPerformed(null);
                } else {
                    if (getCaretPosition(tf) >= getText(tf).length() - 1) {
                        actionPerformed(null);
                    }
                }
                return;
            }
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                if (tf instanceof JTextField) {
                    actionPerformed(null);
                } else {
                    if (getCaretPosition(tf) <= 2) {
                        actionPerformed(null);
                    }
                }
                return;
            }
        }

        public void textValueChanged(TextEvent e) {
            if (cmp instanceof com.codename1.ui.TextField) {
                updateText();
            }
        }

        private void updateText() {
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    if (cmp instanceof com.codename1.ui.TextField) {
                        ((com.codename1.ui.TextField) cmp).setText(getText(tf));
                    }
                }
            });
        }

        public void insertUpdate(DocumentEvent e) {
            updateText();
        }

        public void removeUpdate(DocumentEvent e) {
            updateText();
        }

        public void changedUpdate(DocumentEvent e) {
            updateText();
        }
    }
    ;
    final Listener l = new Listener();
    if (tf instanceof JTextField) {
        ((JTextField) tf).addActionListener(l);
    }
    ((JTextComponent) tf).getDocument().addDocumentListener(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 : DocumentListener(javax.swing.event.DocumentListener) 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) Rectangle(java.awt.Rectangle) JTextComponent(javax.swing.text.JTextComponent) FocusEvent(java.awt.event.FocusEvent) KeyEvent(java.awt.event.KeyEvent) DefaultCaret(javax.swing.text.DefaultCaret) FontRenderContext(java.awt.font.FontRenderContext) BrowserComponent(com.codename1.ui.BrowserComponent) Component(com.codename1.ui.Component) JTextComponent(javax.swing.text.JTextComponent) PeerComponent(com.codename1.ui.PeerComponent) TextEvent(java.awt.event.TextEvent) Color(java.awt.Color) TextListener(java.awt.event.TextListener) DocumentEvent(javax.swing.event.DocumentEvent) JTextComponent(javax.swing.text.JTextComponent) Point(java.awt.Point) ActionListener(java.awt.event.ActionListener) java.util(java.util) UITimer(com.codename1.ui.util.UITimer) KeyListener(java.awt.event.KeyListener) TextArea(com.codename1.ui.TextArea) FocusListener(java.awt.event.FocusListener) java.awt(java.awt)

Example 24 with Font

use of com.codename1.ui.Font 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 25 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class Component method createStyleAnimation.

ComponentAnimation createStyleAnimation(final Style sourceStyle, final Style destStyle, final int duration, final String destUIID) {
    int d = duration;
    Motion m = null;
    if (sourceStyle.getFgColor() != destStyle.getFgColor()) {
        m = Motion.createLinearColorMotion(sourceStyle.getFgColor(), destStyle.getFgColor(), d);
    }
    final Motion fgColorMotion = m;
    m = null;
    if (sourceStyle.getOpacity() != destStyle.getOpacity()) {
        m = Motion.createLinearColorMotion(sourceStyle.getOpacity(), destStyle.getOpacity(), d);
    }
    final Motion opacityMotion = m;
    m = null;
    if (sourceStyle.getFont().getHeight() != destStyle.getFont().getHeight() && sourceStyle.getFont().isTTFNativeFont()) {
        // allows for fractional font sizes
        m = Motion.createLinearMotion(sourceStyle.getFont().getHeight() * 100, destStyle.getFont().getHeight() * 100, d);
    }
    final Motion fontMotion = m;
    m = null;
    if (sourceStyle.getPaddingTop() != destStyle.getPaddingTop()) {
        m = Motion.createLinearMotion(sourceStyle.getPaddingTop(), destStyle.getPaddingTop(), d);
    }
    final Motion paddingTop = m;
    m = null;
    if (sourceStyle.getPaddingBottom() != destStyle.getPaddingBottom()) {
        m = Motion.createLinearMotion(sourceStyle.getPaddingBottom(), destStyle.getPaddingBottom(), d);
    }
    final Motion paddingBottom = m;
    m = null;
    if (sourceStyle.getPaddingLeftNoRTL() != destStyle.getPaddingLeftNoRTL()) {
        m = Motion.createLinearMotion(sourceStyle.getPaddingLeftNoRTL(), destStyle.getPaddingLeftNoRTL(), d);
    }
    final Motion paddingLeft = m;
    m = null;
    if (sourceStyle.getPaddingRightNoRTL() != destStyle.getPaddingRightNoRTL()) {
        m = Motion.createLinearMotion(sourceStyle.getPaddingRightNoRTL(), destStyle.getPaddingRightNoRTL(), d);
    }
    final Motion paddingRight = m;
    m = null;
    if (sourceStyle.getMarginTop() != destStyle.getMarginTop()) {
        m = Motion.createLinearMotion(sourceStyle.getMarginTop(), destStyle.getMarginTop(), d);
    }
    final Motion marginTop = m;
    m = null;
    if (sourceStyle.getMarginBottom() != destStyle.getMarginBottom()) {
        m = Motion.createLinearMotion(sourceStyle.getMarginBottom(), destStyle.getMarginBottom(), d);
    }
    final Motion marginBottom = m;
    m = null;
    if (sourceStyle.getMarginLeftNoRTL() != destStyle.getMarginLeftNoRTL()) {
        m = Motion.createLinearMotion(sourceStyle.getMarginLeftNoRTL(), destStyle.getMarginLeftNoRTL(), d);
    }
    final Motion marginLeft = m;
    m = null;
    if (sourceStyle.getMarginRightNoRTL() != destStyle.getMarginRightNoRTL()) {
        m = Motion.createLinearMotion(sourceStyle.getMarginRightNoRTL(), destStyle.getMarginRightNoRTL(), d);
    }
    final Motion marginRight = m;
    m = null;
    if (paddingLeft != null || paddingRight != null || paddingTop != null || paddingBottom != null) {
        // convert the padding to pixels for smooth animation
        int left = sourceStyle.getPaddingLeftNoRTL();
        int right = sourceStyle.getPaddingRightNoRTL();
        int top = sourceStyle.getPaddingTop();
        int bottom = sourceStyle.getPaddingBottom();
        sourceStyle.setPaddingUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS);
        sourceStyle.setPadding(top, bottom, left, right);
    }
    if (marginLeft != null || marginRight != null || marginTop != null || marginBottom != null) {
        // convert the margin to pixels for smooth animation
        int left = sourceStyle.getMarginLeftNoRTL();
        int right = sourceStyle.getMarginRightNoRTL();
        int top = sourceStyle.getMarginTop();
        int bottom = sourceStyle.getMarginBottom();
        sourceStyle.setMarginUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS);
        sourceStyle.setMargin(top, bottom, left, right);
    }
    final AnimationTransitionPainter ap = new AnimationTransitionPainter();
    if (sourceStyle.getBgTransparency() != 0 || destStyle.getBgTransparency() != 0 || (sourceStyle.getBorder() != null && sourceStyle.getBorder().isEmptyBorder()) || (destStyle.getBorder() != null && destStyle.getBorder().isEmptyBorder()) || sourceStyle.getBgImage() != null || destStyle.getBgImage() != null) {
        ap.original = sourceStyle.getBgPainter();
        ap.dest = destStyle.getBgPainter();
        ap.originalStyle = sourceStyle;
        ap.destStyle = destStyle;
        if (ap.dest == null) {
            ap.dest = new BGPainter();
        }
        sourceStyle.setBgPainter(ap);
    }
    final Motion bgMotion = Motion.createLinearMotion(0, 255, d);
    return new ComponentAnimation() {

        private boolean finished;

        private boolean stepMode;

        private boolean started;

        @Override
        public boolean isStepModeSupported() {
            return true;
        }

        @Override
        public int getMaxSteps() {
            return duration;
        }

        @Override
        public void setStep(int step) {
            stepMode = true;
            if (!finished) {
                if (bgMotion != null) {
                    bgMotion.setCurrentMotionTime(step);
                }
                if (fgColorMotion != null) {
                    fgColorMotion.setCurrentMotionTime(step);
                }
                if (opacityMotion != null) {
                    opacityMotion.setCurrentMotionTime(step);
                }
                if (fontMotion != null) {
                    fontMotion.setCurrentMotionTime(step);
                }
                if (paddingTop != null) {
                    paddingTop.setCurrentMotionTime(step);
                }
                if (paddingBottom != null) {
                    paddingBottom.setCurrentMotionTime(step);
                }
                if (paddingLeft != null) {
                    paddingLeft.setCurrentMotionTime(step);
                }
                if (paddingRight != null) {
                    paddingRight.setCurrentMotionTime(step);
                }
                if (marginTop != null) {
                    marginTop.setCurrentMotionTime(step);
                }
                if (marginBottom != null) {
                    marginBottom.setCurrentMotionTime(step);
                }
                if (marginLeft != null) {
                    marginLeft.setCurrentMotionTime(step);
                }
                if (marginRight != null) {
                    marginRight.setCurrentMotionTime(step);
                }
            }
            super.setStep(step);
        }

        @Override
        public boolean isInProgress() {
            if (!stepMode && !started) {
                return true;
            }
            return stepMode || !((bgMotion == null || bgMotion.isFinished()) && (opacityMotion == null || opacityMotion.isFinished()) && (fgColorMotion == null || fgColorMotion.isFinished()) && (paddingLeft == null || paddingLeft.isFinished()) && (paddingRight == null || paddingRight.isFinished()) && (paddingTop == null || paddingTop.isFinished()) && (paddingBottom == null || paddingBottom.isFinished()) && (marginLeft == null || marginLeft.isFinished()) && (marginRight == null || marginRight.isFinished()) && (marginTop == null || marginTop.isFinished()) && (marginBottom == null || marginBottom.isFinished()) && (fontMotion == null || fontMotion.isFinished()));
        }

        @Override
        protected void updateState() {
            if (finished) {
                return;
            }
            if (!started && !stepMode) {
                started = true;
                if (bgMotion != null) {
                    bgMotion.start();
                }
                if (opacityMotion != null) {
                    opacityMotion.start();
                }
                if (fgColorMotion != null) {
                    fgColorMotion.start();
                }
                if (fontMotion != null) {
                    fontMotion.start();
                }
                if (paddingTop != null) {
                    paddingTop.start();
                }
                if (paddingBottom != null) {
                    paddingBottom.start();
                }
                if (paddingLeft != null) {
                    paddingLeft.start();
                }
                if (paddingRight != null) {
                    paddingRight.start();
                }
                if (marginTop != null) {
                    marginTop.start();
                }
                if (marginBottom != null) {
                    marginBottom.start();
                }
                if (marginLeft != null) {
                    marginLeft.start();
                }
                if (marginRight != null) {
                    marginRight.start();
                }
            }
            if (!isInProgress()) {
                finished = true;
                if (destUIID != null) {
                    setUIID(destUIID);
                }
            } else {
                if (opacityMotion != null) {
                    sourceStyle.setOpacity(opacityMotion.getValue());
                }
                if (fgColorMotion != null) {
                    sourceStyle.setFgColor(fgColorMotion.getValue());
                }
                if (bgMotion != null) {
                    ap.alpha = bgMotion.getValue();
                }
                if (fontMotion != null) {
                    Font fnt = sourceStyle.getFont();
                    fnt = fnt.derive(((float) fontMotion.getValue()) / 100.0f, fnt.getStyle());
                    sourceStyle.setFont(fnt);
                }
                if (paddingTop != null) {
                    sourceStyle.setPadding(TOP, paddingTop.getValue());
                }
                if (paddingBottom != null) {
                    sourceStyle.setPadding(BOTTOM, paddingBottom.getValue());
                }
                if (paddingLeft != null) {
                    sourceStyle.setPadding(LEFT, paddingLeft.getValue());
                }
                if (paddingRight != null) {
                    sourceStyle.setPadding(RIGHT, paddingRight.getValue());
                }
                if (marginTop != null) {
                    sourceStyle.setMargin(TOP, marginTop.getValue());
                }
                if (marginBottom != null) {
                    sourceStyle.setMargin(BOTTOM, marginBottom.getValue());
                }
                if (marginLeft != null) {
                    sourceStyle.setMargin(LEFT, marginLeft.getValue());
                }
                if (marginRight != null) {
                    sourceStyle.setMargin(RIGHT, marginRight.getValue());
                }
            }
        }

        @Override
        public void flush() {
            if (bgMotion != null) {
                bgMotion.finish();
            }
            if (opacityMotion != null) {
                opacityMotion.finish();
            }
            if (fgColorMotion != null) {
                fgColorMotion.finish();
            }
            if (fontMotion != null) {
                fontMotion.finish();
            }
            if (paddingTop != null) {
                paddingTop.finish();
            }
            if (paddingBottom != null) {
                paddingBottom.finish();
            }
            if (paddingLeft != null) {
                paddingLeft.finish();
            }
            if (paddingRight != null) {
                paddingRight.finish();
            }
            if (marginTop != null) {
                marginTop.finish();
            }
            if (marginBottom != null) {
                marginBottom.finish();
            }
            if (marginLeft != null) {
                marginLeft.finish();
            }
            if (marginRight != null) {
                marginRight.finish();
            }
            updateState();
        }
    };
}
Also used : Motion(com.codename1.ui.animations.Motion) ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) Point(com.codename1.ui.geom.Point)

Aggregations

Font (com.codename1.ui.Font)30 Image (com.codename1.ui.Image)12 Hashtable (java.util.Hashtable)10 EditorFont (com.codename1.ui.EditorFont)8 Border (com.codename1.ui.plaf.Border)8 Component (com.codename1.ui.Component)7 Style (com.codename1.ui.plaf.Style)7 EncodedImage (com.codename1.ui.EncodedImage)6 TextArea (com.codename1.ui.TextArea)6 AnimationObject (com.codename1.ui.animations.AnimationObject)6 BufferedImage (java.awt.image.BufferedImage)6 IOException (java.io.IOException)6 java.awt (java.awt)5 Container (com.codename1.ui.Container)4 Dimension (com.codename1.ui.geom.Dimension)4 RoundBorder (com.codename1.ui.plaf.RoundBorder)4 RoundRectBorder (com.codename1.ui.plaf.RoundRectBorder)4 ParseException (java.text.ParseException)4 Paint (android.graphics.Paint)3 EditorTTFFont (com.codename1.ui.EditorTTFFont)3