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

import java.awt.Color;
import java.awt.Dimension;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import es.ucm.fdi.tp.basecode.bgame.control.Controller;
import es.ucm.fdi.tp.basecode.bgame.control.Player;
import es.ucm.fdi.tp.basecode.bgame.model.Board;
import es.ucm.fdi.tp.basecode.bgame.model.Game.State;
import es.ucm.fdi.tp.basecode.bgame.model.GameObserver;
import es.ucm.fdi.tp.basecode.bgame.model.Piece;
import es.ucm.fdi.tp.views.swing.SwingView;
import es.ucm.fdi.tp.views.swing.controlpanel.colorchooser.ColorChooserPane;
import es.ucm.fdi.tp.views.swing.controlpanel.textbox.MessagesBox;

public final class ControlPanel extends JPanel implements GameObserver {

	private static final long serialVersionUID = -5012854304785344947L;
	
	final private Controller cntrl;
	final private SwingView view;
	
	final private Map<Piece,SwingView.PlayerMode> playerModes;
	final private Map<Piece,Color> playerColors;
	
	private final Player randPlayer;
	private final Player aiPlayer;
	private final Piece WINDOW_OWNER;

	/**
	 * Text area where messages eare shown.
	 * Needed to add messages from the outside.
	 */
	private MessagesBox messagesBox;
	
	/**
	 * Table where players information is displayed.
	 * Needed to add it as observer to the color chooser.
	 */
	private PlayersInfoTable infoTable;
	
	/**
	 * Enables the user to change players' colors.
	 * Needed to add observers to it on color change.
	 */
	private ColorChooserPane colorChooser;
	
	/**
	 * Enables the user to change players' modes.
	 * Needed to add observers to it on mode change.
	 */
	private PlayerModesPane playerModesPanel;

	private LinkedList<GameObserver> internalObservers = new LinkedList<GameObserver>();
	
	public ControlPanel(Controller c, SwingView v, 
		Map<Piece,SwingView.PlayerMode> playerModes, 
		Map<Piece, Color> playerColors,
		Player randPlayer, Player aiPlayer) 
	{
		this.cntrl = c;
		this.view = v;
		this.playerModes = playerModes;
		this.playerColors = playerColors;
		this.WINDOW_OWNER = view.getWindowOwner();
		this.randPlayer = randPlayer;
		this.aiPlayer = aiPlayer;

		initGUI();
	}
	
	/**
	 * show the message
	 * @param m : text of the message
	 */
	public void showMessage(String m) {
		if(m != null)
			this.messagesBox.append(m);
		else
			this.messagesBox.setText(null);
	}
	/**
	 * build right panel, the control panel, add modules
	 */
	private void initGUI() {
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		addMessagesBox();
		addPlayerInfoTable();
		addColorChangePane();
		addPlayerModesPane();
		addAutoMovesPane();
		addExitPane();
		
		colorChooser.addObserver(this.infoTable);
		colorChooser.addObserver(this.view);
		playerModesPanel.addObserver(this.infoTable);
		playerModesPanel.addObserver(this.view);
	}
	
	private void addMessagesBox() {
		messagesBox = new MessagesBox();
		this.add(messagesBox);
	}
	
	private void addPlayerInfoTable(){
		this.infoTable = new PlayersInfoTable(playerColors, playerModes, WINDOW_OWNER);
		/*infoTable.setMaximumSize(
				new Dimension(Integer.MAX_VALUE, infoTable.getHeight()));*/
		this.add(infoTable);
		this.internalObservers.add(infoTable);
	}
	
	private void addColorChangePane() {
		this.colorChooser = new ColorChooserPane(playerColors);
		colorChooser.setMaximumSize(
				new Dimension(Integer.MAX_VALUE, colorChooser.getHeight()));
		this.add(colorChooser);
		this.internalObservers.add(colorChooser);
	}
	
	private void addPlayerModesPane() {
		byte mode = PlayerModesPane.MANUAL_ONLY;
		if(randPlayer != null && aiPlayer != null)
			mode = PlayerModesPane.MANUAL_RANDOM_AI;
		else if(randPlayer != null)
			mode = PlayerModesPane.MANUAL_RANDOM;
		else if(aiPlayer != null)
			mode = PlayerModesPane.MANUAL_AI;
		
		this.playerModesPanel = new PlayerModesPane(playerModes, mode, WINDOW_OWNER);
		playerModesPanel.setMaximumSize(
				new Dimension(Integer.MAX_VALUE, playerModesPanel.getHeight()));
		
		if(randPlayer != null || aiPlayer != null) {
			this.add(playerModesPanel);
			this.internalObservers.add(playerModesPanel);
		}
	}
	
