package transformation.su_transformationplugin.listeners;

import transformation.associationtags.AssociationTag;

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

import javax.swing.*;

public class UnjoinListener implements ActionListener {

	JFrame frame;
	JPanel joinedTagsPanel;
	JPanel tagPanel;
	List<AssociationTag> joinedTags;
	
	public UnjoinListener(JFrame frame, JPanel joinedTagsPanel, JPanel tagPanel, List<AssociationTag> joinedTags) {
		
		this.frame = frame;
		this.joinedTagsPanel = joinedTagsPanel;
		this.tagPanel = tagPanel;
		this.joinedTags = joinedTags;
		
	}
	
	@Override
	/**
	 * Unjoins the complete association tags and splits them back into the "protected =" and "role =" parts
	 * and removes them from the list of joined tags
	 */
	public void actionPerformed(ActionEvent arg0) {
		
		for (int i=0; i<joinedTags.size(); i++) {
			if (joinedTags.get(i).toString().
					equals(((JTextArea)(tagPanel.getComponent(0))).getText())) {
				joinedTags.remove(i);
			}
		}
				
		frame.setVisible(false);
		joinedTagsPanel.remove(tagPanel);

		if (joinedTagsPanel.getComponentCount() == 0) {
			joinedTagsPanel.add(new JLabel("No complete association tags"));
		}
		
		frame.setVisible(true);
	}

}
