Commit dddbd3f8 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

fix on export to image

issue #46 solved
parent f36820f1
package lcsb.mapviewer.converter.graphics; package lcsb.mapviewer.converter.graphics;
import java.io.IOException; import java.awt.Color;
import java.lang.reflect.InvocationTargetException; import java.io.IOException;
import java.util.ArrayList; import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.log4j.Logger;
import lcsb.mapviewer.common.MimeType;
import lcsb.mapviewer.common.Pair; import lcsb.mapviewer.common.MimeType;
import lcsb.mapviewer.common.exception.InvalidArgumentException; import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.exception.InvalidStateException; import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.converter.graphics.AbstractImageGenerator.Params; import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.model.map.model.Model; import lcsb.mapviewer.converter.graphics.AbstractImageGenerator.Params;
import lcsb.mapviewer.model.map.model.ModelFullIndexed; import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
/** import lcsb.mapviewer.model.user.ConfigurationElementType;
* This class is a util class containing information about all currently
* available implementations of {@link AbstractImageGenerator} class. /**
* * This class is a util class containing information about all currently
* @author Piotr Gawron * available implementations of {@link AbstractImageGenerator} class.
* *
*/ * @author Piotr Gawron
public class ImageGenerators { *
/** */
* Default class logger. public class ImageGenerators {
*/ /**
@SuppressWarnings("unused") * Default class logger.
private final Logger logger = Logger.getLogger(ImageGenerators.class); */
/** @SuppressWarnings("unused")
* List of {@link AbstractImageGenerator} classes available in the system. private final Logger logger = Logger.getLogger(ImageGenerators.class);
*/ /**
private List<Pair<String, Class<? extends AbstractImageGenerator>>> availableGenerators; * List of {@link AbstractImageGenerator} classes available in the system.
*/
/** private List<Pair<String, Class<? extends AbstractImageGenerator>>> availableGenerators;
* List of all possible objects extending {@link AbstractImageGenerator}
* interface that are available in the system. /**
*/ * List of all possible objects extending {@link AbstractImageGenerator}
private List<AbstractImageGenerator> generatorInstances = null; * interface that are available in the system.
*/
/** private List<AbstractImageGenerator> generatorInstances = null;
* Default constructor.
*/ /**
public ImageGenerators() { * Default constructor.
try { */
availableGenerators = new ArrayList<Pair<String, Class<? extends AbstractImageGenerator>>>(); public ImageGenerators() {
Model model = new ModelFullIndexed(null); try {
AbstractImageGenerator.Params params = new AbstractImageGenerator.Params().// availableGenerators = new ArrayList<Pair<String, Class<? extends AbstractImageGenerator>>>();
model(model).// Model model = new ModelFullIndexed(null);
width(1).// AbstractImageGenerator.Params params = new AbstractImageGenerator.Params().//
height(1); model(model).//
generatorInstances = new ArrayList<>(); width(1).//
generatorInstances.add(new PngImageGenerator(params)); minColor(Color.WHITE).
generatorInstances.add(new PdfImageGenerator(params)); maxColor(Color.WHITE).
// generatorInstances.add(new SvgImageGenerator(params)); height(1);
// generatorInstances.add(new JpgImageGenerator(params)); generatorInstances = new ArrayList<>();
for (AbstractImageGenerator abstractImageGenerator : generatorInstances) { generatorInstances.add(new PngImageGenerator(params));
availableGenerators.add(new Pair<String, Class<? extends AbstractImageGenerator>>(abstractImageGenerator.getFormatName(), abstractImageGenerator generatorInstances.add(new PdfImageGenerator(params));
.getClass())); // generatorInstances.add(new SvgImageGenerator(params));
} // generatorInstances.add(new JpgImageGenerator(params));
for (AbstractImageGenerator abstractImageGenerator : generatorInstances) {
} catch (DrawingException e) { availableGenerators.add(new Pair<String, Class<? extends AbstractImageGenerator>>(abstractImageGenerator.getFormatName(), abstractImageGenerator
throw new InvalidStateException("Problem with ImageGenarater instance creation...", e); .getClass()));
} }
}
} catch (DrawingException e) {
/** throw new InvalidStateException("Problem with ImageGenarater instance creation...", e);
* Returns {@link #availableGenerators}. }
* }
* @return {@link #availableGenerators}
*/ /**
public List<Pair<String, Class<? extends AbstractImageGenerator>>> getAvailableImageGenerators() { * Returns {@link #availableGenerators}.
return availableGenerators; *
} * @return {@link #availableGenerators}
*/
/** public List<Pair<String, Class<? extends AbstractImageGenerator>>> getAvailableImageGenerators() {
* Generates image and saves it to a file. return availableGenerators;
* }
* @param generatorClass
* which {@link AbstractImageGenerator} should be used to generate an /**
* image * Generates image and saves it to a file.
* @param params *
* parameters of the image generation process (scaling, size, * @param generatorClass
* nesting, etc) * which {@link AbstractImageGenerator} should be used to generate an
* @param filename * image
* name of the file where result should be saved * @param params
* @return {@link MimeType} of the generated object * parameters of the image generation process (scaling, size,
* @throws IOException * nesting, etc)
* thrown when there is a problem with output file * @param filename
*/ * name of the file where result should be saved
public MimeType generate(Class<? extends AbstractImageGenerator> generatorClass, AbstractImageGenerator.Params params, String filename) throws IOException { * @return {@link MimeType} of the generated object
try { * @throws IOException
AbstractImageGenerator generator = generatorClass.getConstructor(AbstractImageGenerator.Params.class).newInstance(params); * thrown when there is a problem with output file
generator.saveToFile(filename); */
return generator.getMimeType(); public MimeType generate(Class<? extends AbstractImageGenerator> generatorClass, AbstractImageGenerator.Params params, String filename) throws IOException {
} catch (InstantiationException e) { try {
throw new InvalidStateException("Problem with ImageGenarater instance creation...", e); AbstractImageGenerator generator = generatorClass.getConstructor(AbstractImageGenerator.Params.class).newInstance(params);
} catch (IllegalAccessException e) { generator.saveToFile(filename);
throw new InvalidStateException("Problem with ImageGenarater instance creation...", e); return generator.getMimeType();
} catch (IllegalArgumentException e) { } catch (InstantiationException e) {
throw new InvalidStateException("Problem with ImageGenarater instance creation...", e); throw new InvalidStateException("Problem with ImageGenarater instance creation...", e);
} catch (InvocationTargetException e) { } catch (IllegalAccessException e) {
throw new InvalidStateException("Problem with ImageGenarater instance creation...", e); throw new InvalidStateException("Problem with ImageGenarater instance creation...", e);
} catch (NoSuchMethodException e) { } catch (IllegalArgumentException e) {
throw new InvalidStateException("Problem with ImageGenarater instance creation...", e); throw new InvalidStateException("Problem with ImageGenarater instance creation...", e);
} catch (SecurityException e) { } catch (InvocationTargetException e) {
throw new InvalidStateException("Problem with ImageGenarater instance creation...", e); throw new InvalidStateException("Problem with ImageGenarater instance creation...", e);
} } catch (NoSuchMethodException e) {
} throw new InvalidStateException("Problem with ImageGenarater instance creation...", e);
} catch (SecurityException e) {
/** throw new InvalidStateException("Problem with ImageGenarater instance creation...", e);
* Generates image and saves it to a file. }
* }
* @param generatorClass
* name of the {@link AbstractImageGenerator} implementation that /**
* should be used to generate an image * Generates image and saves it to a file.
* @param params *
* parameters of the image generation process (scaling, size, * @param generatorClass
* nesting, etc) * name of the {@link AbstractImageGenerator} implementation that
* @param filename * should be used to generate an image
* name of the file where result should be saved * @param params
* @return {@link MimeType} of the generated object * parameters of the image generation process (scaling, size,
* @throws IOException * nesting, etc)
* thrown when there is a problem with output file * @param filename
*/ * name of the file where result should be saved
public MimeType generate(String generatorClass, Params params, String filename) throws IOException { * @return {@link MimeType} of the generated object
for (Pair<String, Class<? extends AbstractImageGenerator>> element : availableGenerators) { * @throws IOException
if (element.getRight().getCanonicalName().equals(generatorClass)) { * thrown when there is a problem with output file
return generate(element.getRight(), params, filename); */
} public MimeType generate(String generatorClass, Params params, String filename) throws IOException {
} for (Pair<String, Class<? extends AbstractImageGenerator>> element : availableGenerators) {
throw new InvalidArgumentException("Unknown class type: " + generatorClass); if (element.getRight().getCanonicalName().equals(generatorClass)) {
return generate(element.getRight(), params, filename);
} }
}
/** throw new InvalidArgumentException("Unknown class type: " + generatorClass);
* Returns file extension that should be used for files generated by
* implementation of {@link AbstractImageGenerator} class. }
*
* @param generatorClass /**
* name of the class that extends {@link AbstractImageGenerator} * Returns file extension that should be used for files generated by
* @return file extension that should be used for files generated by * implementation of {@link AbstractImageGenerator} class.
* implementation of {@link AbstractImageGenerator} class *
*/ * @param generatorClass
public String getExtension(String generatorClass) { * name of the class that extends {@link AbstractImageGenerator}
for (Pair<String, Class<? extends AbstractImageGenerator>> element : availableGenerators) { * @return file extension that should be used for files generated by
if (element.getRight().getCanonicalName().equals(generatorClass)) { * implementation of {@link AbstractImageGenerator} class
return getExtension(element.getRight()); */
} public String getExtension(String generatorClass) {
} for (Pair<String, Class<? extends AbstractImageGenerator>> element : availableGenerators) {
throw new InvalidArgumentException("Unknown class type: " + generatorClass); if (element.getRight().getCanonicalName().equals(generatorClass)) {
} return getExtension(element.getRight());
}
/** }
* Returns file extension that should be used for files generated by throw new InvalidArgumentException("Unknown class type: " + generatorClass);
* implementation of {@link AbstractImageGenerator} class. }
*
* @param generatorClass /**
* class that extends {@link AbstractImageGenerator} * Returns file extension that should be used for files generated by
* @return file extension that should be used for files generated by * implementation of {@link AbstractImageGenerator} class.
* implementation of {@link AbstractImageGenerator} class *
*/ * @param generatorClass
public String getExtension(Class<? extends AbstractImageGenerator> generatorClass) { * class that extends {@link AbstractImageGenerator}
for (AbstractImageGenerator imageGenerator : generatorInstances) { * @return file extension that should be used for files generated by
if (generatorClass.isAssignableFrom(imageGenerator.getClass())) { * implementation of {@link AbstractImageGenerator} class
return imageGenerator.getFileExtension(); */
} public String getExtension(Class<? extends AbstractImageGenerator> generatorClass) {
} for (AbstractImageGenerator imageGenerator : generatorInstances) {
throw new InvalidArgumentException("Unknown class type: " + generatorClass); if (generatorClass.isAssignableFrom(imageGenerator.getClass())) {
} return imageGenerator.getFileExtension();
} }
}
throw new InvalidArgumentException("Unknown class type: " + generatorClass);
}
}
...@@ -2,6 +2,7 @@ package lcsb.mapviewer.commands; ...@@ -2,6 +2,7 @@ package lcsb.mapviewer.commands;
import java.awt.Color; import java.awt.Color;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.map.layout.ColorSchema; import lcsb.mapviewer.model.map.layout.ColorSchema;
/** /**
...@@ -34,6 +35,9 @@ public class ColorExtractor { ...@@ -34,6 +35,9 @@ public class ColorExtractor {
* {@link ColorSchema#value} * {@link ColorSchema#value}
*/ */
public ColorExtractor(Color minColor, Color maxColor) { public ColorExtractor(Color minColor, Color maxColor) {
if (minColor == null || maxColor == null) {
throw new InvalidArgumentException("Parameters cannot be null");
}
this.minColor = minColor; this.minColor = minColor;
this.maxColor = maxColor; this.maxColor = maxColor;
} }
...@@ -74,4 +78,19 @@ public class ColorExtractor { ...@@ -74,4 +78,19 @@ public class ColorExtractor {
return Color.WHITE; return Color.WHITE;
} }
/**
* @return the minColor
* @see #minColor
*/
public Color getMinColor() {
return minColor;
}
/**
* @return the maxColor
* @see #maxColor
*/
public Color getMaxColor() {
return maxColor;
}
} }
...@@ -35,6 +35,7 @@ import com.google.gson.reflect.TypeToken; ...@@ -35,6 +35,7 @@ import com.google.gson.reflect.TypeToken;
import lcsb.mapviewer.bean.MapBean.ClientMapData; import lcsb.mapviewer.bean.MapBean.ClientMapData;
import lcsb.mapviewer.commands.ClearColorModelCommand; import lcsb.mapviewer.commands.ClearColorModelCommand;
import lcsb.mapviewer.commands.ColorExtractor;
import lcsb.mapviewer.commands.ColorModelCommand; import lcsb.mapviewer.commands.ColorModelCommand;
import lcsb.mapviewer.commands.CopyCommand; import lcsb.mapviewer.commands.CopyCommand;
import lcsb.mapviewer.commands.SubModelCommand; import lcsb.mapviewer.commands.SubModelCommand;
...@@ -1200,6 +1201,8 @@ public class ExportBean extends AbstractManagedBean { ...@@ -1200,6 +1201,8 @@ public class ExportBean extends AbstractManagedBean {
scale /= 2; scale /= 2;
} }
ColorExtractor colorExtractor = userService.getColorExtractorForUser(userBean.getLoggedUser());
Params params = new Params().// Params params = new Params().//
x(minX).// x(minX).//
y(minY).// y(minY).//
...@@ -1208,7 +1211,9 @@ public class ExportBean extends AbstractManagedBean { ...@@ -1208,7 +1211,9 @@ public class ExportBean extends AbstractManagedBean {
level(level).// level(level).//
nested(false).// automatically set nested view as invalid nested(false).// automatically set nested view as invalid
scale(scale).// scale(scale).//
minColor(userBean.getLoggedUser().getMinColor()).maxColor(userBean.getLoggedUser().getMaxColor()).model(colorModel); minColor(colorExtractor.getMinColor()).//
maxColor(colorExtractor.getMaxColor()).//
model(colorModel);
List<Integer> visibleLayoutIds = deserializeIdList(visibleLayouts); List<Integer> visibleLayoutIds = deserializeIdList(visibleLayouts);
for (Integer integer : visibleLayoutIds) { for (Integer integer : visibleLayoutIds) {
Map<Object, ColorSchema> map = getLayoutService().getElementsForLayout(colorModel, integer); Map<Object, ColorSchema> map = getLayoutService().getElementsForLayout(colorModel, integer);
......
Supports Markdown
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