package com.thedeanda.ajaxproxy.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
import javax.swing.UIDefaults;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;

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

public class SwingUtils {
	// private static final Executor executor = Executors.newFixedThreadPool(3);

	private static final Logger log = LoggerFactory.getLogger(SwingUtils.class);

	public static JLabel newJLabel(String label) {
		JLabel ret = new JLabel(label);
		ret.setHorizontalAlignment(SwingConstants.RIGHT);
		return ret;
	}

	public static JComboBox<?> newJComboBox(Object[] items) {
		JComboBox<Object> combo = new JComboBox<>(items);
		Insets insets = new Insets(4, 4, 4, 4);
		combo.setBorder(BorderFactory.createEmptyBorder());

		return combo;
	}

	public static JTextField newJTextField() {
		return prepJTextField(new JTextField());
	}

	public static JTextField prepJTextField(JTextField field) {
		Insets insets = new Insets(4, 4, 4, 4);
		field.setMargin(insets);
		return field;
	}

	public static JTextArea newJTextArea() {
		JTextArea ret = new JTextArea();
		Insets insets = new Insets(4, 4, 4, 4);
		ret.setMargin(insets);
		// ret.setHorizontalAlignment(SwingConstants.RIGHT);
		return ret;
	}

	public static void flattenSplitPane(JSplitPane jSplitPane) {
		UIDefaults defaults = javax.swing.UIManager.getDefaults();
		final Color light = defaults.getColor("SplitPane.highlight");
		final Color dark = defaults.getColor("SplitPane.darkShadow");

		// *
		jSplitPane.setUI(new BasicSplitPaneUI() {
			public BasicSplitPaneDivider createDefaultDivider() {
				BasicSplitPaneDivider divider = new BasicSplitPaneDivider(this) {
					private static final long serialVersionUID = 1L;

					@Override
					public int getDividerSize() {
						return 5;
					}

					@Override
					public void paint(Graphics g) {
						// super.paint(g);
						int orientation = this.getBasicSplitPaneUI()
								.getOrientation();

						Dimension size = this.getSize();

						if (orientation == JSplitPane.VERTICAL_SPLIT) {
							int[] lines = new int[2];
							lines[0] = 0;
							lines[1] = size.height - 2;

							for (int i = 0; i < size.width; i += 4) {
								for (int j = 0; j < lines.length; j++) {
									int y = lines[j];
									g.setColor(light);
									g.fillRect(i, y, 2, 2);
									g.setColor(dark);
									g.fillRect(i, y, 1, 1);
								}
							}
						} else {
							int[] rows = new int[2];
							rows[0] = 0;
							rows[1] = size.width - 2;

							for (int i = 0; i < size.height; i += 4) {
								for (int j = 0; j < rows.length; j++) {
									int x = rows[j];
									g.setColor(light);
									g.fillRect(x, i, 2, 2);
									g.setColor(dark);
									g.fillRect(x, i, 1, 1);
								}
							}
						}
					}
				};
				return divider;
			}
		});
		jSplitPane.setBorder(null);
		// */
	}

	public static void executNonUi(Runnable runnable) {
		// TODO: need to shutdown threadpool so app can quit
		// executor.execute(runnable);

		new Thread(runnable).start();
	}
}

@@ -57,35 +57,8 @@
 or;%0A
-import java.awt.Component;%0A
 impo
@@ -80,16 +80,16 @@
 ension;%0A
+
 import j
@@ -158,24 +158,35 @@
 current.
-Executor
+LinkedBlockingQueue
 ;%0Aimport
@@ -207,25 +207,72 @@
 current.
-Executors
+ThreadPoolExecutor;%0Aimport java.util.concurrent.TimeUnit
 ;%0A%0Aimpor
@@ -361,34 +361,8 @@
 el;%0A
-import javax.swing.JList;%0A
 impo
@@ -453,45 +453,8 @@
 ld;%0A
-import javax.swing.ListCellRenderer;%0A
 impo
@@ -702,19 +702,16 @@
 tils %7B%0A%09
-// 
 private 
@@ -723,16 +723,26 @@
 c final 
+ThreadPool
 Executor
@@ -757,40 +757,153 @@
 r = 
-Executors.newFixedThreadPool(3);
+new ThreadPoolExecutor(%0A%09%09%091, 6, 3, TimeUnit.SECONDS, new LinkedBlockingQueue%3CRunnable%3E());%0A%09static %7B%0A%09%09executor.allowCoreThreadTimeOut(true);%0A%09%7D
 %0A%0A%09p
@@ -3551,19 +3551,16 @@
  quit%0A%09%09
-// 
 executor
@@ -3582,16 +3582,19 @@
 le);%0A%0A%09%09
+// 
 new Thre

fix threadpool... again