Search in sources :

Example 36 with Char

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

the class FontImage method setIcon.

/**
 * <p>Applies an icon from the font 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 font a font with icons
 * @param icon an icon in the font
 * @param size the size of the icon in millimeters, or -1 to use the default height of the font
 */
public static void setIcon(IconHolder l, Font font, char icon, float size) {
    Style s = new Style(l.getIconStyleComponent().getUnselectedStyle());
    s.setFont(font.derive(rightSize(s, size), Font.STYLE_PLAIN));
    l.setIcon(FontImage.create("" + icon, s));
    if (l instanceof SelectableIconHolder) {
        SelectableIconHolder b = (SelectableIconHolder) l;
        Style sel = b.getIconStyleComponent().getSelectedStyle();
        Style pre = b.getIconStyleComponent().getPressedStyle();
        Style dis = b.getIconStyleComponent().getDisabledStyle();
        if (sel.getFgColor() != s.getFgColor() || (sel.getBgColor() != s.getBgColor()) || (sel.getBgTransparency() != s.getBgTransparency()) || (sel.getFgAlpha() != s.getFgAlpha())) {
            sel = new Style(sel);
            sel.setFont(font.derive(rightSize(sel, size), Font.STYLE_PLAIN));
            b.setRolloverIcon(FontImage.create("" + icon, sel));
        } else {
            b.setRolloverIcon(null);
        }
        if (pre.getFgColor() != s.getFgColor() || (pre.getBgColor() != s.getBgColor()) || (pre.getBgTransparency() != s.getBgTransparency()) || (pre.getFgAlpha() != s.getFgAlpha())) {
            pre = new Style(pre);
            pre.setFont(font.derive(rightSize(pre, size), Font.STYLE_PLAIN));
            b.setPressedIcon(FontImage.create("" + icon, pre));
            b.setRolloverPressedIcon(FontImage.create("" + icon, pre));
        } else {
            b.setPressedIcon(null);
        }
        if (dis.getFgColor() != s.getFgColor() || (dis.getBgColor() != s.getBgColor()) || (dis.getBgTransparency() != s.getBgTransparency()) || (dis.getFgAlpha() != s.getFgAlpha())) {
            dis = new Style(dis);
            dis.setFont(font.derive(rightSize(dis, size), Font.STYLE_PLAIN));
            b.setDisabledIcon(FontImage.create("" + icon, dis));
        } else {
            b.setDisabledIcon(null);
        }
    }
}
Also used : Style(com.codename1.ui.plaf.Style)

Example 37 with Char

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

the class TextArea method initRowString.

