Search in sources :

Example 1 with ListModel

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

the class ImageDownloadService method createImageToFileSystem.

/**
 * 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 targetOffset the offset within the list to insert the image
 * @param targetKey the key for the map in the target offset
 * @param destFile local file to store the data into the given path
 */
private static void createImageToFileSystem(final String url, final Component targetList, final ListModel targetModel, final int targetOffset, final String targetKey, final String destFile, final Dimension toScale, final byte priority, final Image placeholderImage, final boolean maintainAspectRatio) {
    if (Display.getInstance().isEdt()) {
        Display.getInstance().scheduleBackgroundTask(new Runnable() {

            public void run() {
                createImageToFileSystem(url, targetList, targetModel, targetOffset, targetKey, destFile, toScale, priority, placeholderImage, maintainAspectRatio);
            }
        });
        return;
    }
    // image not found on cache go and download from the url
    ImageDownloadService i = new ImageDownloadService(url, targetList, targetOffset, targetKey);
    i.targetModel = targetModel;
    i.maintainAspectRatio = maintainAspectRatio;
    Image im = cacheImage(null, false, destFile, toScale, placeholderImage, maintainAspectRatio);
    if (im != null) {
        i.setEntryInListModel(targetOffset, im);
        targetList.repaint();
        return;
    }
    i.cacheImages = true;
    i.destinationFile = destFile;
    i.toScale = toScale;
    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 2 with ListModel

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

the class TestRecorder method bindListListener.

private void bindListListener(final com.codename1.ui.Component cmp, final ListModel m) {
    if (cmp.getClientProperty("CN1$listenerBound") == null) {
        cmp.putClientProperty("CN1$listenerBound", Boolean.TRUE);
        m.addSelectionListener(new SelectionListener() {

            public void selectionChanged(int oldSelected, int newSelected) {
                generatedCode += "        selectInList(" + getPathOrName(cmp) + ", " + newSelected + ");\n";
                updateTestCode();
            }
        });
    }
}
Also used : SelectionListener(com.codename1.ui.events.SelectionListener)

Example 3 with ListModel

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

the class ImageViewer method setImageList.

/**
 * By providing this optional list of images you can allows swiping between multiple images
 *
 * @param model a list of images
 */
public void setImageList(ListModel<Image> model) {
    if (model == null || model.getSize() == 0) {
        return;
    }
    if (image == null) {
        image = model.getItemAt(0);
    }
    if (swipeableImages != null) {
        swipeableImages.removeDataChangedListener(listListener);
        swipeableImages.removeSelectionListener((SelectionListener) listListener);
        model.addDataChangedListener(listListener);
        model.addSelectionListener((SelectionListener) listListener);
    } else {
        class Listener implements SelectionListener, DataChangedListener {

            public void selectionChanged(int oldSelected, int newSelected) {
                if (selectLock) {
                    return;
                }
                if (swipeableImages.getSize() > 0 && newSelected > -1 && newSelected < swipeableImages.getSize()) {
                    setImage(swipeableImages.getItemAt(newSelected));
                }
            }

            public void dataChanged(int type, int index) {
                if (swipeableImages.getSize() > 0 && swipeableImages.getSelectedIndex() > -1 && swipeableImages.getSelectedIndex() < swipeableImages.getSize()) {
                    setImage(swipeableImages.getItemAt(swipeableImages.getSelectedIndex()));
                }
            }
        }
        listListener = new Listener();
        model.addDataChangedListener(listListener);
        model.addSelectionListener((SelectionListener) listListener);
    }
    this.swipeableImages = model;
}
Also used : SelectionListener(com.codename1.ui.events.SelectionListener) DataChangedListener(com.codename1.ui.events.DataChangedListener) DataChangedListener(com.codename1.ui.events.DataChangedListener) SelectionListener(com.codename1.ui.events.SelectionListener)

Example 4 with ListModel

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

the class FaceBookAccess method getAlbumPhotos.

/**
 * This method returns a list model of photos that automatically fetches additional images as necessary
 * @param targetList required for the image download code
 * @param albumId the id of the album
 * @param photoCount the number of photos within the album
 * @param placeholder a placeholder image that will determine the size of the images requested
 * @return the list of the images
 */
private ListModel getAlbumPhotos(final Component targetList, final String albumId, final int photoCount, final Image placeholder) {
    if (!isAuthenticated()) {
        return null;
    }
    Hashtable[] h = new Hashtable[photoCount];
    int hlen = h.length;
    for (int iter = 0; iter < hlen; iter++) {
        h[iter] = new Hashtable();
        h[iter].put("photo", placeholder);
        if (iter < 30) {
            h[iter].put("fetching", Boolean.TRUE);
        }
    }
    DefaultListModel dl = new DefaultListModel((Object[]) h) {

        public Object getItem(int offset) {
            Hashtable hash = (Hashtable) super.getItemAt(offset);
            if (!hash.containsKey("fetching")) {
                int limit = Math.min(30, photoCount - offset);
                for (int iter = 0; iter < limit; iter++) {
                    Hashtable current = (Hashtable) super.getItemAt(iter + offset);
                    if (current.containsKey("fetching")) {
                        break;
                    }
                    current.put("fetching", Boolean.TRUE);
                }
                FacebookRESTService con = new FacebookRESTService(token, albumId, FacebookRESTService.PHOTOS, false);
                con.setResponseDestination(this);
                con.setResponseOffset(0);
                con.addArgument("limit", "" + limit);
                con.addArgument("offset", "" + offset);
                for (int i = 0; i < responseCodeListeners.size(); i++) {
                    con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
                }
                NetworkManager.getInstance().addToQueueAndWait(con);
                for (int iter = 0; iter < limit; iter++) {
                    Hashtable current = (Hashtable) getItemAt(iter + offset);
                    ImageDownloadService.createImageToStorage((String) current.get("photo"), targetList, iter + offset, "photo", ((String) current.get("id")) + placeholder.getHeight(), placeholder, ConnectionRequest.PRIORITY_NORMAL);
                }
            }
            return hash;
        }
    };
    FacebookRESTService con = new FacebookRESTService(token, albumId, FacebookRESTService.PHOTOS, false);
    con.setResponseDestination(dl);
    con.setResponseOffset(0);
    con.addArgument("limit", "30");
    con.addArgument("offset", "0");
    for (int i = 0; i < responseCodeListeners.size(); i++) {
        con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
    }
    NetworkManager.getInstance().addToQueueAndWait(con);
    for (int iter = 0; iter < Math.min(30, photoCount); iter++) {
        Hashtable hash = (Hashtable) dl.getItemAt(iter);
        ImageDownloadService.createImageToStorage((String) hash.get("photo"), targetList, iter, "photo", ((String) hash.get("id")) + placeholder.getHeight(), placeholder, ConnectionRequest.PRIORITY_NORMAL);
    }
    return dl;
}
Also used : Hashtable(java.util.Hashtable) DefaultListModel(com.codename1.ui.list.DefaultListModel)

Example 5 with ListModel

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

the class DefaultLookAndFeel method drawComboBox.

/**
 * {@inheritDoc}
 */
public void drawComboBox(Graphics g, List cb) {
    int border = 2;
    Style style = cb.getStyle();
    int leftPadding = style.getPaddingLeft(cb.isRTL());
    int rightPadding = style.getPaddingRight(cb.isRTL());
    setFG(g, cb);
    ListModel model = cb.getModel();
    ListCellRenderer renderer = cb.getRenderer();
    Object value = model.getItemAt(model.getSelectedIndex());
    int comboImageWidth;
    if (comboImage != null) {
        comboImageWidth = comboImage.getWidth();
    } else {
        comboImageWidth = style.getFont().getHeight();
    }
    int cellX = cb.getX() + style.getPaddingTop();
    if (cb.isRTL()) {
        cellX += comboImageWidth;
    }
    if (model.getSize() > 0) {
        Component cmp = renderer.getListCellRendererComponent(cb, value, model.getSelectedIndex(), cb.hasFocus());
        cmp.setX(cellX);
        cmp.setY(cb.getY() + style.getPaddingTop());
        cmp.setWidth(cb.getWidth() - comboImageWidth - rightPadding - leftPadding);
        cmp.setHeight(cb.getHeight() - style.getPaddingTop() - style.getPaddingBottom());
        cmp.paint(g);
    }
    g.setColor(style.getBgColor());
    int y = cb.getY();
    int height = cb.getHeight();
    int width = comboImageWidth + border;
    int x = cb.getX();
    if (cb.isRTL()) {
        x += leftPadding;
    } else {
        x += cb.getWidth() - comboImageWidth - rightPadding;
    }
    if (comboImage != null) {
        g.drawImage(comboImage, x, y + height / 2 - comboImage.getHeight() / 2);
    } else {
        int color = g.getColor();
        // brighten or darken the color slightly
        int destColor = findDestColor(color);
        g.fillLinearGradient(g.getColor(), destColor, x, y, width, height, false);
        g.setColor(color);
        g.drawRect(x, y, width, height - 1);
        width--;
        height--;
        // g.drawRect(x, y, width, height);
        g.translate(x + 1, y + 1);
        g.setColor(0x111111);
        int x1 = scaleCoordinate(2.5652081f, 16, width);
        int y1 = scaleCoordinate(4.4753664f, 16, height);
        int x2 = scaleCoordinate(8.2872691f, 16, width);
        int y2 = scaleCoordinate(10f, 16, height);
        int x3 = scaleCoordinate(13.516078f, 16, width);
        int y3 = y1;
        g.fillTriangle(x1, y1, x2, y2, x3, y3);
        g.translate(-1, -1);
        g.setColor(style.getFgColor());
        g.fillTriangle(x1, y1, x2, y2, x3, y3);
        // g.setColor(style.getFgColor());
        // g.fillTriangle(x1 + 2, y1 + 2, x2, y2 - 2, x3 - 2, y3 + 2);
        g.translate(-x, -y);
    }
}
Also used : ListModel(com.codename1.ui.list.ListModel) ListCellRenderer(com.codename1.ui.list.ListCellRenderer) Component(com.codename1.ui.Component)

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