Skip to content

Commit

Permalink
Fix #122 Manage multiple loation for images, fonts and style
Browse files Browse the repository at this point in the history
Manage localimage with relative and absolute resource params

Conflicts:
	org.jrebirth.af/core/src/main/java/org/jrebirth/af/core/resource/font/FontBuilder.java
	org.jrebirth.af/core/src/main/java/org/jrebirth/af/core/resource/image/ImageBuilder.java
	org.jrebirth.af/core/src/main/java/org/jrebirth/af/core/resource/image/LocalImage.java
	org.jrebirth.af/core/src/main/java/org/jrebirth/af/core/resource/style/StyleSheetBuilder.java
	org.jrebirth.af/core/src/test/java/org/jrebirth/af/core/resource/image/TestImages.java
	org.jrebirth.af/presentation/src/main/java/org/jrebirth/af/presentation/ui/template/AbstractTemplateView.java
  • Loading branch information
sbordes committed Aug 26, 2014
1 parent 5890416 commit f58b6d7
Show file tree
Hide file tree
Showing 13 changed files with 994 additions and 710 deletions.
Expand Up @@ -17,7 +17,7 @@
*/
package org.jrebirth.af.core.resource.font;

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

import javafx.scene.text.Font;
Expand Down Expand Up @@ -96,9 +96,10 @@ private Font buildFamilyFont(final FamilyFont familyFont) {
}

