Search in sources :

Example 1 with ToastBar

use of com.codename1.components.ToastBar in project CodenameOne by codenameone.

the class ToastBar method setVisible.

/**
 * Shows or hides the {@code ToastBar}.
 * @param visible
 */
public void setVisible(boolean visible) {
    final ToastBarComponent c = getToastBarComponent();
    if (c == null || c.isVisible() == visible) {
        return;
    }
    if (visible) {
        c.hidden = true;
        c.setVisible(false);
        c.setHeight(0);
        c.setShouldCalcPreferredSize(true);
        c.getParent().revalidate();
        c.hidden = false;
        c.label.setPreferredH(UIManager.getInstance().getLookAndFeel().getTextAreaSize(c.label, true).getHeight());
        c.setShouldCalcPreferredSize(true);
        $(c).slideUpAndWait(2);
        $(c).slideDownAndWait(800);
        c.setVisible(true);
        updateStatus();
    } else {
        Form f = c.getComponentForm();
        if (Display.getInstance().getCurrent() == f && !f.getMenuBar().isMenuShowing()) {
            if (this.position == Component.BOTTOM) {
                c.setY(c.getY() + c.getHeight());
            }
            $(c).slideUpAndWait(500);
        } else {
            c.getParent().revalidate();
        }
        c.hidden = true;
        c.setVisible(false);
    }
}
Also used : Form(com.codename1.ui.Form)

Example 2 with ToastBar

use of com.codename1.components.ToastBar 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 3 with ToastBar

use of com.codename1.components.ToastBar in project CodenameOne by codenameone.

the class ToastBar method showMessage.

/**
 * Simplifies a common use case of showing a message with an icon that fades out after a few seconds
 * @param msg the message
 * @param icon the material icon to show from {@link com.codename1.ui.FontImage}
 * @param timeout the timeout value in milliseconds
 * @param listener the action to perform when the ToastBar is tapped
 */
public static void showMessage(String msg, char icon, int timeout, ActionListener listener) {
    ToastBar.Status s = ToastBar.getInstance().createStatus();
    Style stl = UIManager.getInstance().getComponentStyle(s.getMessageUIID());
    s.setIcon(FontImage.createMaterial(icon, stl, 4));
    s.setMessage(msg);
    if (listener != null) {
        s.setListener(listener);
    }
    s.setExpires(timeout);
    s.show();
}
Also used : Style(com.codename1.ui.plaf.Style)

Aggregations

FontImage (com.codename1.ui.FontImage)1 Form (com.codename1.ui.Form)1 Image (com.codename1.ui.Image)1 Label (com.codename1.ui.Label)1 TextArea (com.codename1.ui.TextArea)1 ActionEvent (com.codename1.ui.events.ActionEvent)1 ActionListener (com.codename1.ui.events.ActionListener)1 Dimension (com.codename1.ui.geom.Dimension)1 Style (com.codename1.ui.plaf.Style)1