package ryhma57.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * This component will contain the references.
 * It is currently under construction.
 */
public class ListView extends JPanel implements MouseListener {
    private final Application app;
    private int length;
    private JLabel placeholder;
    private int selectedRow;

    public ListView(Application app) {
        this.app = app;
        this.length = 0;
        this.setLayout(new GridBagLayout());
        this.addMouseListener(this);

        createPlaceHolder();
    }

    public final void createPlaceHolder() {
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.NONE;
        c.gridx = 0;
        c.gridy = this.length++;
        c.gridwidth = 2;
        c.weightx = c.weighty = 1.0;
        c.ipadx = c.ipady = 10;
        c.anchor = GridBagConstraints.CENTER;

        this.placeholder = new JLabel("No references created");
        this.add(this.placeholder, c);
        this.revalidate();
    }

    public void createRow(String id, String name) {
        GridBagConstraints c = new GridBagConstraints();

        /* Remove the placeholder */
        if(this.placeholder.getParent() == this) {
            this.remove(this.placeholder);
        }

        c.fill = GridBagConstraints.NONE;
        c.gridx = 0;
        c.gridy = this.length++;
        c.gridwidth = 1;
        c.weightx = c.weighty = 0.0;
        c.ipadx = c.ipady = 10;
        c.anchor = GridBagConstraints.WEST;

        JLabel idLabel = new JLabel(id);
        JLabel nameLabel = new JLabel(name);
        this.add(idLabel, c);
        c.gridx = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = 3;
        c.weightx = 1.0;
        this.add(nameLabel, c);
        this.revalidate();
    }

    private void renderRow() {
        GridBagLayout layout = (GridBagLayout)this.getLayout();
        for (Component c : this.getComponents()) {
            GridBagConstraints cosntraints;
            JLabel label = (JLabel)c;

            cosntraints = layout.getConstraints(c);
            if(cosntraints.gridy == this.selectedRow) {
                label.setForeground(Color.black);
                label.setBackground(Color.lightGray);
                label.setOpaque(true);
            } else {
                label.setBackground(Color.white);
                label.setOpaque(false);
            }
        }
    }
    private void selectItemAt(Point cursor) {
        GridBagLayout layout = (GridBagLayout)this.getLayout();

        for (Component c : this.getComponents()) {
            GridBagConstraints cosntraints;

            cosntraints = layout.getConstraints(c);

            Point p = new Point(cursor);
            p.x -= c.getX();
            p.y -= c.getY();
            if(c.contains(p)) {
                this.selectedRow = cosntraints.gridy;
                //cosntraints.gridx
            }
        }
        renderRow();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        selectItemAt(e.getPoint());
        /* here check if right button was clicked and create context menu */
        //this.selectedRow tells the index of the selected reference
    }

    @Override
    public void mousePressed(MouseEvent e) { }

    @Override
    public void mouseReleased(MouseEvent e) { }

    @Override
    public void mouseEntered(MouseEvent e) { }

    @Override
    public void mouseExited(MouseEvent e) { }
}

@@ -739,22 +739,19 @@
         
-create
+add
 PlaceHol
@@ -786,22 +786,19 @@
 al void 
-create
+add
 PlaceHol
@@ -2641,24 +2641,668 @@
     %7D%0A    %7D%0A
+%0A    public void removeRow(int index) %7B%0A        GridBagLayout layout = (GridBagLayout)this.getLayout();%0A        for (Component c : this.getComponents()) %7B%0A            GridBagConstraints constraints;%0A%0A            constraints = layout.getConstraints(c);%0A            if(constraints.gridy == index) %7B%0A                this.remove(c);%0A            %7D else if(constraints.gridy %3E index) %7B%0A                constraints.gridy--;%0A                layout.setConstraints(c, constraints);%0A            %7D%0A        %7D%0A        if(this.getComponents().length == 0) %7B%0A            this.addPlaceHolder();%0A        %7D%0A        this.validate();%0A        this.repaint();%0A    %7D%0A%0A
     private 
@@ -3476,34 +3476,34 @@
 agConstraints co
-s
 n
+s
 traints;%0A%0A      
@@ -3502,34 +3502,34 @@
 %0A%0A            co
-s
 n
+s
 traints = layout
@@ -3717,18 +3717,18 @@
 Row = co
-s
 n
+s
 traints.
@@ -3756,25 +3756,92 @@
   //
-cosntraints.gridx
+this.columnSelected = cosntraints.gridx%0A                removeRow(this.selectedRow);
 %0A   

Add method for removing rows.