Skip to content

Commit

Permalink
Fix #122 Manage multiple Image, Style and font folders location
Browse files Browse the repository at this point in the history
  • Loading branch information
sbordes committed Jul 16, 2014
1 parent 90c943c commit 626a395
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 42 deletions.
Expand Up @@ -17,14 +17,14 @@
*/
package org.jrebirth.af.core.resource.font;

import java.net.URL;
import java.util.List;

import javafx.scene.text.Font;

import org.jrebirth.af.core.resource.Resources;
import org.jrebirth.af.core.resource.builder.AbstractResourceBuilder;
import org.jrebirth.af.core.resource.provided.JRebirthParameters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -115,34 +115,44 @@ private void checkFontStatus(final FontParams realFont) {
// Try to load system fonts
final List<String> fonts = Font.getFontNames(transformFontName(realFont.name().name()));

Font font;
Font font = null;
String fontName = null;

if (fonts.isEmpty()) {

// This variable will hold the 2 alternative font names
String fontName = JRebirthParameters.FONT_FOLDER.get() + Resources.PATH_SEP + transformFontName(realFont.name().name()) + JRebirthParameters.TRUE_TYPE_FONT_EXTENSION.get();

LOGGER.trace("Try to load Transformed Font {}", fontName);
font = Font.loadFont(Thread.currentThread().getContextClassLoader().getResourceAsStream(fontName), realFont.size());

// The font name contains '_' in its file name to replace ' '
if (font == null) {
fontName = JRebirthParameters.FONT_FOLDER.get() + Resources.PATH_SEP + realFont.name().name() + JRebirthParameters.TRUE_TYPE_FONT_EXTENSION.get();
LOGGER.trace("Try to load Raw Font {}", fontName);
font = Font.loadFont(
Thread.currentThread().getContextClassLoader().getResourceAsStream(fontName), realFont.size());


List<String> fontPaths = JRebirthParameters.FONT_FOLDER.get();
for(int i = 0 ; i < fontPaths.size() && font == null ;i++){

String fontPath = fontPaths.get(i);

// This variable will hold the 2 alternative font names
fontName = fontPath + Resources.PATH_SEP + transformFontName(realFont.name().name()) + JRebirthParameters.TRUE_TYPE_FONT_EXTENSION.get();

LOGGER.trace("Try to load Transformed Font {}", fontName);
font = Font.loadFont(Thread.currentThread().getContextClassLoader().getResourceAsStream(fontName), realFont.size());

// The font name contains '_' in its file name to replace ' '
if (font == null) {
// Neither transformed nor raw font has been loaded (with or without '_')
LOGGER.error("Font Not Found {}", fontName);
fontName = JRebirthParameters.FONT_FOLDER.get() + Resources.PATH_SEP + realFont.name().name() + JRebirthParameters.TRUE_TYPE_FONT_EXTENSION.get();
LOGGER.trace("Try to load Raw Font {}", fontName);
font = Font.loadFont(
Thread.currentThread().getContextClassLoader().getResourceAsStream(fontName), realFont.size());

if (font != null) {
// Raw font has been loaded
LOGGER.info("{} Raw Font loaded", fontName);
}
} else {
// Raw font has been loaded
LOGGER.info("{} Raw Font loaded", fontName);
// Transformed font has been loaded
LOGGER.info("{} Transformed Font loaded", fontName);
}
} else {
// Transformed font has been loaded
LOGGER.info("{} Transformed Font loaded", fontName);

}

if (font == null) {
// Neither transformed nor raw font has been loaded (with or without '_')
LOGGER.error("Font : {} not found into base folder: {}", realFont.name().name(), JRebirthParameters.FONT_FOLDER.get());
}
}
}
}
Expand Up @@ -18,14 +18,14 @@
package org.jrebirth.af.core.resource.image;

import java.io.InputStream;
import java.util.List;

import javafx.scene.image.Image;

import org.jrebirth.af.core.resource.Resources;
import org.jrebirth.af.core.resource.builder.AbstractResourceBuilder;
import org.jrebirth.af.core.resource.provided.JRebirthImages;
import org.jrebirth.af.core.resource.provided.JRebirthParameters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -54,10 +54,18 @@ protected Image buildResource(final ImageItem imageItem, final ImageParams jrIma
// Build the requested font
image = buildWebImage((WebImage) jrImage);
}
if (image == null) {

// Try to get the default image when an image is not found
if (image == null && !JRebirthParameters.NOT_AVAILABLE_IMAGE_NAME.equals(jrImage.name())) {
// Return the default image
image = JRebirthImages.NOT_AVAILABLE.get();
}

// Default image was not found
if(image == null){
//Build one programmatically TODO
}

return image;
}

