Search in sources :

Example 6 with ListModel

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

the class DefaultLookAndFeel method getListPreferredSizeImpl.

private Dimension getListPreferredSizeImpl(List l) {
    int width = 0;
    int height = 0;
    int selectedHeight;
    int selectedWidth;
    ListModel model = l.getModel();
    int numOfcomponents = Math.max(model.getSize(), l.getMinElementHeight());
    numOfcomponents = Math.min(numOfcomponents, l.getMaxElementHeight());
    Object prototype = l.getRenderingPrototype();
    Style unselectedEntryStyle = null;
    Style selectedEntryStyle;
    if (prototype != null) {
        ListCellRenderer renderer = l.getRenderer();
        Component cmp = renderer.getListCellRendererComponent(l, prototype, 0, false);
        height = cmp.getPreferredH();
        width = cmp.getPreferredW();
        unselectedEntryStyle = cmp.getStyle();
        cmp = renderer.getListCellRendererComponent(l, prototype, 0, true);
        selectedEntryStyle = cmp.getStyle();
        selectedHeight = Math.max(height, cmp.getPreferredH());
        selectedWidth = Math.max(width, cmp.getPreferredW());
    } else {
        int hightCalcComponents = Math.min(l.getListSizeCalculationSampleCount(), numOfcomponents);
        Object dummyProto = l.getRenderingPrototype();
        if (model.getSize() > 0 && dummyProto == null) {
            dummyProto = model.getItemAt(0);
        }
        ListCellRenderer renderer = l.getRenderer();
        for (int i = 0; i < hightCalcComponents; i++) {
            Object value;
            if (i < model.getSize()) {
                value = model.getItemAt(i);
            } else {
                value = dummyProto;
            }
            Component cmp = renderer.getListCellRendererComponent(l, value, i, false);
            if (cmp instanceof Container) {
                cmp.setShouldCalcPreferredSize(true);
            }
            unselectedEntryStyle = cmp.getStyle();
            height = Math.max(height, cmp.getPreferredH());
            width = Math.max(width, cmp.getPreferredW());
        }
        selectedEntryStyle = unselectedEntryStyle;
        selectedHeight = height;
        selectedWidth = width;
        if (model.getSize() > 0) {
            Object value = model.getItemAt(0);
            Component cmp = renderer.getListCellRendererComponent(l, value, 0, true);
            if (cmp instanceof Container) {
                cmp.setShouldCalcPreferredSize(true);
            }
            selectedHeight = Math.max(height, cmp.getPreferredH());
            selectedWidth = Math.max(width, cmp.getPreferredW());
            selectedEntryStyle = cmp.getStyle();
        }
    }
    if (unselectedEntryStyle != null) {
        selectedWidth += selectedEntryStyle.getMarginLeftNoRTL() + selectedEntryStyle.getMarginRightNoRTL();
        selectedHeight += selectedEntryStyle.getMarginTop() + selectedEntryStyle.getMarginBottom();
        width += unselectedEntryStyle.getMarginLeftNoRTL() + unselectedEntryStyle.getMarginRightNoRTL();
        height += unselectedEntryStyle.getMarginTop() + unselectedEntryStyle.getMarginBottom();
    }
    Style lStyle = l.getStyle();
    int verticalPadding = lStyle.getPaddingTop() + lStyle.getPaddingBottom();
    int horizontalPadding = lStyle.getPaddingRightNoRTL() + lStyle.getPaddingLeftNoRTL() + l.getSideGap();
    if (numOfcomponents == 0) {
        return new Dimension(horizontalPadding, verticalPadding);
    }
    // If combobox without ever importing the ComboBox class dependency
    if (l.getOrientation() > List.HORIZONTAL) {
        int boxWidth = l.getStyle().getFont().getHeight() + 2;
        return new Dimension(boxWidth + selectedWidth + horizontalPadding, selectedHeight + verticalPadding);
    } else {
        if (l.getOrientation() == List.VERTICAL) {
            return new Dimension(selectedWidth + horizontalPadding, selectedHeight + (height + l.getItemGap()) * (numOfcomponents - 1) + verticalPadding);
        } else {
            return new Dimension(selectedWidth + (width + l.getItemGap()) * (numOfcomponents - 1) + horizontalPadding, selectedHeight + verticalPadding);
        }
    }
}
Also used : Container(com.codename1.ui.Container) ListModel(com.codename1.ui.list.ListModel) ListCellRenderer(com.codename1.ui.list.ListCellRenderer) Dimension(com.codename1.ui.geom.Dimension) Component(com.codename1.ui.Component)

Example 7 with ListModel

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

the class GenericSpinner method getPropertyValue.

/**
 * {@inheritDoc}
 */
public Object getPropertyValue(String name) {
    if (name.equals("items")) {
        ListModel m = getModel();
        String[] s = new String[m.getSize()];
        int slen = s.length;
        for (int iter = 0; iter < slen; iter++) {
            Object o = m.getItemAt(iter);
            if (o != null) {
                s[iter] = o.toString();
            }
        }
        return s;
    }
    if (name.equals("model")) {
        return getModel();
    }
    if (name.equals("renderer")) {
        return getRenderer();
    }
    if (name.equals("columns")) {
        return new Integer(getColumns());
    }
    return null;
}
Also used : ListModel(com.codename1.ui.list.ListModel) DefaultListModel(com.codename1.ui.list.DefaultListModel)

