Search in sources :

Example 61 with URL

use of com.codename1.io.URL in project CodenameOne by codenameone.

the class CN1Console method downloadGroovy.

private void downloadGroovy() throws IOException {
    try {
        String url = GROOVY_URL;
        if (url == null) {
            throw new RuntimeException("No Groovy URL found for this platform");
        }
        File groovyDir = new File(GROOVY_HOME);
        if (groovyDir.exists()) {
            delTree(groovyDir);
        }
        groovyDir.getParentFile().mkdirs();
        File groovyZip = new File(groovyDir.getParentFile(), "groovy.zip");
        downloadToFile(url, groovyZip);
        File tmpDir = new File(groovyZip.getParentFile(), "groovy.tmp." + System.currentTimeMillis());
        try {
            new UnzipUtility().unzip(groovyZip.getAbsolutePath(), tmpDir.getAbsolutePath());
            groovyDir.mkdir();
            File libDirTmp = findDir(tmpDir, "lib");
            File legalDirTmp = findDir(tmpDir, "licenses");
            if (libDirTmp == null || !libDirTmp.exists()) {
                throw new IOException("No lib dir found within Groovy zip");
            }
            if (legalDirTmp == null || !legalDirTmp.exists()) {
                throw new IOException("No legal dir found within Groovy zip");
            }
            libDirTmp.renameTo(new File(groovyDir, "lib"));
            legalDirTmp.renameTo(new File(groovyDir, "licenses"));
        } finally {
            delTree(tmpDir);
            groovyZip.delete();
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(CN1Console.class.getName()).log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) UnzipUtility(com.codename1.impl.javase.UnzipUtility) IOException(java.io.IOException) File(java.io.File)

Example 62 with URL

use of com.codename1.io.URL in project CodenameOne by codenameone.

the class JavaSEPort method loadSkinFile.

private void loadSkinFile(String f, JFrame frm) {
    try {
        File fsFile = new File(f);
        if (fsFile.exists()) {
            f = fsFile.toURI().toString();
        }
        if (f.contains("://") || f.startsWith("file:")) {
            try {
                // load Via URL loading
                loadSkinFile(new URL(f).openStream(), frm);
            } catch (FileNotFoundException ex) {
                String d = System.getProperty("dskin");
                loadSkinFile(d, frm);
                return;
            } catch (MalformedURLException ex) {
                loadSkinFile(getResourceAsStream(getClass(), DEFAULT_SKIN), frm);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else {
            InputStream is = getResourceAsStream(getClass(), f);
            if (is != null) {
                loadSkinFile(is, frm);
            } else {
                loadSkinFile(getResourceAsStream(getClass(), DEFAULT_SKIN), frm);
            }
        }
        Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
        pref.put("skin", f);
        addSkinName(f);
    } catch (Throwable t) {
        System.out.println("Failed loading the skin file: " + f);
        t.printStackTrace();
        Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
        pref.remove("skin");
    }
}
Also used : BufferedInputStream(com.codename1.io.BufferedInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) ZipInputStream(java.util.zip.ZipInputStream) AttributedString(java.text.AttributedString) Preferences(java.util.prefs.Preferences)

Example 63 with URL

use of com.codename1.io.URL 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 64 with URL

use of com.codename1.io.URL 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 65 with URL

use of com.codename1.io.URL 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)

Aggregations

IOException (java.io.IOException)33 InputStream (java.io.InputStream)18 ConnectionRequest (com.codename1.io.ConnectionRequest)12 ActionEvent (com.codename1.ui.events.ActionEvent)12 ByteArrayInputStream (java.io.ByteArrayInputStream)12 File (java.io.File)11 Form (com.codename1.ui.Form)10 OutputStream (java.io.OutputStream)10 Image (com.codename1.ui.Image)9 Vector (java.util.Vector)9 EncodedImage (com.codename1.ui.EncodedImage)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 URISyntaxException (java.net.URISyntaxException)7 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)5 RemoteException (android.os.RemoteException)5 BufferedInputStream (com.codename1.io.BufferedInputStream)5 FileSystemStorage (com.codename1.io.FileSystemStorage)5 MediaException (com.codename1.media.AsyncMedia.MediaException)5 ActionListener (com.codename1.ui.events.ActionListener)5 FileInputStream (java.io.FileInputStream)5