Search in sources :

Example 71 with List

use of com.codename1.ui.List 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 72 with List

use of com.codename1.ui.List 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 73 with List

use of com.codename1.ui.List in project CodenameOne by codenameone.

the class Purchase method loadReceipts.

/**
 * Fetches receipts from the IAP service so that we know we are dealing
 * with current data.  This method should be called before checking a
 * subscription expiry date so that any changes the user has made in the
 * store is reflected here (e.g. cancelling or renewing subscription).
 * @param ifOlderThanMs Update is only performed if more than {@code ifOlderThanMs} milliseconds has elapsed
 * since the last successful fetch.
 * @param callback Callback called when request is complete.  Passed {@code true} if
 * the data was successfully fetched.  {@code false} otherwise.
 */
private final void loadReceipts(long ifOlderThanMs, final SuccessCallback<Boolean> callback) {
    if (loadInProgress) {
        Log.p("Did not load receipts because another load is in progress");
        callback.onSucess(false);
        return;
    }
    loadInProgress = true;
    Date lastRefreshTime = getReceiptsRefreshTime();
    Date now = new Date();
    if (lastRefreshTime.getTime() + ifOlderThanMs > now.getTime()) {
        Log.p("Receipts were last refreshed at " + lastRefreshTime + " so we won't refetch.");
        loadInProgress = false;
        callback.onSucess(true);
        return;
    }
    List<Receipt> oldData = new ArrayList<Receipt>();
    oldData.addAll(getReceipts());
    SuccessCallback<Receipt[]> onSuccess = new SuccessCallback<Receipt[]>() {

        public void onSucess(Receipt[] value) {
            if (value != null) {
                setReceipts(Arrays.asList(value));
                setReceiptsRefreshTime(new Date());
                loadInProgress = false;
                callback.onSucess(Boolean.TRUE);
            } else {
                loadInProgress = false;
                callback.onSucess(Boolean.FALSE);
            }
        }
    };
    if (receiptStore != null) {
        receiptStore.fetchReceipts(onSuccess);
    } else {
        Log.p("No receipt store is currently registered so no receipts were fetched");
        loadInProgress = false;
        callback.onSucess(Boolean.FALSE);
    }
}
Also used : SuccessCallback(com.codename1.util.SuccessCallback) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 74 with List

use of com.codename1.ui.List in project CodenameOne by codenameone.

the class Purchase method getPendingPurchases.

/**
 * Gets a list of purchases that haven't yet been sent to the server.
 * @return
 */
private List<Receipt> getPendingPurchases() {
    synchronized (PENDING_PURCHASE_KEY) {
        Storage s = Storage.getInstance();
        Util.register(new Receipt());
        if (s.exists(PENDING_PURCHASE_KEY)) {
            return (List<Receipt>) s.readObject(PENDING_PURCHASE_KEY);
        } else {
            return new ArrayList<Receipt>();
        }
    }
}
Also used : Storage(com.codename1.io.Storage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 75 with List

use of com.codename1.ui.List in project CodenameOne by codenameone.

the class Purchase method addPendingPurchase.

/**
 * Adds a receipt to be pushed to the server.
 * @param receipt
 */
private void addPendingPurchase(Receipt receipt) {
    synchronized (PENDING_PURCHASE_KEY) {
        Storage s = Storage.getInstance();
        List<Receipt> pendingPurchases = getPendingPurchases();
        pendingPurchases.add(receipt);
        s.writeObject(PENDING_PURCHASE_KEY, pendingPurchases);
    }
}
Also used : Storage(com.codename1.io.Storage)

Aggregations

Paint (com.codename1.charts.compat.Paint)26 ArrayList (java.util.ArrayList)24 Component (com.codename1.ui.Component)16 List (com.codename1.ui.List)13 Point (com.codename1.charts.models.Point)11 BorderLayout (com.codename1.ui.layouts.BorderLayout)11 Hashtable (java.util.Hashtable)11 Container (com.codename1.ui.Container)9 ContainerList (com.codename1.ui.list.ContainerList)9 Button (com.codename1.ui.Button)8 List (java.util.List)8 TextArea (com.codename1.ui.TextArea)7 Dimension (com.codename1.ui.geom.Dimension)7 EncodedImage (com.codename1.ui.EncodedImage)6 Label (com.codename1.ui.Label)6 RadioButton (com.codename1.ui.RadioButton)5 ActionListener (com.codename1.ui.events.ActionListener)5 JList (javax.swing.JList)4 FileEncodedImage (com.codename1.components.FileEncodedImage)3 StorageImage (com.codename1.components.StorageImage)3