Search in sources :

Example 31 with TextArea

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

the class HTMLForm method submit.

/**
 * Called when the a form submit is needed.
 * This querys all form fields, creates a URL accordingly and sets it to the HTMLComponent
 */
void submit(String submitKey, String submitVal) {
    if (action == null) {
        return;
    }
    // If this is turned to true anywhere, the form will not be submitted
    boolean error = false;
    String url = action;
    String params = null;
    if (comps.size() > 0) {
        params = "";
        for (Enumeration e = comps.keys(); e.hasMoreElements(); ) {
            String key = (String) e.nextElement();
            Object input = comps.get(key);
            key = HTMLUtils.encodeString(key);
            String value = "";
            if (input instanceof String) {
                // hidden
                value = HTMLUtils.encodeString((String) input);
                params += key + "=" + value + "&";
            } else if (input instanceof Hashtable) {
                // checkbox / radiobutton
                Hashtable options = (Hashtable) input;
                for (Enumeration e2 = options.keys(); e2.hasMoreElements(); ) {
                    Button b = (Button) e2.nextElement();
                    if (b.isSelected()) {
                        params += key + "=" + HTMLUtils.encodeString((String) options.get(b)) + "&";
                    }
                }
            } else if (input instanceof TextArea) {
                // catches both textareas and text input fields
                TextArea tf = ((TextArea) input);
                String text = tf.getText();
                String errorMsg = null;
                if (HTMLComponent.SUPPORT_INPUT_FORMAT) {
                    boolean ok = false;
                    if (text.equals("")) {
                        // check empty - Note that emptyok/-wap-input-required overrides input format
                        if (emptyNotOk.contains(tf)) {
                            errorMsg = htmlC.getUIManager().localize("html.format.emptynotok", "Field can't be empty");
                            error = true;
                        } else if (emptyOk.contains(tf)) {
                            ok = true;
                        }
                    }
                    if ((!error) && (!ok)) {
                        // If there's already an error or it has been cleared by the emptyOK field, no need to check
                        HTMLInputFormat inputFormat = (HTMLInputFormat) inputFormats.get(tf);
                        if ((inputFormat != null) && (!inputFormat.verifyString(text))) {
                            String emptyStr = "";
                            if (emptyOk.contains(tf)) {
                                emptyStr = htmlC.getUIManager().localize("html.format.oremptyok", " or an empty string");
                            } else if (emptyNotOk.contains(tf)) {
                                emptyStr = htmlC.getUIManager().localize("html.format.andemptynotok", " and cannot be an empty string");
                            }
                            errorMsg = htmlC.getUIManager().localize("html.format.errordesc", "Malformed text. Correct value: ") + inputFormat.toString() + emptyStr;
                            error = true;
                        }
                    }
                }
                if (htmlC.getHTMLCallback() != null) {
                    int type = HTMLCallback.FIELD_TEXT;
                    if ((tf.getConstraint() & TextArea.PASSWORD) != 0) {
                        type = HTMLCallback.FIELD_PASSWORD;
                    }
                    text = htmlC.getHTMLCallback().fieldSubmitted(htmlC, tf, url, key, text, type, errorMsg);
                }
                if (errorMsg == null) {
                    params += key + "=" + HTMLUtils.encodeString(text) + "&";
                }
            } else if (input instanceof ComboBox) {
                // drop down lists (single selection)
                Object item = ((ComboBox) input).getSelectedItem();
                if (item instanceof OptionItem) {
                    value = ((OptionItem) item).getValue();
                    params += key + "=" + HTMLUtils.encodeString(value) + "&";
                }
            // if not - value may be an OPTGROUP label in an only optgroup combobox
            } else if (input instanceof MultiComboBox) {
                // drop down lists (multiple selection)
                Vector selected = ((MultiComboBox) input).getSelected();
                for (int i = 0; i < selected.size(); i++) {
                    Object item = selected.elementAt(i);
                    if (item instanceof OptionItem) {
                        value = ((OptionItem) item).getValue();
                        params += key + "=" + HTMLUtils.encodeString(value) + "&";
                    }
                // if not - value may be an OPTGROUP label in an only optgroup combobox
                }
            }
        }
        if (params.endsWith("&")) {
            // trim the extra &
            params = params.substring(0, params.length() - 1);
        }
    }
    // Add the submit button param, only if the key is non-null (unnamed submit buttons are not passed as parameters)
    if (submitKey != null) {
        if (params == null) {
            params = "";
        }
        if (!params.equals("")) {
            params = params + "&";
        }
        params = params + HTMLUtils.encodeString(submitKey) + "=" + HTMLUtils.encodeString(submitVal);
    }
    if (!error) {
        DocumentInfo docInfo = new DocumentInfo(url, params, isPostMethod);
        if ((encType != null) && (!encType.equals(""))) {
            docInfo.setEncoding(encType);
        }
        htmlC.setPage(docInfo);
    }
}
Also used : Enumeration(java.util.Enumeration) TextArea(com.codename1.ui.TextArea) Hashtable(java.util.Hashtable) ComboBox(com.codename1.ui.ComboBox) RadioButton(com.codename1.ui.RadioButton) Button(com.codename1.ui.Button) Vector(java.util.Vector)

