Search in sources :

Example 41 with Entity

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

the class DefaultEntityViewFactory method makeSwipeable.

private EntityView makeSwipeable(Entity entity, ViewNode node, Component view) {
    // Check for swipeable container
    SwipeContainer swipe = (SwipeContainer) node.findAttribute(SwipeContainer.class);
    if (swipe != null) {
        EntityView leftCnt = null;
        EntityView rightCnt = null;
        ViewNode leftNode = swipe.getLeft();
        if (leftNode != null) {
            leftCnt = leftNode.createView(entity, this);
        }
        ViewNode rightNode = swipe.getRight();
        if (rightNode != null) {
            rightCnt = rightNode.createView(entity, this);
        }
        SwipeableContainer swipeWrapper = new SwipeableContainer((Component) leftCnt, (Component) rightCnt, view);
        return new WrapperEntityView(swipeWrapper, entity, node);
    }
    Actions leftSwipeActions = node.getActions(ActionCategories.LEFT_SWIPE_MENU);
    Actions rightSwipeActions = node.getActions(ActionCategories.RIGHT_SWIPE_MENU);
    if (!leftSwipeActions.isEmpty() || !rightSwipeActions.isEmpty()) {
        Container leftCnt = null;
        Container rightCnt = null;
        if (!leftSwipeActions.isEmpty()) {
            leftCnt = new Container(BoxLayout.y());
            NodeUtilFunctions.buildActionsBar(node, leftCnt, entity, null, leftSwipeActions, null);
        }
        if (!rightSwipeActions.isEmpty()) {
            rightCnt = new Container(BoxLayout.y());
            NodeUtilFunctions.buildActionsBar(node, leftCnt, entity, rightSwipeActions, null, null);
        }
        SwipeableContainer swipeWrapper = new SwipeableContainer((Component) leftCnt, (Component) rightCnt, view);
        return new WrapperEntityView(swipeWrapper, entity, node);
    }
    return (EntityView) view;
}
Also used : WrapperEntityView(com.codename1.rad.ui.entityviews.WrapperEntityView) Container(com.codename1.ui.Container) SwipeContainer(com.codename1.rad.nodes.SwipeContainer) SwipeableContainer(com.codename1.ui.SwipeableContainer) WrapperEntityView(com.codename1.rad.ui.entityviews.WrapperEntityView) MultiButtonEntityView(com.codename1.rad.ui.entityviews.MultiButtonEntityView) ViewNode(com.codename1.rad.nodes.ViewNode) SwipeableContainer(com.codename1.ui.SwipeableContainer) SwipeContainer(com.codename1.rad.nodes.SwipeContainer)

Example 42 with Entity

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

the class EntityEditor method buildSections.

private void buildSections() {
    for (SectionNode section : (FormNode) rootNode) {
        SectionEditor sectionEditor = new SectionEditor(entity, section);
        sectionEditor.build();
        add(sectionEditor);
    }
}
Also used : FormNode(com.codename1.rad.nodes.FormNode) SectionNode(com.codename1.rad.nodes.SectionNode)

Example 43 with Entity

use of com.codename1.rad.models.Entity 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 44 with Entity

use of com.codename1.rad.models.Entity 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 45 with Entity

use of com.codename1.rad.models.Entity 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)

Aggregations

Entity (com.codename1.rad.models.Entity)38 Property (com.codename1.rad.models.Property)15 EntityList (com.codename1.rad.models.EntityList)13 Container (com.codename1.ui.Container)12 BorderLayout (com.codename1.ui.layouts.BorderLayout)11 ActionNode (com.codename1.rad.nodes.ActionNode)10 FieldNode (com.codename1.rad.nodes.FieldNode)10 Form (com.codename1.ui.Form)8 ViewNode (com.codename1.rad.nodes.ViewNode)7 GridLayout (com.codename1.ui.layouts.GridLayout)7 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)6 ResultParser (com.codename1.rad.io.ResultParser)6 Component (com.codename1.ui.Component)6 Element (com.codename1.xml.Element)6 BadgeUIID (com.codename1.rad.attributes.BadgeUIID)5 Thing (com.codename1.rad.schemas.Thing)5 Button (com.codename1.ui.Button)5 BoxLayout (com.codename1.ui.layouts.BoxLayout)5 Map (java.util.Map)5 Log (com.codename1.io.Log)4