/*
* UFO Saved Game Editor
* Copyright (C) 2010 Christopher Davoren
*
* 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 net.rubikscomplex.ufosge.gui.table.BaseInventoryTableModel;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
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.gui.event.BaseLayoutEvent;
import net.rubikscomplex.ufosge.gui.event.BaseLayoutListener;
import net.rubikscomplex.ufosge.resources.ResourceManager;
import net.rubikscomplex.ufosge.util.Util;
/**
*
* @author Chris Davoren
*/
public class BaseEditDialog extends JDialog implements ActionListener, BaseLayoutListener {
protected static final String ACTION_APPLY = "Apply";
protected static final String ACTION_CLOSE = "Close";
protected static final String ACTION_CANCEL = "Cancel";
protected static final String ACTION_UPDATECELL = "Update";
protected SavedGame game;
protected Base base;
protected JTextField nameField;
protected NumericFormattedTextField scientistsField;
protected NumericFormattedTextField engineersField;
protected JCheckBox activeCheckBox;
protected BaseFacilityEditor facilityEditor;
protected JLabel currentCellLabel;
protected JComboBox currentFacilityBox;
protected NumericFormattedTextField currentFacilityDaysLeft;
protected BaseInventoryTableModel inventoryTableModel;
public BaseEditDialog(Window owner, SavedGame game, Base base) {
super(owner, "Edit Base - " + base.name);
setIconImage(ResourceManager.getDefaultFrameIcon().getImage());
this.base = base;
this.game = game;
inventoryTableModel = new BaseInventoryTableModel(game, base);
setModal(true);
nameField = new JTextField();
((AbstractDocument)nameField.getDocument()).setDocumentFilter(new LengthDocumentFilter(Base.NAME_LEN - 1));
scientistsField = new NumericFormattedTextField(0, 255, null);
engineersField = new NumericFormattedTextField(0, 255, null);
activeCheckBox = new JCheckBox("Active");
JPanel generalDetailsPanel = new JPanel();
generalDetailsPanel.setBorder(BorderFactory.createTitledBorder("General Information"));
generalDetailsPanel.setLayout(new GridBagLayout());
generalDetailsPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
generalDetailsPanel.setPreferredSize(new Dimension(200, 150));
generalDetailsPanel.setMinimumSize(generalDetailsPanel.getPreferredSize());
Util.addFormRow(generalDetailsPanel, nameField, "Name", 0, 0, false);
Util.addFormRow(generalDetailsPanel, scientistsField, "Scientists", 1, 0, false);
Util.addFormRow(generalDetailsPanel, engineersField, "Engineers", 2, 0, false);
Util.addFormRow(generalDetailsPanel, activeCheckBox, null, 3, 0, true);
JTable inventoryTable = new JTable();
inventoryTable.setModel(inventoryTableModel);
inventoryTable.setFillsViewportHeight(true);
JScrollPane inventoryScrollPane = new JScrollPane(inventoryTable);
inventoryScrollPane.setAlignmentX(Component.RIGHT_ALIGNMENT);
JPanel inventoryPanel = new JPanel();
inventoryPanel.setBorder(BorderFactory.createTitledBorder("Inventory"));
inventoryPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
inventoryPanel.setLayout(new BoxLayout(inventoryPanel, BoxLayout.Y_AXIS));
inventoryPanel.add(inventoryScrollPane);
inventoryPanel.setPreferredSize(new Dimension(200, 300));
inventoryPanel.setMinimumSize(inventoryPanel.getPreferredSize());
JButton applyButton = new JButton("Apply");
applyButton.addActionListener(this);
applyButton.setMnemonic(KeyEvent.VK_A);
applyButton.setActionCommand(ACTION_APPLY);
Util.setButtonSize(applyButton, 100, 25);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(this);
cancelButton.setMnemonic(KeyEvent.VK_C);
cancelButton.setActionCommand(ACTION_CANCEL);
Util.setButtonSize(cancelButton, 100, 25);
JButton closeButton = new JButton("Apply & Close");
closeButton.addActionListener(this);
closeButton.setMnemonic(KeyEvent.VK_S);
closeButton.setActionCommand(ACTION_CLOSE);
Util.setButtonSize(closeButton, 100, 25);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 10));
buttonPanel.add(applyButton);
buttonPanel.add(Box.createRigidArea(new Dimension(10, 10)));
buttonPanel.add(closeButton);
buttonPanel.add(Box.createRigidArea(new Dimension(10, 10)));
buttonPanel.add(cancelButton);
buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
facilityEditor = new BaseFacilityEditor(game, base, 2);
facilityEditor.setAlignmentX(Component.LEFT_ALIGNMENT);
facilityEditor.addBaseLayoutEventListener(this);
JPanel facilityDetailsPanel = new JPanel();
facilityDetailsPanel.setLayout(new GridBagLayout());
facilityDetailsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
facilityDetailsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
currentCellLabel = new JLabel(" ");
currentFacilityBox = new JComboBox(Base.getFacilityNameList(true, true).toArray());
currentFacilityDaysLeft = new NumericFormattedTextField(-1, Base.MAX_DAYS_LEFT, null);
JButton facilityUpdateButton = new JButton("Update");
facilityUpdateButton.setMnemonic(KeyEvent.VK_U);
facilityUpdateButton.setActionCommand(ACTION_UPDATECELL);
facilityUpdateButton.addActionListener(this);
Util.addFormRow(facilityDetailsPanel, currentCellLabel, null, 0, 0, false);
Util.addFormRow(facilityDetailsPanel, currentFacilityBox, "Type", 1, 0, false);
Util.addFormRow(facilityDetailsPanel, currentFacilityDaysLeft, "Construction left", 2, 0, false);
Util.addFormRow(facilityDetailsPanel, facilityUpdateButton, null, 3, 0, true);
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
leftPanel.setAlignmentY(Component.TOP_ALIGNMENT);
leftPanel.add(generalDetailsPanel);
leftPanel.add(inventoryPanel);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.setBorder(BorderFactory.createTitledBorder("Layout"));
rightPanel.setAlignmentY(Component.TOP_ALIGNMENT);
rightPanel.add(facilityEditor);
rightPanel.add(facilityDetailsPanel);
JPanel columnPanel = new JPanel();
columnPanel.setLayout(new BoxLayout(columnPanel, BoxLayout.X_AXIS));
columnPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
columnPanel.add(leftPanel);
columnPanel.add(rightPanel);
Container contentPane = getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add(columnPanel);
contentPane.add(buttonPanel);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setMinimumSize(getSize());
rightPanel.setMaximumSize(rightPanel.getSize());
int locx = ResourceManager.getUserPreferenceInt(ResourceManager.PREF_BASEEDITDIALOG_POSX);
int locy = ResourceManager.getUserPreferenceInt(ResourceManager.PREF_BASEEDITDIALOG_POSY);
if (locx != -1) {
setLocation(locx, locy);
}
else {
setLocationRelativeTo(owner);
}
getRootPane().setDefaultButton(closeButton);
setValues();
}
public void setValues() {
nameField.setText(base.name);
scientistsField.setValue(base.scientists);
engineersField.setValue(base.engineers);
activeCheckBox.setSelected(base.isActive);
inventoryTableModel.updateCache();
facilityEditor.updateCache();
currentCellLabel.setText("Currently editing cell (" + facilityEditor.getSelX() + ", " + facilityEditor.getSelY() + "):");
currentFacilityDaysLeft.setValue(base.facilities[facilityEditor.getSelY()][facilityEditor.getSelX()]);
currentFacilityBox.setSelectedItem(Base.facilityToString(base.facilities[facilityEditor.getSelY()][facilityEditor.getSelX()]));
}
public void update() {
base.name = nameField.getText();
base.scientists = (short)scientistsField.getValue();
base.engineers = (short)engineersField.getValue();
base.isActive = activeCheckBox.isSelected();
inventoryTableModel.update();
facilityEditor.update();
base.recalculateRadarValues();
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals(ACTION_APPLY)) {
update();
}
else if (e.getActionCommand().equals(ACTION_CANCEL)) {
this.dispose();
}
else if (e.getActionCommand().equals(ACTION_CLOSE)) {
update();
this.dispose();
}
else if (e.getActionCommand().equals(ACTION_UPDATECELL)) {
int selx = facilityEditor.getSelX();
int sely = facilityEditor.getSelY();
facilityEditor.setSelectedFacility(Base.stringToFacility(currentFacilityBox.getSelectedItem().toString()), (byte)currentFacilityDaysLeft.getValue());
// facilityEditor.updateImage();
currentFacilityDaysLeft.setValue(facilityEditor.dlCache[sely][selx]);
currentFacilityBox.setSelectedItem(Base.facilityToString(facilityEditor.fCache[sely][selx]));
}
}
public void baseLayoutPerformed(BaseLayoutEvent e) {
int selx = facilityEditor.getSelX();
int sely = facilityEditor.getSelY();
currentCellLabel.setText("Currently editing cell (" + selx + ", " + sely + ")");
currentFacilityDaysLeft.setValue(facilityEditor.dlCache[sely][selx]);
currentFacilityBox.setSelectedItem(Base.facilityToString(facilityEditor.fCache[sely][selx]));
}
@Override
public void dispose() {
Point loc = getLocation();
ResourceManager.setUserPreferenceInt(ResourceManager.PREF_BASEEDITDIALOG_POSX, loc.x);
ResourceManager.setUserPreferenceInt(ResourceManager.PREF_BASEEDITDIALOG_POSY, loc.y);
super.dispose();
}
}