Expand Down Expand Up @@ -112,12 +120,18 @@ private Image buildWebImage(final WebImage jrImage) {
*/
private Image loadImage(final String resourceName) {
Image image = null;
final InputStream imageInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(JRebirthParameters.IMAGE_FOLDER.get() + Resources.PATH_SEP + resourceName);
if (imageInputStream != null) {
image = new Image(imageInputStream);

List<String> imagePaths = JRebirthParameters.IMAGE_FOLDER.get();
for(int i = 0 ; i < imagePaths.size() && image == null ;i++){

String imagePath = imagePaths.get(i);
final InputStream imageInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(imagePath + Resources.PATH_SEP + resourceName);
if (imageInputStream != null) {
image = new Image(imageInputStream);
}
}
if (image == null) {
LOGGER.error("Image : {} not found into base folder: {}", resourceName, JRebirthParameters.IMAGE_FOLDER.get() + Resources.PATH_SEP);
LOGGER.error("Image : {} not found into base folder: {}", resourceName, JRebirthParameters.IMAGE_FOLDER.get());
}
return image;
}
Expand Down
Expand Up @@ -17,6 +17,7 @@
*/
package org.jrebirth.af.core.resource.parameter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -106,6 +107,8 @@ public Object parseObject(final ParameterEntry parameterEntry) {
res = this.object;
} else if (this.object instanceof Class<?>) {
res = parseClassParameter(parameterEntry.getSerializedString());
} else if (this.object instanceof List<?>) {
res = parseListParameter(parameterEntry.getSerializedString());
} else {
res = parsePrimitive(parameterEntry.getSerializedString());
}
Expand Down Expand Up @@ -134,6 +137,23 @@ private Object parseClassParameter(final String serializedObject) {
return res;
}

/**
* Parse a generic list.
*
* @param serializedObject the concatenated list
*
* @return the list object
*/
private Object parseListParameter(final String serializedObject) {
List<Object> res = new ArrayList<>();

for(String item : serializedObject.split(";")){
res.add(item);
}

return res;
}

/**
* Parse primitive serialized object.
*
Expand Down
Expand Up @@ -17,6 +17,9 @@
*/
package org.jrebirth.af.core.resource.provided;

import java.util.Collections;
import java.util.List;

import org.jrebirth.af.core.facade.DefaultComponentFactory;
import org.jrebirth.af.core.link.DefaultUnprocessedWaveHandler;
import org.jrebirth.af.core.resource.color.WebColor;
Expand All @@ -38,6 +41,9 @@ public interface JRebirthParameters {

/** The name of the AUTO_REFRESH parameter which is quite special because it modify how other parameters will be processed. */
String AUTO_REFRESH_NAME = "autoRefreshResource";

/** The image name of the NOT_AVAILABLE_IMAGE parameter, this image can lead to StackOverFlowError when it was not available. */
String NOT_AVAILABLE_IMAGE_NAME = "NotAvailableImage";

/**************************************************************************************/
/** __________________________Application Core Parameters.___________________________ */
Expand Down Expand Up @@ -84,18 +90,18 @@ public interface JRebirthParameters {
/**************************************************************************************/

/** Fonts default folder, Multiple folder can be managed by separating them with a comma ','. */
ParameterItem<String> FONT_FOLDER = create("fontsFolder", "fonts");
ParameterItem<List<String>> FONT_FOLDER = create("fontsFolder", Collections.singletonList("fonts"));

/** Images default folder, Multiple folder can be managed by separating them with a comma ','. */
ParameterItem<String> IMAGE_FOLDER = create("imagesFolder", "images");
ParameterItem<List<String>> IMAGE_FOLDER = create("imagesFolder", Collections.singletonList("images"));

/**
* The <code>NOT_AVAILABLE_IMAGE</code> field is used to define the image to use when an image is missing.
*/
ParameterItem<LocalImage> NOT_AVAILABLE_IMAGE = create("notAvailableImage", new LocalImage("NotAvailableImage", ImageExtension.PNG));
ParameterItem<LocalImage> NOT_AVAILABLE_IMAGE = create("notAvailableImage", new LocalImage(NOT_AVAILABLE_IMAGE_NAME, ImageExtension.PNG));

/** Styles default folder, Multiple folder can be managed by separating them with a comma ','. */
ParameterItem<String> STYLE_FOLDER = create("stylesFolder", "styles");
ParameterItem<List<String>> STYLE_FOLDER = create("stylesFolder", Collections.singletonList("styles"));

/**
* The <code>DEFAULT_CSS</code> field is used to parameterize the name of the default style sheet.
Expand Down
Expand Up @@ -18,11 +18,11 @@
package org.jrebirth.af.core.resource.style;

import java.net.URL;
import java.util.List;

import org.jrebirth.af.core.resource.Resources;
import org.jrebirth.af.core.resource.builder.AbstractResourceBuilder;
import org.jrebirth.af.core.resource.provided.JRebirthParameters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -65,8 +65,6 @@ private URL buildStyleSheetUrl(final StyleSheet ss) {

final StringBuilder sb = new StringBuilder();

sb.append(JRebirthParameters.STYLE_FOLDER.get()).append(Resources.PATH_SEP);

if (!ss.path().isEmpty()) {
sb.append(ss.path()).append(Resources.PATH_SEP);
}
Expand All @@ -89,10 +87,18 @@ private URL buildStyleSheetUrl(final StyleSheet ss) {
*/
private URL buildUrl(final String styleSheetPath) {

final URL cssResource = Thread.currentThread().getContextClassLoader().getResource(styleSheetPath);

URL cssResource = null;

List<String> stylePaths = JRebirthParameters.STYLE_FOLDER.get();
for(int i = 0 ; i < stylePaths.size() && cssResource == null ;i++){

String stylePath = stylePaths.get(i);

cssResource = Thread.currentThread().getContextClassLoader().getResource(stylePath + Resources.PATH_SEP + styleSheetPath);
}

if (cssResource == null) {
LOGGER.error("Style Sheet : " + styleSheetPath + " not found !");
LOGGER.error("Style Sheet : {} not found into base folder: {}", styleSheetPath , JRebirthParameters.STYLE_FOLDER.get());
}
return cssResource;
}
Expand Down
@@ -1,12 +1,14 @@
package org.jrebirth.af.core.resource.image;

import java.util.Arrays;

import javafx.scene.image.Image;

import org.jrebirth.af.core.resource.AbstractBaseParams;
import org.jrebirth.af.core.resource.ResourceBuilders;
import org.jrebirth.af.core.resource.image.ImageItem;
import org.jrebirth.af.core.resource.image.LocalImage;

import org.jrebirth.af.core.resource.provided.JRebirthParameters;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
Expand All @@ -29,7 +31,7 @@ public static void setUpBeforeClass() throws Exception {

@Before
public void setUp() throws Exception {

JRebirthParameters.IMAGE_FOLDER.define(Arrays.asList("images", "imagesBis"));
}

@Test
Expand All @@ -38,6 +40,7 @@ public void localImage() {
checkLocalImage(TestImages.TEST_LOCAL_IMAGE_1);
checkLocalImage(TestImages.TEST_LOCAL_IMAGE_2);
checkLocalImage(TestImages.TEST_LOCAL_IMAGE_3);
checkLocalImage(TestImages.TEST_LOCAL_IMAGE_4);
}

private void checkLocalImage(final ImageItem imageItem) {
Expand Down
Expand Up @@ -43,6 +43,9 @@ public interface TestImages {

/** The local image. */
ImageItem TEST_LOCAL_IMAGE_3 = create(new LocalImage("logo", ImageExtension.PNG));

/** The local image. */
ImageItem TEST_LOCAL_IMAGE_4 = create(new LocalImage("logoBiss", ImageExtension.PNG));

/**************************************************************************************/
/** ___________________________________Web Image.____________________________________ */
Expand Down
Expand Up @@ -35,6 +35,6 @@ applicationSceneBgColor=000000||0.0

trueTypeFontExtension=.ttf
fontsFolder=fonts
imagesFolder=images
imagesFolder=images,imagesBis
stylesFolder=styles
defaultStyleSheet=default
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 626a395

Please sign in to comment.