packagecom.sr.biofunet.web.controller.utils;importjava.awt.ComponentOrientation;importjava.awt.Dimension;importjava.awt.Graphics;importjava.awt.Rectangle;importjava.awt.image.BufferedImage;importjava.beans.PropertyChangeEvent;importjava.beans.PropertyChangeListener;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.net.URL;importjava.util.Iterator;importjava.util.List;importjava.util.Map.Entry;importjavax.imageio.ImageIO;importjavax.swing.JEditorPane;public classHtmlImageGenerator {private JEditorPane editorPane = this.createJEditorPane();static final Dimension DEFAULT_SIZE = new Dimension(800, 800);publicHtmlImageGenerator() {
}publicComponentOrientation getOrientation() {return this.editorPane.getComponentOrientation();
}public voidsetOrientation(ComponentOrientation orientation) {this.editorPane.setComponentOrientation(orientation);
}publicDimension getSize() {return this.editorPane.getSize();
}public voidsetSize(Dimension dimension) {this.editorPane.setSize(dimension);
}public voidloadUrl(URL url) {try{this.editorPane.setPage(url);
}catch(IOException var3) {throw new RuntimeException(String.format("Exception while loading %s", newObject[]{url}), var3);
}
}public voidloadUrl(String url) {try{this.editorPane.setPage(url);
}catch(IOException var3) {throw new RuntimeException(String.format("Exception while loading %s", newObject[]{url}), var3);
}
}public voidloadHtml(String html) {this.editorPane.setText(html);this.onDocumentLoad();
}publicString getLinksMapMarkup(String mapName) {
StringBuilder markup= newStringBuilder();
markup.append("\n");
Iterator i$= this.getLinks().iterator();while(i$.hasNext()) {
LinkInfo link=(LinkInfo)i$.next();
List bounds=link.getBounds();
Iterator i$1 =bounds.iterator();while(i$1.hasNext()) {
Rectangle bound= (Rectangle)i$1.next();int x1 = (int)bound.getX();int y1 = (int)bound.getY();int x2 = (int)((double)x1 +bound.getWidth());int y2 = (int)((double)y1 +bound.getHeight());
markup.append(String.format("
Iterator i$2 =link.getAttributes().entrySet().iterator();while(i$2.hasNext()) {
Entry entry= (Entry)i$2.next();
String attName=(String)entry.getKey();
String value=(String)entry.getValue();
markup.append(" ").append(attName).append("=\"").append(value.replace("\"", """)).append("\"");
}
markup.append(">\n");
}
}
markup.append("\n");returnmarkup.toString();
}public ListgetLinks() {
LinkHarvester harvester= new LinkHarvester(this.editorPane);returnharvester.getLinks();
}public voidsaveAsHtmlWithMap(String file, String imageUrl) {this.saveAsHtmlWithMap(newFile(file), imageUrl);
}public voidsaveAsHtmlWithMap(File file, String imageUrl) {
FileWriter writer= null;try{
writer= newFileWriter(file);
writer.append(""-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
writer.append("\n
\n");writer.append("
\n");String e= this.getLinksMapMarkup("map");
writer.write(e);
writer.append("
writer.append(imageUrl);
writer.append("\"/>\n");
writer.append("\n");
}catch(IOException var12) {throw new RuntimeException(String.format("Exception while saving \'%s\' html file", newObject[]{file}), var12);
}finally{if(writer != null) {try{
writer.close();
}catch(IOException var11) {
;
}
}
}
}public voidsaveAsImage(String file) {this.saveAsImage(newFile(file));
}public voidsaveAsImage(File file) {
BufferedImage img= this.getBufferedImage();try{
String e=FormatNameUtil.formatForFilename(file.getName());
ImageIO.write(img, e, file);
}catch(IOException var4) {throw new RuntimeException(String.format("Exception while saving \'%s\' image", newObject[]{file}), var4);
}
}protected voidonDocumentLoad() {
}publicDimension getDefaultSize() {returnDEFAULT_SIZE;
}publicBufferedImage getBufferedImage() {
Dimension prefSize= this.editorPane.getPreferredSize();
BufferedImage img= new BufferedImage(prefSize.width, this.editorPane.getPreferredSize().height, 2);
Graphics graphics=img.getGraphics();this.editorPane.setSize(prefSize);this.editorPane.paint(graphics);returnimg;
}protectedJEditorPane createJEditorPane() {
JEditorPane editorPane= newJEditorPane();
editorPane.setSize(this.getDefaultSize());
editorPane.setEditable(false);
SynchronousHTMLEditorKit kit= newSynchronousHTMLEditorKit();
editorPane.setEditorKitForContentType("text/html", kit);
editorPane.setContentType("text/html");
editorPane.addPropertyChangeListener(newPropertyChangeListener() {public voidpropertyChange(PropertyChangeEvent evt) {if(evt.getPropertyName().equals("page")) {
HtmlImageGenerator.this.onDocumentLoad();
}
}
});returneditorPane;
}
}