package org.reldb.dbrowser.ui.html;

import java.awt.Frame;
import java.io.IOException;

import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

public class BrowserSwing implements HtmlBrowser {
	
	private Composite browserPanel;
	private JTextPane browser;
	private Style style;
	private StringBuffer text;

	private void setEnhancedOutputStyle(JTextPane pane) {
		pane.setContentType("text/html");
		pane.setEditable(false);
		HTMLEditorKit editorKit = new HTMLEditorKit();
		HTMLDocument defaultDocument = (HTMLDocument)editorKit.createDefaultDocument();
		pane.setEditorKit(editorKit);
		pane.setDocument(defaultDocument);
		StyleSheet css = editorKit.getStyleSheet();
		for (String entry: style.getFormattedStyle())
			css.addRule(entry);
	}

	@Override
	public boolean createWidget(Composite parent) {
	    browserPanel = new Composite(parent, SWT.EMBEDDED);
	    Frame frame = SWT_AWT.new_Frame(browserPanel);
	    
	    style = new Style(0);
	    
		browser = new JTextPane();
		setEnhancedOutputStyle(browser);
		browser.setDoubleBuffered(true);
		DefaultCaret caret = (DefaultCaret)browser.getCaret();
	    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
	 		
		JScrollPane jScrollPaneOutput = new JScrollPane();
		jScrollPaneOutput.setAutoscrolls(true);
		jScrollPaneOutput.setViewportView(browser);
		
		frame.add(jScrollPaneOutput);
		
		clear();
		
		return true;
	}

	@Override
	public void clear() {
		text = new StringBuffer();
		browser.setText(style.getEmptyHTMLDocument());
	}

	@Override
	public void appendHtml(String s) {
		HTMLDocument doc = (HTMLDocument)browser.getDocument();
		HTMLEditorKit kit = (HTMLEditorKit)browser.getEditorKit();
	    try {
	    	kit.insertHTML((HTMLDocument) doc, doc.getLength(), s, 0, 0, null);
		} catch (BadLocationException | IOException e) {
			e.printStackTrace();
		}
	    text.append(s);
	}

	@Override
	public void scrollToBottom() {}

	@Override
	public Control getWidget() {
		return browserPanel;
	}

	@Override
	public String getText() {
		return style.getHTMLDocument(text.toString());
	}

	@Override
	public String getSelectedText() {
		return browser.getSelectedText();
	}

	@Override
	public boolean isSelectedTextSupported() {
		return true;
	}

	@Override
	public Style getStyle() {
		return style;
	}

	@Override
	public void dispose() {
	}

	@Override
	public void setContent(String content) {
		text = new StringBuffer(content);
		browser.setText(style.getHTMLDocument(content));
	}
	
	@Override
	public String getContent() {
		return text.toString();
	}

}

@@ -555,17 +555,16 @@
 owser %7B%0A
-%09
 %0A%09privat
@@ -882,16 +882,17 @@
 ocument)
+ 
 editorKi
@@ -1052,16 +1052,17 @@
 ng entry
+ 
 : style.
@@ -1170,20 +1170,17 @@
 ent) %7B%0A%09
-    
+%09
 browserP
@@ -1224,20 +1224,17 @@
 DDED);%0A%09
-    
+%09
 Frame fr
@@ -1276,19 +1276,11 @@
 l);%0A
-%09    %0A%09    
+%0A%09%09
 styl
@@ -1297,21 +1297,16 @@
 yle(0);%0A
-%09    
 %0A%09%09brows
@@ -1434,16 +1434,17 @@
 ltCaret)
+ 
 browser.
@@ -1456,20 +1456,17 @@
 ret();%0A%09
-    
+%09
 caret.se
@@ -1512,12 +1512,8 @@
 E);%0A
-%09 %09%09
 %0A%09%09J
@@ -1650,18 +1650,16 @@
 owser);%0A
-%09%09
 %0A%09%09frame
@@ -1683,18 +1683,16 @@
 utput);%0A
-%09%09
 %0A%09%09clear
@@ -1695,18 +1695,16 @@
 lear();%0A
-%09%09
 %0A%09%09retur
@@ -1913,16 +1913,17 @@
 ocument)
+ 
 browser.
@@ -1974,16 +1974,17 @@
 itorKit)
+ 
 browser.
@@ -2004,23 +2004,17 @@
 );%0A%09
-    
+%09
 try %7B%0A%09
-    
+%09
 %09kit
@@ -2162,12 +2162,9 @@
 %09%7D%0A%09
-    
+%09
 text
@@ -2221,16 +2221,18 @@
 ttom() %7B
+%0A%09
 %7D%0A%0A%09@Ove
@@ -2786,17 +2786,16 @@
 t));%0A%09%7D%0A
-%09
 %0A%09@Overr

Fix formatting.