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

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;
import devopsdistilled.operp.client.abstracts.EntityOperation;
import devopsdistilled.operp.client.abstracts.EntityPane;
import devopsdistilled.operp.client.abstracts.libs.BeanTableModel;
import devopsdistilled.operp.client.business.sales.controllers.SaleDescController;
import devopsdistilled.operp.client.business.sales.panes.controllers.SaleDescPaneController;
import devopsdistilled.operp.client.business.sales.panes.models.observers.SaleDescPaneModelObserver;
import devopsdistilled.operp.server.data.entity.business.BusinessDescRow;
import devopsdistilled.operp.server.data.entity.business.SaleDesc;
import devopsdistilled.operp.server.data.entity.business.SaleDescRow;

public class SaleDescPane extends
		EntityPane<SaleDesc, SaleDescController, SaleDescPaneController>
		implements SaleDescPaneModelObserver {

	private final JPanel pane;
	private final JTextField discountField;
	private final JTextField amountField;

	private final JTable table;
	BeanTableModel<SaleDescRow> tableModel;
	private JPanel rowDescPanel;

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

		rowDescPanel = new JPanel();
		pane.add(rowDescPanel, "cell 0 0,grow");
		rowDescPanel.setLayout(new MigLayout("", "[]", "[]"));

		table = new JTable(tableModel);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				if (SwingUtilities.isLeftMouseButton(e)
						&& e.getClickCount() == 2
						&& table.getSelectedRow() != -1) {
					// SaleDescRow saleDescRow = tableModel.getRow(table
					// .getSelectedRow());

					// XXX
				}
			}
		});

		final JScrollPane scrollPane = new JScrollPane(table,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		pane.add(scrollPane, "cell 1 0,grow");

		JLabel lblDiscount = new JLabel("Discount");
		pane.add(lblDiscount, "flowx,cell 1 1,alignx right");

		JLabel lblAmount = new JLabel("Amount");
		pane.add(lblAmount, "flowx,cell 1 2,alignx right");

		discountField = new JTextField();
		pane.add(discountField, "cell 1 1,alignx right");
		discountField.setColumns(10);

		amountField = new JTextField();
		amountField.setEditable(false);
		pane.add(amountField, "cell 1 2,alignx right");
		amountField.setColumns(10);
	}

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

	}

	@Override
	public SaleDescController getEntityController() {
		return null;
	}

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

	@Override
	public void updateEntity(SaleDesc saleDesc, EntityOperation entityOperation) {
		if (EntityOperation.Create == entityOperation) {
			tableModel = null;
			tableModel = new BeanTableModel<>(SaleDescRow.class,
					BusinessDescRow.class, saleDesc.getDescRows());

			for (int i = 0; i < table.getColumnCount(); i++) {
				tableModel.setColumnEditable(i, false);
			}
			tableModel.setModelEditable(false);
			table.setModel(tableModel);
		}
	}

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

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

@@ -49,24 +49,97 @@
 les.panes;%0A%0A
+import java.awt.event.ActionEvent;%0Aimport java.awt.event.ActionListener;%0A
 import java.
@@ -197,16 +197,44 @@
 Event;%0A%0A
+import javax.swing.JButton;%0A
 import j
@@ -1542,16 +1542,47 @@
 scPanel;
+%0A%09private final JButton btnAdd;
 %0A%0A%09publi
@@ -1651,24 +1651,35 @@
  MigLayout(%22
+debug,flowy
 %22, %22%5B%5D%5Bgrow%5D
@@ -1688,16 +1688,18 @@
  %22%5B%5D%5B%5D%5B%5D
+%5B%5D
 %22));%0A%0A%09%09
@@ -1770,16 +1770,17 @@
 grow%22);%0A
+%0A
 %09%09rowDes
@@ -2446,24 +2446,28 @@
 cell 1 0
+ 1 2
 ,grow%22);
 %0A%0A%09%09JLab
@@ -2454,24 +2454,234 @@
  1 2,grow%22);
+%0A%09%09btnAdd = new JButton(%22Add%22);%0A%09%09btnAdd.addActionListener(new ActionListener() %7B%0A%09%09%09@Override%0A%09%09%09public void actionPerformed(ActionEvent e) %7B%0A%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09pane.add(btnAdd, %22cell 0 1,alignx right,aligny top%22);
 %0A%0A%09%09JLabel l
@@ -2751,25 +2751,25 @@
 lowx,cell 1 
-1
+2
 ,alignx righ
@@ -2845,33 +2845,33 @@
 , %22flowx,cell 1 
-2
+3
 ,alignx right%22);
@@ -2942,17 +2942,17 @@
 %22cell 1 
-1
+2
 ,alignx 
@@ -3085,33 +3085,33 @@
 tField, %22cell 1 
-2
+3
 ,alignx right%22);

Manage UI for "Add" button