Search in sources :

Example 61 with Property

use of com.codename1.rad.models.Property in project CodeRAD by shannah.

the class Properties method getName.

public static String getName(Property p) {
    Name n = (Name) p.getAttribute(Name.class);
    String nameStr = null;
    if (n != null) {
        nameStr = n.getValue();
        if (nameStr != null && !nameStr.isEmpty()) {
            return nameStr;
        }
    }
    Tags tags = (Tags) p.getAttribute(Tags.class);
    if (tags != null) {
        for (Tag t : tags) {
            String tname = t.getName();
            if (tname != null && !tname.isEmpty()) {
                return tname;
            }
        }
    }
    return p.toString();
}
Also used : Name(com.codename1.rad.models.Property.Name)

Example 62 with Property

use of com.codename1.rad.models.Property in project CodeRAD by shannah.

the class ButtonListPropertyView method updateMultiSelectionModel.

private void updateMultiSelectionModel() {
    MultipleSelectionListModel model = getComponent().getMultiListModel();
    Entity e = getPropertySelector().getLeafEntity();
    Property p = getPropertySelector().getLeafProperty();
    Object val = p.getValue(e.getEntity());
    int len = model.getSize();
    if (getPropertySelector().isEmpty()) {
        if (model.getSelectedIndices().length > 0) {
            model.setSelectedIndices(new int[0]);
        }
    } else {
        List<Integer> selectedIndices = new ArrayList<>();
        if (p.getContentType().isEntityList()) {
            // Property contains an entity list
            List selectedObjects = new ArrayList();
            List<String> selectedIds = new ArrayList<>();
            boolean useIds = true;
            EntityList el = e.getEntity().getEntityList(p);
            for (Object obj : el) {
                selectedObjects.add(obj);
                if (obj instanceof Entity) {
                    String id = ((Entity) obj).getEntity().getText(Thing.identifier);
                    if (id == null) {
                        useIds = false;
                        break;
                    }
                    selectedIds.add(id);
                } else {
                    useIds = false;
                }
            }
            if (useIds) {
                // We will useIds to match rows in options list with rows in entity list
                for (int i = 0; i < len; i++) {
                    Object rowVal = model.getItemAt(i);
                    if (rowVal instanceof Entity) {
                        Entity rowEnt = (Entity) rowVal;
                        String rowId = rowEnt.getEntity().getText(Thing.identifier);
                        if (rowId == null) {
                            throw new IllegalStateException("Attempt to use identifiers for matching items in ButtonListPropertyView, but row item " + rowEnt + " has no identifier.  Property: " + p + " in entity " + e);
                        }
                        if (selectedIds.contains(rowId)) {
                            selectedIndices.add(i);
                        }
                    } else {
                        throw new IllegalStateException("Options for field should all be entities. Property " + p + " entity " + e);
                    }
                }
            } else {
                // Not using IDS.  We will use direct matching.
                for (int i = 0; i < len; i++) {
                    if (el.contains(model.getItemAt(i))) {
                        selectedIndices.add(i);
                    }
                }
            }
        } else if (Collection.class.isAssignableFrom(p.getContentType().getRepresentationClass())) {
            // It's a collection
            for (int i = 0; i < len; i++) {
                if (((Collection) val).contains(model.getItemAt(i))) {
                    selectedIndices.add(i);
                }
            }
        } else {
            throw new IllegalStateException("Property " + p + " must contain either EntityList or Collection in order to be editable by ButtonListPropertyView with a multi-selection options model.");
        }
        java.util.Collections.sort(selectedIndices, (i1, i2) -> {
            return i1 - i2;
        });
        List<Integer> existingSelectedIndices = new ArrayList<>();
        for (int index : model.getSelectedIndices()) {
            existingSelectedIndices.add(index);
        }
        java.util.Collections.sort(existingSelectedIndices, (i1, i2) -> {
            return i1 - i2;
        });
        if (!Objects.deepEquals(selectedIndices, existingSelectedIndices)) {
            int size0 = selectedIndices.size();
            int[] selectedIndicesArr = new int[size0];
            for (int i = 0; i < size0; i++) {
                selectedIndicesArr[i] = selectedIndices.get(i);
            }
            model.setSelectedIndices(selectedIndicesArr);
        }
    }
}
Also used : Entity(com.codename1.rad.models.Entity) EntityList(com.codename1.rad.models.EntityList) MultipleSelectionListModel(com.codename1.ui.list.MultipleSelectionListModel) ButtonList(com.codename1.components.ButtonList) EntityList(com.codename1.rad.models.EntityList) Property(com.codename1.rad.models.Property)

