Commit 7d522510 authored by Nico Mack's avatar Nico Mack

Added VectorImage (SVG) Corona

parent 9e547ed6
......@@ -117,6 +117,12 @@
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>com.kitfox.svg</groupId>
<artifactId>svg-salamander</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
......
/**
* Copyright Luxembourg Institute of Science and Technology, 2017. All rights reserved.
*
* This file is part of TULIP.
*
* TULIP is free software: you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation, version 3 of the
* License.
*
* TULIP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.utility;
import lu.list.itis.dkd.tui.widget.corona.Corona;
import java.util.Comparator;
/**
* @author mack
* @since [major].[minor]
* @version [major].[minor].[micro]
*/
public class CoronaComparator implements Comparator<Corona> {
/** {@inheritDoc} */
@Override
public int compare(Corona o1, Corona o2) {
if ((o1 == null) || (o2 == null))
return 0;
return o1.compareTo(o2);
}
}
......@@ -476,7 +476,7 @@ public class Point extends Float implements KdComparator<Point> {
if (point == null) {
return this;
}
Preconditions.checkArgument(point.getState().getClass().getName().equals(state.getClass().getName()));
Preconditions.checkArgument(point.getState().getClass().getName().equals(state.getClass().getName()), point.getState().toString() + " != " + state.toString());
Point clone = clone();
clone.setLocation(clone.x - point.x, clone.y - point.y);
......
/**
* Copyright Luxembourg Institute of Science and Technology, 2016.
*
* This file is part of TULIP.
*
* TULIP is licensed under a dual-licensing scheme. For non-commercial purposes, the LGPL version 3,
* as stated below, is applicable. For all commercial purposes TULIP is licensed under a LIST
* proprietary license. Please contact LIST at tto@list.lu to obtain a commercial license.
*
* For all non-commercial purposes, TULIP is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, version 3 of the License.
*
* TULIP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.widget.corona;
import lu.list.itis.dkd.dbc.annotation.NonNullByDefault;
import lu.list.itis.dkd.dbc.annotation.Nullable;
import lu.list.itis.dkd.tui.utility.Point;
import lu.list.itis.dkd.tui.widget.corona.builder.ImageBuilder;
import lu.list.itis.dkd.tui.widget.corona.builder.VectorImageBuilder;
import com.kitfox.svg.app.beans.SVGIcon;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
/**
* Class implementing an image corona for displaying simple images with different options.
*
* @author Eric Tobias [eric.tobias@list.lu]
* @since 1.0
* @version 2.3.0
*/
@NonNullByDefault
public class VectorImage extends Corona {
/** Field holding the image to display. */
@Nullable
private SVGIcon image;
private int width;
private int height;
/**
* Constructor building all fields and calling the implicit super constructor using a
* {@link ImageBuilder} instance holding all values.
*
* @param builder
* The {@link ImageBuilder} instance holding all values.
*/
public VectorImage(VectorImageBuilder builder) {
super(builder);
image = builder.image;
width = builder.width;
height = builder.height;
}
/**
* {@inheritDoc}<br>
* <br>
*
* @pre image != null
*/
@Override
public void paint(Graphics2D canvas) {
if (!active || image == null) {
return;
}
centre.toScreenCoordinates();
if (initialTranslation != null) {
initialTranslation.toScreenCoordinates();
} else {
initialTranslation = new Point();
}
Point drawAt = centre.add(initialTranslation);
AffineTransform rotation = new AffineTransform();
rotation.rotate(rotateWithHandle ? drawAt.getAngle() : initialTranslation != null ? initialTranslation.getAngle() : 0, centre.getX(), centre.getY());
rotation.translate(drawAt.x, drawAt.y);
if (spinOnCoronaCentre) {
rotation.rotate(rotateWithHandle ? -drawAt.getAngle() : 0);
rotation.translate(-image.getIconWidth() / 2, -image.getIconHeight() / 2);
}
rotation.transform(new Point(), drawAt);
if (Double.compare(initialRotation, 0d) != 0) {
AffineTransform transformation = new AffineTransform();
transformation.rotate(initialRotation, spinOnCoronaCentre ? drawAt.getX() + image.getIconWidth() / 2 : drawAt.getX(), spinOnCoronaCentre ? drawAt.getY() + image.getIconHeight() / 2 : drawAt.getY());
canvas.setTransform(transformation);
}
image.paintIcon(null, canvas, (int) drawAt.x, (int) drawAt.y);
canvas.setTransform(new AffineTransform());
}
}
\ No newline at end of file
......@@ -56,7 +56,9 @@ public abstract class BaseImageBuilder<B extends BaseImageBuilder<B>> extends Co
/** Field holding an {@link InformationProvider} instances to draw information from. */
@Nullable
public InformationProvider<java.awt.Image> informationProvider = null;
/** */
public int width;
/** */
public int height;
/**
......
/**
* Copyright Luxembourg Institute of Science and Technology, 2016.
*
* This file is part of TULIP.
*
* TULIP is licensed under a dual-licensing scheme. For non-commercial purposes, the LGPL version 3,
* as stated below, is applicable. For all commercial purposes TULIP is licensed under a LIST
* proprietary license. Please contact LIST at tto@list.lu to obtain a commercial license.
*
* For all non-commercial purposes, TULIP is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, version 3 of the License.
*
* TULIP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.widget.corona.builder;
import lu.list.itis.dkd.dbc.annotation.NonNullByDefault;
import lu.list.itis.dkd.dbc.annotation.Nullable;
import lu.list.itis.dkd.tui.bootstrapping.BootstrapCallback;
import lu.list.itis.dkd.tui.bootstrapping.BootstrapContext;
import lu.list.itis.dkd.tui.bootstrapping.BootstrappingUtils;
import lu.list.itis.dkd.tui.exception.BuildException;
import lu.list.itis.dkd.tui.utility.Externalization;
import lu.list.itis.dkd.tui.utility.Point;
import lu.list.itis.dkd.tui.widget.corona.Image;
import lu.list.itis.dkd.tui.widget.corona.VectorImage;
import com.kitfox.svg.SVGCache;
import com.kitfox.svg.app.beans.SVGIcon;
import org.jdom2.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
/**
* Builder serving as abstract super class for all {@link Image} builders.
*
* @author Eric Tobias [eric.tobias@list.lu]
* @since 1.0
* @version 2.3.0
* @param <B>
* The concrete builder.
*/
@NonNullByDefault
public abstract class BaseVectorImageBuilder<B extends BaseVectorImageBuilder<B>> extends CoronaBuilder<B> {
/** Field holding the image to display. */
@Nullable
public SVGIcon image = null;
/** */
public int width;
/** */
public int height;
private static Logger LOGGER = LoggerFactory.getLogger(BaseVectorImageBuilder.class.getSimpleName());
/**
* 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.
*
* @param rootElement
* The element harbouring, on child nodes, the necessary information to initialize all
* fields of the builder.
* @throws BuildException
* Exception raised when the building of a corona instance cannot complete successfully
* due 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
* @param context
* @param callback
* @throws BuildException
*/
protected BaseVectorImageBuilder(Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException {
super(rootElement, context, callback);
this.buildFromBootstrap(rootElement, context, callback);
}
/**
*
* @param rootElement
* @param context
* @param callback
* @throws BuildException
*/
private void buildFromBootstrap(@Nullable Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException {
String imagePath = BootstrappingUtils.getContentAsString(rootElement, Externalization.IMAGE_ELEMENT, BootstrappingUtils.MANDATORY, null, context);
image = loadSVGResource(imagePath);
width = BootstrappingUtils.getContentAsInteger(rootElement, Externalization.WIDTH_NODE, BootstrappingUtils.OPTIONAL, -1, context);
height = BootstrappingUtils.getContentAsInteger(rootElement, Externalization.HEIGHT_NODE, BootstrappingUtils.OPTIONAL, -1, context);
if ((width >= 0) && (height >= 0)) {
image.setPreferredSize(new Dimension(width, height));
}
}
private SVGIcon loadSVGResource(String resourcePath) {
SVGIcon icon = null;
try {
URI uri = SVGCache.getSVGUniverse().loadSVG(new BufferedReader(new InputStreamReader(getClass().getResource(resourcePath).openStream())), "image"); //$NON-NLS-1$
icon = new SVGIcon();
icon.setSvgURI(uri);
icon.setAntiAlias(true);
} catch (IOException e) {
LOGGER.error("Failed to load SVG resource {}!", resourcePath); //$NON-NLS-1$
}
return icon;
}
/**
* Method for adding an {@link java.awt.Image} to the corona.
*
* @param picture
* 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
/**
* Copyright Luxembourg Institute of Science and Technology, 2016.
*
* This file is part of TULIP.
*
* TULIP is licensed under a dual-licensing scheme. For non-commercial purposes, the LGPL version 3,
* as stated below, is applicable. For all commercial purposes TULIP is licensed under a LIST
* proprietary license. Please contact LIST at tto@list.lu to obtain a commercial license.
*
* For all non-commercial purposes, TULIP is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, version 3 of the License.
*
* TULIP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with TULIP. If
* not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package lu.list.itis.dkd.tui.widget.corona.builder;
import lu.list.itis.dkd.dbc.annotation.NonNullByDefault;
import lu.list.itis.dkd.tui.bootstrapping.BootstrapCallback;
import lu.list.itis.dkd.tui.bootstrapping.BootstrapContext;
import lu.list.itis.dkd.tui.exception.BuildException;
import lu.list.itis.dkd.tui.utility.Point;
import lu.list.itis.dkd.tui.widget.corona.Image;
import lu.list.itis.dkd.tui.widget.corona.VectorImage;
import org.jdom2.Element;
/**
* {@link VectorImageBuilder} class used to construct an {@link Image} corona by providing methods
* to set all parameters and permutations thereof.
*
* @author Eric Tobias [eric.tobias@list.lu]
* @since 1.0
* @version 2.3.0
*/
@NonNullByDefault
public final class VectorImageBuilder extends BaseVectorImageBuilder<VectorImageBuilder> {
/**
* Constructor setting the centre of the corona.
*
* @param centre
* The centre of the corona, usually the centre of the handle.
*/
public VectorImageBuilder(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.
*
* @param rootElement
* The element harbouring, on child nodes, the necessary information to initialize all
* fields of the builder.
* @throws BuildException
* Exception raised when the building of a corona instance cannot complete successfully
* due to the violation of one or more contracts associated with the instance, including
* missing, incomplete, or erroneous parameters or values thereof.
*/
public VectorImageBuilder(Element rootElement) throws BuildException {
super(rootElement);
}
/**
* 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.
*
* @param rootElement
* The element harbouring, on child nodes, the necessary information to initialize all
* fields of the builder.
* @param context
* @param callback
* @throws BuildException
* Exception raised when the building of a corona instance cannot complete successfully
* due to the violation of one or more contracts associated with the instance, including
* missing, incomplete, or erroneous parameters or values thereof.
*/
public VectorImageBuilder(Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException {
super(rootElement, context, callback);
}
/** {@inheritDoc} */
@Override
public VectorImage build() {
return new VectorImage(this);
}
}
\ 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