Example 8 with ListModel

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

the class ImageDownloadService method setEntryInListModel.

/**
 * This method is invoked when an image finished downloading and should be set to an offset in the list
 * model. This is useful for special cases with complex list model hierarchies or proxies.
 *
 * @param offset the offset in the list given when creating the service
 * @param img the image
 */
protected void setEntryInListModel(int offset, Image img) {
    Map h;
    ListModel model;
    if (targetModel != null) {
        model = targetModel;
    } else {
        if (targetList instanceof List) {
            model = ((List) targetList).getModel();
        } else {
            model = ((ContainerList) targetList).getModel();
        }
    }
    h = (Map) model.getItemAt(targetOffset);
    if (!fastScale && toScale != null) {
        img = scaleImage(img, toScale, maintainAspectRatio);
    }
    h.put(targetKey, img);
    if (model instanceof DefaultListModel) {
        ((DefaultListModel) model).setItem(targetOffset, h);
    }
}
Also used : DefaultListModel(com.codename1.ui.list.DefaultListModel) ListModel(com.codename1.ui.list.ListModel) DefaultListModel(com.codename1.ui.list.DefaultListModel) ContainerList(com.codename1.ui.list.ContainerList) List(com.codename1.ui.List) Map(java.util.Map)

Example 9 with ListModel

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

the class ImageDownloadService method createImageToStorage.

/**
 * Constructs an image request that will automatically populate the given list
 * when the response arrives, it will cache the file locally as a file
 * in the file storage.
 * This assumes the GenericListCellRenderer style of
 * list which relies on a map based model approach.
 *
 * @param url the image URL
 * @param targetList the list that should be updated when the data arrives
 * @param targetModel the model
 * @param targetOffset the offset within the list to insert the image
 * @param targetKey the key for the map in the target offset
 * @param cacheId a unique identifier to be used to store the image into storage
 * @param keep if set to true keeps the file in RAM once loaded
 * @param scale the scale of the image to put in the List or null
 */
private static void createImageToStorage(final String url, final Component targetList, final ListModel targetModel, final int targetOffset, final String targetKey, final String cacheId, final boolean keep, final Dimension scale, final byte priority, final Image placeholderImage, final boolean maintainAspectRatio) {
    if (Display.getInstance().isEdt()) {
        Display.getInstance().scheduleBackgroundTask(new Runnable() {

            public void run() {
                createImageToStorage(url, targetList, targetModel, targetOffset, targetKey, cacheId, keep, scale, priority, placeholderImage, maintainAspectRatio);
            }
        });
        return;
    }
    Image im = cacheImage(cacheId, keep, null, scale, placeholderImage, maintainAspectRatio);
    ImageDownloadService i = new ImageDownloadService(url, targetList, targetOffset, targetKey);
    i.targetModel = targetModel;
    i.maintainAspectRatio = maintainAspectRatio;
    if (im != null) {
        i.setEntryInListModel(targetOffset, im);
        targetList.repaint();
        return;
    }
    // image not found on cache go and download from the url
    i.cacheImages = true;
    i.cacheId = cacheId;
    i.keep = keep;
    i.toScale = scale;
    i.placeholder = placeholderImage;
    i.setPriority(priority);
    i.setFailSilently(true);
    NetworkManager.getInstance().addToQueue(i);
}
Also used : EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 10 with ListModel

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

the class Calendar method setYearRange.

/**
 * Sets the Calendar min and max years
 *
 * @param minYear the min year
 * @param maxYear the max year
 */
public void setYearRange(int minYear, int maxYear) {
    if (minYear > maxYear) {
        throw new IllegalArgumentException("Max year should be bigger than or equal to min year!");
    }
    // The year combobox may not exist in the current context
    if (year != null) {
        Object previouslySelectedYear = year.getSelectedItem();
        Vector years = new Vector();
        for (int i = maxYear; i >= minYear; i--) {
            years.addElement("" + i);
        }
        ListModel yearModel = new DefaultListModel(years);
        year.setModel(yearModel);
        if (years.contains(previouslySelectedYear)) {
            year.setSelectedItem(previouslySelectedYear);
        }
    }
}
Also used : ListModel(com.codename1.ui.list.ListModel) DefaultListModel(com.codename1.ui.list.DefaultListModel) DefaultListModel(com.codename1.ui.list.DefaultListModel) Vector(java.util.Vector)

Aggregations

ListModel (com.codename1.ui.list.ListModel)5 DefaultListModel (com.codename1.ui.list.DefaultListModel)4 FileEncodedImage (com.codename1.components.FileEncodedImage)2 StorageImage (com.codename1.components.StorageImage)2 Component (com.codename1.ui.Component)2 EncodedImage (com.codename1.ui.EncodedImage)2 Image (com.codename1.ui.Image)2 SelectionListener (com.codename1.ui.events.SelectionListener)2 ListCellRenderer (com.codename1.ui.list.ListCellRenderer)2 Container (com.codename1.ui.Container)1 List (com.codename1.ui.List)1 DataChangedListener (com.codename1.ui.events.DataChangedListener)1 Dimension (com.codename1.ui.geom.Dimension)1 ContainerList (com.codename1.ui.list.ContainerList)1 Hashtable (java.util.Hashtable)1 Map (java.util.Map)1 Vector (java.util.Vector)1