Example 63 with Property

use of com.codename1.rad.models.Property in project CodeRAD by shannah.

the class ButtonListPropertyView method commitMultiSelectionModel.

private void commitMultiSelectionModel() {
    Entity e = getPropertySelector().getLeafEntity();
    Property p = getPropertySelector().getLeafProperty();
    int selectedIndex = getComponent().getModel().getSelectedIndex();
    if (selectedIndex < 0) {
        if (p.getContentType().isEntityList()) {
            e.getEntity().getEntityList(p).clear();
            e.getEntity().setChanged(p, true);
        } else if (Collection.class.isAssignableFrom(p.getContentType().getRepresentationClass())) {
            ((Collection) e.getEntity().get(p)).clear();
            e.getEntity().setChanged(p, true);
        } else {
            throw new IllegalStateException("Unsupported property content type for property");
        }
    } else {
        ListModel model = getComponent().getModel();
        if (model instanceof MultipleSelectionListModel) {
            MultipleSelectionListModel multiModel = (MultipleSelectionListModel) model;
            int[] selectedIndices = multiModel.getSelectedIndices();
            boolean changed = false;
            Set selectedObjects = new HashSet();
            for (int i = 0; i < selectedIndices.length; i++) {
                selectedObjects.add(multiModel.getItemAt(selectedIndices[i]));
            }
            Set oldSelectedObjects = new HashSet();
            if (Iterable.class.isAssignableFrom(p.getContentType().getRepresentationClass())) {
                Iterable it = e.getEntity().getAs(p, Iterable.class);
                if (it != null) {
                    for (Object i : it) {
                        oldSelectedObjects.add(i);
                    }
                }
            }
            changed = !(oldSelectedObjects.containsAll(selectedObjects) && selectedObjects.containsAll(oldSelectedObjects));
            if (!changed)
                return;
            if (p.getContentType().isEntityList()) {
                EntityList el = e.getEntity().getEntityList(p);
                if (el == null) {
                    el = new EntityList();
                    e.getEntity().set(p, el);
                }
                for (Object o : selectedObjects) {
                    if (!(o instanceof Entity)) {
                        throw new IllegalStateException("Cannot add non-entity to entity list for property " + p);
                    }
                    if (!el.contains(o)) {
                        el.add((Entity) o);
                    }
                }
                List<Entity> toRemove = new ArrayList<>();
                for (Object entity : el) {
                    if (!selectedObjects.contains(entity)) {
                        toRemove.add((Entity) entity);
                    }
                }
                if (!toRemove.isEmpty()) {
                    for (Entity entity : toRemove) {
                        el.remove(entity);
                    }
                }
                e.setChanged(p, true);
            } else if (Collection.class.isAssignableFrom(p.getContentType().getRepresentationClass())) {
                Collection c = e.getAs(p, Collection.class);
                if (c == null) {
                    if (p.getContentType().getRepresentationClass().isAssignableFrom(List.class)) {
                        c = new ArrayList();
                        e.getEntity().set(p, c);
                    } else if (p.getContentType().getRepresentationClass().isAssignableFrom(Set.class)) {
                        c = new HashSet();
                        e.getEntity().set(p, c);
                    } else {
                        throw new IllegalStateException("Cannot set item in collection of property " + p + " because the collection is null.");
                    }
                }
                for (Object o : selectedObjects) {
                    if (!c.contains(o)) {
                        c.add(o);
                    }
                }
                List toRemove = new ArrayList<>();
                for (Object entity : c) {
                    if (!selectedObjects.contains(entity)) {
                        toRemove.add(entity);
                    }
                }
                if (!toRemove.isEmpty()) {
                    for (Object entity : toRemove) {
                        c.remove(entity);
                    }
                }
                e.setChanged(p, true);
            }
        } else {
            Object selectedObject = getComponent().getModel().getItemAt(selectedIndex);
            if (p.getContentType().isEntityList()) {
                if (!(selectedObject instanceof Entity)) {
                    throw new IllegalStateException("Attempt to add non-entity " + selectedObject + " to property of type entity list");
                }
                EntityList el = e.getEntity().getEntityList(p);
                if (el == null) {
                    el = new EntityList();
                    e.getEntity().set(p, el);
                }
                if (el.size() == 1 && el.contains(selectedObject)) {
                    return;
                }
                if (el.size() != 0) {
                    el.clear();
                }
                el.add((Entity) selectedObject);
                e.setChanged(p, true);
            } else if (Collection.class.isAssignableFrom(p.getContentType().getRepresentationClass())) {
                Collection c = (Collection) e.getEntity().getAs(p, Collection.class);
                if (c == null) {
                    if (p.getContentType().getRepresentationClass().isAssignableFrom(List.class)) {
                        c = new ArrayList();
                        e.getEntity().set(p, c);
                    } else if (p.getContentType().getRepresentationClass().isAssignableFrom(Set.class)) {
                        c = new HashSet();
                        e.getEntity().set(p, c);
                    } else {
                        throw new IllegalStateException("Cannot set item in collection of property " + p + " because the collection is null.");
                    }
                }
                if (c.size() == 1 && c.contains(selectedObject)) {
                    return;
                }
                if (!c.isEmpty()) {
                    c.clear();
                }
                c.add(selectedObject);
                e.setChanged(p, true);
            }
            e.getEntity().set(p, selectedObject);
        }
    }
}
Also used : Entity(com.codename1.rad.models.Entity) EntityList(com.codename1.rad.models.EntityList) MultipleSelectionListModel(com.codename1.ui.list.MultipleSelectionListModel) ListModel(com.codename1.ui.list.ListModel) DefaultListModel(com.codename1.ui.list.DefaultListModel) MultipleSelectionListModel(com.codename1.ui.list.MultipleSelectionListModel) ButtonList(com.codename1.components.ButtonList) EntityList(com.codename1.rad.models.EntityList) Property(com.codename1.rad.models.Property)

