Search in sources :

Example 1 with PickerComponent

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

the class StringPickerSample method testStringPicker.

private void testStringPicker() {
    Form hi = new Form("Hi World", BoxLayout.y());
    Picker languagePicker = new Picker();
    languagePicker.setName("LanguagePicker");
    languagePicker.setType(Display.PICKER_TYPE_STRINGS);
    languagePicker.setStrings("Italian", "English", "German");
    languagePicker.setSelectedString("English");
    languagePicker.addActionListener((ev) -> {
        Log.p("Action Event fired");
        String newLanguage = languagePicker.getSelectedString();
        if (newLanguage != null && newLanguage.length() > 0) {
            Log.p("Language selected: " + newLanguage);
        }
    });
    Picker datePicker = new Picker();
    datePicker.setType(Display.PICKER_TYPE_DATE);
    Picker timePicker = new Picker();
    timePicker.setType(Display.PICKER_TYPE_TIME);
    Picker dateTimePicker = new Picker();
    dateTimePicker.setType(Display.PICKER_TYPE_DATE_AND_TIME);
    Picker durationPicker = new Picker();
    durationPicker.setType(Display.PICKER_TYPE_DURATION);
    Picker durationHoursPicker = new Picker();
    durationHoursPicker.setType(Display.PICKER_TYPE_DURATION_HOURS);
    Picker durationMinutesPicker = new Picker();
    durationMinutesPicker.setType(Display.PICKER_TYPE_DURATION_MINUTES);
    Picker calendarPicker = new Picker();
    calendarPicker.setType(Display.PICKER_TYPE_CALENDAR);
    CheckBox lightweight = new CheckBox("LightWeight");
    lightweight.setSelected(languagePicker.isUseLightweightPopup());
    lightweight.addActionListener(e -> {
        languagePicker.setUseLightweightPopup(lightweight.isSelected());
        datePicker.setUseLightweightPopup(lightweight.isSelected());
        timePicker.setUseLightweightPopup(lightweight.isSelected());
        dateTimePicker.setUseLightweightPopup(lightweight.isSelected());
        durationPicker.setUseLightweightPopup(lightweight.isSelected());
        durationHoursPicker.setUseLightweightPopup(lightweight.isSelected());
        durationMinutesPicker.setUseLightweightPopup(lightweight.isSelected());
        calendarPicker.setUseLightweightPopup(lightweight.isSelected());
    });
    TextField tf = new TextField();
    createFocusChain(tf, languagePicker, datePicker, timePicker, lightweight, dateTimePicker, durationPicker, durationHoursPicker, durationMinutesPicker, calendarPicker);
    hi.add(tf);
    hi.add(languagePicker);
    hi.add(datePicker);
    hi.add(timePicker);
    hi.add(lightweight);
    hi.add(dateTimePicker);
    hi.add(durationPicker);
    hi.add(durationHoursPicker);
    hi.add(durationMinutesPicker);
    hi.add(calendarPicker);
    PickerComponent gender = PickerComponent.createStrings("Male", "Female", "Other", "Don't ask me").label("Gender");
    gender.getPicker().setUseLightweightPopup(true);
    PickerComponent date = PickerComponent.createDate(new Date()).label("Birthday");
    date.getPicker().setUseLightweightPopup(true);
    hi.add(gender).add(date);
    hi.add(new SpanLabel("Date Time between July 7 and July 12 2019"));
    Picker rangePicker1 = new Picker();
    rangePicker1.setType(Display.PICKER_TYPE_DATE_AND_TIME);
    System.out.println("Start date " + newDate(119, 6, 7));
    rangePicker1.setStartDate(newDate(119, 6, 7));
    rangePicker1.setEndDate(newDate(119, 6, 12));
    hi.add(rangePicker1);
    hi.add(new SpanLabel("Date Time between July 7 and July 12 2019 between 8am and 11am"));
    rangePicker1 = new Picker();
    rangePicker1.setType(Display.PICKER_TYPE_DATE_AND_TIME);
    System.out.println("Start date " + newDate(119, 6, 7));
    rangePicker1.setStartDate(newDate(119, 6, 7));
    rangePicker1.setEndDate(newDate(119, 6, 12));
    rangePicker1.setHourRange(8, 11);
    hi.add(rangePicker1);
    hi.add(new SpanLabel("Date Time between July 7 and July 12 2019 between 8am and 11am, with current time at 9am"));
    rangePicker1 = new Picker();
    rangePicker1.setType(Display.PICKER_TYPE_DATE_AND_TIME);
    System.out.println("Start date " + newDate(119, 6, 7));
    rangePicker1.setStartDate(newDate(119, 6, 7));
    rangePicker1.setEndDate(newDate(119, 6, 12));
    rangePicker1.setHourRange(8, 11);
    rangePicker1.setDate(newDate(119, 6, 8, 9));
    hi.add(rangePicker1);
    hi.add(new SpanLabel("Date Time with current time at 9am"));
    rangePicker1 = new Picker();
    rangePicker1.setType(Display.PICKER_TYPE_DATE_AND_TIME);
    rangePicker1.setDate(newDate(118, 6, 8, 9));
    hi.add(rangePicker1);
    hi.show();
}
Also used : Form(com.codename1.ui.Form) CheckBox(com.codename1.ui.CheckBox) Picker(com.codename1.ui.spinner.Picker) TextField(com.codename1.ui.TextField) PickerComponent(com.codename1.ui.PickerComponent) SpanLabel(com.codename1.components.SpanLabel) Date(java.util.Date)

