Skip to content
Browse files

Issue #16. Refactoring towards support of different image formats

  • Loading branch information...
1 parent 9690cdc commit 2fc6181868bdd5bcaa2da47f32d7117d54c02177 @esteinberg committed Jun 23, 2012
View
48 src/org/plantuml/idea/plantuml/PlantUml.java
@@ -1,5 +1,7 @@
package org.plantuml.idea.plantuml;
+import net.sourceforge.plantuml.FileFormat;
+import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader;
import javax.imageio.ImageIO;
@@ -10,15 +12,39 @@
import java.io.InputStream;
/**
- *
* @author Eugene Steinberg
*/
public class PlantUml {
+ public enum ImageFormat {
+ SVG {
+ @Override
+ FileFormat getFormat() {
+ return FileFormat.SVG;
+ }
+ },
+ PDF {
+ @Override
+ FileFormat getFormat() {
+ return FileFormat.PDF;
+ }
+ },
+ PNG {
+ @Override
+ FileFormat getFormat() {
+ return FileFormat.PNG;
+ }
+ };
+
+ abstract FileFormat getFormat();
+ }
+
public static final String STARTUML = "@startuml";
- public static PlantUmlResult render(String source) {
- BufferedImage renderedImage = null;
+ public static PlantUmlResult render (String source) {
+ return render (source, ImageFormat.PNG);
+ }
+ public static PlantUmlResult render(String source, ImageFormat format) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream is = null;
String desc = null;
@@ -27,14 +53,12 @@ public static PlantUmlResult render(String source) {
// image generation.
SourceStringReader reader = new SourceStringReader(source);
// Write the image to "os"
- desc = reader.generateImage(os);
+
+ desc = reader.generateImage(os, new FileFormatOption(format.getFormat()));
// close all the flow.
os.flush();
os.close();
- is = new ByteArrayInputStream(os.toByteArray());
- renderedImage = ImageIO.read(new ByteArrayInputStream(os.toByteArray()));
- // close the input stream
- is.close();
+
} catch (IOException e) {
error = e.getMessage();
} finally {
@@ -43,17 +67,13 @@ public static PlantUmlResult render(String source) {
} catch (IOException e) {
// do nothing
}
- try {
- if (is != null) is.close();
- } catch (IOException e) {
- // do nothing
- }
}
- return new PlantUmlResult(renderedImage, desc, error);
+ return new PlantUmlResult(os.toByteArray(), desc, error);
}
/**
* Checks wherever content contains PlantUML source code
+ *
* @param content input content
* @return true if content is a PlantUML source
*/
View
18 src/org/plantuml/idea/plantuml/PlantUmlResult.java
@@ -1,28 +1,28 @@
package org.plantuml.idea.plantuml;
-import java.awt.image.BufferedImage;
+import java.util.Arrays;
/**
*
* @author Eugene Steinberg
*/
public class PlantUmlResult {
- private BufferedImage diagram;
+ private byte[] diagramBytes;
private String description;
private String error;
- public PlantUmlResult(BufferedImage diagram, String description, String error) {
- this.diagram = diagram;
+ public PlantUmlResult(byte[] diagramBytes, String description, String error) {
+ this.diagramBytes = diagramBytes;
this.description = description;
this.error = error;
}
- public BufferedImage getDiagram() {
- return diagram;
+ public byte[] getDiagramBytes() {
+ return diagramBytes;
}
- public void setDiagram(BufferedImage diagram) {
- this.diagram = diagram;
+ public void setDiagramBytes(byte[] diagramBytes) {
+ this.diagramBytes = diagramBytes;
}
public String getDescription() {
@@ -48,7 +48,7 @@ public void setError(String error) {
@Override
public String toString() {
return "PlantUmlResult{" +
- "diagram=" + diagram +
+ "diagramBytes=" + Arrays.toString(diagramBytes) +
", description='" + description + '\'' +
", error='" + error + '\'' +
'}';
View
31 src/org/plantuml/idea/toolwindow/PlantUmlToolWindowFactory.java
@@ -35,6 +35,7 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
/**
@@ -148,12 +149,30 @@ public void run() {
}
private void render(String source) {
- final PlantUmlResult result = PlantUml.render(source);
- ApplicationManager.getApplication().invokeLater(new Runnable() {
- public void run() {
- setDiagram(result.getDiagram());
- }
- });
+ PlantUmlResult result = PlantUml.render(source);
+ try {
+ final BufferedImage image = getBufferedImage(result.getDiagramBytes());
+ ApplicationManager.getApplication().invokeLater(new Runnable() {
+ public void run() {
+ setDiagram(image);
+ }
+ });
+ } catch (IOException e) {
+ System.out.println("Exception occured rendering source = " + source + ": " + e);
+ }
+
+ }
+
+ private static BufferedImage getBufferedImage(byte[] imageBytes) throws IOException {
+ ByteArrayInputStream input = new ByteArrayInputStream(imageBytes);
+ BufferedImage bufferedImage;
+ try {
+ bufferedImage = ImageIO.read(input);
+ } finally {
+ if (input != null)
+ input.close();
+ }
+ return bufferedImage;
}
private class copyToClipboardActionListener implements ActionListener {

0 comments on commit 2fc6181

Please sign in to comment.
Something went wrong with that request. Please try again.