package com.dua3.utility.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dua3.utility.text.MarkDownStyle;
import com.dua3.utility.text.MarkDownTest;
import com.dua3.utility.text.MarkDownUtil;
import com.dua3.utility.text.RichText;

public class SwingUtilTest extends JFrame {

	private static final long serialVersionUID = 1L;

    private static final Logger LOG = LoggerFactory.getLogger(SwingUtilTest.class);

	public static void main(String[] args) throws Exception {
	    LOG.info("setting native look and feel");
        SwingUtil.setNativeLookAndFeel(SwingUtilTest.class.getSimpleName());

        String testfile = args.length == 0 ? "syntax.md" : args[0];
        LOG.info("test file is '{}'", testfile);

        SwingUtilTest inst = new SwingUtilTest(testfile);
        inst.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        inst.setSize(new Dimension(800,600));
        LOG.info("instance created");

        inst.setVisible(true);
    }

    public SwingUtilTest(String testfile) throws Exception {
        super("Swing Utilities test");
        JTabbedPane tabs = new JTabbedPane();
        add(tabs);
        tabs.add("MarkDown Test", new MdTestPanel(testfile));
        pack();
    }

    static class MdTestPanel extends JPanel {
        private static final long serialVersionUID = 1L;

        private JTextPane mdComponent = new JTextPane();
        private JTextArea sourceComponent = new JTextArea();

        public MdTestPanel(String testfile) throws Exception {
            setLayout(new BorderLayout());
            String mdSource = MarkDownTest.getTestData(testfile);

            JTabbedPane tabs = new JTabbedPane();
            add(tabs, BorderLayout.CENTER);
            tabs.add("preview", new JScrollPane(mdComponent));
            tabs.add("source", new JScrollPane(sourceComponent));

            setSource(mdSource);
        }

        void setSource(String mdSource) {
            LOG.info("setting source");
            sourceComponent.setText(mdSource);
            LOG.info("creating RichText from source");
            RichText richtext = MarkDownUtil.convert(mdSource);
            LOG.info("converting RichText to StyledDocument");
			StyledDocument doc = StyledDocumentBuilder.toStyledDocument(richtext, MarkDownStyle::getAttributes);
            LOG.info("updating document in UI component");
            mdComponent.setDocument(doc);
            LOG.info("source set");
        }

    }
}

@@ -269,24 +269,60 @@
 JTextPane;%0D%0A
+import javax.swing.SwingUtilities;%0D%0A
 import javax
@@ -1076,32 +1076,78 @@
 , testfile);%0D%0A%0D%0A
+        SwingUtilities.invokeLater(()-%3E%7B%0D%0A    
         SwingUti
@@ -1189,32 +1189,36 @@
 file);%0D%0A        
+ 
+   
 inst.setDefaultC
@@ -1258,32 +1258,36 @@
 LOSE);%0D%0A        
+   
+ 
 inst.setSize(new
@@ -1309,32 +1309,36 @@
 600));%0D%0A        
+    
 LOG.info(%22instan
@@ -1348,26 +1348,28 @@
 created%22);%0D%0A
-%0D%0A
+    
         inst
@@ -1384,24 +1384,37 @@
 ble(true);%0D%0A
+        %7D);%0D%0A
     %7D%0D%0A%0D%0A   
@@ -1451,33 +1451,16 @@
 estfile)
- throws Exception
  %7B%0D%0A    
@@ -1637,13 +1637,86 @@
     
-pack(
+LOG.info(%22packing layout%22);%0D%0A        pack();%0D%0A        LOG.info(%22SwingUtilTest%22
 );%0D%0A
@@ -1996,33 +1996,16 @@
 estfile)
- throws Exception
  %7B%0D%0A    

create GUI elements on EDT