Commit 15d9a29c authored by Nico Mack's avatar Nico Mack

Added baseUrl property and possibility to load vector images from

filesystem
parent 473a8ad5
......@@ -33,6 +33,7 @@ import lu.list.itis.dkd.tui.utility.StringUtils;
import lu.list.itis.dkd.tui.widget.corona.Image;
import lu.list.itis.dkd.tui.widget.corona.VectorImage;
import com.google.common.base.Strings;
import com.kitfox.svg.SVGCache;
import com.kitfox.svg.SVGUniverse;
import com.kitfox.svg.app.beans.SVGIcon;
......@@ -43,9 +44,11 @@ import org.slf4j.LoggerFactory;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
/**
* Builder serving as abstract super class for all {@link Image} builders.
......@@ -56,11 +59,18 @@ import java.net.URI;
* @param <B>
* The concrete builder.
*/
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
@NonNullByDefault
public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>> extends CoronaBuilder<B> {
/** Field holding the image to display. */
@Nullable
public SVGIcon image = null;
public URL baseUrl;
/** */
public int width;
/** */
......@@ -68,16 +78,24 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>
private static Logger LOGGER = LoggerFactory.getLogger(BaseVectorImageBuilder.class.getSimpleName());
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* Constructor setting the centre of the corona.
*
* @param centre
* The centre of the corona, usually the centre of the handle.
*/
// ---------------------------------------------------------------------------
protected BaseVectorImageBuilder(Point centre) {
super(centre);
}
// ---------------------------------------------------------------------------
/**
* Constructor initializing the centre of the corona as well as a possible image and information
* provider as given by the children of a provided element.
......@@ -90,11 +108,14 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>
* to the violation of one or more contracts associated with the instance, including
* missing, incomplete, or erroneous parameters or values thereof.
*/
// ---------------------------------------------------------------------------
protected BaseVectorImageBuilder(Element rootElement) throws BuildException {
super(rootElement);
this.buildFromBootstrap(rootElement, null, null);
}
// ---------------------------------------------------------------------------
/**
*
* @param rootElement
......@@ -102,11 +123,18 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>
* @param callback
* @throws BuildException
*/
// ---------------------------------------------------------------------------
protected BaseVectorImageBuilder(Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException {
super(rootElement, context, callback);
this.buildFromBootstrap(rootElement, context, callback);
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Primitive(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
*
* @param rootElement
......@@ -114,7 +142,19 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>
* @param callback
* @throws BuildException
*/
// ---------------------------------------------------------------------------
private void buildFromBootstrap(@Nullable Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException {
String url = BootstrappingUtils.getContentAsString(rootElement, Externalization.BASE_URL_NODE, BootstrappingUtils.OPTIONAL, Externalization.EMPTY_STRING, context);
if (!Strings.isNullOrEmpty(url)) {
try {
File urlFile = new File(url);
this.baseUrl = urlFile.toURI().toURL();
} catch (IOException e) {
throw new BuildException(StringUtils.build("Failed to set base URL to {}", url)); //$NON-NLS-1$
}
}
String imagePath = BootstrappingUtils.getContentAsString(rootElement, Externalization.IMAGE_ELEMENT, BootstrappingUtils.MANDATORY, null, context);
image = loadSVGResource(imagePath);
if (image == null) {
......@@ -131,11 +171,22 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>
}
}
// ---------------------------------------------------------------------------
private SVGIcon loadSVGResource(String resourcePath) {
SVGIcon icon = null;
try {
SVGUniverse universe = SVGCache.getSVGUniverse();
URI uri = universe.loadSVG(new BufferedReader(new InputStreamReader(TangibleApplication.class.getResource(resourcePath).openStream())), "image"); //$NON-NLS-1$
URI uri;
if (this.baseUrl != null) {
StringBuilder path = new StringBuilder("File:");
path.append(this.baseUrl.getPath()).append(resourcePath);
URL resourceUrl = new URL(path.toString());
uri = universe.loadSVG(new BufferedReader(new InputStreamReader(resourceUrl.openStream())), "image"); //$NON-NLS-1$
} else {
uri = universe.loadSVG(new BufferedReader(new InputStreamReader(TangibleApplication.class.getResource(resourcePath).openStream())), "image"); //$NON-NLS-1$
}
icon = new SVGIcon();
icon.setSvgURI(uri);
icon.setAntiAlias(true);
......@@ -147,6 +198,11 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* Method for adding an {@link java.awt.Image} to the corona.
*
......@@ -154,14 +210,18 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>
* The {@link java.awt.Image} to add to the corona.
* @return An instance of this builder for chain calling.
*/
// ---------------------------------------------------------------------------
@SuppressWarnings("unchecked")
public B withVectorImage(String imagePath) {
image = this.loadSVGResource(imagePath);
return (B) this;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public abstract VectorImage build();
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment