Skip to content
Snippets Groups Projects
Commit 3b3d45e8 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

graphics converter refactored

drawing is performed when save method is executed (not in the
constructor)
parent 7b8778b7
No related branches found
No related tags found
1 merge request!44Resolve "semantic zoom"
Showing
with 128 additions and 52 deletions
......@@ -344,6 +344,12 @@ public abstract class AbstractImageGenerator {
*/
public Params model(final Model model) {
this.model = model;
if (this.width == null) {
this.width = model.getWidth();
}
if (this.height == null) {
this.height = model.getHeight();
}
return this;
}
......@@ -578,7 +584,6 @@ public abstract class AbstractImageGenerator {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(AbstractImageGenerator.class);
/**
......@@ -591,6 +596,8 @@ public abstract class AbstractImageGenerator {
*/
private boolean sbgnFormat = false;
private Params params;
/**
* This method creates a graphics object for different implementations of
* canvas with fixed width and height.
......@@ -614,7 +621,17 @@ public abstract class AbstractImageGenerator {
* thrown when there was a problem with drawing a map
*
*/
protected AbstractImageGenerator(final Params params) throws DrawingException {
protected AbstractImageGenerator(final Params params) {
this.params = params;
}
private boolean drawn = false;
protected void draw() throws DrawingException {
if (isDrawn()) {
logger.warn("Model was already drawn. Skipping");
return;
}
this.level = params.getLevel();
this.scale = params.getScale();
......@@ -683,6 +700,7 @@ public abstract class AbstractImageGenerator {
for (Layer layer : params.getModel().getLayers()) {
drawLayer(layer);
}
setDrawn(true);
}
/**
......@@ -718,13 +736,6 @@ public abstract class AbstractImageGenerator {
* thrown when there was a problem with drawing {@link Compartment}
*/
protected void drawCompartment(final Compartment compartment, List<ColorSchema> visibleLayouts, Params params) throws DrawingException {
// If 'compartment' has not the big enough visibility level then should not
// be visible.
if (!(zoomLevelMatcher.isVisible(level, compartment.getVisibilityLevel())) && params.nested) {
return;
}
/**
* This part is responsible for transparency of compartments. Compartment
* should be transparent if the field of it is big enough, its visibility
......@@ -799,14 +810,6 @@ public abstract class AbstractImageGenerator {
*/
protected void drawSpecies(final Species species, List<ColorSchema> visibleLayouts, Params params) throws DrawingException {
// The displaying of species is indicated by values of VisibilityLevel. If
// VisibilityLevel is big enough, then it is
// displayed.
if (!(zoomLevelMatcher.isVisible(level, species.getVisibilityLevel())) && params.nested) {
return;
}
/**
* Last conditions in this 'ifs' provides the certainty of visibility of
* most bottom Complexes.
......@@ -930,8 +933,16 @@ public abstract class AbstractImageGenerator {
* file where the images should be saved
* @throws IOException
* thrown when there is problem with output file
* @throws DrawingException
*/
public abstract void saveToFile(String fileName) throws IOException;
public final void saveToFile(String fileName) throws IOException, DrawingException {
if (!isDrawn()) {
draw();
}
saveToFileImplementation(fileName);
};
protected abstract void saveToFileImplementation(String fileName) throws IOException;
/**
* Saves generated image into {@link OutputStream}.
......@@ -940,8 +951,16 @@ public abstract class AbstractImageGenerator {
* stream where the images should be saved
* @throws IOException
* thrown when there is problem with output stream
* @throws DrawingException
*/
public abstract void saveToOutputStream(OutputStream os) throws IOException;
public final void saveToOutputStream(OutputStream os) throws IOException, DrawingException {
if (!isDrawn()) {
draw();
}
saveToOutputStream(os);
}
protected abstract void saveToOutputStreamImplementation(OutputStream os) throws IOException;
/**
* Saves part of the generated image file.
......@@ -958,8 +977,16 @@ public abstract class AbstractImageGenerator {
* hieght of the image part
* @throws IOException
* thrown when there is problem with output file
* @throws DrawingException
*/
public abstract void savePartToFile(final int x, final int y, final int width, final int height, final String fileName) throws IOException;
public final void savePartToFile(final int x, final int y, final int width, final int height, final String fileName) throws IOException, DrawingException {
if (!isDrawn()) {
draw();
}
savePartToFileImplementation(x, y, width, height, fileName);
}
protected abstract void savePartToFileImplementation(final int x, final int y, final int width, final int height, final String fileName) throws IOException;
/**
* Saves part of the generated image into {@link OutputStream}.
......@@ -976,8 +1003,18 @@ public abstract class AbstractImageGenerator {
* hieght of the image part
* @throws IOException
* thrown when there is problem with output file
* @throws DrawingException
*/
public abstract void savePartToOutputStream(final int x, final int y, final int width, final int height, final OutputStream os) throws IOException;
public final void savePartToOutputStream(final int x, final int y, final int width, final int height, final OutputStream os)
throws IOException, DrawingException {
if (!isDrawn()) {
draw();
}
savePartToOutputStreamImplementation(x, y, width, height, os);
}
public abstract void savePartToOutputStreamImplementation(final int x, final int y, final int width, final int height, final OutputStream os)
throws IOException;
/**
* Returns name of the format to which this graphic converter will transform.
......@@ -1016,4 +1053,38 @@ public abstract class AbstractImageGenerator {
protected void sbgnFormat(boolean sbgnFormat) {
this.sbgnFormat = sbgnFormat;
}
/**
* @return the drawn
* @see #drawn
*/
private boolean isDrawn() {
return drawn;
}
/**
* @param drawn
* the drawn to set
* @see #drawn
*/
private void setDrawn(boolean drawn) {
this.drawn = drawn;
}
/**
* @return the params
* @see #params
*/
protected Params getParams() {
return params;
}
/**
* @param params
* the params to set
* @see #params
*/
protected void setParams(Params params) {
this.params = params;
}
}
\ No newline at end of file
......@@ -89,8 +89,9 @@ public class ImageGenerators {
* @return {@link MimeType} of the generated object
* @throws IOException
* thrown when there is a problem with output file
* @throws DrawingException
*/
public MimeType generate(Class<? extends AbstractImageGenerator> generatorClass, AbstractImageGenerator.Params params, String filename) throws IOException {
public MimeType generate(Class<? extends AbstractImageGenerator> generatorClass, AbstractImageGenerator.Params params, String filename) throws IOException, DrawingException {
try {
AbstractImageGenerator generator = generatorClass.getConstructor(AbstractImageGenerator.Params.class).newInstance(params);
generator.saveToFile(filename);
......@@ -124,8 +125,9 @@ public class ImageGenerators {
* @return {@link MimeType} of the generated object
* @throws IOException
* thrown when there is a problem with output file
* @throws DrawingException
*/
public MimeType generate(String generatorClass, Params params, String filename) throws IOException {
public MimeType generate(String generatorClass, Params params, String filename) throws IOException, DrawingException {
for (Pair<String, Class<? extends AbstractImageGenerator>> element : availableGenerators) {
if (element.getRight().getCanonicalName().equals(generatorClass)) {
return generate(element.getRight(), params, filename);
......
......@@ -36,26 +36,26 @@ public class JpgImageGenerator extends NormalImageGenerator {
}
@Override
public void saveToFile(String fileName) throws IOException {
public void saveToFileImplementation(String fileName) throws IOException {
FileOutputStream fos = new FileOutputStream(new File(fileName));
saveToOutputStream(fos);
saveToOutputStreamImplementation(fos);
fos.close();
}
@Override
public void saveToOutputStream(OutputStream os) throws IOException {
public void saveToOutputStreamImplementation(OutputStream os) throws IOException {
ImageIO.write(getBi(), "JPG", os);
}
@Override
public void savePartToFile(int x, int y, int width, int height, String fileName) throws IOException {
public void savePartToFileImplementation(int x, int y, int width, int height, String fileName) throws IOException {
FileOutputStream fos = new FileOutputStream(new File(fileName));
savePartToOutputStream(x, y, width, height, fos);
savePartToOutputStreamImplementation(x, y, width, height, fos);
fos.close();
}
@Override
public void savePartToOutputStream(int x, int y, int width, int height, OutputStream os) throws IOException {
public void savePartToOutputStreamImplementation(int x, int y, int width, int height, OutputStream os) throws IOException {
BufferedImage tmpBI = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D tmpGraphics = tmpBI.createGraphics();
tmpGraphics.drawImage(getBi(), 0, 0, width, height, x, y, x + width, y + width, null);
......
......@@ -16,7 +16,7 @@ public abstract class NormalImageGenerator extends AbstractImageGenerator {
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(NormalImageGenerator.class);
private Logger logger = Logger.getLogger(NormalImageGenerator.class);
/**
* Buffered image structure used for image generating.
......@@ -42,11 +42,11 @@ public abstract class NormalImageGenerator extends AbstractImageGenerator {
protected void createImageObject(final double width, final double height) {
bi = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_ARGB);
setGraphics(bi.createGraphics());
}
/**
* @return the bi
* @throws DrawingException
* @see #bi
*/
protected BufferedImage getBi() {
......
......@@ -55,9 +55,9 @@ public class PdfImageGenerator extends AbstractImageGenerator {
}
@Override
public void saveToFile(final String fileName) throws IOException {
public void saveToFileImplementation(final String fileName) throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
saveToOutputStream(fos);
saveToOutputStreamImplementation(fos);
fos.close();
}
......@@ -79,17 +79,17 @@ public class PdfImageGenerator extends AbstractImageGenerator {
}
@Override
public void saveToOutputStream(OutputStream os) throws IOException {
public void saveToOutputStreamImplementation(OutputStream os) throws IOException {
inMemoryOutputStream.writeTo(os);
}
@Override
public void savePartToFile(int x, int y, int width, int height, String fileName) throws IOException {
public void savePartToFileImplementation(int x, int y, int width, int height, String fileName) throws IOException {
throw new NotImplementedException("Partial save is not implemented in PNG image generator");
}
@Override
public void savePartToOutputStream(int x, int y, int width, int height, OutputStream os) throws IOException {
public void savePartToOutputStreamImplementation(int x, int y, int width, int height, OutputStream os) throws IOException {
throw new NotImplementedException("Partial save is not implemented in PNG image generator");
}
......
......@@ -44,26 +44,26 @@ public class PngImageGenerator extends NormalImageGenerator {
}
@Override
public void saveToFile(String fileName) throws IOException {
public void saveToFileImplementation(String fileName) throws IOException {
FileOutputStream fos = new FileOutputStream(new File(fileName));
saveToOutputStream(fos);
saveToOutputStreamImplementation(fos);
fos.close();
}
@Override
public void saveToOutputStream(OutputStream os) throws IOException {
public void saveToOutputStreamImplementation(OutputStream os) throws IOException {
ImageIO.write(getBi(), "PNG", os);
}
@Override
public void savePartToFile(int x, int y, int width, int height, String fileName) throws IOException {
public void savePartToFileImplementation(int x, int y, int width, int height, String fileName) throws IOException {
FileOutputStream fos = new FileOutputStream(new File(fileName));
savePartToOutputStream(x, y, width, height, fos);
savePartToOutputStreamImplementation(x, y, width, height, fos);
fos.close();
}
@Override
public void savePartToOutputStream(int x, int y, int width, int height, OutputStream os) throws IOException {
public void savePartToOutputStreamImplementation(int x, int y, int width, int height, OutputStream os) throws IOException {
BufferedImage tmpBI = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D tmpGraphics = tmpBI.createGraphics();
tmpGraphics.drawImage(getBi(), 0, 0, width, height, x, y, x + width, y + width, null);
......
......@@ -38,7 +38,7 @@ public class SvgImageGenerator extends AbstractImageGenerator {
}
@Override
public void saveToFile(final String fileName) throws SVGGraphics2DIOException {
public void saveToFileImplementation(final String fileName) throws SVGGraphics2DIOException {
((SVGGraphics2D) getGraphics()).stream(fileName);
}
......@@ -58,18 +58,18 @@ public class SvgImageGenerator extends AbstractImageGenerator {
}
@Override
public void saveToOutputStream(OutputStream os) throws IOException {
public void saveToOutputStreamImplementation(OutputStream os) throws IOException {
((SVGGraphics2D) getGraphics()).stream(new OutputStreamWriter(os));
}
@Override
public void savePartToFile(int x, int y, int width, int height, String fileName) throws IOException {
public void savePartToFileImplementation(int x, int y, int width, int height, String fileName) throws IOException {
throw new NotImplementedException("Partial save is not implemented in SVG image generator");
}
@Override
public void savePartToOutputStream(int x, int y, int width, int height, OutputStream os) throws IOException {
public void savePartToOutputStreamImplementation(int x, int y, int width, int height, OutputStream os) throws IOException {
throw new NotImplementedException("Partial save is not implemented in SVG image generator");
}
......
......@@ -375,22 +375,22 @@ public class MinervaCanvas extends JComponent {
}
@Override
public void saveToFile(String fileName) throws IOException {
public void saveToFileImplementation(String fileName) throws IOException {
throw new NotImplementedException();
}
@Override
public void saveToOutputStream(OutputStream os) throws IOException {
public void saveToOutputStreamImplementation(OutputStream os) throws IOException {
throw new NotImplementedException();
}
@Override
public void savePartToFile(int x, int y, int width, int height, String fileName) throws IOException {
public void savePartToFileImplementation(int x, int y, int width, int height, String fileName) throws IOException {
throw new NotImplementedException();
}
@Override
public void savePartToOutputStream(int x, int y, int width, int height, OutputStream os) throws IOException {
public void savePartToOutputStreamImplementation(int x, int y, int width, int height, OutputStream os) throws IOException {
throw new NotImplementedException();
}
......
......@@ -17,6 +17,7 @@ import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.commands.CommandExecutionException;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.converter.ConverterException;
import lcsb.mapviewer.converter.graphics.DrawingException;
import lcsb.mapviewer.model.cache.FileEntry;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.layout.InvalidColorSchemaException;
......@@ -64,7 +65,7 @@ public class ProjectController extends BaseController {
@RequestParam(value = "overlayIds", defaultValue = "") String overlayIds, //
@RequestParam(value = "zoomLevel", defaultValue = "") String zoomLevel, //
@RequestParam(value = "polygonString", defaultValue = "") String polygonString//
) throws SecurityException, QueryException, IOException, InvalidColorSchemaException, CommandExecutionException {
) throws SecurityException, QueryException, IOException, InvalidColorSchemaException, CommandExecutionException, DrawingException {
FileEntry file = projectController.getModelAsImage(token, projectId, modelId, handlerClass, backgroundOverlayId, overlayIds, zoomLevel, polygonString);
MediaType type = MediaType.APPLICATION_OCTET_STREAM;
......
......@@ -33,6 +33,7 @@ import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.converter.ConverterException;
import lcsb.mapviewer.converter.IConverter;
import lcsb.mapviewer.converter.graphics.AbstractImageGenerator.Params;
import lcsb.mapviewer.converter.graphics.DrawingException;
import lcsb.mapviewer.converter.graphics.ImageGenerators;
import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser;
import lcsb.mapviewer.converter.model.sbgnml.SbgnmlXmlConverter;
......@@ -215,7 +216,8 @@ public class ProjectRestImpl extends BaseRestImpl {
}
public FileEntry getModelAsImage(String token, String projectId, String modelId, String handlerClass, String backgroundOverlayId, String overlayIds,
String zoomLevel, String polygonString) throws SecurityException, QueryException, IOException, InvalidColorSchemaException, CommandExecutionException {
String zoomLevel, String polygonString)
throws SecurityException, QueryException, IOException, InvalidColorSchemaException, CommandExecutionException, DrawingException {
AuthenticationToken authenticationToken = userService.getToken(token);
User user = userService.getUserByToken(authenticationToken);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment