package org.reldb.dbrowser.ui.html;

import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
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.jface.util.Util;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

public class BrowserSwing implements HtmlBrowser {

	private BrowserSwingWidget browserPanel;
	private JTextPane browser;
	private Style style;
	private StringBuffer text;

	private void setEnhancedOutputStyle(JTextPane pane) {
		pane.setContentType("text/html");
		pane.setEditable(false);
		pane.setEnabled(true);
		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 BrowserSwingWidget(parent);
		Frame frame = SWT_AWT.new_Frame(browserPanel);

		// Mac DPI = 72
		// Windows DPI = 96
		if (Util.isWin32())
			style = new Style((int)(14 * ((double)Display.getCurrent().getDPI().x / 72.0)));
		else
			style = new Style(0);

		Panel root = new Panel();
		root.setLayout(new GridLayout());
		
		browser = new JTextPane();
		KeyListener[] listeners = browser.getKeyListeners();
		for (KeyListener listener: listeners)
			browser.removeKeyListener(listener);
		browser.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (!(e.getKeyCode() == KeyEvent.VK_C))
					return;
				if (!(e.getModifiers() == Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()))
					return;
				e.consume();
				browserPanel.getDisplay().asyncExec(new Runnable() {
					@Override
					public void run() {
						browserPanel.copy();
					}
				});
			}
		});
		
		browserPanel.setJTextPane(browser);
		
		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);

		root.add(jScrollPaneOutput);
		
		frame.add(root);

		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();
	}

}

@@ -606,16 +606,106 @@
 WT_AWT;%0A
+import org.eclipse.swt.graphics.Image;%0Aimport org.eclipse.swt.graphics.ImageDataProvider;%0A
 import o
@@ -1454,24 +1454,310 @@
 entry);%0A%09%7D%0A%0A
+%09private int zoom;%0A%09%0A%09private void setZoomFactor(int zoom) %7B%0A%09%09this.zoom = zoom;%0A%09%7D%0A%09%0A%09private int getZoom() %7B%0A%09%09final ImageDataProvider imageDataProvider = zoom -%3E %7B%0A%09%09%09setZoomFactor(zoom);%0A%09%09%09return null;%0A%09%09%7D;%0A%09%09new Image(Display.getCurrent(), imageDataProvider);%0A%09%09return zoom;%0A%09%7D%0A%09%0A
 %09@Override%0A%09
@@ -1906,48 +1906,10 @@
 l);%0A
-%0A
 %09%09
-// Mac DPI = 72%0A%09%09// Windows DPI = 96
 %0A%09%09i
@@ -1960,20 +1960,21 @@
 )(14
+.0
  * 
-(
 (double)
 Disp
@@ -1973,49 +1973,27 @@
 ble)
-Display.getCurrent().getDPI().x / 72
+getZoom() / 100
 .0))
-)
 ;%0A%09%09

Add experimental scaling mechanism.