private void initRowString() {
    if (!Display.getInstance().isEdt()) {
        if (rowStrings == null) {
            rowStrings = new ArrayList();
            rowStrings.add(getText());
            return;
        }
    }
    Style style = getUnselectedStyle();
    rowStrings = new ArrayList();
    widthForRowCalculations = getWidth() - style.getHorizontalPadding();
    // to allow subclasses to override it
    if (isSingleLineTextArea()) {
        rowStrings.add(getText());
        return;
    }
    if (widthForRowCalculations <= 0) {
        rowStrings.add(getText());
        setShouldCalcPreferredSize(true);
        return;
    }
    if (text == null || text.equals("")) {
        return;
    }
    Font font = style.getFont();
    if (actAsLabel && text.length() <= columns && text.indexOf('\n') < 0) {
        int w = font.stringWidth(text);
        if (w <= getWidth()) {
            if (rowStrings == null) {
                rowStrings = new ArrayList();
                rowStrings.add(getText());
                return;
            } else {
                rowStrings.clear();
                rowStrings.add(getText());
                return;
            }
        }
    }
    char[] text = preprocess(getText());
    int rows = this.rows;
    if (growByContent) {
        rows = Math.max(rows, getLines());
    }
    int charWidth = font.charWidth(widestChar);
    Style selectedStyle = getSelectedStyle();
    if (selectedStyle.getFont() != style.getFont()) {
        int cw = selectedStyle.getFont().charWidth(widestChar);
        if (cw > charWidth) {
            charWidth = cw;
            font = selectedStyle.getFont();
        }
    }
    style = getStyle();
    int tPadding = style.getHorizontalPadding();
    int textAreaWidth = getWidth() - tPadding;
    /*if(textAreaWidth <= 0) {
            if(columns < 1) {
                textAreaWidth = Math.min(Display.getInstance().getDisplayWidth() - tPadding, getText().length()) * charWidth;
            } else {
                textAreaWidth = Math.min(Display.getInstance().getDisplayWidth() - tPadding, columns) * charWidth;
            }
        }*/
    if (textAreaWidth <= charWidth) {
        if (!isInitialized()) {
            rowStrings.add(getText());
        } else {
            // special case for the edge case of "no room".
            // Its important since sometimes this case occurs in the GUI builder by accident
            int tlen = text.length;
            for (int iter = 0; iter < tlen; iter++) {
                rowStrings.add("" + text[iter]);
            }
        }
        return;
    }
    int minCharactersInRow = Math.max(1, textAreaWidth / charWidth);
    int from = 0;
    int to = from + minCharactersInRow;
    int textLength = text.length;
    String rowText = null;
    int i, spaceIndex;
    // width to accommodate it
    if (textLength / minCharactersInRow > Math.max(2, rows)) {
        textAreaWidth -= getUIManager().getLookAndFeel().getVerticalScrollWidth();
        textAreaWidth -= charWidth / 2;
    }
    String unsupported = getUnsupportedChars();
    /*
        iteration over the string using indexes, from - the beginning of the row , to - end of a row
        for each row we will try to search for a "space" character at the end of the row ( row is text area available width)
        indorder to improve the efficiency we do not search an entire row but we start from minCharactersInRow which indicates
        what is the minimum amount of characters that can feet in the text area width.
        if we dont find we will go backwards and search for the first space available,
        if there is no space in the entire row we will cut the line inorder to fit in.
         */
    // Don't rely on the fact that short text has no newline character. we always have to parse the text.
    to = Math.max(Math.min(textLength - 1, to), 0);
    while (to < textLength) {
        if (to > textLength) {
            to = textLength;
        }
        spaceIndex = -1;
        rowText = "";
        int maxLength = to;
        if (useStringWidth || actAsLabel) {
            // fix for an infinite loop issue: http://forums.java.net/jive/thread.jspa?messageID=482802
            // currentRowWidth = 0;
            String currentRow = "";
            // search for "space" character at close as possible to the end of the row
            for (i = to; i < textLength && fastCharWidthCheck(text, from, i - from + 1, textAreaWidth, charWidth, font); i++) {
                char c = text[i];
                /*if(updateRowWidth(c, font) >= textAreaWidth) {
                        break;
                    }*/
                currentRow += c;
                if (i < textLength - 1 && Character.isSurrogatePair(c, text[i + 1])) {
                    // Surrogate pairs (e.g. emojis) shouldn't be split up.
                    currentRow += text[++i];
                    maxLength += 2;
                    if (font.stringWidth(currentRow) >= textAreaWidth) {
                        break;
                    }
                    continue;
                }
                if (font.stringWidth(currentRow) >= textAreaWidth) {
                    break;
                }
                if (unsupported.indexOf(c) > -1) {
                    text[i] = ' ';
                    c = ' ';
                }
                if (c == ' ' || c == '\n') {
                    spaceIndex = i;
                    // newline has been found. We can end the loop here as the line cannot grow more
                    if (c == '\n')
                        break;
                }
                maxLength++;
            }
        } else {
            currentRowWidth = 0;
            if (to != from) {
                currentRowWidth = font.charsWidth(text, from, to - from);
            }
            // search for "space" character at close as possible to the end of the row
            for (i = to; i < textLength; i++) {
                char c = text[i];
                if (i < textLength - 1 && Character.isSurrogatePair(c, text[i + 1])) {
                    // Surrogate pairs (e.g. emojis) shouldn't be split up.
                    String testStr = new String(new char[] { text[i], text[i + 1] });
                    i++;
                    if (updateRowWidth(testStr, font) >= textAreaWidth) {
                        break;
                    }
                    maxLength += 2;
                    continue;
                }
                if (updateRowWidth(c, font) >= textAreaWidth) {
                    break;
                }
                if (unsupported.indexOf(c) > -1) {
                    text[i] = ' ';
                    c = ' ';
                }
                if (c == ' ' || c == '\n') {
                    spaceIndex = i;
                    // newline has been found. We can end the loop here as the line cannot grow more
                    if (c == '\n')
                        break;
                }
                maxLength++;
            }
        }
        // also if space is next character (in the next row) we can cut the line
        if (i == textLength || text[i] == ' ' || text[i] == '\n') {
            spaceIndex = i;
        }
        // if we found space in the limit width of the row (searched only from minCharactersInRow)
        if (spaceIndex != -1) {
            // make sure that if we have a newline character before the end of the line we should
            // break there instead
            int newLine = indexOf(text, '\n', from, spaceIndex - from);
            if (newLine > -1 && newLine < spaceIndex) {
                spaceIndex = newLine;
            }
            rowText = new String(text, from, spaceIndex - from);
            from = spaceIndex + 1;
        } else // if there is no space from minCharactersInRow to limit need to search backwards
        {
            for (i = to; spaceIndex == -1 && i >= from; i--) {
                char chr = text[i];
                if (chr == ' ' || chr == '\n' || chr == '\t') {
                    spaceIndex = i;
                    // don't forget to search for line breaks in the
                    // remaining part. otherwise we overlook possible
                    // line breaks!
                    int newLine = indexOf(text, '\n', from, i - from);
                    if (newLine > -1 && newLine < spaceIndex) {
                        spaceIndex = newLine;
                    }
                    rowText = new String(text, from, spaceIndex - from);
                    from = spaceIndex + 1;
                }
            }
            if (spaceIndex == -1) {
                // from = to + 1;
                if (maxLength <= 0) {
                    maxLength = 1;
                }
                spaceIndex = maxLength;
                if (spaceIndex > 0 && spaceIndex < textLength && Character.isSurrogatePair(text[spaceIndex - 1], text[spaceIndex])) {
                    // Make sure the space index isn't on the 2nd char of a surrogate pair (e.g. for emojis).
                    spaceIndex++;
                    maxLength++;
                }
                rowText = new String(text, from, spaceIndex - from);
                from = spaceIndex;
            }
        }
        if (rowText.length() == 0) {
            // This happens due to a race condition or something, no idea why???
            if (textAreaWidth <= charWidth) {
                if (!isInitialized()) {
                    rowStrings.add(getText());
                } else {
                    // special case for the edge case of "no room".
                    // Its important since sometimes this case occurs in the GUI builder by accident
                    int tlen = text.length;
                    for (int iter = 0; iter < tlen; iter++) {
                        rowStrings.add("" + text[iter]);
                    }
                }
                return;
            }
        }
        rowStrings.add(rowText);
        // adding minCharactersInRow doesn't work if what is left is less
        // then minCharactersInRow
        // +minCharactersInRow;
        to = from;
    }
    if (text[text.length - 1] == '\n') {
        rowStrings.add("");
    }
}
Also used : ArrayList(java.util.ArrayList) Style(com.codename1.ui.plaf.Style)

