Search in sources :

Example 1 with MultipleSelectionListModel

use of com.codename1.ui.list.MultipleSelectionListModel in project CodeRAD by shannah.

the class ButtonListPropertyView method isMultiSelectionListModel.

private boolean isMultiSelectionListModel() {
    ListModel model = getComponent().getModel();
    if (!(model instanceof MultipleSelectionListModel)) {
        return false;
    }
    MultipleSelectionListModel multiModel = (MultipleSelectionListModel) model;
    if (multiModel instanceof DefaultListModel) {
        DefaultListModel dlm = (DefaultListModel) multiModel;
        return dlm.isMultiSelectionMode();
    }
    return true;
}
Also used : MultipleSelectionListModel(com.codename1.ui.list.MultipleSelectionListModel) ListModel(com.codename1.ui.list.ListModel) DefaultListModel(com.codename1.ui.list.DefaultListModel) MultipleSelectionListModel(com.codename1.ui.list.MultipleSelectionListModel) DefaultListModel(com.codename1.ui.list.DefaultListModel)

Example 2 with MultipleSelectionListModel

use of com.codename1.ui.list.MultipleSelectionListModel in project CodenameOne by codenameone.

the class ButtonList method refresh.

/**
 * Refreshes the container - regenerating all of the buttons in the list from
 * the model.  This usually doesn't ever need to be called explicitly as it will be called
 * automatically when the model changes, or the layout changes.
 */
public void refresh() {
    group = new ButtonGroup();
    removeAll();
    int selectedIndex = getModel().getSelectedIndex();
    int[] selectedIndices = new int[0];
    if (getModel() instanceof MultipleSelectionListModel) {
        selectedIndices = getMultiListModel().getSelectedIndices();
    }
    int len = model.getSize();
    for (int i = 0; i < len; i++) {
        Component b = createComponent(model.getItemAt(i));
        if (isAllowMultipleSelection()) {
            if (Arrays.binarySearch(selectedIndices, i) >= 0) {
                setSelected(b, true);
            }
        } else {
            if (i == selectedIndex) {
                setSelected(b, true);
            }
        }
        add(b);
    }
}
Also used : ButtonGroup(com.codename1.ui.ButtonGroup) MultipleSelectionListModel(com.codename1.ui.list.MultipleSelectionListModel) Component(com.codename1.ui.Component)

Example 3 with MultipleSelectionListModel

use of com.codename1.ui.list.MultipleSelectionListModel in project CodeRAD by shannah.

the class EntityList method toMultipleSelectionListModel.

