Search in sources :

Example 1 with ButtonListPropertyView

use of com.codename1.rad.propertyviews.ButtonListPropertyView 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)

Aggregations

ButtonList (com.codename1.components.ButtonList)1 Entity (com.codename1.rad.models.Entity)1 EntityList (com.codename1.rad.models.EntityList)1 Property (com.codename1.rad.models.Property)1 MultipleSelectionListModel (com.codename1.ui.list.MultipleSelectionListModel)1