package ryhma57.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

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

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

        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();
    }
}

@@ -35,20 +35,13 @@
 awt.
-BorderLayout
+Color
 ;%0Aim
@@ -60,38 +60,15 @@
 t.Co
-lor;%0Aimport java.awt.Dimension
+mponent
 ;%0Aim
@@ -152,17 +152,12 @@
 awt.
-GridLayou
+Poin
 t;%0Ai
@@ -170,111 +170,66 @@
 java
-x.swing.BorderFactory;%0Aimport javax.swing.Box;%0Aimport javax.swing.BoxLayout;%0Aimport javax.swing.JButton
+.awt.event.MouseEvent;%0Aimport java.awt.event.MouseListener
 ;%0Aim
@@ -283,43 +283,8 @@
 nel;
-%0Aimport javax.swing.SwingConstants;
 %0A%0A/*
@@ -412,16 +412,41 @@
  JPanel 
+implements MouseListener 
 %7B%0A    pr
@@ -529,16 +529,45 @@
 eholder;
+%0A    private int selectedRow;
 %0A%0A    pu
@@ -692,16 +692,53 @@
 yout());
+%0A        this.addMouseListener(this);
 %0A%0A      
@@ -2023,16 +2023,1666 @@
 date();%0A
-
     %7D
+%0A%0A    private void renderRow() %7B%0A        GridBagLayout layout = (GridBagLayout)this.getLayout();%0A        for (Component c : this.getComponents()) %7B%0A            GridBagConstraints cosntraints;%0A            JLabel label = (JLabel)c;%0A%0A            cosntraints = layout.getConstraints(c);%0A            if(cosntraints.gridy == this.selectedRow) %7B%0A                label.setForeground(Color.black);%0A                label.setBackground(Color.lightGray);%0A                label.setOpaque(true);%0A            %7D else %7B%0A                label.setBackground(Color.white);%0A                label.setOpaque(false);%0A            %7D%0A        %7D%0A    %7D%0A    private void selectItemAt(Point cursor) %7B%0A        GridBagLayout layout = (GridBagLayout)this.getLayout();%0A%0A        for (Component c : this.getComponents()) %7B%0A            GridBagConstraints cosntraints;%0A%0A            cosntraints = layout.getConstraints(c);%0A%0A            Point p = new Point(cursor);%0A            p.x -= c.getX();%0A            p.y -= c.getY();%0A            if(c.contains(p)) %7B%0A                this.selectedRow = cosntraints.gridy;%0A                //cosntraints.gridx%0A            %7D%0A        %7D%0A        renderRow();%0A    %7D%0A%0A    @Override%0A    public void mouseClicked(MouseEvent e) %7B%0A        selectItemAt(e.getPoint());%0A        /* here check if right button was clicked and create context menu */%0A        //this.selectedRow tells the index of the selected reference%0A    %7D%0A%0A    @Override%0A    public void mousePressed(MouseEvent e) %7B %7D%0A%0A    @Override%0A    public void mouseReleased(MouseEvent e) %7B %7D%0A%0A    @Override%0A    public void mouseEntered(MouseEvent e) %7B %7D%0A%0A    @Override%0A    public void mouseExited(MouseEvent e) %7B %7D
 %0A%7D%0A

Make the references selectable.