Example 2 with PickerComponent

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

the class Validator method addConstraint.

/**
 * Places a constraint on the validator, returns this object so constraint
 * additions can be chained. Shows validation errors messages even when the
 * TextModeLayout is not {@code onTopMode} (it's possible to disable this
 * functionality setting to false the theme constant
 * {@code showValidationErrorsIfNotOnTopMode}: basically, the error
 * message is shown for two second in place of the label on the left of the
 * InputComponent (or on right of the InputComponent for RTL languages);
 * this solution never breaks the layout, because the error message is
 * trimmed to fit the available space. The error message UIID is
 * "ErrorLabel" when it's not onTopMode.
 *
 * @param cmp the component to validate
 * @param c the constraint or constraints
 * @return this object so we can write code like v.addConstraint(cmp1,
 * cons).addConstraint(cmp2, otherConstraint);
 */
public Validator addConstraint(Component cmp, Constraint... c) {
    Constraint constraint = null;
    if (c.length == 1) {
        constraint = c[0];
        constraintList.put(cmp, constraint);
    } else if (c.length > 1) {
        constraint = new GroupConstraint(c);
        constraintList.put(cmp, constraint);
    }
    if (constraint == null) {
        throw new IllegalArgumentException("addConstraint needs at least a Constraint, but the Constraint array in empty");
    }
    bindDataListener(cmp);
    boolean isV = isValid();
    for (Component btn : submitButtons) {
        btn.setEnabled(isV);
    }
    // Show validation error on iPhone
    if (UIManager.getInstance().isThemeConstant("showValidationErrorsIfNotOnTopMode", true) && cmp instanceof InputComponent) {
        final InputComponent inputComponent = (InputComponent) cmp;
        if (!inputComponent.isOnTopMode()) {
            Label labelForComponent = null;
            if (inputComponent instanceof TextComponent) {
                labelForComponent = ((TextComponent) inputComponent).getField().getLabelForComponent();
            } else if (inputComponent instanceof PickerComponent) {
                labelForComponent = ((PickerComponent) inputComponent).getPicker().getLabelForComponent();
            }
            if (labelForComponent != null) {
                final Label myLabel = labelForComponent;
                final String originalText = myLabel.getText();
                final String originalUIID = myLabel.getUIID();
                final Constraint myConstraint = constraint;
                final Runnable showError = new Runnable() {

                    @Override
                    public void run() {
                        boolean isValid = false;
                        if (inputComponent instanceof TextComponent) {
                            isValid = myConstraint.isValid(((TextComponent) inputComponent).getField().getText());
                        } else if (inputComponent instanceof PickerComponent) {
                            isValid = myConstraint.isValid(((PickerComponent) inputComponent).getPicker().getValue());
                        }
                        String errorMessage = trimLongString(UIManager.getInstance().localize(myConstraint.getDefaultFailMessage(), myConstraint.getDefaultFailMessage()), "ErrorLabel", myLabel.getWidth());
                        if (errorMessage != null && errorMessage.length() > 0 && !isValid) {
                            // show the error in place of the label for component
                            myLabel.setUIID("ErrorLabel");
                            myLabel.setText(errorMessage);
                            UITimer.timer(2000, false, Display.getInstance().getCurrent(), new Runnable() {

                                @Override
                                public void run() {
                                    myLabel.setUIID(originalUIID);
                                    myLabel.setText(originalText);
                                }
                            });
                        } else {
                            // show the label for component without the error
                            myLabel.setUIID(originalUIID);
                            myLabel.setText(originalText);
                        }
                    }
                };
                FocusListener myFocusListener = new FocusListener() {

                    @Override
                    public void focusLost(Component cmp) {
                        showError.run();
                    }

                    @Override
                    public void focusGained(Component cmp) {
                    // no code here
                    }
                };
                if (inputComponent instanceof TextComponent) {
                    ((TextComponent) inputComponent).getField().addFocusListener(myFocusListener);
                } else if (inputComponent instanceof PickerComponent) {
                    ((PickerComponent) inputComponent).getPicker().addFocusListener(myFocusListener);
                }
            }
        }
    }
    return this;
}
Also used : TextComponent(com.codename1.ui.TextComponent) InputComponent(com.codename1.ui.InputComponent) Label(com.codename1.ui.Label) PickerComponent(com.codename1.ui.PickerComponent) PickerComponent(com.codename1.ui.PickerComponent) TextComponent(com.codename1.ui.TextComponent) Component(com.codename1.ui.Component) InputComponent(com.codename1.ui.InputComponent) FocusListener(com.codename1.ui.events.FocusListener)

Aggregations

PickerComponent (com.codename1.ui.PickerComponent)2 SpanLabel (com.codename1.components.SpanLabel)1 CheckBox (com.codename1.ui.CheckBox)1 Component (com.codename1.ui.Component)1 Form (com.codename1.ui.Form)1 InputComponent (com.codename1.ui.InputComponent)1 Label (com.codename1.ui.Label)1 TextComponent (com.codename1.ui.TextComponent)1 TextField (com.codename1.ui.TextField)1 FocusListener (com.codename1.ui.events.FocusListener)1 Picker (com.codename1.ui.spinner.Picker)1 Date (java.util.Date)1