Search in sources :

Example 1 with CloudObject

use of com.codename1.cloud.CloudObject in project CodenameOne by codenameone.

the class CloudImageProperty method propertyValue.

/**
 * {@inheritDoc}
 */
public Object propertyValue(CloudObject obj, String propertyName) {
    final String key = (String) obj.getObject(idProperty);
    if (key == null) {
        return placeholderImage;
    }
    Image image = (Image) getCache().get(key);
    if (image == null) {
        ReplaceableImage r = inProgress.get(key);
        if (r != null) {
            return r;
        }
        final ReplaceableImage rp = ReplaceableImage.create(placeholderImage);
        inProgress.put(key, rp);
        ConnectionRequest cr = new ConnectionRequest() {

            private EncodedImage e;

            protected void readResponse(InputStream input) throws IOException {
                e = EncodedImage.create(input);
                ;
                if (e.getWidth() != placeholderImage.getWidth() || e.getHeight() != placeholderImage.getHeight()) {
                    ImageIO io = ImageIO.getImageIO();
                    if (io != null) {
                        ByteArrayOutputStream bo = new ByteArrayOutputStream();
                        io.save(new ByteArrayInputStream(e.getImageData()), bo, ImageIO.FORMAT_JPEG, placeholderImage.getWidth(), placeholderImage.getHeight(), 0.9f);
                        e = EncodedImage.create(bo.toByteArray());
                    }
                }
            }

            protected void postResponse() {
                rp.replace(e);
                getCache().put(key, e);
                inProgress.remove(key);
            }
        };
        cr.setPost(false);
        cr.setUrl(CloudStorage.getInstance().getUrlForCloudFileId(key));
        NetworkManager.getInstance().addToQueue(cr);
        return rp;
    }
    return image;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ReplaceableImage(com.codename1.components.ReplaceableImage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Image(com.codename1.ui.Image) ReplaceableImage(com.codename1.components.ReplaceableImage) EncodedImage(com.codename1.ui.EncodedImage) EncodedImage(com.codename1.ui.EncodedImage) ImageIO(com.codename1.ui.util.ImageIO)

Example 2 with CloudObject

use of com.codename1.cloud.CloudObject 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 3 with CloudObject

use of com.codename1.cloud.CloudObject 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 4 with CloudObject

use of com.codename1.cloud.CloudObject in project CodenameOne by codenameone.

the class GenericListCellRenderer method getCellRendererComponent.

/**
 * {@inheritDoc}
 */
public Component getCellRendererComponent(Component list, Object model, T value, int index, boolean isSelected) {
    Component cmp;
    Component[] entries;
    if (!fisheye && !Display.getInstance().shouldRenderSelection(list)) {
        isSelected = false;
    }
    if (isSelected && (fisheye || list.hasFocus())) {
        cmp = selected;
        entries = selectedEntries;
        if (selectedEven != null && index % 2 == 0) {
            cmp = selectedEven;
            entries = selectedEntriesEven;
            // prevent the list from over-optimizing the background painting
            if (list instanceof List) {
                ((List) list).setMutableRendererBackgrounds(true);
            }
        }
        cmp.setFocus(true);
        boolean lead = false;
        if (cmp instanceof Container) {
            lead = ((Container) cmp).getLeadComponent() != null;
        }
        if (value instanceof Map) {
            Map h = (Map) value;
            Boolean enabled = (Boolean) h.get(ENABLED);
            if (enabled != null) {
                cmp.setEnabled(enabled.booleanValue());
            }
            int elen = entries.length;
            for (int iter = 0; iter < elen; iter++) {
                String currentName = entries[iter].getName();
                Object val;
                if (currentName.equals("$number")) {
                    val = "" + (index + 1);
                } else {
                    // a selected entry might differ in its value to allow for
                    // behavior such as rollover images
                    val = h.get("#" + currentName);
                    if (val == null) {
                        val = h.get(currentName);
                    }
                    val = updateModelValues(h, currentName, entries, iter, val);
                }
                setComponentValueWithTickering(entries[iter], val, list, cmp);
                entries[iter].setFocus(lead || entries[iter].isFocusable());
            }
        } else {
            if (value instanceof CloudObject) {
                CloudObject h = (CloudObject) value;
                Boolean enabled = (Boolean) h.getBoolean(ENABLED);
                if (enabled != null) {
                    cmp.setEnabled(enabled.booleanValue());
                }
                int elen = entries.length;
                for (int iter = 0; iter < elen; iter++) {
                    String currentName = entries[iter].getName();
                    Object val;
                    if (currentName.equals("$number")) {
                        val = "" + (index + 1);
                    } else {
                        // a selected entry might differ in its value to allow for
                        // behavior such as rollover images
                        val = h.getObject("#" + currentName);
                        if (val == null) {
                            val = h.getObject(currentName);
                        }
                    }
                    setComponentValueWithTickering(entries[iter], val, list, cmp);
                    entries[iter].setFocus(entries[iter].isFocusable());
                }
            } else {
                setComponentValueWithTickering(entries[0], value, list, cmp);
                entries[0].setFocus(entries[0].isFocusable());
            }
        }
        return cmp;
    } else {
        cmp = unselected;
        entries = unselectedEntries;
        if (unselectedEven != null && index % 2 == 0) {
            cmp = unselectedEven;
            entries = unselectedEntriesEven;
            // prevent the list from over-optimizing the background painting
            if (list instanceof List) {
                ((List) list).setMutableRendererBackgrounds(true);
            }
        }
        cmp.setFocus(false);
        if (value instanceof Map) {
            Map h = (Map) value;
            Boolean enabled = (Boolean) h.get(ENABLED);
            if (enabled != null) {
                cmp.setEnabled(enabled.booleanValue());
            }
            int elen = entries.length;
            for (int iter = 0; iter < elen; iter++) {
                String currentName = entries[iter].getName();
                if (currentName.equals("$number")) {
                    setComponentValue(entries[iter], "" + (index + 1), list, cmp);
                    continue;
                }
                Object val = h.get(currentName);
                val = updateModelValues(h, currentName, entries, iter, val);
                setComponentValue(entries[iter], val, list, cmp);
            }
        } else {
            if (value instanceof CloudObject) {
                CloudObject h = (CloudObject) value;
                Boolean enabled = h.getBoolean(ENABLED);
                if (enabled != null) {
                    cmp.setEnabled(enabled.booleanValue());
                }
                int elen = entries.length;
                for (int iter = 0; iter < elen; iter++) {
                    String currentName = entries[iter].getName();
                    if (currentName.equals("$number")) {
                        setComponentValue(entries[iter], "" + (index + 1), list, cmp);
                        continue;
                    }
                    setComponentValue(entries[iter], h.getObject(currentName), list, cmp);
                }
            } else {
                if (entries.length > 0) {
                    setComponentValue(entries[0], value, list, cmp);
                }
            }
        }
        return cmp;
    }
}
Also used : Container(com.codename1.ui.Container) CloudObject(com.codename1.cloud.CloudObject) List(com.codename1.ui.List) ArrayList(java.util.ArrayList) CloudObject(com.codename1.cloud.CloudObject) Component(com.codename1.ui.Component) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Component (com.codename1.ui.Component)3 Container (com.codename1.ui.Container)2 BindTarget (com.codename1.cloud.BindTarget)1 CloudObject (com.codename1.cloud.CloudObject)1 ReplaceableImage (com.codename1.components.ReplaceableImage)1 ConnectionRequest (com.codename1.io.ConnectionRequest)1 EncodedImage (com.codename1.ui.EncodedImage)1 Image (com.codename1.ui.Image)1 List (com.codename1.ui.List)1 ImageIO (com.codename1.ui.util.ImageIO)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Map (java.util.Map)1