<commit_before>package es.ucm.fdi.tp.views.swing.controlpanel.colorchooser;

import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

import es.ucm.fdi.tp.basecode.bgame.model.Board;
import es.ucm.fdi.tp.basecode.bgame.model.Game.State;
import es.ucm.fdi.tp.views.swing.controlpanel.ControlPanelObserver;
import es.ucm.fdi.tp.basecode.bgame.model.GameObserver;
import es.ucm.fdi.tp.basecode.bgame.model.Observable;
import es.ucm.fdi.tp.basecode.bgame.model.Piece;

public class ColorChooserPane extends JPanel implements ActionListener, Observable<ControlPanelObserver>, GameObserver {

	private static final long serialVersionUID = 111272096861569383L;
	
	protected Map<Piece, Color> colorList;
	protected final Window parentWindow;
	
	private JComboBox<Piece> piecesCombo;
	private final JButton changeColorBtn;
	
	private List<Piece> pieces;
	
	/**
	 * List of observers.
	 */
	private ArrayList<ControlPanelObserver> observers = new ArrayList<ControlPanelObserver>(4); //should be static?
	
	public ColorChooserPane(Map<Piece, Color> pieceColors) {
		this.colorList = pieceColors;
		
		piecesCombo = new JComboBox<Piece>();
		this.add(piecesCombo);

		changeColorBtn = new JButton("Choose color");
		changeColorBtn.addActionListener(this);
		this.add(changeColorBtn);
		
		
		this.setBorder(new TitledBorder("Piece Colors"));
		
		this.parentWindow = SwingUtilities.getWindowAncestor(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Piece selected = (Piece) piecesCombo.getSelectedItem();
		ColorChooser chooser = new ColorChooser(parentWindow, "Choose your color", colorList.get(selected));
		Color color = chooser.getColor();
		
		if (color != null) {
			colorList.put(selected, color);
			notifyColorChange(selected, color);
		}
		
	}
	
	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		this.changeColorBtn.setEnabled(enabled);
		this.piecesCombo.setEnabled(enabled);
	}
	
	public void refresh() {
		piecesCombo.removeAllItems();
		for(Piece p : pieces) {
			piecesCombo.addItem(p);
		}
	}
	
	
	/**
	 * Adds a color change observer. When the color of a player is changed all
	 * observers are notified.
	 */
	@Override<commit_msg>Fix color chooser dialog not being centered<commit_after>package es.ucm.fdi.tp.views.swing.controlpanel.colorchooser;

import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

import es.ucm.fdi.tp.basecode.bgame.model.Board;
import es.ucm.fdi.tp.basecode.bgame.model.Game.State;
import es.ucm.fdi.tp.views.swing.controlpanel.ControlPanelObserver;
import es.ucm.fdi.tp.basecode.bgame.model.GameObserver;
import es.ucm.fdi.tp.basecode.bgame.model.Observable;
import es.ucm.fdi.tp.basecode.bgame.model.Piece;

public class ColorChooserPane extends JPanel implements ActionListener, Observable<ControlPanelObserver>, GameObserver {

	private static final long serialVersionUID = 111272096861569383L;
	
	protected Map<Piece, Color> colorList;
	
	private JComboBox<Piece> piecesCombo;
	private final JButton changeColorBtn;
	
	private List<Piece> pieces;
	
	/**
	 * List of observers.
	 */
	private ArrayList<ControlPanelObserver> observers = new ArrayList<ControlPanelObserver>(4); //should be static?
	
	public ColorChooserPane(Map<Piece, Color> pieceColors) {
		this.colorList = pieceColors;
		
		piecesCombo = new JComboBox<Piece>();
		this.add(piecesCombo);

		changeColorBtn = new JButton("Choose color");
		changeColorBtn.addActionListener(this);
		this.add(changeColorBtn);
		
		
		this.setBorder(new TitledBorder("Piece Colors"));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Piece selected = (Piece) piecesCombo.getSelectedItem();
		Window parent = SwingUtilities.getWindowAncestor(this);
		
		ColorChooser chooser = new ColorChooser(parent, "Choose your color", colorList.get(selected));
		Color color = chooser.getColor();
		
		if (color != null) {
			colorList.put(selected, color);
			notifyColorChange(selected, color);
		}
		
	}
	
	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		this.changeColorBtn.setEnabled(enabled);
		this.piecesCombo.setEnabled(enabled);
	}
	
	public void refresh() {
		piecesCombo.removeAllItems();
		for(Piece p : pieces) {
			piecesCombo.addItem(p);
		}
	}
	
	
	/**
	 * Adds a color change observer. When the color of a player is changed all
	 * observers are notified.
	 */
	@Override
{'fraction_non_alphanumeric': 0.10950080515297907, 'fraction_numerical': 0.007648953301127214, 'mean_word_length': 4.901425178147268, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4156794', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1584, 'n_words': 330}