Search in sources :

Example 1 with Resource

use of me.coley.recaf.util.Resource in project Recaf by Col-E.

the class Themes method get.

private static Collection<Resource> get(String prefix, String suffix) {
    List<Resource> resources = new ArrayList<>();
    File styleDirectory = Recaf.getDirectory("style").toFile();
    if (!styleDirectory.exists())
        styleDirectory.mkdir();
    for (File file : styleDirectory.listFiles()) {
        String name = file.getName();
        if (name.startsWith(prefix) && name.endsWith(suffix))
            resources.add(Resource.external(file.getPath().replace('\\', '/')));
    }
    return resources;
}
Also used : Resource(me.coley.recaf.util.Resource) ArrayList(java.util.ArrayList) File(java.io.File)

Example 2 with Resource

use of me.coley.recaf.util.Resource in project Recaf by Col-E.

the class Configurable method save.

/**
 * @param path
 * 		Path to json file of config.
 *
 * @throws IOException
 * 		When the file cannot be written to.
 */
@SuppressWarnings("rawtypes")
default void save(Path path) throws IOException {
    JsonObject json = Json.object();
    for (FieldWrapper field : getConfigFields()) {
        String name = field.key();
        if (name == null)
            continue;
        Object value = field.get();
        if (value == null)
            continue;
        Class<?> type = field.type();
        if (type.equals(Boolean.TYPE))
            json.set(name, (boolean) value);
        else if (type.equals(Integer.TYPE))
            json.set(name, (int) value);
        else if (type.equals(Long.TYPE))
            json.set(name, (long) value);
        else if (type.equals(Float.TYPE))
            json.set(name, (float) value);
        else if (type.equals(Double.TYPE))
            json.set(name, (double) value);
        else if (type.equals(String.class))
            json.set(name, (String) value);
        else if (type.isEnum())
            json.set(name, ((Enum) value).name());
        else if (type.equals(Resource.class)) {
            Resource resource = (Resource) value;
            JsonObject object = new JsonObject();
            object.set("path", resource.getPath());
            object.set("internal", resource.isInternal());
            json.set(name, object);
        } else if (type.equals(List.class)) {
            JsonArray array = Json.array();
            List<?> list = field.get();
            // Don't write if empty/null
            if (list == null || list.isEmpty())
                continue;
            // We're gonna assume our lists just hold strings
            // TODO: Proper generic list writing
            list.forEach(v -> array.add(v.toString()));
            json.set(name, array);
        } else if (supported(type))
            saveType(field, type, value, json);
        else
            warn("Didn't write config for {}, unsure how to serialize type {}.", name, type.getName());
    }
    StringWriter w = new StringWriter();
    json.writeTo(w, WriterConfig.PRETTY_PRINT);
    FileUtils.write(path.toFile(), w.toString(), StandardCharsets.UTF_8);
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) StringWriter(java.io.StringWriter) Resource(me.coley.recaf.util.Resource) JsonObject(com.eclipsesource.json.JsonObject) JsonObject(com.eclipsesource.json.JsonObject)

Example 3 with Resource

use of me.coley.recaf.util.Resource in project Recaf by Col-E.

the class SelfReferenceUtil method getFiles.

/**
 * @param prefix
 *            File prefix to match.
 * @param suffix
 *            File suffix to match <i>(such as a file extension)</i>.
 * @return List of matching files.
 */
private List<Resource> getFiles(String prefix, String suffix) {
    List<Resource> list = new ArrayList<>();
    if (isJar()) {
        // Read self as jar
        try (ZipFile file = new ZipFile(getFile())) {
            Enumeration<? extends ZipEntry> entries = file.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                // skip directories
                if (entry.isDirectory())
                    continue;
                String name = entry.getName();
                if (prefix != null && !name.startsWith(prefix))
                    continue;
                if (suffix != null && !name.endsWith(suffix))
                    continue;
                list.add(Resource.internal(name));
            }
        } catch (Exception ex) {
            error(ex, "Failed internal file (archive) lookup: {}", getFile());
        }
    } else {
        // Read self as file directory
        Path dir = getFile().toPath();
        try {
            Files.walk(dir).forEach(p -> {
                File file = dir.relativize(p).toFile();
                String path = file.getPath().replace('\\', '/');
                if (prefix != null && !path.startsWith(prefix))
                    return;
                if (suffix != null && !path.endsWith(suffix))
                    return;
                list.add(Resource.internal(path));
            });
        } catch (IOException ex) {
            error(ex, "Failed internal file (directory) lookup: {}", getFile());
        }
    }
    return list;
}
Also used : Path(java.nio.file.Path) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) Resource(me.coley.recaf.util.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 4 with Resource

use of me.coley.recaf.util.Resource in project Recaf by Col-E.

the class WindowManager method reapplyStyle.

/**
 * Applies the current style to the given scene.
 *
 * @param scene
 * 		Scene to reapply styles to.
 */
public void reapplyStyle(Scene scene) {
    Resource appStyle = controller.config().display().appStyle;
    Resource textStyle = controller.config().display().textStyle;
    Resource[] fallbacks = new Resource[] { Resource.internal("/style/base.css"), Resource.internal("/style/ui-default.css"), Resource.internal("/style/text-default.css") };
    Resource[] paths = new Resource[] { fallbacks[0], appStyle, textStyle };
    // Clear, then reapply sheets
    scene.getStylesheets().clear();
    for (int i = 0; i < paths.length; i++) {
        Resource resource = paths[i];
        if (resource.isInternal()) {
            // Load internal stylesheet
            String path = resource.getPath();
            if (resourceExists(path))
                scene.getStylesheets().add(path);
            else
                scene.getStylesheets().add(fallbacks[i].getPath());
        } else {
            // Load external stylesheet
            try {
                URL url = resource.getURL();
                if (new File(url.getFile()).exists())
                    scene.getStylesheets().add(url.toString());
                else
                    throw new IOException("Failed to load CSS: " + url);
            } catch (IOException ex) {
                scene.getStylesheets().add(fallbacks[i].getPath());
            }
        }
    }
    addFontSizeStyleSheet(scene);
    addMonoFontStyleSheet(scene);
}
Also used : Resource(me.coley.recaf.util.Resource) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL)

Aggregations

Resource (me.coley.recaf.util.Resource)4 File (java.io.File)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 JsonArray (com.eclipsesource.json.JsonArray)1 JsonObject (com.eclipsesource.json.JsonObject)1 StringWriter (java.io.StringWriter)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1