Search in sources :

Example 56 with File

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

the class CodenameOneImplementation method setBrowserPageInHierarchy.

/**
 * Sets a relative URL from the html hierarchy
 *
 * @param browserPeer the peer component
 * @param url the url relative to the HTML directory
 */
public void setBrowserPageInHierarchy(PeerComponent browserPeer, String url) throws IOException {
    installTar();
    FileSystemStorage fs = FileSystemStorage.getInstance();
    String tardir = fs.getAppHomePath() + "cn1html";
    if (tardir.startsWith("/")) {
        tardir = "file://" + tardir;
    }
    if (url.startsWith("/")) {
        setBrowserURL(browserPeer, tardir + url);
    } else {
        setBrowserURL(browserPeer, tardir + "/" + url);
    }
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage)

Example 57 with File

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

the class CodenameOneImplementation method installTar.

/**
 * Installs a tar file from the build server into the file system storage so it can be used with respect for hierarchy
 */
public void installTar() throws IOException {
    String p = Preferences.get("cn1$InstallKey", null);
    String buildKey = Display.getInstance().getProperty("build_key", null);
    if (p == null || !p.equals(buildKey)) {
        FileSystemStorage fs = FileSystemStorage.getInstance();
        String tardir = fs.getAppHomePath() + "cn1html/";
        fs.mkdir(tardir);
        TarInputStream is = new TarInputStream(Display.getInstance().getResourceAsStream(getClass(), "/html.tar"));
        TarEntry t = is.getNextEntry();
        byte[] data = new byte[8192];
        while (t != null) {
            String name = t.getName();
            if (t.isDirectory()) {
                fs.mkdir(tardir + name);
            } else {
                String path = tardir + name;
                String dir = path.substring(0, path.lastIndexOf('/'));
                if (!fs.exists(dir)) {
                    mkdirs(fs, dir);
                }
                OutputStream os = fs.openOutputStream(tardir + name);
                int count;
                while ((count = is.read(data)) != -1) {
                    os.write(data, 0, count);
                }
                os.close();
            }
            t = is.getNextEntry();
        }
        Util.cleanup(is);
        Preferences.set("cn1$InstallKey", buildKey);
    }
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage) TarInputStream(com.codename1.io.tar.TarInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) TarEntry(com.codename1.io.tar.TarEntry)

Example 58 with File

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

the class CodenameOneImplementation method downloadImageToCache.

/**
 * Downloads an image from a URL to the cache. Platforms
 * that support a native image cache {@link #supportsNativeImageCache() } (e.g. Javascript) override this method to defer to the
 * platform's handling of cached images.  Platforms that have a caches directory ({@link FileSystemStorage#hasCachesDir() }
 * will use that directory to cache the image.  Other platforms will just download to storage.
 *
 * @param url The URL of the image to download.
 * @param onSuccess Callback on success.
 * @param onFail Callback on fail.
 *
 * @see URLImage#createToCache(com.codename1.ui.EncodedImage, java.lang.String, com.codename1.ui.URLImage.ImageAdapter)
 */
public void downloadImageToCache(String url, SuccessCallback<Image> onSuccess, final FailureCallback<Image> onFail) {
    FileSystemStorage fs = FileSystemStorage.getInstance();
    if (fs.hasCachesDir()) {
        String name = "cn1_image_cache[" + url + "]";
        name = StringUtil.replaceAll(name, "/", "_");
        name = StringUtil.replaceAll(name, "\\", "_");
        name = StringUtil.replaceAll(name, "%", "_");
        name = StringUtil.replaceAll(name, "?", "_");
        name = StringUtil.replaceAll(name, "*", "_");
        name = StringUtil.replaceAll(name, ":", "_");
        name = StringUtil.replaceAll(name, "=", "_");
        String filePath = fs.getCachesDir() + fs.getFileSystemSeparator() + name;
        // We use Util.downloadImageToFileSystem rather than CodenameOneImplementation.downloadImageToFileSystem
        // because we want it to try to load from file system first.
        Util.downloadImageToFileSystem(url, filePath, onSuccess, onFail);
    } else {
        // We use Util.downloadImageToStorage rather than CodenameOneImplementation.downloadImageToStorage
        // because we want it to try to load from storage first.
        Util.downloadImageToStorage(url, "cn1_image_cache[" + url + "]", onSuccess, onFail);
    }
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage)

Example 59 with File

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

the class UIManager method initNamedTheme.

/**
 * Same as the initFirstTheme method, but unlike that method this allows specifying the theme resource name
 * @param resourceFile the name of the resource file starting with / and without the res extension
 * @param resName the name of the theme to use from the file if it contains more than one theme
 * @return the resource file or null in case of a failure
 */
