package devopsdistilled.operp.client.stock.panes;

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

import javax.inject.Inject;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;
import devopsdistilled.operp.client.abstracts.SubTaskPane;
import devopsdistilled.operp.client.abstracts.libs.BeanTableModel;
import devopsdistilled.operp.client.stock.models.observers.StockKeeperModelObserver;
import devopsdistilled.operp.client.stock.panes.details.StockKeepingDetailsPane;
import devopsdistilled.operp.client.stock.panes.details.TransferStockDetailsPane;
import devopsdistilled.operp.client.stock.panes.models.observers.ListStockActivitiesPaneModelObserver;
import devopsdistilled.operp.server.data.entity.stock.StockKeeper;

public class ListStockActivitiesPane extends SubTaskPane implements
		ListStockActivitiesPaneModelObserver, StockKeeperModelObserver {

	@Inject
	private StockKeepingDetailsPane stockKeepingDetailsPane;

	@Inject
	private TransferStockDetailsPane transferStockDetailsPane;

	private final JPanel pane;
	private final JTable table;
	BeanTableModel<StockKeeper> tableModel;
	
	public ListStockActivitiesPane() {
		pane = new JPanel();
		pane.setLayout(new MigLayout("fill"));
		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) {
					StockKeeper stockKeeper = tableModel.getRow(table
							.getSelectedRow());
					if (stockKeeper.getTransferStockKeeper() == null)
						stockKeepingDetailsPane.show(stockKeeper, getPane());
					else {
						transferStockDetailsPane.show(stockKeeper, getPane());
					}

				}
			}
		});

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

	}

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

	@Override
	public void updateStockActivities(List<StockKeeper> stockActivities) {
		tableModel = null;
		tableModel = new BeanTableModel<>(StockKeeper.class, stockActivities);

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

@@ -110,24 +110,54 @@
 MouseEvent;%0A
+import java.util.Enumeration;%0A
 import java.
@@ -167,16 +167,16 @@
 l.List;%0A
-
 %0Aimport 
@@ -386,16 +386,93 @@
 ilities;
+%0Aimport javax.swing.table.JTableHeader;%0Aimport javax.swing.table.TableColumn;
 %0A%0Aimport
@@ -1425,10 +1425,9 @@
 el;%0A
-%09
 %0A
+
 %09pub
@@ -2700,16 +2700,16 @@
 false);%0A
-
 %09%09table.
@@ -2729,14 +2729,612 @@
 eModel);
+%0A%0A%09%09JTableHeader tableHeader = table.getTableHeader();%0A%09%09Enumeration%3CTableColumn%3E columns = tableHeader.getColumnModel()%0A%09%09%09%09.getColumns();%0A%09%09while (columns.hasMoreElements()) %7B%0A%09%09%09final TableColumn column = columns.nextElement();%0A%09%09%09String columnName = (String) column.getHeaderValue();%0A%09%09%09if (columnName.equalsIgnoreCase(%22Stock Keeper Id%22)%0A%09%09%09%09%09%7C%7C columnName.equalsIgnoreCase(%22Stock%22)%0A%09%09%09%09%09%7C%7C columnName.equalsIgnoreCase(%22Transfer Stock Keeper%22)) %7B%0A%0A%09%09%09%09SwingUtilities.invokeLater(new Runnable() %7B%0A%09%09%09%09%09@Override%0A%09%09%09%09%09public void run() %7B%0A%09%09%09%09%09%09table.removeColumn(column);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D);%0A%09%09%09%7D%0A%0A%09%09%7D
 %0A%09%7D%0A%7D%0A

Remove columns: StockKeeperId, Stock, TransferStockKeeper