<commit_before>package org.joverseer.ui.viewers;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

import org.joverseer.joApplication;
import org.joverseer.domain.ClimateEnum;
import org.joverseer.domain.FortificationSizeEnum;
import org.joverseer.domain.HarborSizeEnum;
import org.joverseer.domain.HexInfo;
import org.joverseer.domain.InfoSourceValue;
import org.joverseer.domain.NationRelations;
import org.joverseer.domain.PopulationCenter;
import org.joverseer.domain.PopulationCenterSizeEnum;
import org.joverseer.domain.ProductEnum;
import org.joverseer.game.Game;
import org.joverseer.game.Turn;
import org.joverseer.game.TurnElementsEnum;
import org.joverseer.metadata.GameMetadata;
import org.joverseer.metadata.domain.Hex;
import org.joverseer.metadata.domain.Nation;
import org.joverseer.metadata.domain.NationAllegianceEnum;
import org.joverseer.support.Container;
import org.joverseer.support.GameHolder;
import org.joverseer.support.infoSources.DerivedFromInfluenceOtherInfoSource;
import org.joverseer.support.infoSources.PopCenterXmlInfoSource;
import org.joverseer.tools.CombatUtils;
import org.joverseer.tools.PopulationCenterLoyaltyEstimator;
import org.joverseer.tools.combatCalc.Combat;
import org.joverseer.tools.combatCalc.CombatArmy;
import org.joverseer.tools.combatCalc.CombatPopCenter;
import org.joverseer.ui.LifecycleEventsEnum;
import org.joverseer.ui.map.MapPanel;
import org.joverseer.ui.support.GraphicUtils;
import org.joverseer.ui.support.Messages;
import org.joverseer.ui.support.UIUtils;
import org.joverseer.ui.support.commands.ShowInfoSourcePopupCommand;
import org.joverseer.ui.support.controls.PopupMenuActionListener;
import org.joverseer.ui.support.drawing.ColorPicker;
import org.joverseer.ui.views.EditPopulationCenterForm;
import org.springframework.binding.form.FormModel;
import org.springframework.richclient.application.Application;
import org.springframework.richclient.command.ActionCommand;
import org.springframework.richclient.command.CommandGroup;
import org.springframework.richclient.dialog.ConfirmationDialog;
import org.springframework.richclient.dialog.FormBackedDialogPage;
import org.springframework.richclient.dialog.MessageDialog;
import org.springframework.richclient.dialog.TitledPageApplicationDialog;
import org.springframework.richclient.form.FormModelHelper;
import org.springframework.richclient.image.ImageSource;
import org.springframework.richclient.layout.GridBagLayoutBuilder;
import org.springframework.richclient.layout.TableLayoutBuilder;

/**
 * Shows pcs in the Current Hex View
 * 
 * @author <NAME>
 */
public class PopulationCenterViewer extends ObjectViewer {

	public static final String FORM_PAGE = "PopulationCenterViewer"; //$NON-NLS-1$

	boolean showColor = true;

	JTextField loyalty;
	JTextField name;
	JTextField nation;
	JTextField sizeFort;
	JTextField lostThisTurn;
	JTextField productionDescription;
	JTextField turnInfo;
	HashMap<ProductEnum, JTextField> production = new HashMap<ProductEnum, JTextField>();
	HashMap<ProductEnum, JTextField> stores = new HashMap<ProductEnum, JTextField>();
	ArrayList<JLabel> productionLabels = new ArrayList<JLabel>();

	ActionCommand editPopulationCenter = new EditPopulationCenterCommand();
	ActionCommand toggleLostThisTurnCommand = new ToggleLostThisTurnCommand();
	ActionCommand deletePopulationCenterCommand = new DeletePopulationCenterCommand();

	public PopulationCenterViewer(FormModel formModel) {
		super(formModel, FORM_PAGE);
	}

	@Override
	public boolean appliesTo(Object obj) {
		return PopulationCenter.class.isInstance(obj);
	}

