Search in sources :

Example 66 with Label

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

the class ImageDownloadService method createImageToFileSystem.

/**
 * Constructs an image request that will automatically populate the given Label
 * when the response arrives, it will cache the file locally.
 *
 * @param url the image URL
 * @param callback the callback that should be updated when the data arrives
 * @param destFile local file to store the data into the given path
 */
public static void createImageToFileSystem(String url, ActionListener callback, String destFile) {
    Image im = cacheImage(null, false, destFile, null, null, defaultMaintainAspectRatio);
    if (im != null) {
        callback.actionPerformed(new NetworkEvent(null, im));
        return;
    }
    // image not found on cache go and download from the url
    ImageDownloadService i = new ImageDownloadService(url, callback);
    i.cacheImages = true;
    i.destinationFile = destFile;
    i.setFailSilently(true);
    NetworkManager.getInstance().addToQueue(i);
}
Also used : NetworkEvent(com.codename1.io.NetworkEvent) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 67 with Label

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

the class ImageDownloadService method readResponse.

/**
 * {@inheritDoc}
 */
protected void readResponse(InputStream input) throws IOException {
    int imageScaleWidth = -1, imageScaleHeight = -1;
    if (fastScale) {
        if (toScale != null) {
            imageScaleWidth = toScale.getWidth();
            imageScaleHeight = toScale.getHeight();
        } else {
            if (placeholder != null) {
                imageScaleWidth = placeholder.getWidth();
                imageScaleHeight = placeholder.getHeight();
            }
        }
    }
    if (cacheImages) {
        if (destinationFile != null) {
            result = FileEncodedImage.create(destinationFile, input, imageScaleWidth, imageScaleHeight);
        } else {
            EncodedImage e = EncodedImage.create(input);
            if (maintainAspectRatio) {
                float actualW = e.getWidth();
                float actualH = e.getHeight();
                float r2 = Math.min(((float) imageScaleWidth) / actualW, ((float) imageScaleHeight) / actualH);
                imageScaleWidth = (int) (actualW * r2);
                imageScaleHeight = (int) (actualH * r2);
            }
            // workaround for http://stackoverflow.com/questions/35347353/label-image-scale-issue-in-codename-one-library-3-3/35354605
            if (imageScaleWidth > -1 || imageScaleHeight > -1) {
                e = e.scaledEncoded(imageScaleWidth, imageScaleHeight);
            }
            result = StorageImage.create(cacheId, e.getImageData(), imageScaleWidth, imageScaleHeight, keep);
            // if the storage has failed create the image from the stream
            if (result == null) {
                result = e;
            }
        }
    } else {
        result = EncodedImage.create(input);
    }
}
Also used : EncodedImage(com.codename1.ui.EncodedImage) FileEncodedImage(com.codename1.components.FileEncodedImage)

Example 68 with Label

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

the class ImageDownloadService method createImageToStorage.

/**
 * Constructs an image request that will automatically populate the given Label
 * when the response arrives, it will cache the file locally.
 *
 * @param url the image URL
 * @param callback the callback that should be updated when the data arrives
 * @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
 */
public static void createImageToStorage(String url, ActionListener callback, String cacheId, boolean keep) {
    Image im = cacheImage(cacheId, keep, null, null, null, defaultMaintainAspectRatio);
    if (im != null) {
        callback.actionPerformed(new NetworkEvent(null, im));
        return;
    }
    // image not found on cache go and download from the url
    ImageDownloadService i = new ImageDownloadService(url, callback);
    i.cacheImages = true;
    i.cacheId = cacheId;
    i.setFailSilently(true);
    NetworkManager.getInstance().addToQueue(i);
}
Also used : NetworkEvent(com.codename1.io.NetworkEvent) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 69 with Label

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

the class ImageDownloadService method createImageToStorage.

