/* * Copyright (C) 2010 Dav * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package net.rubikscomplex.ufosge.gui; import java.awt.Component; import java.awt.Dimension; import java.awt.GridBagLayout; import java.util.HashMap; import java.util.Map; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.text.AbstractDocument; import net.rubikscomplex.ufosge.data.Base; import net.rubikscomplex.ufosge.data.SavedGame; import net.rubikscomplex.ufosge.data.Soldier; import net.rubikscomplex.ufosge.gui.table.SoldierStatisticsTableModel; import net.rubikscomplex.ufosge.util.Util; /** * * @author Chris Davoren */ public class SoldierEditPanel extends JPanel { protected SavedGame game; protected Soldier soldier; protected JTextField nameField; protected NumericFormattedTextField killsField; protected NumericFormattedTextField recoveryField; protected NumericFormattedTextField victoryField; protected NumericFormattedTextField psiStrengthField; protected NumericFormattedTextField psiSkillField; protected JComboBox rankComboBox; protected JComboBox armorComboBox; protected JComboBox genderComboBox; protected JComboBox appearanceComboBox; protected JCheckBox psiTrainingCheckBox; protected SoldierStatisticsTableModel statisticsModel; public SoldierEditPanel(SavedGame game, int soldierIndex) { this(game, game.soldiers.get(soldierIndex)); } public SoldierEditPanel(SavedGame game, Soldier soldier) { this.game = game; this.soldier = soldier; nameField = new JTextField(); ((AbstractDocument)nameField.getDocument()).setDocumentFilter(new LengthDocumentFilter(Soldier.NAME_LEN - 1)); nameField.setColumns(Soldier.NAME_LEN - 1); rankComboBox = new JComboBox(Soldier.getRankMap().values().toArray()); killsField = new NumericFormattedTextField(0, Short.MAX_VALUE, null); recoveryField = new NumericFormattedTextField(0, 255, null); victoryField = new NumericFormattedTextField(0, Short.MAX_VALUE, null); armorComboBox = new JComboBox(Soldier.getArmorMap().values().toArray()); psiTrainingCheckBox = new JCheckBox("Currently in psi training"); genderComboBox = new JComboBox(Soldier.getGenderMap().values().toArray()); appearanceComboBox = new JComboBox(Soldier.getAppearanceMap().values().toArray()); psiStrengthField = new NumericFormattedTextField(0, 255, null); psiSkillField = new NumericFormattedTextField(0, 255, null); JPanel generalInfoPanel = new JPanel(); generalInfoPanel.setLayout(new GridBagLayout()); generalInfoPanel.setAlignmentY(Component.TOP_ALIGNMENT); generalInfoPanel.setBorder(BorderFactory.createTitledBorder("General Details")); generalInfoPanel.setMinimumSize(new Dimension(200, 300)); generalInfoPanel.setMaximumSize(new Dimension(300, 10000)); Util.addFormRow(generalInfoPanel, nameField, "Name", 0, 0, false); Util.addFormRow(generalInfoPanel, rankComboBox, "Rank", 1, 0, false); Util.addFormRow(generalInfoPanel, killsField, "Kills", 2, 0, false); Util.addFormRow(generalInfoPanel, recoveryField, "Recovery days", 3, 0, false); Util.addFormRow(generalInfoPanel, victoryField, "Value", 4, 0, false); Util.addFormRow(generalInfoPanel, armorComboBox, "Armor", 5, 0, false); Util.addFormRow(generalInfoPanel, genderComboBox, "Gender", 6, 0, false); Util.addFormRow(generalInfoPanel, appearanceComboBox, "Appearance", 7, 0, false); Util.addFormRow(generalInfoPanel, psiStrengthField, "Psi strength", 8, 0, false); Util.addFormRow(generalInfoPanel, psiSkillField, "Psi skill", 9, 0, false); Util.addFormRow(generalInfoPanel, psiTrainingCheckBox, null, 10, 0, true); statisticsModel = new SoldierStatisticsTableModel(game, soldier); JTable statisticsTable = new JTable(); statisticsTable.setModel(statisticsModel); statisticsTable.setFillsViewportHeight(true); JScrollPane statisticsScrollPane = new JScrollPane(statisticsTable); statisticsScrollPane.setAlignmentY(Component.TOP_ALIGNMENT); statisticsScrollPane.setPreferredSize(new Dimension(200, 100)); statisticsScrollPane.setMinimumSize(statisticsScrollPane.getPreferredSize()); JPanel tablePanel = new JPanel(); tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.Y_AXIS)); tablePanel.add(statisticsScrollPane); tablePanel.setAlignmentY(Component.TOP_ALIGNMENT); tablePanel.setBorder(BorderFactory.createTitledBorder("Statistics")); setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); add(generalInfoPanel); add(tablePanel); setPreferredSize(new Dimension(500, 400)); setValues(); } protected void setValues() { nameField.setText(soldier.name); killsField.setValue(soldier.kills); recoveryField.setValue(soldier.recoveryDaysLeft); victoryField.setValue(soldier.victoryValue); psiStrengthField.setValue(soldier.psionicStrength); psiSkillField.setValue(soldier.psionicSkill); rankComboBox.setSelectedItem(Soldier.rankToString(soldier.rank)); armorComboBox.setSelectedItem(Soldier.armorToString(soldier.armor)); genderComboBox.setSelectedItem(Soldier.genderToString(soldier.gender)); appearanceComboBox.setSelectedItem(Soldier.appearanceToString(soldier.appearance)); psiTrainingCheckBox.setSelected(soldier.inPsiTraining); statisticsModel.updateCache(); } public void udpate() { soldier.name = nameField.getText(); soldier.kills = (short)killsField.getValue(); soldier.recoveryDaysLeft = (short)recoveryField.getValue(); soldier.victoryValue = (short)victoryField.getValue(); soldier.psionicStrength = (short)psiStrengthField.getValue(); soldier.psionicSkill = (short)psiSkillField.getValue(); soldier.rank = Soldier.stringToRank(rankComboBox.getSelectedItem().toString()); soldier.armor = Soldier.stringToArmor(armorComboBox.getSelectedItem().toString()); soldier.gender = Soldier.stringToGender(genderComboBox.getSelectedItem().toString()); soldier.appearance = Soldier.stringToAppearance(appearanceComboBox.getSelectedItem().toString()); soldier.inPsiTraining = psiTrainingCheckBox.isSelected(); statisticsModel.update(); } protected Map<Integer, Base> makeActiveBaseMap() { HashMap<Integer, Base> map = new HashMap<Integer, Base>(); for(int i = 0; i < game.bases.size(); i++) { if (game.bases.get(i).isActive) { map.put(game.bases.get(i).index, game.bases.get(i)); } } return map; } }