Search in sources :

Example 76 with com.codename1.ui

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

the class TextArea method keyPressed.

/**
 * {@inheritDoc}
 */
public void keyPressed(int keyCode) {
    super.keyPressed(keyCode);
    int action = com.codename1.ui.Display.getInstance().getGameAction(keyCode);
    // this works around a bug where fire is also a softkey on devices such as newer Nokia
    // series 40's (e.g. the Nokia emulator). It closes its native text box on fire then
    // as a result of a Nokia bug we get the key released of that closing and assume the
    // users wants to edit the text... When means the only way to exit the native text box
    // is via the cancel option (after pressing OK once).
    triggerClose = action == Display.GAME_FIRE;
    // scroll the TextArea
    Rectangle rect = new Rectangle(getScrollX(), getScrollY(), getWidth(), getHeight());
    Font textFont = getStyle().getFont();
    if (action == Display.GAME_DOWN) {
        if ((getScrollY() + getHeight()) < (rowsGap + getStyle().getFont().getHeight()) * getLines()) {
            rect.setY(rect.getY() + (textFont.getHeight() + rowsGap) * linesToScroll);
            scrollRectToVisible(rect, this);
        } else {
            setHandlesInput(false);
        }
    } else {
        if (action == Display.GAME_UP) {
            if (getScrollY() > 0) {
                rect.setY(Math.max(0, rect.getY() - (textFont.getHeight() + rowsGap) * linesToScroll));
                scrollRectToVisible(rect, this);
            } else {
                setHandlesInput(false);
            }
        }
    }
    if (action == Display.GAME_RIGHT || action == Display.GAME_LEFT) {
        setHandlesInput(false);
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 77 with com.codename1.ui

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

the class CloudObject method bindTree.

/**
 * Binds a UI tree to the cloud object so its values automatically update in the cloud object
 *
 * @param ui the component tree to bind
 * @param defer bind settings whether to defer the binding which requires developers to explicitly commit
 * the binding to perform the changes
 * @param objectLead if set to true the UI property is initialized from values in the CloudObject, if false
 * the cloud object property is initialized from the UI
 */
public void bindTree(Container ui, int defer, boolean objectLead) {
    int componentCount = ui.getComponentCount();
    for (int iter = 0; iter < componentCount; iter++) {
        Component c = ui.getComponentAt(iter);
        if (c instanceof Container) {
            bindTree((Container) c, defer, objectLead);
            continue;
        }
        String bind = c.getCloudBoundProperty();
        if (bind != null && bind.length() > 0) {
            String attributeName = c.getCloudDestinationProperty();
            if (attributeName != null) {
                bindProperty(c, bind, attributeName, defer, objectLead);
            }
        }
    }
}
Also used : Container(com.codename1.ui.Container) Component(com.codename1.ui.Component)

Example 78 with com.codename1.ui

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

the class CloudObject method bindProperty.

/**
 * Binds a property value within the given component to this cloud object, this means that
 * when the component changes the cloud object changes unless deferred. If the defer flag is
 * false all changes are stored in a temporary location and only "committed" once commitBindings()
 * is invoked.
 * @param cmp the component to bind
 * @param propertyName the name of the property in the bound component
 * @param attributeName the key within the cloud object
 * @param defer bind settings whether to defer the binding which requires developers to explicitly commit
 * the binding to perform the changes
 * @param objectLead if set to true the UI property is initialized from values in the CloudObject, if false
 * the cloud object property is initialized from the UI
 */
public void bindProperty(Component cmp, final String propertyName, final String attributeName, final int defer, boolean objectLead) {
    if (objectLead) {
        Object val = values.get(attributeName);
        Object cmpVal = cmp.getBoundPropertyValue(propertyName);
        if (val == null) {
            if (cmpVal != null) {
                cmp.setBoundPropertyValue(propertyName, null);
            }
        } else {
            if (cmpVal == null || !(val.equals(cmpVal))) {
                cmp.setBoundPropertyValue(propertyName, val);
            }
        }
    } else {
        Object val = values.get(attributeName);
        Object cmpVal = cmp.getBoundPropertyValue(propertyName);
        if (cmpVal == null) {
            if (val != null) {
                values.remove(attributeName);
                status = STATUS_MODIFIED;
            }
        } else {
            if (val == null || !(val.equals(cmpVal))) {
                values.put(attributeName, cmpVal);
                status = STATUS_MODIFIED;
            }
        }
    }
    BindTarget target = new BindTarget() {

        public void propertyChanged(Component source, String propertyName, Object oldValue, Object newValue) {
            switch(defer) {
                case BINDING_DEFERRED:
                    if (deferedValues == null) {
                        deferedValues = new Hashtable();
                    }
                    deferedValues.put(attributeName, newValue);
                    break;
                case BINDING_IMMEDIATE:
                    values.put(attributeName, newValue);
                    status = STATUS_MODIFIED;
                    break;
                case BINDING_AUTO_SAVE:
                    values.put(attributeName, newValue);
                    status = STATUS_MODIFIED;
                    CloudStorage.getInstance().save(CloudObject.this);
                    break;
            }
        }
    };
    cmp.bindProperty(propertyName, target);
    cmp.putClientProperty("CN1Bind" + propertyName, target);
}
Also used : Hashtable(java.util.Hashtable) BindTarget(com.codename1.cloud.BindTarget) Component(com.codename1.ui.Component)

Example 79 with com.codename1.ui

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

use of com.codename1.ui 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)

Aggregations

EncodedImage (com.codename1.ui.EncodedImage)26 Component (com.codename1.ui.Component)23 Point (java.awt.Point)23 IOException (java.io.IOException)23 AnimationObject (com.codename1.ui.animations.AnimationObject)22 ArrayList (java.util.ArrayList)22 BufferedImage (java.awt.image.BufferedImage)19 Hashtable (java.util.Hashtable)18 Form (com.codename1.ui.Form)15 Timeline (com.codename1.ui.animations.Timeline)15 Image (com.codename1.ui.Image)13 EditableResources (com.codename1.ui.util.EditableResources)13 File (java.io.File)13 Vector (java.util.Vector)13 TextArea (com.codename1.ui.TextArea)12 Border (com.codename1.ui.plaf.Border)12 Label (com.codename1.ui.Label)10 BorderLayout (com.codename1.ui.layouts.BorderLayout)10 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)10 Container (com.codename1.ui.Container)9