Example 32 with TextArea

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

the class HTMLInputFormat method applyConstraints.

/**
 * Applies the constrains represented by this object to the given TextArea.
 * After invoking this method the returned TextArea should be used as restrictions are made sometimes on a new object.
 * In case this is a TextField, this method will also set the input modes as needed.
 *
 * @param ta The TextArea to apply the constraints on.
 * @return An instance of TextArea (Either the given one or a new one) with the constraints.
 */
TextArea applyConstraints(TextArea ta) {
    int widestConstraint = 0;
    for (Enumeration e = formatConstraints.elements(); e.hasMoreElements(); ) {
        FormatConstraint constraint = (FormatConstraint) e.nextElement();
        for (int i = 1; i <= 16; i *= 2) {
            if ((constraint.type & i) != 0) {
                widestConstraint |= i;
            }
        }
    }
    if (maxLength != Integer.MAX_VALUE) {
        ta.setMaxSize(maxLength);
    }
    if (widestConstraint == FormatConstraint.TYPE_NUMERIC) {
        ta.setConstraint(ta.getConstraint() | TextArea.NUMERIC);
    }
    if (ta instanceof TextField) {
        TextField tf = (TextField) ta;
        if (((widestConstraint & FormatConstraint.TYPE_SYMBOL) == 0) && ((widestConstraint & FormatConstraint.TYPE_ANY) == 0)) {
            // No symbols allowed
            tf = new TextField(ta.getText()) {

                protected void showSymbolDialog() {
                // Block symbols dialog
                }
            };
            tf.setConstraint(ta.getConstraint());
            ta = tf;
        }
        if ((widestConstraint & FormatConstraint.TYPE_ANY) != 0) {
            if ((widestConstraint & FormatConstraint.TYPE_UPPERCASE) != 0) {
                tf.setInputMode("ABC");
            } else {
                tf.setInputMode("abc");
            }
        } else {
            if ((widestConstraint & FormatConstraint.TYPE_LOWERCASE) == 0) {
                excludeInputMode(tf, "abc");
                excludeInputMode(tf, "Abc");
            }
            if ((widestConstraint & FormatConstraint.TYPE_UPPERCASE) == 0) {
                excludeInputMode(tf, "ABC");
                excludeInputMode(tf, "Abc");
            }
            if ((widestConstraint & FormatConstraint.TYPE_NUMERIC) == 0) {
                excludeInputMode(tf, "123");
            }
        }
    }
    return ta;
}
Also used : Enumeration(java.util.Enumeration) TextField(com.codename1.ui.TextField)

Example 33 with TextArea

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

the class ClearableTextField method wrap.

/**
 * Wraps the given text field with a UI that will allow us to clear it
 * @param tf the text field
 * @param iconSize size in millimeters for the clear icon, -1 for default size
 * @return a Container that should be added to the UI instead of the actual text field
 */
public static ClearableTextField wrap(final TextArea tf, float iconSize) {
    ClearableTextField cf = new ClearableTextField();
    Button b = new Button("", tf.getUIID());
    if (iconSize > 0) {
        FontImage.setMaterialIcon(b, FontImage.MATERIAL_CLEAR, iconSize);
    } else {
        FontImage.setMaterialIcon(b, FontImage.MATERIAL_CLEAR);
    }
    removeCmpBackground(tf);
    removeCmpBackground(b);
    cf.setUIID(tf.getUIID());
    cf.add(BorderLayout.CENTER, tf);
    cf.add(BorderLayout.EAST, b);
    b.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            tf.stopEditing();
            tf.setText("");
            tf.startEditingAsync();
        }
    });
    return cf;
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) Button(com.codename1.ui.Button) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 34 with TextArea

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

the class ToastBar method updateStatus.

/**
 * Updates the ToastBar UI component with the settings of the current status.
 */
