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; ...@@ -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.Image;
import lu.list.itis.dkd.tui.widget.corona.VectorImage; import lu.list.itis.dkd.tui.widget.corona.VectorImage;
import com.google.common.base.Strings;
import com.kitfox.svg.SVGCache; import com.kitfox.svg.SVGCache;
import com.kitfox.svg.SVGUniverse; import com.kitfox.svg.SVGUniverse;
import com.kitfox.svg.app.beans.SVGIcon; import com.kitfox.svg.app.beans.SVGIcon;
...@@ -43,9 +44,11 @@ import org.slf4j.LoggerFactory; ...@@ -43,9 +44,11 @@ import org.slf4j.LoggerFactory;
import java.awt.Dimension; import java.awt.Dimension;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URI; import java.net.URI;
import java.net.URL;
/** /**
* Builder serving as abstract super class for all {@link Image} builders. * Builder serving as abstract super class for all {@link Image} builders.
...@@ -56,11 +59,18 @@ import java.net.URI; ...@@ -56,11 +59,18 @@ import java.net.URI;
* @param <B> * @param <B>
* The concrete builder. * The concrete builder.
*/ */
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
@NonNullByDefault @NonNullByDefault
public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>> extends CoronaBuilder<B> { public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>> extends CoronaBuilder<B> {
/** Field holding the image to display. */ /** Field holding the image to display. */
@Nullable @Nullable
public SVGIcon image = null; public SVGIcon image = null;
public URL baseUrl;
/** */ /** */
public int width; public int width;
/** */ /** */
...@@ -68,16 +78,24 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B> ...@@ -68,16 +78,24 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>
private static Logger LOGGER = LoggerFactory.getLogger(BaseVectorImageBuilder.class.getSimpleName()); private static Logger LOGGER = LoggerFactory.getLogger(BaseVectorImageBuilder.class.getSimpleName());
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
/** /**
* Constructor setting the centre of the corona. * Constructor setting the centre of the corona.
* *
* @param centre * @param centre
* The centre of the corona, usually the centre of the handle. * The centre of the corona, usually the centre of the handle.
*/ */
// ---------------------------------------------------------------------------
protected BaseVectorImageBuilder(Point centre) { protected BaseVectorImageBuilder(Point centre) {
super(centre); super(centre);
} }
// ---------------------------------------------------------------------------
/** /**
* Constructor initializing the centre of the corona as well as a possible image and information * 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. * provider as given by the children of a provided element.
...@@ -90,11 +108,14 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B> ...@@ -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 * to the violation of one or more contracts associated with the instance, including
* missing, incomplete, or erroneous parameters or values thereof. * missing, incomplete, or erroneous parameters or values thereof.
*/ */
// ---------------------------------------------------------------------------
protected BaseVectorImageBuilder(Element rootElement) throws BuildException { protected BaseVectorImageBuilder(Element rootElement) throws BuildException {
super(rootElement); super(rootElement);
this.buildFromBootstrap(rootElement, null, null); this.buildFromBootstrap(rootElement, null, null);
} }
// ---------------------------------------------------------------------------
/** /**
* *
* @param rootElement * @param rootElement
...@@ -102,11 +123,18 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B> ...@@ -102,11 +123,18 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>
* @param callback * @param callback
* @throws BuildException * @throws BuildException
*/ */
// ---------------------------------------------------------------------------
protected BaseVectorImageBuilder(Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException { protected BaseVectorImageBuilder(Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException {
super(rootElement, context, callback); super(rootElement, context, callback);
this.buildFromBootstrap(rootElement, context, callback); this.buildFromBootstrap(rootElement, context, callback);
} }
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Primitive(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
/** /**
* *
* @param rootElement * @param rootElement
...@@ -114,7 +142,19 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B> ...@@ -114,7 +142,19 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>
* @param callback * @param callback
* @throws BuildException * @throws BuildException
*/ */
// ---------------------------------------------------------------------------
private void buildFromBootstrap(@Nullable Element rootElement, BootstrapContext context, BootstrapCallback 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); String imagePath = BootstrappingUtils.getContentAsString(rootElement, Externalization.IMAGE_ELEMENT, BootstrappingUtils.MANDATORY, null, context);
image = loadSVGResource(imagePath); image = loadSVGResource(imagePath);
if (image == null) { if (image == null) {
...@@ -131,11 +171,22 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B> ...@@ -131,11 +171,22 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>
} }
} }
// ---------------------------------------------------------------------------
private SVGIcon loadSVGResource(String resourcePath) { private SVGIcon loadSVGResource(String resourcePath) {
SVGIcon icon = null; SVGIcon icon = null;
try { try {
SVGUniverse universe = SVGCache.getSVGUniverse(); 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 = new SVGIcon();
icon.setSvgURI(uri); icon.setSvgURI(uri);
icon.setAntiAlias(true); icon.setAntiAlias(true);
...@@ -147,6 +198,11 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B> ...@@ -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. * Method for adding an {@link java.awt.Image} to the corona.
* *
...@@ -154,14 +210,18 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B> ...@@ -154,14 +210,18 @@ public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>
* The {@link java.awt.Image} to add to the corona. * The {@link java.awt.Image} to add to the corona.
* @return An instance of this builder for chain calling. * @return An instance of this builder for chain calling.
*/ */
// ---------------------------------------------------------------------------
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public B withVectorImage(String imagePath) { public B withVectorImage(String imagePath) {
image = this.loadSVGResource(imagePath); image = this.loadSVGResource(imagePath);
return (B) this; return (B) this;
} }
// ---------------------------------------------------------------------------
/** {@inheritDoc} */ /** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override @Override
public abstract VectorImage build(); 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