Search in sources :

Example 11 with Char

use of com.codename1.ui.TextSelection.Char 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 : TextArea(com.codename1.ui.TextArea) UITimer(com.codename1.ui.util.UITimer) Timer(java.util.Timer) AttributedString(java.text.AttributedString) JTextComponent(javax.swing.text.JTextComponent) Point(java.awt.Point) java.util(java.util) UITimer(com.codename1.ui.util.UITimer) Timer(java.util.Timer) java.awt(java.awt)

Example 12 with Char

use of com.codename1.ui.TextSelection.Char in project CodenameOne by codenameone.

the class DefaultLookAndFeel method append.

private void append(TextSelection sel, Component l, Span span, String text, Font f, int posOffset, int x, int y, int h) {
    int len = text.length();
    int xPos = 0;
    int curPos = 1;
    // int ty = l.getAbsoluteY() - sel.getSelectionRoot().getAbsoluteY() - l.getY();
    while (curPos <= len) {
        int newXpos = f.stringWidth(text.substring(0, curPos));
        Char next = sel.newChar(posOffset + curPos - 1, x + xPos, y, newXpos - xPos, h);
        span.add(next);
        xPos = newXpos;
        curPos++;
    }
}
Also used : Char(com.codename1.ui.TextSelection.Char)

Example 13 with Char

use of com.codename1.ui.TextSelection.Char in project CodenameOne by codenameone.

the class DefaultListCellRenderer method getCellRendererComponent.

/**
 * {@inheritDoc}
 */
public Component getCellRendererComponent(Component list, Object model, T value, int index, boolean isSelected) {
    if (!alwaysRenderSelection && !Display.getInstance().shouldRenderSelection(list)) {
        isSelected = false;
    }
    setFocus(isSelected);
    if (showNumbers) {
        String text = "" + value;
        Map<String, String> t = UIManager.getInstance().getBundle();
        if (t != null && value != null) {
            Object o = t.get(value.toString());
            if (o != null) {
                text = (String) o;
            }
        }
        if (isRTL()) {
            setText(text + " ." + (index + 1));
        } else {
            setText("" + (index + 1) + ". " + text);
        }
    } else {
        if (value != null) {
            String v = value.toString();
            setText(v);
            if (isRightAlignNumbers()) {
                char c = v.charAt(0);
                Style s = getStyle();
                if (c >= '0' && c <= '9') {
                    s.setAlignment(RIGHT);
                } else {
                    s.setAlignment(LEFT);
                }
            }
        } else {
            setText("null");
        }
    }
    if (value instanceof Command) {
        Image i = ((Command) value).getIcon();
        if (i == null) {
            if (((Command) value).getMaterialIcon() != 0) {
                if (((Command) value).getIconFont() != null) {
                    setFontIcon(((Command) value).getIconFont(), ((Command) value).getMaterialIcon());
                } else {
                    setMaterialIcon(((Command) value).getMaterialIcon());
                }
            } else {
                setIcon(null);
            }
        } else {
            setIcon(i);
        }
        setEnabled(((Command) value).isEnabled());
    }
    return this;
}
Also used : Command(com.codename1.ui.Command) Style(com.codename1.ui.plaf.Style) Image(com.codename1.ui.Image)

Example 14 with Char

use of com.codename1.ui.TextSelection.Char in project CodenameOne by codenameone.

the class ComponentSelector method setIcon.

/**
 * Sets the icon of all elements in this set to a material icon.  This will use
 * the foreground color of each label as the icon's foreground color.
 * @param materialIcon The icon charcode.
 * @param size The size of the icon (in mm)
 * @return Self for chaining
 * @see FontImage#createMaterial(char, com.codename1.ui.plaf.Style, float)
 */
public ComponentSelector setIcon(char materialIcon, float size) {
    for (Component c : this) {
        if (c instanceof Label) {
            Label l = (Label) c;
            Style style = new Style();
            Style cStyle = c.getUnselectedStyle();
            style.setBgTransparency(0);
            style.setFgColor(cStyle.getFgColor());
            l.setIcon(FontImage.createMaterial(materialIcon, style, size));
            if (c instanceof Button) {
                Button b = (Button) c;
                style = new Style();
                cStyle = c.getPressedStyle();
                style.setBgTransparency(0);
                style.setFgColor(cStyle.getFgColor());
                b.setPressedIcon(FontImage.createMaterial(materialIcon, style, size));
            }
        }
    }
    return this;
}
Also used : SpanButton(com.codename1.components.SpanButton) SpanLabel(com.codename1.components.SpanLabel) Style(com.codename1.ui.plaf.Style)

Example 15 with Char

use of com.codename1.ui.TextSelection.Char in project CodenameOne by codenameone.

the class FontImage method setMaterialIcon.

/**
 * <p>Applies a material design icon (one of the MATERIAL_* icon constants) to the given label using the
 * styling of the label. Notice that when the argument is a button the pressed/selected &amp; disabled states
 * will be set appropriately.</p>
 * <script src="https://gist.github.com/codenameone/8cf6f70188959524474b.js"></script>
 *
 * @param l a label or subclass (e.g. Button etc.)
 * @param icon one of the MATERIAL_* icons
 * @param size the size of the icon in millimeters
 */
public static void setMaterialIcon(Label l, char icon, float size) {
    if (Font.isTrueTypeFileSupported()) {
        Style s = new Style(l.getUnselectedStyle());
        s.setFont(getMaterialDesignFont().derive(rightSize(s, size), Font.STYLE_PLAIN));
        l.setIcon(FontImage.create("" + icon, s));
        if (l instanceof Button) {
            Button b = (Button) l;
            Style sel = b.getSelectedStyle();
            Style pre = b.getPressedStyle();
            Style dis = b.getDisabledStyle();
            if (sel.getFgColor() != s.getFgColor() || (sel.getBgColor() != s.getBgColor()) || (sel.getBgTransparency() != s.getBgTransparency())) {
                sel = new Style(sel);
                sel.setFont(getMaterialDesignFont().derive(rightSize(sel, size), Font.STYLE_PLAIN));
                b.setRolloverIcon(FontImage.create("" + icon, sel));
            }
            if (pre.getFgColor() != s.getFgColor() || (pre.getBgColor() != s.getBgColor()) || (pre.getBgTransparency() != s.getBgTransparency())) {
                pre = new Style(pre);
                pre.setFont(getMaterialDesignFont().derive(rightSize(pre, size), Font.STYLE_PLAIN));
                b.setPressedIcon(FontImage.create("" + icon, pre));
                b.setRolloverPressedIcon(FontImage.create("" + icon, pre));
            }
            if (dis.getFgColor() != s.getFgColor() || (dis.getBgColor() != s.getBgColor()) || (dis.getBgTransparency() != s.getBgTransparency())) {
                dis = new Style(dis);
                dis.setFont(getMaterialDesignFont().derive(rightSize(dis, size), Font.STYLE_PLAIN));
                b.setDisabledIcon(FontImage.create("" + icon, dis));
            }
        }
    }
}
Also used : SpanButton(com.codename1.components.SpanButton) MultiButton(com.codename1.components.MultiButton) Style(com.codename1.ui.plaf.Style)

Aggregations

Style (com.codename1.ui.plaf.Style)15 Font (com.codename1.ui.Font)8 ArrayList (java.util.ArrayList)5 ComponentImage (ca.weblite.shared.components.ComponentImage)4 Image (com.codename1.ui.Image)4 Point (java.awt.Point)4 Hashtable (java.util.Hashtable)4 MultiButton (com.codename1.components.MultiButton)3 SpanButton (com.codename1.components.SpanButton)3 EventObject (java.util.EventObject)3 List (java.util.List)3 Vector (java.util.Vector)3 SpanLabel (com.codename1.components.SpanLabel)2 BadgeUIID (com.codename1.rad.attributes.BadgeUIID)2 IconUIID (com.codename1.rad.attributes.IconUIID)2 UIID (com.codename1.rad.attributes.UIID)2 Button (com.codename1.ui.Button)2 TextArea (com.codename1.ui.TextArea)2 BorderLayout (com.codename1.ui.layouts.BorderLayout)2 UIManager (com.codename1.ui.plaf.UIManager)2