private void updateStatus() {
    final ToastBarComponent c = getToastBarComponent();
    if (c != null) {
        try {
            if (updatingStatus) {
                pendingUpdateStatus = true;
                return;
            }
            updatingStatus = true;
            if (c.currentlyShowing != null && !statuses.contains(c.currentlyShowing)) {
                c.currentlyShowing = null;
            }
            if (c.currentlyShowing == null || statuses.isEmpty()) {
                if (!statuses.isEmpty()) {
                    c.currentlyShowing = statuses.get(statuses.size() - 1);
                } else {
                    setVisible(false);
                    return;
                }
            }
            Status s = c.currentlyShowing;
            Label l = new Label(s.getMessage() != null ? s.getMessage() : "");
            c.leadButton.getListeners().clear();
            c.leadButton.addActionListener(s.getListener());
            c.leadButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    if (c.currentlyShowing != null && !c.currentlyShowing.showProgressIndicator) {
                        c.currentlyShowing.clear();
                    }
                    ToastBar.this.setVisible(false);
                }
            });
            c.progressLabel.setVisible(s.isShowProgressIndicator());
            if (c.progressLabel.isVisible()) {
                if (!c.contains(c.progressLabel)) {
                    c.addComponent(BorderLayout.EAST, c.progressLabel);
                }
                Image anim = c.progressLabel.getAnimation();
                if (anim != null && anim.getWidth() > 0) {
                    c.progressLabel.setWidth(anim.getWidth());
                }
                if (anim != null && anim.getHeight() > 0) {
                    c.progressLabel.setHeight(anim.getHeight());
                }
            } else {
                if (c.contains(c.progressLabel)) {
                    c.removeComponent(c.progressLabel);
                }
            }
            c.progressBar.setVisible(s.getProgress() >= -1);
            if (s.getProgress() >= -1) {
                if (!c.contains(c.progressBar)) {
                    c.addComponent(BorderLayout.SOUTH, c.progressBar);
                }
                if (s.getProgress() < 0) {
                    c.progressBar.setInfinite(true);
                } else {
                    c.progressBar.setInfinite(false);
                    c.progressBar.setProgress(s.getProgress());
                }
            } else {
                c.removeComponent(c.progressBar);
            }
            c.icon.setVisible(s.getIcon() != null);
            if (s.getIcon() != null && c.icon.getIcon() != s.getIcon()) {
                c.icon.setIcon(s.getIcon());
            }
            if (s.getIcon() == null && c.contains(c.icon)) {
                c.removeComponent(c.icon);
            } else if (s.getIcon() != null && !c.contains(c.icon)) {
                c.addComponent(BorderLayout.WEST, c.icon);
            }
            String oldText = c.label.getText();
            if (!oldText.equals(l.getText())) {
                if (s.getUiid() != null) {
                    c.setUIID(s.getUiid());
                } else if (defaultUIID != null) {
                    c.setUIID(defaultUIID);
                }
                if (c.isVisible()) {
                    TextArea newLabel = new TextArea();
                    // newLabel.setColumns(l.getText().length()+1);
                    // newLabel.setRows(l.getText().length()+1);
                    newLabel.setFocusable(false);
                    newLabel.setEditable(false);
                    // newLabel.getAllStyles().setFgColor(0xffffff);
                    if (s.getMessageUIID() != null) {
                        newLabel.setUIID(s.getMessageUIID());
                    } else if (defaultMessageUIID != null) {
                        newLabel.setUIID(defaultMessageUIID);
                    } else {
                        newLabel.setUIID(c.label.getUIID());
                    }
                    if (s.getUiid() != null) {
                        c.setUIID(s.getUiid());
                    } else if (defaultUIID != null) {
                        c.setUIID(defaultUIID);
                    }
                    newLabel.setWidth(c.label.getWidth());
                    newLabel.setText(l.getText());
                    Dimension oldTextAreaSize = UIManager.getInstance().getLookAndFeel().getTextAreaSize(c.label, true);
                    Dimension newTexAreaSize = UIManager.getInstance().getLookAndFeel().getTextAreaSize(newLabel, true);
                    // https://stackoverflow.com/questions/46172993/codename-one-toastbar-nullpointerexception
                    if (c.label.getParent() != null) {
                        c.label.getParent().replaceAndWait(c.label, newLabel, CommonTransitions.createCover(CommonTransitions.SLIDE_VERTICAL, true, 300));
                        c.label = newLabel;
                        if (oldTextAreaSize.getHeight() != newTexAreaSize.getHeight()) {
                            c.label.setPreferredH(newTexAreaSize.getHeight());
                            c.getParent().animateHierarchyAndWait(300);
                        }
                    }
                } else {
                    if (s.getMessageUIID() != null) {
                        c.label.setUIID(s.getMessageUIID());
                    } else if (defaultMessageUIID != null) {
                        c.label.setUIID(defaultMessageUIID);
                    }
                    if (s.getUiid() != null) {
                        c.setUIID(s.getUiid());
                    } else if (defaultUIID != null) {
                        c.setUIID(defaultUIID);
                    }
                    c.label.setText(l.getText());
                    // c.label.setColumns(l.getText().length()+1);
                    // c.label.setRows(l.getText().length()+1);
                    c.label.setPreferredW(c.getWidth());
                    c.revalidate();
                }
            } else {
                c.revalidate();
            }
        } finally {
            updatingStatus = false;
            if (pendingUpdateStatus) {
                pendingUpdateStatus = false;
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        updateStatus();
                    }
                });
            }
        }
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) TextArea(com.codename1.ui.TextArea) ActionEvent(com.codename1.ui.events.ActionEvent) Label(com.codename1.ui.Label) Dimension(com.codename1.ui.geom.Dimension) FontImage(com.codename1.ui.FontImage) Image(com.codename1.ui.Image)