public MultipleSelectionListModel<T> toMultipleSelectionListModel() {
    return new MultipleSelectionListModel<T>() {

        private List<SelectionListener> selectionListeners;

        private Set<Integer> selectedIndices = new HashSet<>();

        @Override
        public void addSelectedIndices(int... ints) {
            List<Integer> added = new ArrayList();
            for (int i : ints) {
                if (selectedIndices.contains(i))
                    continue;
                selectedIndices.add(i);
                added.add(i);
            }
            if (selectionListeners != null && !selectionListeners.isEmpty() && !added.isEmpty()) {
                List<SelectionListener> toSend = new ArrayList<>(selectionListeners);
                for (Integer i : added) {
                    for (SelectionListener l : toSend) {
                        l.selectionChanged(-1, i);
                    }
                }
            }
        }

        @Override
        public void removeSelectedIndices(int... ints) {
            List<Integer> removed = new ArrayList<>();
            for (int i : ints) {
                if (!selectedIndices.contains(i)) {
                    continue;
                }
                selectedIndices.remove(i);
                removed.add(i);
            }
            if (selectionListeners != null && !selectionListeners.isEmpty() && !removed.isEmpty()) {
                List<SelectionListener> toSend = new ArrayList<>(selectionListeners);
                for (Integer i : removed) {
                    for (SelectionListener l : toSend) {
                        l.selectionChanged(i, -1);
                    }
                }
            }
        }

        @Override
        public void setSelectedIndices(int... ints) {
            int[] existing = getSelectedIndices();
            Set<Integer> existingInts = new HashSet<>();
            Set<Integer> newInts = new HashSet<>();
            Set<Integer> toAdd = new HashSet<>();
            Set<Integer> toRemove = new HashSet<>();
            for (int i : ints) {
                newInts.add(i);
            }
            for (int i : existing) {
                existingInts.add(i);
            }
            for (int i : newInts) {
                if (!existingInts.contains(i)) {
                    toAdd.add(i);
                }
            }
            for (int i : existingInts) {
                if (!newInts.contains(i)) {
                    toRemove.add(i);
                }
            }
            if (!toRemove.isEmpty()) {
                int[] toRemoveArr = new int[toRemove.size()];
                int index = 0;
                for (Integer remI : toRemove) {
                    toRemoveArr[index++] = remI;
                }
                removeSelectedIndices(toRemoveArr);
            }
            if (!toAdd.isEmpty()) {
                int[] toAddArr = new int[toAdd.size()];
                int index = 0;
                for (Integer addI : toAdd) {
                    toAddArr[index++] = addI;
                }
                addSelectedIndices(toAddArr);
            }
        }

        @Override
        public int[] getSelectedIndices() {
            List<Integer> ints = new ArrayList<>(selectedIndices);
            Collections.sort(ints);
            int[] out = new int[ints.size()];
            int index = 0;
            for (Integer i : ints) {
                out[index++] = i;
            }
            return out;
        }

        @Override
        public T getItemAt(int i) {
            return EntityList.this.get(i);
        }

        @Override
        public int getSize() {
            return EntityList.this.size();
        }

        @Override
        public int getSelectedIndex() {
            if (selectedIndices.isEmpty())
                return -1;
            return selectedIndices.iterator().next();
        }

        @Override
        public void setSelectedIndex(int i) {
            if (i < 0) {
                if (!selectedIndices.isEmpty()) {
                    removeSelectedIndices(getSelectedIndices());
                }
            } else
                setSelectedIndices(i);
        }

        class DataChangedListenerWrapper implements ActionListener<EntityListEvent> {

            DataChangedListener l;

            DataChangedListenerWrapper(DataChangedListener l) {
                this.l = l;
            }

            @Override
            public void actionPerformed(EntityListEvent entityListEvent) {
                if (entityListEvent instanceof EntityAddedEvent) {
                    EntityAddedEvent e = (EntityAddedEvent) entityListEvent;
                    l.dataChanged(DataChangedListener.ADDED, e.getIndex());
                } else if (entityListEvent instanceof EntityRemovedEvent) {
                    EntityRemovedEvent e = (EntityRemovedEvent) entityListEvent;
                    l.dataChanged(DataChangedListener.REMOVED, e.getIndex());
                }
            }
        }

        private List<DataChangedListenerWrapper> dataChangedListenerWrappers;

        @Override
        public void addDataChangedListener(DataChangedListener dataChangedListener) {
            if (dataChangedListenerWrappers == null) {
                dataChangedListenerWrappers = new ArrayList<>();
            }
            DataChangedListenerWrapper w = new DataChangedListenerWrapper(dataChangedListener);
            dataChangedListenerWrappers.add(w);
            EntityList.this.addActionListener(w);
        }

        @Override
        public void removeDataChangedListener(DataChangedListener dataChangedListener) {
            DataChangedListenerWrapper w = null;
            if (dataChangedListenerWrappers == null || dataChangedListenerWrappers.isEmpty())
                return;
            for (DataChangedListenerWrapper candidate : dataChangedListenerWrappers) {
                if (candidate.l == dataChangedListener) {
                    w = candidate;
                    break;
                }
            }
            if (w == null)
                return;
            EntityList.this.removeActionListener(w);
        }

        @Override
        public void addSelectionListener(SelectionListener selectionListener) {
            if (selectionListeners == null)
                selectionListeners = new ArrayList<>();
            selectionListeners.add(selectionListener);
        }

        @Override
        public void removeSelectionListener(SelectionListener selectionListener) {
            if (selectionListeners == null || selectionListeners.isEmpty())
                return;
            selectionListeners.remove(selectionListener);
        }

        @Override
        public void addItem(T o) {
            EntityList.this.add((T) o);
        }

        @Override
        public void removeItem(int i) {
            Object o = EntityList.this.get(i);
            if (o != null) {
                EntityList.this.remove((T) o);
            }
        }
    };
}
Also used : DataChangedListener(com.codename1.ui.events.DataChangedListener) MultipleSelectionListModel(com.codename1.ui.list.MultipleSelectionListModel) SelectionListener(com.codename1.ui.events.SelectionListener)

Example 4 with MultipleSelectionListModel

use of com.codename1.ui.list.MultipleSelectionListModel 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 5 with MultipleSelectionListModel

use of com.codename1.ui.list.MultipleSelectionListModel 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)

Aggregations

MultipleSelectionListModel (com.codename1.ui.list.MultipleSelectionListModel)5 ButtonList (com.codename1.components.ButtonList)2 Entity (com.codename1.rad.models.Entity)2 EntityList (com.codename1.rad.models.EntityList)2 Property (com.codename1.rad.models.Property)2 DefaultListModel (com.codename1.ui.list.DefaultListModel)2 ListModel (com.codename1.ui.list.ListModel)2 ButtonGroup (com.codename1.ui.ButtonGroup)1 Component (com.codename1.ui.Component)1 DataChangedListener (com.codename1.ui.events.DataChangedListener)1 SelectionListener (com.codename1.ui.events.SelectionListener)1