Search in sources :

Example 16 with Ui

use of com.codename1.ui.util.xml.Ui in project CodenameOne by codenameone.

the class CSSEngine method handleContentProperty.

/**
 * Handles a CSS content property
 *
 * @param element The element this content property
 * @param selector The CSS selector that includes the content property
 * @param htmlC The HTMLComponent
 */
private void handleContentProperty(HTMLElement element, CSSElement selector, HTMLComponent htmlC) {
    boolean after = ((selector.getSelectorPseudoClass() & CSSElement.PC_AFTER) != 0);
    String content = selector.getAttributeById(CSSElement.CSS_CONTENT);
    if (content != null) {
        // if there's no content, we don't add anything
        Component cmp = after ? (Component) element.getUi().lastElement() : (Component) element.getUi().firstElement();
        Component styleCmp = cmp;
        Container parent = null;
        int pos = 0;
        if (cmp instanceof Container) {
            parent = ((Container) cmp);
            while ((parent.getComponentCount() > 0) && (parent.getComponentAt(after ? parent.getComponentCount() - 1 : 0) instanceof Container)) {
                // find the actual content
                parent = (Container) parent.getComponentAt(after ? parent.getComponentCount() - 1 : 0);
            }
            if (parent.getComponentCount() > 0) {
                pos = after ? parent.getComponentCount() - 1 : 0;
                styleCmp = parent.getComponentAt(pos);
            }
        } else {
            parent = cmp.getParent();
            pos = cmp.getParent().getComponentIndex(cmp);
        }
        if (after) {
            pos++;
        }
        int initPos = pos;
        String str = "";
        // to make sure the last expression is evaluated, note that this will not print an extra space in any case, since it is out of the quotes if any
        content = content + " ";
        boolean segment = false;
        for (int i = 0; i < content.length(); i++) {
            char c = content.charAt(i);
            Label lbl = null;
            if (c == '"') {
                segment = !segment;
                if ((!segment) && (str.length() > 0)) {
                    lbl = new Label(str);
                    str = "";
                }
            } else if (CSSParser.isWhiteSpace(c)) {
                if (segment) {
                    str += c;
                    lbl = new Label(str);
                } else if (str.length() > 0) {
                    lbl = evalContentExpression(htmlC, str, element, selector);
                    if (lbl == null) {
                        // if we didn't find a match we search for the following expressions which are used to remove added content
                        int removeQuoteType = -1;
                        boolean removeAll = false;
                        if ((str.equals("none")) || (str.equals("normal"))) {
                            // normal/none means remove all content
                            removeAll = true;
                        } else if (str.equals("no-open-quote")) {
                            // 0 is the quote type for open quote, 1 for closed one
                            removeQuoteType = 0;
                        } else if (str.equals("no-close-quote")) {
                            removeQuoteType = 1;
                        }
                        if ((removeAll) || (removeQuoteType != -1)) {
                            Vector v = element.getUi();
                            if (v != null) {
                                Vector toRemove = new Vector();
                                for (Enumeration e = v.elements(); e.hasMoreElements(); ) {
                                    Component ui = (Component) e.nextElement();
                                    String conStr = (String) ui.getClientProperty(CLIENT_PROPERTY_CSS_CONTENT);
                                    if ((conStr != null) && (((after) && (conStr.equals("a"))) || ((!after) && (conStr.equals("b"))))) {
                                        boolean remove = true;
                                        if (removeQuoteType != -1) {
                                            Object obj = ui.getClientProperty(HTMLComponent.CLIENT_PROPERTY_QUOTE);
                                            if (obj != null) {
                                                int quoteType = ((Integer) obj).intValue();
                                                remove = (quoteType == removeQuoteType);
                                            } else {
                                                remove = false;
                                            }
                                        }
                                        if (remove) {
                                            parent.removeComponent(ui);
                                            toRemove.addElement(ui);
                                        }
                                    }
                                }
                                for (Enumeration e = toRemove.elements(); e.hasMoreElements(); ) {
                                    v.removeElement(e.nextElement());
                                }
                            }
                            // stop processing after removal clauses such as none/normal
                            return;
                        }
                    }
                }
                str = "";
            } else {
                str += c;
            }
            if (lbl != null) {
                if (after) {
                    element.addAssociatedComponent(lbl);
                } else {
                    element.addAssociatedComponentAt(pos - initPos, lbl);
                }
                lbl.setUnselectedStyle(new Style(styleCmp.getUnselectedStyle()));
                lbl.putClientProperty(CLIENT_PROPERTY_CSS_CONTENT, after ? "a" : "b");
                if (parent.getComponentCount() == 0) {
                    parent.addComponent(lbl);
                } else {
                    parent.addComponent(pos, lbl);
                }
                pos++;
                applyStyleToUIElement(lbl, selector, element, htmlC);
            }
        }
    }
}
Also used : Container(com.codename1.ui.Container) Enumeration(java.util.Enumeration) Label(com.codename1.ui.Label) Style(com.codename1.ui.plaf.Style) Component(com.codename1.ui.Component) Vector(java.util.Vector)

Example 17 with Ui

use of com.codename1.ui.util.xml.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 18 with Ui

use of com.codename1.ui.util.xml.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 19 with Ui

use of com.codename1.ui.util.xml.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 20 with Ui

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

Component (com.codename1.ui.Component)8 ArrayList (java.util.ArrayList)7 Container (com.codename1.ui.Container)6 Label (com.codename1.ui.Label)6 TextArea (com.codename1.ui.TextArea)6 Vector (java.util.Vector)6 IOException (java.io.IOException)5 Hashtable (java.util.Hashtable)5 EncodedImage (com.codename1.ui.EncodedImage)4 Border (com.codename1.ui.plaf.Border)4 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)4 EditorTTFFont (com.codename1.ui.EditorTTFFont)3 Form (com.codename1.ui.Form)3 TextField (com.codename1.ui.TextField)3 AnimationObject (com.codename1.ui.animations.AnimationObject)3 EditableResources (com.codename1.ui.util.EditableResources)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3