Example 64 with Property

use of com.codename1.rad.models.Property in project CodeRAD by shannah.

the class ButtonListPropertyView method commitSingleSelectionModel.

private void commitSingleSelectionModel() {
    Entity e = getPropertySelector().getLeafEntity();
    Property p = getPropertySelector().getLeafProperty();
    int selectedIndex = getComponent().getModel().getSelectedIndex();
    if (selectedIndex < 0) {
        e.getEntity().set(p, null);
    } else {
        Object selectedObject = getComponent().getModel().getItemAt(selectedIndex);
        e.getEntity().set(p, selectedObject);
    }
}
Also used : Entity(com.codename1.rad.models.Entity) Property(com.codename1.rad.models.Property)

Example 65 with Property

use of com.codename1.rad.models.Property in project CodeRAD by shannah.

the class CheckBoxPropertyView method commit.

@Override
public void commit() {
    if (getPropertySelector().isFalsey() == getComponent().isSelected()) {
        Entity e = getPropertySelector().getLeafEntity();
        Property p = getPropertySelector().getLeafProperty();
        e.getEntity().setBoolean(p, getComponent().isSelected());
    }
}
Also used : Entity(com.codename1.rad.models.Entity) Property(com.codename1.rad.models.Property)

Aggregations

Entity (com.codename1.rad.models.Entity)22 Property (com.codename1.rad.models.Property)16 IOException (java.io.IOException)11 ResultParser (com.codename1.rad.io.ResultParser)10 ArrayList (java.util.ArrayList)10 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)9 Component (com.codename1.ui.Component)9 Element (com.codename1.xml.Element)8 ParseException (com.codename1.l10n.ParseException)7 Container (com.codename1.ui.Container)7 Label (com.codename1.ui.Label)7 SortedProperties (com.codename1.ant.SortedProperties)6 Log (com.codename1.io.Log)6 Form (com.codename1.ui.Form)6 AnimationObject (com.codename1.ui.animations.AnimationObject)6 Map (java.util.Map)6 Thing (com.codename1.rad.schemas.Thing)5 XMLParser (com.codename1.xml.XMLParser)5 StringReader (java.io.StringReader)5 List (java.util.List)5