public static Resources initNamedTheme(String resourceFile, String resName) {
    try {
        Resources theme = Resources.openLayered(resourceFile);
        UIManager.getInstance().setThemeProps(theme.getTheme(resName));
        Resources.setGlobalResources(theme);
        return theme;
    } catch (IOException e) {
        Log.e(e);
    }
    return null;
}
Also used : Resources(com.codename1.ui.util.Resources) IOException(java.io.IOException)

Example 60 with File

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

the class UIManager method buildTheme.

private void buildTheme(Hashtable themeProps) {
    String con = (String) themeProps.get("@includeNativeBool");
    if (con != null && con.equalsIgnoreCase("true") && Display.getInstance().hasNativeTheme()) {
        boolean a = accessible;
        accessible = true;
        Display.getInstance().installNativeTheme();
        accessible = a;
    }
    Enumeration e = themeProps.keys();
    while (e.hasMoreElements()) {
        String key = (String) e.nextElement();
        // this is a constant not a theme entry
        if (key.startsWith("@")) {
            themeConstants.put(key.substring(1, key.length()), themeProps.get(key));
            continue;
        }
        this.themeProps.put(key, themeProps.get(key));
    }
    if (!this.themeProps.containsKey("PickerButtonBar.derive")) {
        // For the button bar (with Cancel and Done) of the Picker interaction dialog in lightweight mode
        if (this.themeProps.containsKey("PickerButtonBarNative.derive")) {
            this.themeProps.put("PickerButtonBar.derive", "PickerButtonBarNative");
        } else {
            this.themeProps.put("PickerButtonBar.margin", "0,0,0,0");
            this.themeProps.put("PickerButtonBar.border", Border.createCompoundBorder(Border.createLineBorder(1, ColorUtil.rgb(148, 150, 151)), Border.createEmpty(), Border.createEmpty(), Border.createEmpty()));
            this.themeProps.put("PickerButtonBar.bgColor", "F0F1F3");
            this.themeProps.put("PickerButtonBar.transparency", "255");
        }
    }
    if (!this.themeProps.containsKey("PickerButtonTablet.derive")) {
        // For the buttons of the picker in lightweight mode (the Cancel and Done buttons)
        if (this.themeProps.containsKey("PickerButtonTabletNative.derive")) {
            this.themeProps.put("PickerButtonTablet.derive", "PickerButtonTabletNative");
            this.themeProps.put("PickerButtonTablet.sel#derive", "PickerButtonTabletNative.sel");
            this.themeProps.put("PickerButtonTablet.press#derive", "PickerButtonTabletNative.press");
        } else {
            this.themeProps.put("PickerButtonTablet.derive", "Button");
            this.themeProps.put("PickerButtonTablet.sel#derive", "Button");
            this.themeProps.put("PickerButtonTablet.press#derive", "Button");
        }
    }
    // necessary to clear up the style so we don't get resedue from the previous UI
    defaultStyle = new Style();
    // create's the default style
    defaultStyle = createStyle("", "", false);
    defaultSelectedStyle = new Style(defaultStyle);
    defaultSelectedStyle = createStyle("", "sel#", true);
    String overlayThemes = (String) themeProps.get("@OverlayThemes");
    if (overlayThemes != null) {
        java.util.List<String> overlayThemesArr = StringUtil.tokenize(overlayThemes, ',');
        for (String th : overlayThemesArr) {
            th = th.trim();
            if (th.length() == 0) {
                continue;
            }
            try {
                Resources res = Resources.openLayered("/" + th);
                boolean a = accessible;
                accessible = true;
                addThemeProps(res.getTheme(res.getThemeResourceNames()[0]));
                accessible = a;
            } catch (Exception ex) {
                System.err.println("Failed to load overlay theme file specified by @overlayThemes theme constant: " + th);
                Log.e(ex);
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) Resources(com.codename1.ui.util.Resources) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)76 File (java.io.File)61 FileInputStream (java.io.FileInputStream)43 InputStream (java.io.InputStream)33 EncodedImage (com.codename1.ui.EncodedImage)23 FileOutputStream (java.io.FileOutputStream)23 OutputStream (java.io.OutputStream)22 SortedProperties (com.codename1.ant.SortedProperties)18 FileSystemStorage (com.codename1.io.FileSystemStorage)17 BufferedInputStream (com.codename1.io.BufferedInputStream)15 Image (com.codename1.ui.Image)15 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ActionEvent (com.codename1.ui.events.ActionEvent)14 RandomAccessFile (java.io.RandomAccessFile)14 ArrayList (java.util.ArrayList)14 EditableResources (com.codename1.ui.util.EditableResources)13 InvocationTargetException (java.lang.reflect.InvocationTargetException)13 Properties (java.util.Properties)12 File (com.codename1.io.File)11 BufferedImage (java.awt.image.BufferedImage)11