	@Override
	public void setFormObject(Object object) {
		super.setFormObject(object);

		PopulationCenter pc = (PopulationCenter) object;
		Game game = GameHolder.instance().getGame();
		if (game == null)
			return;

		this.name.setText(GraphicUtils.parseName(pc.getName()));
		this.name.setCaretPosition(0);
		GameMetadata gm = game.getMetadata();

		int nationNo = 0;
		int loyaltyNo = 0;
		if (pc.getSize() != PopulationCenterSizeEnum.ruins) {
			nationNo = pc.getNationNo();
			loyaltyNo = pc.getLoyalty();
		}
		if (loyaltyNo == 0) {
			InfoSourceValue isv = PopulationCenterLoyaltyEstimator.getLoyaltyEstimateForPopCenter(pc);
			if (isv != null) {
				String txt = Messages.getString("PopulationCenterViewer.FromTurn", new Object[] {isv.getValue(),((DerivedFromInfluenceOtherInfoSource) isv.getInfoSource()).getTurnNo()}); //$NON-NLS-1$
				this.loyalty.setText(txt);
			} else {
				this.loyalty.setText(String.valueOf(loyaltyNo));
			}
		} else {
			this.loyalty.setText(String.valueOf(loyaltyNo));
		}

		Nation pcNation = gm.getNationByNum(nationNo);
		this.nation.setText(pcNation.getShortName());
		this.nation.setCaretPosition(0);
		this.nation.setToolTipText(pcNation.getName());
		this.sizeFort.setText(UIUtils.enumToString(pc.getSize()));
		if (!pc.getFortification().equals(FortificationSizeEnum.none)) {
			this.sizeFort.setText(this.sizeFort.getText() + " - " + UIUtils.enumToString(pc.getFortification())); //$NON-NLS-1$
		}
		if (pc.getHarbor() != HarborSizeEnum.none) {
			this.sizeFort.setText(this.sizeFort.getText() + " - " + UIUtils.enumToString(pc.getHarbor())); //$NON-NLS-1$
		}
		this.sizeFort.setCaretPosition(0);

		// show production
		// if the pop center is a ruin, search in past turns and find the first
		// turn where the pop center was not a ruin and report that production
		PopulationCenter pcForProduction = pc;
		Turn productionTurn = null;
		if (!hasProduction(pcForProduction)) {
			for (int i = game.getMaxTurn() - 1; i >= 0; i--) {
				productionTurn = game.getTurn(i);
				if (productionTurn == null)
					continue;
				PopulationCenter pop = (PopulationCenter) productionTurn.getContainer(TurnElementsEnum.PopulationCenter).findFirstByProperty("hexNo", pc.getHexNo()); //$NON-NLS-1$
				if (pop == null)
					continue;
				if (pop.getSize() != PopulationCenterSizeEnum.ruins) {
					if (hasProduction(pop)) {
						pcForProduction = pop;
						break;
					}<commit_msg>fix silent exception in CME<commit_after>package org.joverseer.ui.viewers;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

import org.joverseer.joApplication;
import org.joverseer.domain.ClimateEnum;
import org.joverseer.domain.FortificationSizeEnum;
import org.joverseer.domain.HarborSizeEnum;
import org.joverseer.domain.HexInfo;
import org.joverseer.domain.InfoSourceValue;
import org.joverseer.domain.NationRelations;
import org.joverseer.domain.PopulationCenter;
import org.joverseer.domain.PopulationCenterSizeEnum;
import org.joverseer.domain.ProductEnum;
import org.joverseer.game.Game;
import org.joverseer.game.Turn;
import org.joverseer.game.TurnElementsEnum;
import org.joverseer.metadata.GameMetadata;
import org.joverseer.metadata.domain.Hex;
import org.joverseer.metadata.domain.Nation;
import org.joverseer.metadata.domain.NationAllegianceEnum;
import org.joverseer.support.Container;
import org.joverseer.support.GameHolder;
import org.joverseer.support.infoSources.DerivedFromInfluenceOtherInfoSource;
import org.joverseer.support.infoSources.PopCenterXmlInfoSource;
import org.joverseer.tools.CombatUtils;
import org.joverseer.tools.PopulationCenterLoyaltyEstimator;
import org.joverseer.tools.combatCalc.Combat;
import org.joverseer.tools.combatCalc.CombatArmy;
import org.joverseer.tools.combatCalc.CombatPopCenter;
import org.joverseer.ui.LifecycleEventsEnum;
import org.joverseer.ui.map.MapPanel;
import org.joverseer.ui.support.GraphicUtils;
import org.joverseer.ui.support.Messages;
import org.joverseer.ui.support.UIUtils;
import org.joverseer.ui.support.commands.ShowInfoSourcePopupCommand;
import org.joverseer.ui.support.controls.PopupMenuActionListener;
import org.joverseer.ui.support.drawing.ColorPicker;
import org.joverseer.ui.views.EditPopulationCenterForm;
import org.springframework.binding.form.FormModel;
import org.springframework.richclient.application.Application;
import org.springframework.richclient.command.ActionCommand;
import org.springframework.richclient.command.CommandGroup;
import org.springframework.richclient.dialog.ConfirmationDialog;
import org.springframework.richclient.dialog.FormBackedDialogPage;
import org.springframework.richclient.dialog.MessageDialog;
import org.springframework.richclient.dialog.TitledPageApplicationDialog;
import org.springframework.richclient.form.FormModelHelper;
import org.springframework.richclient.image.ImageSource;
import org.springframework.richclient.layout.GridBagLayoutBuilder;
import org.springframework.richclient.layout.TableLayoutBuilder;

/**
 * Shows pcs in the Current Hex View
 * 
 * @author <NAME>
 */
public class PopulationCenterViewer extends ObjectViewer {

	public static final String FORM_PAGE = "PopulationCenterViewer"; //$NON-NLS-1$

	boolean showColor = true;

	JTextField loyalty;
	JTextField name;
	JTextField nation;
	JTextField sizeFort;
	JTextField lostThisTurn;
	JTextField productionDescription;
	JTextField turnInfo;
	HashMap<ProductEnum, JTextField> production = new HashMap<ProductEnum, JTextField>();
	HashMap<ProductEnum, JTextField> stores = new HashMap<ProductEnum, JTextField>();
	ArrayList<JLabel> productionLabels = new ArrayList<JLabel>();

	ActionCommand editPopulationCenter = new EditPopulationCenterCommand();
	ActionCommand toggleLostThisTurnCommand = new ToggleLostThisTurnCommand();
	ActionCommand deletePopulationCenterCommand = new DeletePopulationCenterCommand();

	public PopulationCenterViewer(FormModel formModel) {
		super(formModel, FORM_PAGE);
	}

	@Override
	public boolean appliesTo(Object obj) {
		return PopulationCenter.class.isInstance(obj);
	}

	@Override
	public void setFormObject(Object object) {
		super.setFormObject(object);

		PopulationCenter pc = (PopulationCenter) object;
		Game game = GameHolder.instance().getGame();
		if (game == null)
			return;

		this.name.setText(GraphicUtils.parseName(pc.getName()));
		this.name.setCaretPosition(0);
		GameMetadata gm = game.getMetadata();

		int nationNo = 0;
		int loyaltyNo = 0;
		if (pc.getSize() != PopulationCenterSizeEnum.ruins) {
			nationNo = pc.getNationNo();
			loyaltyNo = pc.getLoyalty();
		}
		if (loyaltyNo == 0) {
			InfoSourceValue isv = PopulationCenterLoyaltyEstimator.getLoyaltyEstimateForPopCenter(pc);
			if (isv != null) {
				String txt = Messages.getString("PopulationCenterViewer.FromTurn", new Object[] {isv.getValue(),((DerivedFromInfluenceOtherInfoSource) isv.getInfoSource()).getTurnNo()}); //$NON-NLS-1$
				this.loyalty.setText(txt);
			} else {
				this.loyalty.setText(String.valueOf(loyaltyNo));
			}
		} else {
			this.loyalty.setText(String.valueOf(loyaltyNo));
		}

		Nation pcNation = gm.getNationByNum(nationNo);
		if (pcNation == null) {
			pcNation = gm.getNationByNum(0);
		}
		this.nation.setText(pcNation.getShortName());
		this.nation.setCaretPosition(0);
		this.nation.setToolTipText(pcNation.getName());
		this.sizeFort.setText(UIUtils.enumToString(pc.getSize()));
		if (!pc.getFortification().equals(FortificationSizeEnum.none)) {
			this.sizeFort.setText(this.sizeFort.getText() + " - " + UIUtils.enumToString(pc.getFortification())); //$NON-NLS-1$
		}
		if (pc.getHarbor() != HarborSizeEnum.none) {
			this.sizeFort.setText(this.sizeFort.getText() + " - " + UIUtils.enumToString(pc.getHarbor())); //$NON-NLS-1$
		}
		this.sizeFort.setCaretPosition(0);

		// show production
		// if the pop center is a ruin, search in past turns and find the first
		// turn where the pop center was not a ruin and report that production
		PopulationCenter pcForProduction = pc;
		Turn productionTurn = null;
		if (!hasProduction(pcForProduction)) {
			for (int i = game.getMaxTurn() - 1; i >= 0; i--) {
				productionTurn = game.getTurn(i);
				if (productionTurn == null)
					continue;
				PopulationCenter pop = (PopulationCenter) productionTurn.getContainer(TurnElementsEnum.PopulationCenter).findFirstByProperty("hexNo", pc.getHexNo()); //$NON-NLS-1$
				if (pop == null)
					continue;
				if (pop.getSize() != PopulationCenterSizeEnum.ruins) {
					if (hasProduction(pop)) {
						pcForProduction = pop;
						break;
					}
{'fraction_non_alphanumeric': 0.11131243106979676, 'fraction_numerical': 0.002126989128722231, 'mean_word_length': 5.752659574468085, 'pattern_counts': {'":': 0, '<': 17, '<?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': '1116814', 'n_tokens_mistral': 4319, 'n_tokens_neox': 3987, 'n_words': 701}