package transformation.associationtags.buttonlisteners;

import transformation.associationtags.AssociationTag;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DeleteButtonListener implements ActionListener {
	
	List<AssociationTag> tags;
	AssociationTag tag;
	JPanel tagPanel;
	JPanel buttonPanel;
	List<String> roles;
	
	
	public DeleteButtonListener(List<AssociationTag> tags, AssociationTag tag, 
								JPanel tagPanel, JPanel buttonPanel, List<String> roles) {
		
		this.tags = tags;
		this.tag = tag;
		this.tagPanel = tagPanel;
		this.buttonPanel = buttonPanel;
		this.roles = roles;
		
	}

	/**
	 * Deletes the association tag from the tags list
	 */
	public void actionPerformed(ActionEvent arg0) {

	    // Removes the tag from the list
		tags.remove(tags.indexOf(tag));
		
		// Refreshes the list
		JFrame frame = (JFrame) tagPanel.getRootPane().getParent();
		Container container = tagPanel.getParent(); 
		
		frame.setVisible(false);
		container.remove(tagPanel);
		container.remove(buttonPanel);
		int x = tags.size() + 1;
		int y = roles.size();
		
		if (x > 5) {
			frame.setSize(400, 600 + y*20);
		}
		else {
			frame.setSize(400, 120*x + y*20 + 40);
		}
		
		frame.setVisible(true);
		
	}
	
	
	
	

}