Example 38 with Char

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

the class TextField method createSymbolTable.

/**
 * Creates a symbol table container used by the showSymbolDialog method.
 * This method is designed for subclases to override and customize.
 *
 * @return container for the symbol table.
 */
protected Container createSymbolTable() {
    char[] symbolArray = getSymbolTable();
    Container symbols = new Container(new GridLayout(symbolArray.length / 5, 5));
    int slen = symbolArray.length;
    for (int iter = 0; iter < slen; iter++) {
        Button button = new Button(new Command("" + symbolArray[iter]));
        button.setUIID("VKBButton");
        button.setAlignment(CENTER);
        symbols.addComponent(button);
    }
    return symbols;
}
Also used : GridLayout(com.codename1.ui.layouts.GridLayout)

Example 39 with Char

use of com.codename1.ui.TextSelection.Char in project CodeRAD by shannah.

the class DefaultActionViewFactory method initUI.

public static void initUI(Button btn, Entity entity, ActionNode action) {
    btn.setBlockLead(true);
    boolean text = action.isTextStyle();
    boolean includeIcon = action.isIconStyle();
    UIID uiid = action.getUIID();
    if (uiid != null) {
        btn.setUIID(uiid.getValue(entity));
    }
    IconUIID iconUiid = action.getIconUIID();
    if (iconUiid != null) {
        btn.setIconUIID(iconUiid.getValue());
    }
    Button button = btn;
    if (action.getLabel() != null && text) {
        button.setText(action.getLabel().getValue(entity.getEntity()));
    }
    if (!text && button.getText().length() > 0) {
        button.setText("");
    }
    if (action.getImageIcon() != null && includeIcon) {
        button.setIcon(action.getImageIcon().getValue());
        if (action.getSelected() != action && action.getSelected().getImageIcon() != action.getImageIcon()) {
            button.setRolloverIcon(action.getSelected().getImageIcon().getValue());
        }
        if (action.getPressed() != action && action.getPressed().getImageIcon() != action.getImageIcon()) {
            button.setPressedIcon(action.getPressed().getImageIcon().getValue());
        }
        if (action.getDisabled() != action && action.getDisabled().getImageIcon() != action.getImageIcon()) {
            button.setDisabledIcon(action.getDisabled().getImageIcon().getValue());
        }
    }
    if (action.getMaterialIcon() != null && includeIcon) {
        char unselectedIcon = action.getMaterialIcon().getValue();
        char selectedIcon = action.getSelected().getMaterialIcon().getValue();
        char pressedIcon = action.getPressed().getMaterialIcon().getValue();
        char selectedPressed = pressedIcon;
        if (action.getPressed() == action) {
            selectedPressed = selectedIcon;
        }
        char disabledIcon = action.getDisabled().getMaterialIcon().getValue();
        // button.setMaterialIcon(action.getMaterialIcon().getValue());
        FontImage.setMaterialIcon(btn, new char[] { unselectedIcon, selectedIcon, pressedIcon, selectedPressed, disabledIcon }, -1);
    }
    if (action.getTextIcon() != null && includeIcon) {
        String iconText = action.getTextIcon().getValue(entity);
        Button iconLabel = new Button(iconText);
        if (btn.getIconStyleComponent() != btn) {
            iconLabel.setUIID(btn.getIconStyleComponent().getUIID());
        } else {
            iconLabel.setUIID(btn.getUIID());
            // IF the button has a border and padding set, we don't
            // want to to also be applied to the icon.
            iconLabel.getAllStyles().stripMarginAndPadding();
        }
        iconLabel.setWidth(iconLabel.getPreferredW());
        iconLabel.setHeight(iconLabel.getPreferredH());
        btn.setIcon(new ComponentImage(iconLabel, iconLabel.getWidth(), iconLabel.getHeight()));
    }
    if (includeIcon && text) {
        ActionStyle style = action.getActionStyle();
        if (style != null) {
            switch(style) {
                case IconTop:
                    button.setTextPosition(BOTTOM);
                    break;
                case IconBottom:
                    button.setTextPosition(TOP);
                    break;
                case IconLeft:
                    button.setTextPosition(RIGHT);
                    break;
                case IconRight:
                    button.setTextPosition(LEFT);
            }
        }
    }
    button.addActionListener(evt -> {
        action.fireEvent(entity, button);
        update(button, entity, action);
    });
    update(button, entity, action);
    action.decorateComponent(btn);
}
Also used : ComponentImage(ca.weblite.shared.components.ComponentImage) Button(com.codename1.ui.Button) MultiButton(com.codename1.components.MultiButton) IconUIID(com.codename1.rad.attributes.IconUIID) IconUIID(com.codename1.rad.attributes.IconUIID) BadgeUIID(com.codename1.rad.attributes.BadgeUIID) UIID(com.codename1.rad.attributes.UIID)

