Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
NewCityDialog.java
Go to the documentation of this file.
1 // This file is part of MicropolisJ.
2 // Copyright (C) 2013 Jason Long
3 // Portions Copyright (C) 1989-2007 Electronic Arts Inc.
4 //
5 // MicropolisJ is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GPLv3, with additional terms.
7 // See the README file, included in this distribution, for details.
8 
9 package micropolisj.gui;
10 
11 import java.awt.*;
12 import java.awt.event.*;
13 import java.io.*;
14 import java.util.*;
15 
16 import javax.swing.*;
17 import javax.swing.filechooser.FileNameExtensionFilter;
18 
19 import micropolisj.engine.*;
20 import micropolisj.network.ServerMicropolis;
21 import micropolisj.research.ResearchState;
22 import static micropolisj.gui.MainWindow.EXTENSION;
23 
24 public class NewCityDialog extends JDialog
25 {
26  Micropolis engine;
27  JButton previousMapBtn;
28  Stack<Micropolis> previousMaps = new Stack<Micropolis>();
29  Stack<Micropolis> nextMaps = new Stack<Micropolis>();
30  OverlayMapView mapPane;
31  HashMap<Integer,JRadioButton> levelBtns = new HashMap<Integer,JRadioButton>();
32 
33  static final ResourceBundle strings = MainWindow.strings;
34 
35  public NewCityDialog(MainWindow owner, boolean showCancelOption) {
36  this(owner, showCancelOption, new Micropolis());
37  }
38 
39  public NewCityDialog(MainWindow owner, boolean showCancelOption, Micropolis engine)
40  {
41  super(owner, strings.getString("welcome.caption"));
42  setModal(true);
43 
44  assert owner != null;
45 
46  JPanel p1 = new JPanel(new BorderLayout());
47  p1.setBorder(BorderFactory.createEmptyBorder(10,20,10,20));
48  getContentPane().add(p1, BorderLayout.CENTER);
49 
50  this.engine = engine;
51  this.engine.getPlayerInfo().setResearchState(new ResearchState(engine));
52  this.engine.getPlayerInfo().researchState.toolBtns = owner.toolBtns; // TODO? getter for state
53 
54  new MapGenerator(engine).generateNewCity();
55 
56  mapPane = new OverlayMapView(engine);
57  mapPane.setBorder(BorderFactory.createLoweredBevelBorder());
58  p1.add(mapPane, BorderLayout.WEST);
59 
60  JPanel p2 = new JPanel(new BorderLayout());
61  p1.add(p2, BorderLayout.CENTER);
62 
63  Box levelBox = new Box(BoxLayout.Y_AXIS);
64  levelBox.setBorder(BorderFactory.createEmptyBorder(0,10,0,10));
65  p2.add(levelBox, BorderLayout.CENTER);
66 
67  levelBox.add(Box.createVerticalGlue());
68  JRadioButton radioBtn;
69  for (int lev = GameLevel.MIN_LEVEL; lev <= GameLevel.MAX_LEVEL; lev++)
70  {
71  final int x = lev;
72  radioBtn = new JRadioButton(strings.getString("menu.difficulty."+lev));
73  radioBtn.addActionListener(new ActionListener() {
74  public void actionPerformed(ActionEvent evt) {
75  setGameLevel(x);
76  }});
77  levelBox.add(radioBtn);
78  levelBtns.put(lev, radioBtn);
79  }
80  levelBox.add(Box.createVerticalGlue());
82 
83  JPanel buttonPane = new JPanel();
84  getContentPane().add(buttonPane, BorderLayout.SOUTH);
85 
86  JButton btn;
87  btn = new JButton(strings.getString("welcome.previous_map"));
88  btn.addActionListener(new ActionListener() {
89  public void actionPerformed(ActionEvent evt) {
91  }});
92  btn.setEnabled(false);
93  buttonPane.add(btn);
94  previousMapBtn = btn;
95 
96  btn = new JButton(strings.getString("welcome.play_this_map"));
97  btn.addActionListener(new ActionListener() {
98  public void actionPerformed(ActionEvent evt) {
99  onPlayClicked();
100  }});
101  buttonPane.add(btn);
102  getRootPane().setDefaultButton(btn);
103 
104  btn = new JButton(strings.getString("welcome.next_map"));
105  btn.addActionListener(new ActionListener() {
106  public void actionPerformed(ActionEvent evt) {
108  }});
109  buttonPane.add(btn);
110 
111  btn = new JButton(strings.getString("welcome.load_city"));
112  btn.addActionListener(new ActionListener() {
113  public void actionPerformed(ActionEvent evt) {
115  }});
116  buttonPane.add(btn);
117 
118  if (showCancelOption) {
119  btn = new JButton(strings.getString("welcome.cancel"));
120  btn.addActionListener(new ActionListener() {
121  public void actionPerformed(ActionEvent evt) {
122  onCancelClicked();
123  }});
124  buttonPane.add(btn);
125  }
126  else {
127  btn = new JButton(strings.getString("welcome.quit"));
128  btn.addActionListener(new ActionListener() {
129  public void actionPerformed(ActionEvent evt) {
130  onQuitClicked();
131  }});
132  buttonPane.add(btn);
133  }
134 
135  pack();
136  setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
137 
138  setLocationRelativeTo(owner);
139  getRootPane().registerKeyboardAction(new ActionListener() {
140  public void actionPerformed(ActionEvent evt) {
141  dispose();
142  }},
143  KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
144  JComponent.WHEN_IN_FOCUSED_WINDOW);
145  }
146 
147  private void onPreviousMapClicked()
148  {
149  if (previousMaps.isEmpty())
150  return;
151 
152  nextMaps.push(engine);
153  engine = previousMaps.pop();
154  mapPane.setEngine(engine);
155 
156  previousMapBtn.setEnabled(!previousMaps.isEmpty());
157  }
158 
159  private void onNextMapClicked()
160  {
161  if (nextMaps.isEmpty())
162  {
163  Micropolis m = new Micropolis();
164  new MapGenerator(m).generateNewCity();
165  nextMaps.add(m);
166  }
167 
168  previousMaps.push(engine);
169  engine = nextMaps.pop();
170  mapPane.setEngine(engine);
171 
172  previousMapBtn.setEnabled(true);
173  }
174 
175  private void onLoadCityClicked()
176  {
177  try
178  {
179  JFileChooser fc = new JFileChooser();
180  FileNameExtensionFilter filter1 = new FileNameExtensionFilter(strings.getString("cty_file"), EXTENSION);
181  fc.setFileFilter(filter1);
182 
183  int rv = fc.showOpenDialog(this);
184  if (rv == JFileChooser.APPROVE_OPTION) {
185  File file = fc.getSelectedFile();
186  Micropolis newEngine = new Micropolis();
187  newEngine.load(file);
188  startPlaying(newEngine, file);
189  }
190  }
191  catch (Exception e)
192  {
193  e.printStackTrace(System.err);
194  JOptionPane.showMessageDialog(this, e, strings.getString("main.error_caption"),
195  JOptionPane.ERROR_MESSAGE);
196  }
197  }
198 
199  void startPlaying(Micropolis newEngine, File file)
200  {
201  MainWindow win = (MainWindow) getOwner();
202  win.setEngine(newEngine);
203  win.currentFile = file;
204  win.makeClean();
205  dispose();
206  }
207 
208  private void onPlayClicked()
209  {
212  startPlaying(engine, null);
213  }
214 
215  private void onCancelClicked()
216  {
217  dispose();
218  }
219 
220  private void onQuitClicked()
221  {
222  System.exit(0);
223  }
224 
225  private int getSelectedGameLevel()
226  {
227  for (int lev : levelBtns.keySet())
228  {
229  if (levelBtns.get(lev).isSelected()) {
230  return lev;
231  }
232  }
233  return GameLevel.MIN_LEVEL;
234  }
235 
236  private void setGameLevel(int level)
237  {
238  for (int lev : levelBtns.keySet())
239  {
240  levelBtns.get(lev).setSelected(lev == level);
241  }
242  }
243 }