package cpup.littleBoxes.gui;

import cpup.littleBoxes.Box;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class BoxGUI extends JComponent {
	protected final Box box;

	protected final Font font = new Font(Font.MONOSPACED, Font.BOLD, 20);
	protected final Font smallFont = new Font(Font.MONOSPACED, Font.PLAIN, 15);
	protected boolean isFocused = false;

	public BoxGUI(Box _box) {
		box = _box;
		setFont(font);
		setFocusable(true);
		addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent mouseEvent) {
				grabFocus();
			}

			@Override
			public void mousePressed(MouseEvent mouseEvent) {}

			@Override
			public void mouseReleased(MouseEvent mouseEvent) {}

			@Override
			public void mouseEntered(MouseEvent mouseEvent) {}

			@Override
			public void mouseExited(MouseEvent mouseEvent) {}
		});
		addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent focusEvent) {
				isFocused = true;
				repaint();
			}

			@Override
			public void focusLost(FocusEvent focusEvent) {
				isFocused = false;
				repaint();
			}
		});
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);

		final FontMetrics fontMetrics = getFontMetrics(font);
		final int charWidth = fontMetrics.getWidths()[0];
		final int charHeight = fontMetrics.getHeight();

		g.setFont(font);
		if(isFocused) {
			g.setColor(Color.gray);
			g.fillRect(0, 0, getWidth(), getHeight());
		}
		g.setColor(Color.black);
		g.drawRect(0, 0, getWidth(), getHeight()); //, getWidth() / 4, getHeight() / 4);

		g.setColor(Color.black);
		g.drawString(Integer.toString(box.getValue()), (getWidth() - charWidth) / 2 + 1, (getHeight() - charHeight) / 2 + 12);

		final FontMetrics littleMetrics = getFontMetrics(smallFont);
		final int littleWidth = littleMetrics.getWidths()[0];
		final int littleHeight = littleMetrics.getHeight();

		int littlePlace = getWidth() - box.getLittleBoxes().size() * (littleWidth + 2) - 2;

		g.setFont(smallFont);
		for(Integer littleBox : box.getLittleBoxes()) {
			g.drawString(littleBox.toString(), littlePlace, 10 + littleHeight / 2);
			littlePlace += littleWidth + 2;
		}
	}

	// Getters and Setters
	public Box getBox() {
		return box;
	}
}
@@ -121,126 +121,9 @@
 ent.
-FocusEvent;%0Aimport java.awt.event.FocusListener;%0Aimport java.awt.event.MouseEvent;%0Aimport java.awt.event.MouseListener
+*
 ;%0A%0Ap
@@ -1124,16 +1124,736 @@
 %7D%0A%09%09%7D);%0A
+%09%09addKeyListener(new KeyListener() %7B%0A%09%09%09@Override%0A%09%09%09public void keyTyped(KeyEvent ev) %7B%0A%09%09%09%09System.out.printf(%22key: %25s%25n%22, ev.getKeyChar());%0A%09%09%09%09try %7B%0A%09%09%09%09%09Integer val = Integer.parseInt(Character.toString(ev.getKeyChar()));%0A%09%09%09%09%09System.out.printf(%22num: %25d%25n%22, val);%0A%09%09%09%09%09if(ev.isShiftDown()) %7B%0A%09%09%09%09%09%09box.setValue(val);%0A%09%09%09%09%09%09box.getLittleBoxes().clear();%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09if(box.getLittleBoxes().contains(val)) %7B%0A%09%09%09%09%09%09%09box.getLittleBoxes().remove(val);%0A%09%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09%09box.getLittleBoxes().add(val);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%09repaint();%0A%09%09%09%09%7D catch(NumberFormatException ex) %7B%7D%0A%09%09%09%7D%0A%0A%09%09%09@Override%0A%09%09%09public void keyPressed(KeyEvent keyEvent) %7B%7D%0A%0A%09%09%09@Override%0A%09%09%09public void keyReleased(KeyEvent keyEvent) %7B%7D%0A%09%09%7D);%0A
 %09%7D%0A%0A%09@Ov

Add a key listener to BoxGUI to change stuff