Search in sources :

Example 11 with EntityList

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

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

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

the class TablePropertyView method commit.

@Override
public void commit() {
    EntityListTableModel model = (EntityListTableModel) getComponent().getModel();
    EntityList list = model.getEntityList();
    EntityList ePropertyVal = getPropertyAsEntityList();
    if (ePropertyVal == list) {
        return;
    }
    getEntity().getEntity().set(getProperty(), list);
}
Also used : EntityList(com.codename1.rad.models.EntityList) EntityListTableModel(com.codename1.rad.ui.table.EntityListTableModel)

Example 14 with EntityList

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

the class TablePropertyView method getPropertyAsEntityList.

private EntityList getPropertyAsEntityList() {
    Object propertyVal = getEntity().getEntity().get(getProperty());
    if (!(propertyVal instanceof EntityList)) {
        throw new IllegalStateException("TablePropertyView only supports EntityList properties");
    }
    EntityList ePropertyVal = (EntityList) propertyVal;
    return ePropertyVal;
}
Also used : EntityList(com.codename1.rad.models.EntityList)

Example 15 with EntityList

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

the class ResultParserTest method manualExampleTest.

private void manualExampleTest() throws Exception {
    String xml = "<?xml version=\"1.0\"?>\n" + "<catalog>\n" + "   <book id=\"bk101\">\n" + "      <author>Gambardella, Matthew</author>\n" + "      <title>XML Developer's Guide</title>\n" + "      <genre>Computer</genre>\n" + "      <price>44.95</price>\n" + "      <publish_date>2000-10-01</publish_date>\n" + "      <description>An in-depth look at creating applications \n" + "      with XML.</description>\n" + "   </book>\n" + "   <book id=\"bk102\">\n" + "      <author>Ralls, Kim</author>\n" + "      <title>Midnight Rain</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2000-12-16</publish_date>\n" + "      <description>A former architect battles corporate zombies, \n" + "      an evil sorceress, and her own childhood to become queen \n" + "      of the world.</description>\n" + "   </book>\n" + "   <book id=\"bk103\">\n" + "      <author>Corets, Eva</author>\n" + "      <title>Maeve Ascendant</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2000-11-17</publish_date>\n" + "      <description>After the collapse of a nanotechnology \n" + "      society in England, the young survivors lay the \n" + "      foundation for a new society.</description>\n" + "   </book>\n" + "   <book id=\"bk104\">\n" + "      <author>Corets, Eva</author>\n" + "      <title>Oberon's Legacy</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2001-03-10</publish_date>\n" + "      <description>In post-apocalypse England, the mysterious \n" + "      agent known only as Oberon helps to create a new life \n" + "      for the inhabitants of London. Sequel to Maeve \n" + "      Ascendant.</description>\n" + "   </book>\n" + "   <book id=\"bk105\">\n" + "      <author>Corets, Eva</author>\n" + "      <title>The Sundered Grail</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2001-09-10</publish_date>\n" + "      <description>The two daughters of Maeve, half-sisters, \n" + "      battle one another for control of England. Sequel to \n" + "      Oberon's Legacy.</description>\n" + "   </book>\n" + "   <book id=\"bk106\">\n" + "      <author>Randall, Cynthia</author>\n" + "      <title>Lover Birds</title>\n" + "      <genre>Romance</genre>\n" + "      <price>4.95</price>\n" + "      <publish_date>2000-09-02</publish_date>\n" + "      <description>When Carla meets Paul at an ornithology \n" + "      conference, tempers fly as feathers get ruffled.</description>\n" + "   </book>\n" + "   <book id=\"bk107\">\n" + "      <author>Thurman, Paula</author>\n" + "      <title>Splish Splash</title>\n" + "      <genre>Romance</genre>\n" + "      <price>4.95</price>\n" + "      <publish_date>2000-11-02</publish_date>\n" + "      <description>A deep sea diver finds true love twenty \n" + "      thousand leagues beneath the sea.</description>\n" + "   </book>\n" + "   <book id=\"bk108\">\n" + "      <author>Knorr, Stefan</author>\n" + "      <title>Creepy Crawlies</title>\n" + "      <genre>Horror</genre>\n" + "      <price>4.95</price>\n" + "      <publish_date>2000-12-06</publish_date>\n" + "      <description>An anthology of horror stories about roaches,\n" + "      centipedes, scorpions  and other insects.</description>\n" + "   </book>\n" + "   <book id=\"bk109\">\n" + "      <author>Kress, Peter</author>\n" + "      <title>Paradox Lost</title>\n" + "      <genre>Science Fiction</genre>\n" + "      <price>6.95</price>\n" + "      <publish_date>2000-11-02</publish_date>\n" + "      <description>After an inadvertant trip through a Heisenberg\n" + "      Uncertainty Device, James Salway discovers the problems \n" + "      of being quantum.</description>\n" + "   </book>\n" + "   <book id=\"bk110\">\n" + "      <author>O'Brien, Tim</author>\n" + "      <title>Microsoft .NET: The Programming Bible</title>\n" + "      <genre>Computer</genre>\n" + "      <price>36.95</price>\n" + "      <publish_date>2000-12-09</publish_date>\n" + "      <description>Microsoft's .NET initiative is explored in \n" + "      detail in this deep programmer's reference.</description>\n" + "   </book>\n" + "   <book id=\"bk111\">\n" + "      <author>O'Brien, Tim</author>\n" + "      <title>MSXML3: A Comprehensive Guide</title>\n" + "      <genre>Computer</genre>\n" + "      <price>36.95</price>\n" + "      <publish_date>2000-12-01</publish_date>\n" + "      <description>The Microsoft MSXML3 parser is covered in \n" + "      detail, with attention to XML DOM interfaces, XSLT processing, \n" + "      SAX and more.</description>\n" + "   </book>\n" + "   <book id=\"bk112\">\n" + "      <author>Galos, Mike</author>\n" + "      <title>Visual Studio 7: A Comprehensive Guide</title>\n" + "      <genre>Computer</genre>\n" + "      <price>49.95</price>\n" + "      <publish_date>2001-04-16</publish_date>\n" + "      <description>Microsoft Visual Studio 7 is explored in depth,\n" + "      looking at how Visual Basic, Visual C++, C#, and ASP+ are \n" + "      integrated into a comprehensive development \n" + "      environment.</description>\n" + "   </book>\n" + "</catalog>";
    EntityType bookType = new EntityTypeBuilder().string(Thing.identifier).string(Thing.name).string(Thing.description).build();
    class Book extends BaseEntity {
    }
    EntityType.register(Book.class, bookType, cls -> {
        return new Book();
    });
    class Books extends EntityList<Book> {
    }
    EntityType.registerList(Books.class, Book.class, cls -> {
        return new Books();
    });
    Tag BOOKS = new Tag("Books");
    EntityType catalogType = new EntityTypeBuilder().list(Books.class, BOOKS).build();
    class Catalog extends BaseEntity {

        {
            setEntityType(catalogType);
        }
    }
    EntityType.register(Catalog.class, cls -> {
        return new Catalog();
    });
    ResultParser parser = new ResultParser(catalogType).property("./book", BOOKS).entityType(bookType).property("@id", Thing.identifier).property("title", Thing.name).property("description", Thing.description);
    Catalog catalog = (Catalog) parser.parseXML(xml, new Catalog());
    Books books = (Books) catalog.get(BOOKS);
    assertEqual("bk101", books.get(0).get(Thing.identifier));
    assertEqual("bk112", books.get(books.size() - 1).get(Thing.identifier));
    assertEqual("XML Developer's Guide", books.get(0).get(Thing.name));
}
Also used : ResultParser(com.codename1.rad.io.ResultParser)

Aggregations

EntityList (com.codename1.rad.models.EntityList)18 Entity (com.codename1.rad.models.Entity)14 Map (java.util.Map)6 Thing (com.codename1.rad.schemas.Thing)5 AbstractTest (com.codename1.testing.AbstractTest)5 Form (com.codename1.ui.Form)5 List (java.util.List)5 Log (com.codename1.io.Log)4 ResultParser (com.codename1.rad.io.ResultParser)4 ActionNode (com.codename1.rad.nodes.ActionNode)4 ProfileListView (com.codename1.rad.ui.entityviews.ProfileListView)4 ArrayList (java.util.ArrayList)4 BaseEntity (com.codename1.rad.models.BaseEntity)3 BaseEntity.entityTypeBuilder (com.codename1.rad.models.BaseEntity.entityTypeBuilder)3 Result (com.codename1.rad.processing.Result)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 ButtonList (com.codename1.components.ButtonList)2 NetworkEvent (com.codename1.io.NetworkEvent)2 ParseException (com.codename1.l10n.ParseException)2 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)2