/**
 * Constructs an image request that will automatically populate the given Label
 * when the response arrives, it will cache the file locally to the Storage
 *
 * @param url the image URL
 * @param l the Label that should be updated when the data arrives
 * to just use storage and the url as the key
 * @param cacheId a unique identifier to be used to store the image into storage
 * @param toScale the scale dimension or null
 * @param priority the priority for the task
 */
private static void createImageToStorage(final String url, final Label l, final String cacheId, final boolean keep, final Dimension toScale, final byte priority, final Image placeholder, final boolean maintainAspectRatio) {
    if (Display.getInstance().isEdt()) {
        Display.getInstance().scheduleBackgroundTask(new Runnable() {

            public void run() {
                createImageToStorage(url, l, cacheId, keep, toScale, priority, placeholder, maintainAspectRatio);
            }
        });
        return;
    }
    Image im = cacheImage(cacheId, keep, null, toScale, placeholder, maintainAspectRatio);
    if (im != null) {
        if (!fastScale && toScale != null) {
            im = scaleImage(im, toScale, defaultMaintainAspectRatio);
        }
        final Image i = im;
        Display.getInstance().callSerially(new Runnable() {

            public void run() {
                l.setIcon(i);
                Form f = l.getComponentForm();
                if (f != null) {
                    f.revalidate();
                }
            }
        });
        return;
    }
    // image not found on cache go and download from the url
    ImageDownloadService i = new ImageDownloadService(url, l);
    i.maintainAspectRatio = maintainAspectRatio;
    i.setDuplicateSupported(true);
    i.cacheImages = true;
    i.toScale = toScale;
    i.cacheId = cacheId;
    i.placeholder = placeholder;
    i.setPriority(priority);
    i.setFailSilently(true);
    NetworkManager.getInstance().addToQueue(i);
}
Also used : Form(com.codename1.ui.Form) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 70 with Label

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

the class Dialog method initImpl.

private void initImpl(String dialogUIID, String dialogTitleUIID, Layout lm) {
    super.getContentPane().setUIID(dialogUIID);
    super.getTitleComponent().setText("");
    super.getTitleComponent().setVisible(false);
    super.getTitleArea().setVisible(false);
    super.getTitleArea().setUIID("Container");
    lockStyleImages(getUnselectedStyle());
    titleArea.setVisible(false);
    if (lm != null) {
        dialogContentPane = new Container(lm);
    } else {
        dialogContentPane = new Container();
    }
    dialogContentPane.setUIID("DialogContentPane");
    dialogTitle = new Label("", dialogTitleUIID);
    super.getContentPane().setLayout(new BorderLayout());
    super.getContentPane().addComponent(BorderLayout.NORTH, dialogTitle);
    super.getContentPane().addComponent(BorderLayout.CENTER, dialogContentPane);
    super.getContentPane().setScrollable(false);
    super.getContentPane().setAlwaysTensile(false);
    super.getStyle().setBgTransparency(0);
    super.getStyle().setBgImage(null);
    super.getStyle().setBorder(null);
    setSmoothScrolling(false);
    deregisterAnimated(this);
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout)

Aggregations

Label (com.codename1.ui.Label)41 BorderLayout (com.codename1.ui.layouts.BorderLayout)22 Container (com.codename1.ui.Container)21 Component (com.codename1.ui.Component)16 Form (com.codename1.ui.Form)15 Button (com.codename1.ui.Button)14 TextArea (com.codename1.ui.TextArea)14 Style (com.codename1.ui.plaf.Style)13 ActionListener (com.codename1.ui.events.ActionListener)12 ActionEvent (com.codename1.ui.events.ActionEvent)11 Image (com.codename1.ui.Image)10 Dimension (com.codename1.ui.geom.Dimension)10 Vector (java.util.Vector)10 BoxLayout (com.codename1.ui.layouts.BoxLayout)9 EncodedImage (com.codename1.ui.EncodedImage)8 RadioButton (com.codename1.ui.RadioButton)7 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)6 Hashtable (java.util.Hashtable)6 Paint (com.codename1.charts.compat.Paint)5 Font (com.codename1.ui.Font)5