package basics.gui.toolkit;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.WindowListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 * Convenience utilities for creating the gui.
 * @author kst
 */
public class GU
{
public static String defaultFontFamily = "Monospaced";
public static int    defaultFontSize   = 10;
public static Font   defaultFont       = new Font(defaultFontFamily, Font.PLAIN, defaultFontSize);
public static Border defaultBorder     = BorderFactory.createLineBorder(Color.lightGray, 1);
public static int    defaultTabSize    = 3;

private GU()
{
}

// ================ component default setter =====================
public static void defaultborder(JComponent c)
{
   c.setBorder(defaultBorder);
}

public static void defaultfont(Component c)
{
   c.setFont(defaultFont);
}

public static void defaulttabsize(JTextArea c)
{
   c.setTabSize(defaultTabSize);
}

// ------------------ component setter ----------------------
public static void align_topleft(JComponent c)
{
   c.setAlignmentX(Component.LEFT_ALIGNMENT);
   c.setAlignmentY(Component.TOP_ALIGNMENT);
}

public static void borderless(JComponent c)
{
   c.setBorder(BorderFactory.createEmptyBorder());
}

public static void readonly(JTextComponent c)
{
   c.setEditable(false);
}

public static void size(Component c, int w, int h)
{
   if(w > 0 && h > 0) c.setPreferredSize(new Dimension(w, h));
}

public static void size(Component c, Dimension d)
{
   if(d.getWidth() > 0 && d.getHeight() > 0) c.setPreferredSize(d);
}

public static void autoscroll(JComponent c)
{
   c.setAutoscrolls(true);
}

public static void wrap(JTextArea c)
{
   c.setLineWrap(true);
   c.setWrapStyleWord(true);
}

// ==================== COMPONENTS ==============================
public static JFrame mainFrame(String name, Dimension d, WindowListener winListener)
{
   JFrame c = new JFrame(name);
   if(d.getWidth() > 0 && d.getHeight() > 0) c.setPreferredSize(d);
   c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   c.addWindowListener(winListener);
   return c;
}

// ----------------------- text -------------------------------
public static JLabel label(String text)
{
   JLabel c = new JLabel(text);
   borderless(c);
   defaultfont(c);
   return c;
}

public static JTextArea textarea()
{
   return(textarea(new JTextArea().getDocument()));
}

public static JTextArea textarea(Document d)
{
   JTextArea c = new JTextArea(d);
   borderless(c);
   defaultfont(c);
   wrap(c);
   autoscroll(c);
   defaulttabsize(c);
   return c;
}

public static JTextField textfield()
{
   JTextField c = new JTextField();
   borderless(c);
   defaultfont(c);
   return c;
}

public static JTextPane textPane()
{
   return(textPane(new DefaultStyledDocument()));
}

public static JTextPane textPane(DefaultStyledDocument doc)
{
   JTextPane c = new JTextPane(doc);
   borderless(c);
   defaultfont(c);
   autoscroll(c);
   return c;
}

// ----------------------- tree -------------------------------
public static JTree jtree(DefaultMutableTreeNode node, int w, int h, KeyListener key,
   MouseListener mouse)
{
   JTree c = new JTree(node);
   defaultfont(c);
   borderless(c);
   if(key != null) c.addKeyListener(key);
   if(mouse != null) c.addMouseListener(mouse);
   size(c, w, h);
   return c;
}

// ----------------------- panes -------------------------------
public static JScrollPane scrollpane(JComponent c)
{
   // autoscroll(c);
   // borderless(c);
   JScrollPane pane = new JScrollPane();
   // pane.setPreferredSize(c.getPreferredSize());
   pane.getViewport().add(c);
   borderless(pane);
   pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
   pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
   return pane;
}

public static JScrollPane tablepane(String [] colheads, JTable c, KeyListener listener,
   LayoutManager layout)
{
   // defaultfont(c);
   // borderless(c);
   // autoscroll(c);
   DefaultTableModel tableModel = (DefaultTableModel)c.getModel();
   tableModel.setColumnIdentifiers(colheads);
   JScrollPane pane = new JScrollPane(c);
   borderless(pane);
   pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
   return pane;
}

public static JTabbedPane tabbedpane()
{
   JTabbedPane c = new JTabbedPane();
   borderless(c);
   defaultfont(c);
   return c;
}

public static JSplitPane hsplitpane(Component left, Component right)
{
   JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false);
   borderless(splitPane);
   splitPane.setLeftComponent(left);
   splitPane.setRightComponent(right);
   return splitPane;
}

public static JSplitPane vsplitpane(Component top, Component bottom)
{
   JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false);
   borderless(splitPane);
   splitPane.setTopComponent(top);
   splitPane.setBottomComponent(bottom);
   return splitPane;
}

// ----------------------- box -------------------------------
public static Box vbox(Component [] components)
{
   return setupbox(Box.createVerticalBox(), components);
}

public static Box hbox(Component [] components)
{
   return setupbox(Box.createHorizontalBox(), components);
}

private static Box setupbox(Box b, Component [] cs)
{
   borderless(b);
   if(cs != null) for(Component c : cs)
      b.add(c);
   return b;
}

// ----------------------- other -------------------------------
public static JButton button(String name)
{
   JButton jb = new JButton(name);
   jb.setFont(defaultFont);
   return jb;
}

public static void hourglass(Component c, boolean on)
{
   Component parent = c.getParent();
   if(parent != null && parent.isShowing()) parent.setCursor(Cursor
      .getPredefinedCursor(on ? Cursor.WAIT_CURSOR : Cursor.DEFAULT_CURSOR));
}
//public static void paintNode (Node node, Graphics graphics, Design design)
//{
//   int x = node.screen_x ;
//   int y = node.screen_y;
//   FontMetrics fontMetrics = graphics.getFontMetrics();
//   int w = fontMetrics.stringWidth(node.label);
//   int h = fontMetrics.getHeight();
//   if (w % 2 == 0) x -= w / 2;
//   else x -= (w + 1) / 2;
//   int z = y - h / 2 + 1;
//   graphics.setColor(design.background);
//   graphics.fillRect(x, z, w, h - 1);
//   node.vp1 = new Point(x, z);
//   node.vp2 = new Point(x + w, z + h - 1);
//   y += fontMetrics.getDescent() + 1;
//   graphics.setColor(design.color);
//   graphics.setFont(design.font);
//   graphics.setColor(design.color);
//   graphics.drawString(node.label, x, y);
//}
}