	private void addAutoMovesPane() {<commit_msg>Reanem color chooser class<commit_after>package es.ucm.fdi.tp.views.swing.controlpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import es.ucm.fdi.tp.basecode.bgame.control.Controller;
import es.ucm.fdi.tp.basecode.bgame.control.Player;
import es.ucm.fdi.tp.basecode.bgame.model.Board;
import es.ucm.fdi.tp.basecode.bgame.model.Game.State;
import es.ucm.fdi.tp.basecode.bgame.model.GameObserver;
import es.ucm.fdi.tp.basecode.bgame.model.Piece;
import es.ucm.fdi.tp.views.swing.SwingView;
import es.ucm.fdi.tp.views.swing.controlpanel.colorchooser.ColorChooserPanel;
import es.ucm.fdi.tp.views.swing.controlpanel.textbox.MessagesBox;

public final class ControlPanel extends JPanel implements GameObserver {

	private static final long serialVersionUID = -5012854304785344947L;
	
	final private Controller cntrl;
	final private SwingView view;
	
	final private Map<Piece,SwingView.PlayerMode> playerModes;
	final private Map<Piece,Color> playerColors;
	
	private final Player randPlayer;
	private final Player aiPlayer;
	private final Piece WINDOW_OWNER;

	/**
	 * Text area where messages eare shown.
	 * Needed to add messages from the outside.
	 */
	private MessagesBox messagesBox;
	
	/**
	 * Table where players information is displayed.
	 * Needed to add it as observer to the color chooser.
	 */
	private PlayersInfoTable infoTable;
	
	/**
	 * Enables the user to change players' colors.
	 * Needed to add observers to it on color change.
	 */
	private ColorChooserPanel colorChooser;
	
	/**
	 * Enables the user to change players' modes.
	 * Needed to add observers to it on mode change.
	 */
	private PlayerModesPane playerModesPanel;

	private LinkedList<GameObserver> internalObservers = new LinkedList<GameObserver>();
	
	public ControlPanel(Controller c, SwingView v, 
		Map<Piece,SwingView.PlayerMode> playerModes, 
		Map<Piece, Color> playerColors,
		Player randPlayer, Player aiPlayer) 
	{
		this.cntrl = c;
		this.view = v;
		this.playerModes = playerModes;
		this.playerColors = playerColors;
		this.WINDOW_OWNER = view.getWindowOwner();
		this.randPlayer = randPlayer;
		this.aiPlayer = aiPlayer;

		initGUI();
	}
	
	/**
	 * show the message
	 * @param m : text of the message
	 */
	public void showMessage(String m) {
		if(m != null)
			this.messagesBox.append(m);
		else
			this.messagesBox.setText(null);
	}
	/**
	 * build right panel, the control panel, add modules
	 */
	private void initGUI() {
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		addMessagesBox();
		addPlayerInfoTable();
		addColorChangePane();
		addPlayerModesPane();
		addAutoMovesPane();
		addExitPane();
		
		colorChooser.addObserver(this.infoTable);
		colorChooser.addObserver(this.view);
		playerModesPanel.addObserver(this.infoTable);
		playerModesPanel.addObserver(this.view);
	}
	
	private void addMessagesBox() {
		messagesBox = new MessagesBox();
		this.add(messagesBox);
	}
	
	private void addPlayerInfoTable(){
		this.infoTable = new PlayersInfoTable(playerColors, playerModes, WINDOW_OWNER);
		/*infoTable.setMaximumSize(
				new Dimension(Integer.MAX_VALUE, infoTable.getHeight()));*/
		this.add(infoTable);
		this.internalObservers.add(infoTable);
	}
	
	private void addColorChangePane() {
		this.colorChooser = new ColorChooserPanel(playerColors);
		colorChooser.setMaximumSize(
				new Dimension(Integer.MAX_VALUE, colorChooser.getHeight()));
		this.add(colorChooser);
		this.internalObservers.add(colorChooser);
	}
	
	private void addPlayerModesPane() {
		byte mode = PlayerModesPane.MANUAL_ONLY;
		if(randPlayer != null && aiPlayer != null)
			mode = PlayerModesPane.MANUAL_RANDOM_AI;
		else if(randPlayer != null)
			mode = PlayerModesPane.MANUAL_RANDOM;
		else if(aiPlayer != null)
			mode = PlayerModesPane.MANUAL_AI;
		
		this.playerModesPanel = new PlayerModesPane(playerModes, mode, WINDOW_OWNER);
		playerModesPanel.setMaximumSize(
				new Dimension(Integer.MAX_VALUE, playerModesPanel.getHeight()));
		
		if(randPlayer != null || aiPlayer != null) {
			this.add(playerModesPanel);
			this.internalObservers.add(playerModesPanel);
		}
	}
	
	private void addAutoMovesPane() {
{'fraction_non_alphanumeric': 0.10575573233504913, 'fraction_numerical': 0.004445484323818437, 'mean_word_length': 4.602228047182176, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4625486', 'n_tokens_mistral': 3081, 'n_tokens_neox': 2794, 'n_words': 603}