Example 35 with TextArea

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

the class DefaultLookAndFeel method getTextFieldCursorX.

/**
 * Calculates the position of the text field cursor within the string
 */
private int getTextFieldCursorX(TextArea ta) {
    Style style = ta.getStyle();
    Font f = style.getFont();
    // display ******** if it is a password field
    String displayText = getTextFieldString(ta);
    String inputMode = ta.getInputMode();
    int inputModeWidth = f.stringWidth(inputMode);
    // QWERTY devices don't quite have an input mode hide it also when we have a VK
    if (ta.isQwertyInput() || Display.getInstance().isVirtualKeyboardShowing()) {
        inputMode = "";
        inputModeWidth = 0;
    }
    int xPos = 0;
    int cursorCharPosition = ta.getCursorX();
    int cursorX = 0;
    int x = 0;
    if (reverseAlignForBidi(ta) == Component.RIGHT) {
        if (Display.getInstance().isBidiAlgorithm()) {
            // char[] dest = displayText.toCharArray();
            cursorCharPosition = Display.getInstance().getCharLocation(displayText, cursorCharPosition - 1);
            if (cursorCharPosition == -1) {
                xPos = f.stringWidth(displayText);
            } else {
                displayText = Display.getInstance().convertBidiLogicalToVisual(displayText);
                if (!isRTLOrWhitespace((displayText.charAt(cursorCharPosition)))) {
                    cursorCharPosition++;
                }
                xPos = f.stringWidth(displayText.substring(0, cursorCharPosition));
            }
        }
        int displayX = ta.getX() + ta.getWidth() - style.getPaddingLeft(ta.isRTL()) - f.stringWidth(displayText);
        cursorX = displayX + xPos;
        x = 0;
    } else {
        if (cursorCharPosition > 0) {
            cursorCharPosition = Math.min(displayText.length(), cursorCharPosition);
            xPos = f.stringWidth(displayText.substring(0, cursorCharPosition));
        }
        cursorX = ta.getX() + style.getPaddingLeft(ta.isRTL()) + xPos;
        if (ta.isSingleLineTextArea() && ta.getWidth() > (f.getHeight() * 2) && cursorX >= ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL())) {
            if (x + xPos >= ta.getWidth() - inputModeWidth - style.getPaddingLeftNoRTL() - style.getPaddingRightNoRTL()) {
                x = ta.getWidth() - inputModeWidth - style.getPaddingLeftNoRTL() - style.getPaddingRightNoRTL() - xPos - 1;
            }
        }
    }
    return cursorX + x;
}
Also used : Font(com.codename1.ui.Font)

Aggregations

TextArea (com.codename1.ui.TextArea)46 Component (com.codename1.ui.Component)22 Label (com.codename1.ui.Label)11 Button (com.codename1.ui.Button)10 Container (com.codename1.ui.Container)10 Form (com.codename1.ui.Form)10 RadioButton (com.codename1.ui.RadioButton)8 PeerComponent (com.codename1.ui.PeerComponent)6 TextField (com.codename1.ui.TextField)6 BorderLayout (com.codename1.ui.layouts.BorderLayout)6 Font (com.codename1.ui.Font)5 BoxLayout (com.codename1.ui.layouts.BoxLayout)5 CheckBox (com.codename1.ui.CheckBox)4 Dialog (com.codename1.ui.Dialog)4 List (com.codename1.ui.List)4 Hashtable (java.util.Hashtable)4 Paint (android.graphics.Paint)3 Command (com.codename1.ui.Command)3 Slider (com.codename1.ui.Slider)3 ActionEvent (com.codename1.ui.events.ActionEvent)3