package devopsdistilled.operp.client.business.sales.panes;

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

import javax.inject.Inject;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;
import devopsdistilled.operp.client.abstracts.EntityOperation;
import devopsdistilled.operp.client.abstracts.EntityPane;
import devopsdistilled.operp.client.business.sales.controllers.SaleController;
import devopsdistilled.operp.client.business.sales.panes.controllers.SalePaneController;
import devopsdistilled.operp.client.business.sales.panes.models.observers.SalePaneModelObserver;
import devopsdistilled.operp.client.party.controllers.CustomerController;
import devopsdistilled.operp.client.party.models.observers.CustomerModelObserver;
import devopsdistilled.operp.server.data.entity.business.Sale;
import devopsdistilled.operp.server.data.entity.party.Customer;

public class SalePane extends
		EntityPane<Sale, SaleController, SalePaneController> implements
		SalePaneModelObserver, CustomerModelObserver {

	@Inject
	private SaleController saleController;

	@Inject
	private CustomerController customerController;

	private final JPanel pane;
	private final JComboBox<Customer> customerCombo;
	private JPanel opBtnPanel;
	private JPanel saleDescPanel;

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

		JLabel lblCustomer = new JLabel("Customer");
		pane.add(lblCustomer, "cell 0 0,alignx trailing");

		customerCombo = new JComboBox<>();
		customerCombo.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED)
					getController().getModel().getEntity()
							.setParty((Customer) e.getItem());
			}
		});
		pane.add(customerCombo, "flowx,cell 1 0,growx");

		JButton btnNewCustomer = new JButton("New Customer");
		btnNewCustomer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				customerController.create();
			}
		});
		pane.add(btnNewCustomer, "cell 1 0");

		saleDescPanel = new JPanel();
		pane.add(saleDescPanel, "cell 0 2 2097051 1,grow");
		saleDescPanel.setLayout(new MigLayout("", "[]", "[]"));

		opBtnPanel = new JPanel();
		pane.add(opBtnPanel, "cell 1 4,grow");
		opBtnPanel.setLayout(new MigLayout("", "[][][grow]", "[grow]"));

		JPanel panel = new JPanel();
		opBtnPanel.add(panel, "cell 2 0,grow");
	}

	@Override
	public void resetComponents() {
		// TODO Auto-generated method stub

	}

	@Override
	public SaleController getEntityController() {
		return saleController;
	}

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

	@Override
	public void updateEntity(Sale sale, EntityOperation entityOperation) {
		if (EntityOperation.Create == entityOperation) {
			getController().getModel().setTitle("New Sale");
			opBtnPanel = setBtnPanel(createOpPanel, opBtnPanel);
		}
	}

	public void setSaleDescpanel(JPanel saleDescPanel) {
		MigLayout layout = (MigLayout) pane.getLayout();
		Object constraints = layout.getComponentConstraints(this.saleDescPanel);

		pane.remove(this.saleDescPanel);
		pane.add(saleDescPanel, constraints);
		this.saleDescPanel = saleDescPanel;
		pane.validate();
	}

	@Override
	public void updateCustomers(List<Customer> customers) {
		Customer prevSelected = (Customer) customerCombo.getSelectedItem();
		customerCombo.removeAllItems();

		for (Customer customer : customers) {
			customerCombo.addItem(customer);
			if (prevSelected != null)
				if (prevSelected.compareTo(customer) == 0)
					customerCombo.setSelectedItem(customer);
		}
	}
}

@@ -1448,21 +1448,24 @@
  JPanel 
-opBtn
+saleDesc
 Panel;%0A%09
@@ -1472,36 +1472,71 @@
 private 
-JP
+final JButton btnC
 an
+c
 el
- saleDescPanel
+;%0A%09private final JButton btnSale
 ;%0A%0A%09publ
@@ -2421,14 +2421,8 @@
  2 2
-097051
  1,g
@@ -2489,24 +2489,23 @@
 %22));%0A%0A%09%09
-opBtnP
+btnC
 an
+c
 el = new
@@ -2510,187 +2510,280 @@
 ew J
-P
+Button(%22C
 an
+c
 el
-(
+%22
 );%0A%09%09
-pane.add(opBtnPanel, %22cell 1 4,grow%22);%0A%09%09opBtnPanel.setLayout(new MigLayout(%22%22, %22%5B%5D%5B%5D%5Bgrow%5D%22, %22%5Bgrow%5D%22));%0A%0A%09%09JPanel panel = new JPanel();%0A%09%09opBtnP
+btnCancel.addActionListener(new ActionListener() %7B%0A%09%09%09@Override%0A%09%09%09public void actionPerformed(ActionEvent e) %7B%0A%09%09%09%09dispose();%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09pane.add(btnCancel, %22flowx,cell 1 4,alignx right%22);%0A%0A%09%09btnSale = new JButton(%22Sale%22);%0A%09%09p
 ane
-l
 .add(
-panel
+btnSale
 , %22cell 
 2 0,
@@ -2782,16 +2782,24 @@
 ell 
-2 0,grow
+1 4,alignx right
 %22);%0A
@@ -3226,64 +3226,8 @@
 %22);%0A
-%09%09%09opBtnPanel = setBtnPanel(createOpPanel, opBtnPanel);%0A
 %09%09%7D%0A

Use own buttons,Impl "Cancel"