代码合并

This commit is contained in:
zhoudaxia 2016-08-22 08:31:10 +08:00
parent 1d5f0aad08
commit d5cd1a6b3d
2 changed files with 59 additions and 28 deletions

View File

@ -1,3 +1,4 @@
package net.sf.robocode.ui.dialog;
@ -31,14 +32,17 @@ public class TeamCreator extends JDialog implements WizardListener {
private RobotSelectionPanel robotSelectionPanel;
private TeamCreatorOptionsPanel teamCreatorOptionsPanel;
private final int minRobots = 2;
private final int maxRobots = 10;
private final int minRobots = 2;//每个编队至少两人
private final int maxRobots = 10;//每个编队最多10人
private final EventHandler eventHandler = new EventHandler();
//EventHandler具体实现ActionListener接口实现动作的监听操作
class EventHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
//e为传入的参数传入临时变量e为ActionEvent类型
if (e.getActionCommand().equals("Refresh")) {
//执行跟新操作
getRobotSelectionPanel().refreshRobotList(true);
}
}
@ -59,9 +63,12 @@ public class TeamCreator extends JDialog implements WizardListener {
return teamCreatorOptionsPanel;
}
//获取创建编队嵌入框中的内容
private JPanel getTeamCreatorContentPane() {
if (teamCreatorContentPane == null) {
//如果嵌入框内容为空
teamCreatorContentPane = new JPanel();
//新建嵌入框
teamCreatorContentPane.setLayout(new BorderLayout());
teamCreatorContentPane.add(getWizardController(), BorderLayout.SOUTH);
teamCreatorContentPane.add(getWizardPanel(), BorderLayout.CENTER);
@ -75,8 +82,12 @@ public class TeamCreator extends JDialog implements WizardListener {
return teamCreatorContentPane;
}
protected RobotSelectionPanel getRobotSelectionPanel() {
if (robotSelectionPanel == null) {
//选择机器人组成编队
protected RobotSelectionPanel getRobotSelectionPanel()
{
//如果当前选择为空
if (robotSelectionPanel == null)
{
robotSelectionPanel = net.sf.robocode.core.Container.createComponent(RobotSelectionPanel.class);
robotSelectionPanel.setup(minRobots, maxRobots, false, "Select the robots for this team.", false, true, true,
false, false, false, null);
@ -84,8 +95,11 @@ public class TeamCreator extends JDialog implements WizardListener {
return robotSelectionPanel;
}
private WizardCardPanel getWizardPanel() {
if (wizardPanel == null) {
//新建一个嵌入板
private WizardCardPanel getWizardPanel()
{
if (wizardPanel == null)
{
wizardPanel = new WizardCardPanel(this);
wizardPanel.add(getRobotSelectionPanel(), "Select robots");
wizardPanel.add(getTeamCreatorOptionsPanel(), "Select options");
@ -93,23 +107,27 @@ public class TeamCreator extends JDialog implements WizardListener {
return wizardPanel;
}
public void initialize() {
//窗口初始化
public void initialize()
{
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Create a team");
setContentPane(getTeamCreatorContentPane());
}
private WizardController getWizardController() {
if (wizardController == null) {
if (wizardController == null)
{
wizardController = getWizardPanel().getWizardController();
}
return wizardController;
}
//添加取消键动作
public void cancelButtonActionPerformed() {
dispose();
}
//添加完成键动作
public void finishButtonActionPerformed() {
try {
int rc = createTeam();
@ -127,12 +145,14 @@ public class TeamCreator extends JDialog implements WizardListener {
}
}
//将创建的编队写入数据库
public int createTeam() throws IOException {
File file = new File(repositoryManager.getRobotsDirectory(),
teamCreatorOptionsPanel.getTeamPackage().replace('.', File.separatorChar)
+ teamCreatorOptionsPanel.getTeamNameField().getText() + ".team");
if (file.exists()) {
//如果当前编队已经存在
int ok = JOptionPane.showConfirmDialog(this, file + " already exists. Are you sure you want to replace it?",
"Warning", JOptionPane.YES_NO_CANCEL_OPTION);
@ -150,10 +170,13 @@ public class TeamCreator extends JDialog implements WizardListener {
String webPageFieldString = teamCreatorOptionsPanel.getWebpageField().getText();
if (webPageFieldString != null && webPageFieldString.length() > 0) {
try {
try
{
webPageUrl = new URL(webPageFieldString);
} catch (MalformedURLException e) {
try {
} catch (MalformedURLException e)
{
try
{
webPageUrl = new URL("http://" + webPageFieldString);
teamCreatorOptionsPanel.getWebpageField().setText(webPageUrl.toString());
} catch (MalformedURLException ignored) {}

View File

@ -1,3 +1,4 @@
package net.sf.robocode.ui.dialog;
@ -17,6 +18,7 @@ import java.net.URL;
import java.util.List;
@SuppressWarnings("serial")
public class TeamCreatorOptionsPanel extends WizardPanel {
private TeamCreator teamCreator;
@ -39,16 +41,20 @@ public class TeamCreatorOptionsPanel extends WizardPanel {
private String teamPackage;
private class EventHandler implements ComponentListener, DocumentListener {
public void insertUpdate(DocumentEvent e) {
private class EventHandler implements ComponentListener, DocumentListener
{
public void insertUpdate(DocumentEvent e)
{
fireStateChanged();
}
public void changedUpdate(DocumentEvent e) {
public void changedUpdate(DocumentEvent e)
{
fireStateChanged();
}
public void removeUpdate(DocumentEvent e) {
public void removeUpdate(DocumentEvent e)
{
fireStateChanged();
}
@ -59,32 +65,38 @@ public class TeamCreatorOptionsPanel extends WizardPanel {
public void componentShown(ComponentEvent e) {
List<IRobotSpecItem> selectedRobots;
if (teamCreator != null) {
if (teamCreator != null)
{
selectedRobots = teamCreator.getRobotSelectionPanel().getSelectedRobots();
} else {
} else
{
selectedRobots = teamPackager.getRobotSelectionPanel().getSelectedRobots();
}
if (selectedRobots != null) {
if (selectedRobots != null)
{
IRobotSpecItem robotSpecification = selectedRobots.get(0);
getTeamNameLabel().setText("Please choose a name for your team: (Must be a valid Java classname)");
getTeamNameField().setText(robotSpecification.getShortClassName() + "Team");
getTeamPackageLabel().setText(robotSpecification.getFullPackage() + ".");
teamPackage = robotSpecification.getFullPackage();
if (teamPackage != null) {
if (teamPackage != null)
{
teamPackage += ".";
}
String d = robotSpecification.getDescription();
if (d == null) {
if (d == null)
{
d = "";
}
getDescriptionArea().setText(d);
String a = robotSpecification.getAuthorName();
if (a == null) {
if (a == null)
{
a = "";
}
getAuthorField().setText(a);
@ -303,11 +315,7 @@ public class TeamCreatorOptionsPanel extends WizardPanel {
return teamPackageLabel;
}
/**
* 得到队伍包
*
* @return 返回一个字符串
*/
public String getTeamPackage() {
return (teamPackage != null) ? teamPackage : ".";
}