/**
* TRansform the font name by replacing _ by space.
*
* Transform the font name by replacing _ by space.
*
* @param fontName the font name to transform
*
* @return the transformed font
*/
private String transformFontName(final String fontName) {
Expand All @@ -108,35 +109,40 @@ private String transformFontName(final String fontName) {
/**
* Load the font file.
*
* @param realFont the name of the font to load
* @param fontParams the name of the font to load
*/
private void checkFontStatus(final FontParams realFont) {
private void checkFontStatus(final FontParams fontParams) {

// Try to load system fonts
final List<String> fonts = Font.getFontNames(transformFontName(realFont.name().name()));
final List<String> fonts = Font.getFontNames(transformFontName(fontParams.name().name()));

Font font = null;
String fontName = null;

if (fonts.isEmpty()) {

List<String> fontPaths = JRebirthParameters.FONT_FOLDER.get();
List<String> fontPaths = (fontParams instanceof RealFont && ((RealFont)fontParams).skipFontsFolder())
? Collections.singletonList("")
: JRebirthParameters.FONT_FOLDER.get();

for(int i = 0 ; i < fontPaths.size() && font == null ;i++){

String fontPath = fontPaths.get(i);
if(!fontPath.isEmpty()){
fontPath += Resources.PATH_SEP;
}

// This variable will hold the 2 alternative font names
fontName = fontPath + Resources.PATH_SEP + transformFontName(realFont.name().name()) + JRebirthParameters.TRUE_TYPE_FONT_EXTENSION.get();
fontName = fontPath + transformFontName(fontParams.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());
font = Font.loadFont(Thread.currentThread().getContextClassLoader().getResourceAsStream(fontName), fontParams.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();
fontName = fontPath + fontParams.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());
font = Font.loadFont(Thread.currentThread().getContextClassLoader().getResourceAsStream(fontName), fontParams.size());

if (font != null) {
// Raw font has been loaded
Expand All @@ -151,7 +157,7 @@ private void checkFontStatus(final FontParams realFont) {

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());
LOGGER.error("Font : {} not found into base folder: {}", fontParams.name().name(), JRebirthParameters.FONT_FOLDER.get());
}
}
}
Expand Down
Expand Up @@ -20,21 +20,55 @@
import java.util.Arrays;
import java.util.List;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;

/**
* The interface <strong>RealFont</strong>.
*
* @author Sébastien Bordes
*/
public class RealFont extends AbstractBaseFont {

/** The flag used to skip the fontsFolder prefix addition. */
private BooleanProperty skipFontsFolderProperty = new SimpleBooleanProperty();

/**
* Default Constructor.
*
* @param name the font name
* @param size the default font size
*/
public RealFont(final FontName name, final double size, final boolean skipFontsFolder) {
super(name, size);
this.skipFontsFolderProperty.set(skipFontsFolder);
}
/**
* Default Constructor.
*
* @param name the font name
* @param size the default font size
*/
public RealFont(final FontName name, final double size) {
super(name, size);
this(name, size, false);
}

/**
* Return the skipFontsFolder flag.
*
* @return the skipFontsFolder flag
*/
public boolean skipFontsFolder() {
return this.skipFontsFolderProperty.get();
}

/**
* Return the skipFontsFolderProperty property.
*
* @return the skipFontsFolderProperty property
*/
public BooleanProperty skipFontsFolderProperty() {
return skipFontsFolderProperty;
}

/**
Expand All @@ -45,8 +79,11 @@ public void parse(final String[] parameters) {
if (parameters.length >= 1) {
nameProperty().set(new CustomFontName(parameters[0]));
}
if (parameters.length == 2) {
sizeProperty().set(Double.parseDouble(parameters[1]));
if (parameters.length >= 2) {
sizeProperty().set(readDouble(parameters[1], 1.0, 1000.0));
}
if (parameters.length == 3) {
skipFontsFolderProperty().set(readBoolean(parameters[2]));
}
}

Expand All @@ -55,7 +92,7 @@ public void parse(final String[] parameters) {
*/
@Override
protected List<? extends Object> getFieldValues() {
return Arrays.asList(name().toString(), size());
return Arrays.asList(name().toString(), size(), skipFontsFolder());
}

}
@@ -0,0 +1,87 @@
/**
* Get more info at : www.jrebirth.org .
* Copyright JRebirth.org © 2011-2013
* Contact : sebastien.bordes@jrebirth.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jrebirth.af.core.resource.image;

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

/**
* The interface <strong>LocalImage</strong>.
*
* @author Sébastien Bordes
*/
public class AbsImage extends AbstractBaseImage implements ImageParams {

/**
* Default Constructor.
*
* @param path the image local path
* @param name the file name
* @param extension the image extension
*/
public AbsImage(final String path, final String name, final ImageExtension extension) {
super(path, name, extension);
}

/**
* Default Constructor.
*
* @param name the file name
* @param extension the image extension
*/
public AbsImage(final String name, final ImageExtension extension) {
this("", name, extension);
}

/**
* Default Constructor.
*
* @param fullName the full file name (including path and image extension)
*/
public AbsImage(final String fullName) {
this("", fullName, ImageExtension.NONE);
}

/**
* {@inheritDoc}
*/
@Override
public void parse(final String[] parameters) {
if (parameters.length == 1) {
nameProperty().set(parameters[0]);
}
if (parameters.length == 2) {
nameProperty().set(parameters[0]);
extensionProperty().set(ImageExtension.valueOf(parameters[1]));
}
if (parameters.length == 3) {
pathProperty().set(parameters[0]);
nameProperty().set(parameters[1]);
extensionProperty().set(ImageExtension.valueOf(parameters[2]));
}
}

/**
* {@inheritDoc}
*/
@Override
protected List<? extends Object> getFieldValues() {
return Arrays.asList(path(), name(), extension().toString());
}

}
Expand Up @@ -18,6 +18,7 @@
package org.jrebirth.af.core.resource.image;

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

import javafx.scene.image.Image;
Expand Down Expand Up @@ -47,11 +48,14 @@ public final class ImageBuilder extends AbstractResourceBuilder<ImageItem, Image
@Override
protected Image buildResource(final ImageItem imageItem, final ImageParams jrImage) {
Image image = null;
if (jrImage instanceof LocalImage) {
// Build the requested font
image = buildLocalImage((LocalImage) jrImage);
if (jrImage instanceof RelImage) {
// Build the requested relative image
image = buildLocalImage((RelImage) jrImage, false);
} else if (jrImage instanceof AbsImage) {
// Build the requested absolute image
image = buildLocalImage((AbsImage) jrImage, true);
} else if (jrImage instanceof WebImage) {
// Build the requested font
// Build the requested web image
image = buildWebImage((WebImage) jrImage);
}

Expand All @@ -73,10 +77,15 @@ protected Image buildResource(final ImageItem imageItem, final ImageParams jrIma
* Build a local image with its local path.
*
* @param jrImage the local image params
<<<<<<< 8.x
*
=======
* @param skipImagesFolder skip imagesFolder prefix addition
*
>>>>>>> 895028c Fix #122 Manage multiple loation for images, fonts and style Manage localimage with relative and absolute resource params
* @return the JavaFX image object
*/
private Image buildLocalImage(final LocalImage jrImage) {
private Image buildLocalImage(final AbstractBaseImage jrImage, final boolean skipImagesFolder) {
final StringBuilder sb = new StringBuilder();

if (jrImage.path() != null && !jrImage.path().isEmpty()) {
Expand All @@ -88,9 +97,9 @@ private Image buildLocalImage(final LocalImage jrImage) {
if (jrImage.extension() != null) {
sb.append(jrImage.extension());
}
return loadImage(sb.toString());
return loadImage(sb.toString(), skipImagesFolder);
}

/**
* Build a web image with its url parameters.
*
Expand All @@ -116,16 +125,21 @@ private Image buildWebImage(final WebImage jrImage) {
*
* @param resourceName the name of the image, path must be separated by '/'
*
* @param skipImagesFolder skip imagesFolder prefix addition
*
* @return the image loaded
*/
private Image loadImage(final String resourceName) {
private Image loadImage(final String resourceName, final boolean skipImagesFolder) {
Image image = null;

List<String> imagePaths = JRebirthParameters.IMAGE_FOLDER.get();
List<String> imagePaths = (skipImagesFolder) ? Collections.singletonList("") : 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(!imagePath.isEmpty()){
imagePath += Resources.PATH_SEP;
}
final InputStream imageInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(imagePath + resourceName);
if (imageInputStream != null) {
image = new Image(imageInputStream);
}
Expand Down

0 comments on commit f58b6d7

Please sign in to comment.