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
Conflicts:
	org.jrebirth.af/core/src/main/java/org/jrebirth/af/core/resource/provided/JRebirthParameters.java
	org.jrebirth.af/core/src/test/java/org/jrebirth/af/core/resource/image/ImageTest.java
  • Loading branch information
sbordes committed Aug 26, 2014
1 parent a32b632 commit 496c1f5
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 34 deletions.
Expand Up @@ -17,6 +17,7 @@
*/
package org.jrebirth.af.core.resource.font;

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

import javafx.scene.text.Font;
Expand Down Expand Up @@ -114,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,6 +18,7 @@
package org.jrebirth.af.core.resource.image;

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

import javafx.scene.image.Image;

Expand Down Expand Up @@ -53,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 @@ -111,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 @@ -21,6 +21,7 @@
import java.util.List;

import org.jrebirth.af.core.facade.factory.DefaultComponentFactory;
import org.jrebirth.af.core.facade.DefaultComponentFactory;
import org.jrebirth.af.core.link.DefaultUnprocessedWaveHandler;
import org.jrebirth.af.core.resource.color.WebColor;
import org.jrebirth.af.core.resource.image.ImageExtension;
Expand Down
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -64,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 @@ -88,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,10 +1,15 @@
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 @@ -27,7 +32,7 @@ public static void setUpBeforeClass() throws Exception {

@Before
public void setUp() throws Exception {

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

@Test
Expand All @@ -36,6 +41,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 @@ -38,6 +38,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,7 +35,7 @@ applicationSceneBgColor=000000||0.0

trueTypeFontExtension=.ttf
fontsFolder=fonts
imagesFolder=images
imagesFolder=images,imagesBis
stylesFolder=styles
defaultStyleSheet=default

Expand Down
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 496c1f5

Please sign in to comment.