package devopsdistilled.operp.client.items.panes;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.inject.Inject;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import devopsdistilled.operp.client.abstracts.SubTaskPane;
import devopsdistilled.operp.client.items.models.observers.CategoryModelObserver;
import devopsdistilled.operp.client.items.panes.controllers.CreateProductPaneController;
import devopsdistilled.operp.client.items.panes.models.observers.CreateProductPaneModelObserver;
import devopsdistilled.operp.server.data.entity.items.Category;

public class CreateProductPane extends SubTaskPane implements
		CreateProductPaneModelObserver, CategoryModelObserver {

	@Inject
	private CreateProductPaneController controller;

	private final JPanel pane;
	private final JTextField productNameField;
	private final JPanel categoryPanel;

	DefaultListModel<Category> listModel;
	private final JList<Category> categoryList;

	public CreateProductPane() {
		pane = new JPanel();
		pane.setLayout(new MigLayout("", "[][grow]", "[][grow][]"));

		JLabel lblProductName = new JLabel("Product Name");
		pane.add(lblProductName, "cell 0 0,alignx trailing");

		productNameField = new JTextField();
		pane.add(productNameField, "cell 1 0,growx");
		productNameField.setColumns(10);

		JLabel lblCategory = new JLabel("Category");
		pane.add(lblCategory, "cell 0 1");

		categoryPanel = new JPanel();
		categoryPanel.setLayout(new MigLayout("flowy", "[92px,grow]",
				"[23px,grow]"));

		JScrollPane scrollPane = new JScrollPane(categoryPanel);

		categoryList = new JList<>();
		categoryPanel.add(categoryList, "cell 0 0,grow");
		pane.add(scrollPane, "cell 1 1,grow");

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getDialog().dispose();
			}
		});
		pane.add(btnCancel, "flowx,cell 1 2");

		JButton btnCreate = new JButton("Create");
		pane.add(btnCreate, "cell 1 2");
	}

	@Override
	public JComponent getPane() {
		return pane;
	}

	@Override
	public void updateCategories(List<Category> categories) {
		listModel = null;
		listModel = new DefaultListModel<>();
		for (Category category : categories) {
			listModel.addElement(category);
		}
		categoryList.setModel(listModel);
	}
}

@@ -113,24 +113,53 @@
 onListener;%0A
+import java.util.LinkedList;%0A
 import java.
@@ -169,16 +169,16 @@
 l.List;%0A
-
 %0Aimport 
@@ -343,24 +343,56 @@
 wing.JList;%0A
+import javax.swing.JOptionPane;%0A
 import javax
@@ -564,24 +564,177 @@
 ubTaskPane;%0A
+import devopsdistilled.operp.client.items.exceptions.EntityNameExistsException;%0Aimport devopsdistilled.operp.client.items.exceptions.NullFieldException;%0A
 import devop
@@ -1052,16 +1052,79 @@
 ategory;
+%0Aimport devopsdistilled.operp.server.data.entity.items.Product;
 %0A%0Apublic
@@ -2467,24 +2467,24 @@
 ell 1 2%22);%0A%0A
-
 %09%09JButton bt
@@ -2512,24 +2512,788 @@
 (%22Create%22);%0A
+%09%09btnCreate.addActionListener(new ActionListener() %7B%0A%09%09%09@Override%0A%09%09%09public void actionPerformed(ActionEvent e) %7B%0A%09%09%09%09List%3CCategory%3E categories = new LinkedList%3C%3E();%0A%09%09%09%09categories.addAll(categoryList.getSelectedValuesList());%0A%09%09%09%09Product product = new Product();%0A%09%09%09%09String productName = productNameField.getText().trim();%0A%09%09%09%09product.setProductName(productName);%0A%09%09%09%09product.setCategories(categories);%0A%0A%09%09%09%09try %7B%0A%0A%09%09%09%09%09controller.validate(product);%0A%09%09%09%09%09product = controller.save(product);%0A%0A%09%09%09%09%7D catch (NullFieldException e1) %7B%0A%09%09%09%09%09JOptionPane.showMessageDialog(getPane(),%0A%09%09%09%09%09%09%09%22Product Name can't be empty%22);%0A%09%09%09%09%7D catch (EntityNameExistsException e1) %7B%0A%09%09%09%09%09JOptionPane.showMessageDialog(getPane(),%0A%09%09%09%09%09%09%09%22Product Name already exists%22);%0A%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D);%0A
 %09%09pane.add(b

Implement "Create" action