/* This file is part of CivQuest.
*
* CivQuest 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 2 of the License, or
* (at your option) any later version.
*
* CivQuest 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 CivQuest; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: AddNationAction.java 653 2005-08-15 22:14:20Z s_hauf $
*/
package civquest.swing.scenario;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import civquest.io.Messages;
import civquest.nation.gameChange.AddNation;
import civquest.parser.ruleset.Registry;
import civquest.parser.ruleset.exception.RulesetException;
import civquest.swing.CivQuest;
import civquest.swing.panes.managers.ScenarioEditorInfoPaneManager;
import swifu.main.AbstractFunctionAction;
import swifu.main.FunctionActionEvent;
/**
* Action for adding new nations to the scenario.
*/
public class AddNationAction extends AbstractFunctionAction {
private Registry nationsRegistry;
private Registry visRegistry;
private ScenarioEditorInfoPaneManager editor;
/**
* Sets the fields need for adding a new nation.
*
* @param registry the root of the registry
* @param editor the scenarioEditor
*/
public AddNationAction(Registry registry,
ScenarioEditorInfoPaneManager editor) {
this.editor = editor;
try {
this.nationsRegistry = registry.getSubRegistry("nations");
this.visRegistry = registry.getSubRegistry("visibility");
} catch (RulesetException rse) {
CivQuest.showErrorQuitDialog("There is a problem related to" +
"ruleset-loading:", rse);
}
}
/**
* Adds a new nation with the name that was entered by the user.
*/
public void actionPerformed(FunctionActionEvent e) {
AddDialog dialog = new AddDialog();
String nationName = dialog.getNationName();
if (nationName == null || nationName.equals("")) {
return;
}
if (nationName.length() > 20) {
Messages.getMessages().err("AddNationAction", "The name of the " +
"new nation is to long. This would ruin my layout!");
return;
}
try {
AddNation nation = new AddNation(nationName,
this.nationsRegistry, this.visRegistry);
nation.execute();
this.editor.addNationsToCombo();
} catch (RulesetException rse) {
CivQuest.showErrorQuitDialog("There is a problem related to" +
"ruleset-loading:", rse);
}
}
/**
* This class provides a dialog for adding a new nation.
*/
private static class AddDialog extends JDialog {
/** The textfield for the nationName.*/
private JTextField nameField;
/** The name of the new nation, */
private String nationName;
/**
* Creates a dialog for adding a nation.
*/
public AddDialog() {
super((Frame) null, "Add nation", true);
JLabel headerLabel = new JLabel("Please enter the name of the " +
"new nation.");
this.nameField = new JTextField();
JButton addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nationName = nameField.getText();
setVisible(false);
}
});
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nationName = null;
setVisible(false);
}
});
//create and fill panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 5));
buttonPanel.add(addButton);
buttonPanel.add(cancelButton);
getRootPane().setDefaultButton(addButton);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(headerLabel, BorderLayout.NORTH);
getContentPane().add(this.nameField, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
/**
* Getter for this nationName.
*
* @return this nationName
*/
public String getNationName() {
return this.nationName;
}
}
}