Example 40 with Char

use of com.codename1.ui.TextSelection.Char in project CodeRAD by shannah.

the class DefaultActionViewFactory method initUI.

public static void initUI(MultiButton btn, Entity entity, ActionNode action) {
    btn.setBlockLead(true);
    boolean text = action.isTextStyle();
    boolean includeIcon = action.isIconStyle();
    UIID uiid = action.getUIID();
    if (uiid != null) {
        btn.setUIID(uiid.getValue(entity));
    }
    IconUIID iconUiid = action.getIconUIID();
    if (iconUiid != null) {
        btn.setIconUIID(iconUiid.getValue());
    }
    MultiButton button = btn;
    if (action.getLabel() != null && text) {
        button.setTextLines(action.getLabel().getValue(entity.getEntity()));
    }
    if (action.getImageIcon() != null && includeIcon) {
        button.setIcon(action.getImageIcon().getValue());
        if (action.getSelected() != action && action.getSelected().getImageIcon() != action.getImageIcon()) {
            button.setRolloverIcon(action.getSelected().getImageIcon().getValue());
        }
        if (action.getPressed() != action && action.getPressed().getImageIcon() != action.getImageIcon()) {
            button.setPressedIcon(action.getPressed().getImageIcon().getValue());
        }
        if (action.getDisabled() != action && action.getDisabled().getImageIcon() != action.getImageIcon()) {
            button.setDisabledIcon(action.getDisabled().getImageIcon().getValue());
        }
    }
    if (action.getMaterialIcon() != null && includeIcon) {
        char unselectedIcon = action.getMaterialIcon().getValue();
        char selectedIcon = action.getSelected().getMaterialIcon().getValue();
        char pressedIcon = action.getPressed().getMaterialIcon().getValue();
        char selectedPressed = pressedIcon;
        if (action.getPressed() == action) {
            selectedPressed = selectedIcon;
        }
        char disabledIcon = action.getDisabled().getMaterialIcon().getValue();
        // button.setMaterialIcon(action.getMaterialIcon().getValue());
        FontImage.setMaterialIcon(btn, new char[] { unselectedIcon, selectedIcon, pressedIcon, selectedPressed, disabledIcon }, -1);
    }
    if (action.getTextIcon() != null && includeIcon) {
        String iconText = action.getTextIcon().getValue(entity);
        Button iconLabel = new Button(iconText);
        if (btn.getIconStyleComponent() != btn) {
            iconLabel.setUIID(btn.getIconStyleComponent().getUIID());
        } else {
            iconLabel.setUIID(btn.getUIID());
            // IF the button has a border and padding set, we don't
            // want to to also be applied to the icon.
            iconLabel.getAllStyles().stripMarginAndPadding();
        }
        iconLabel.setWidth(iconLabel.getPreferredW());
        iconLabel.setHeight(iconLabel.getPreferredH());
        btn.setIcon(new ComponentImage(iconLabel, iconLabel.getWidth(), iconLabel.getHeight()));
    }
    if (includeIcon && text) {
        ActionStyle style = action.getActionStyle();
        if (style != null) {
            switch(style) {
                case IconTop:
                    button.setTextPosition(BOTTOM);
                    break;
                case IconBottom:
                    button.setTextPosition(TOP);
                    break;
                case IconLeft:
                    button.setTextPosition(RIGHT);
                    break;
                case IconRight:
                    button.setTextPosition(LEFT);
            }
        }
    }
    if (!text) {
        button.setText("");
    }
    button.addActionListener(evt -> {
        action.fireEvent(entity, button);
        update(button, entity, action);
    });
    update(button, entity, action);
    action.decorateComponent(btn);
}
Also used : ComponentImage(ca.weblite.shared.components.ComponentImage) Button(com.codename1.ui.Button) MultiButton(com.codename1.components.MultiButton) IconUIID(com.codename1.rad.attributes.IconUIID) IconUIID(com.codename1.rad.attributes.IconUIID) BadgeUIID(com.codename1.rad.attributes.BadgeUIID) UIID(com.codename1.rad.attributes.UIID) MultiButton(com.codename1.components.MultiButton)

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