9 package micropolisj.gui;
12 import java.awt.event.*;
17 import javax.swing.filechooser.FileNameExtensionFilter;
19 import micropolisj.engine.*;
20 import micropolisj.network.ServerMicropolis;
21 import micropolisj.research.ResearchState;
22 import static micropolisj.gui.MainWindow.EXTENSION;
27 JButton previousMapBtn;
28 Stack<Micropolis> previousMaps =
new Stack<Micropolis>();
29 Stack<Micropolis> nextMaps =
new Stack<Micropolis>();
31 HashMap<Integer,JRadioButton> levelBtns =
new HashMap<Integer,JRadioButton>();
33 static final ResourceBundle strings =
MainWindow.strings;
36 this(owner, showCancelOption,
new Micropolis());
41 super(owner, strings.getString(
"welcome.caption"));
46 JPanel p1 =
new JPanel(
new BorderLayout());
47 p1.setBorder(BorderFactory.createEmptyBorder(10,20,10,20));
48 getContentPane().add(p1, BorderLayout.CENTER);
54 new MapGenerator(engine).generateNewCity();
57 mapPane.setBorder(BorderFactory.createLoweredBevelBorder());
58 p1.add(mapPane, BorderLayout.WEST);
60 JPanel p2 =
new JPanel(
new BorderLayout());
61 p1.add(p2, BorderLayout.CENTER);
63 Box levelBox =
new Box(BoxLayout.Y_AXIS);
64 levelBox.setBorder(BorderFactory.createEmptyBorder(0,10,0,10));
65 p2.add(levelBox, BorderLayout.CENTER);
67 levelBox.add(Box.createVerticalGlue());
68 JRadioButton radioBtn;
72 radioBtn =
new JRadioButton(strings.getString(
"menu.difficulty."+lev));
73 radioBtn.addActionListener(
new ActionListener() {
74 public void actionPerformed(ActionEvent evt) {
77 levelBox.add(radioBtn);
78 levelBtns.put(lev, radioBtn);
80 levelBox.add(Box.createVerticalGlue());
83 JPanel buttonPane =
new JPanel();
84 getContentPane().add(buttonPane, BorderLayout.SOUTH);
87 btn =
new JButton(strings.getString(
"welcome.previous_map"));
88 btn.addActionListener(
new ActionListener() {
89 public void actionPerformed(ActionEvent evt) {
92 btn.setEnabled(
false);
96 btn =
new JButton(strings.getString(
"welcome.play_this_map"));
97 btn.addActionListener(
new ActionListener() {
98 public void actionPerformed(ActionEvent evt) {
102 getRootPane().setDefaultButton(btn);
104 btn =
new JButton(strings.getString(
"welcome.next_map"));
105 btn.addActionListener(
new ActionListener() {
106 public void actionPerformed(ActionEvent evt) {
111 btn =
new JButton(strings.getString(
"welcome.load_city"));
112 btn.addActionListener(
new ActionListener() {
113 public void actionPerformed(ActionEvent evt) {
118 if (showCancelOption) {
119 btn =
new JButton(strings.getString(
"welcome.cancel"));
120 btn.addActionListener(
new ActionListener() {
121 public void actionPerformed(ActionEvent evt) {
127 btn =
new JButton(strings.getString(
"welcome.quit"));
128 btn.addActionListener(
new ActionListener() {
129 public void actionPerformed(ActionEvent evt) {
136 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
138 setLocationRelativeTo(owner);
139 getRootPane().registerKeyboardAction(
new ActionListener() {
140 public void actionPerformed(ActionEvent evt) {
143 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
144 JComponent.WHEN_IN_FOCUSED_WINDOW);
149 if (previousMaps.isEmpty())
152 nextMaps.push(engine);
153 engine = previousMaps.pop();
156 previousMapBtn.setEnabled(!previousMaps.isEmpty());
161 if (nextMaps.isEmpty())
164 new MapGenerator(m).generateNewCity();
168 previousMaps.push(engine);
169 engine = nextMaps.pop();
172 previousMapBtn.setEnabled(
true);
179 JFileChooser fc =
new JFileChooser();
180 FileNameExtensionFilter filter1 =
new FileNameExtensionFilter(strings.getString(
"cty_file"), EXTENSION);
181 fc.setFileFilter(filter1);
183 int rv = fc.showOpenDialog(
this);
184 if (rv == JFileChooser.APPROVE_OPTION) {
185 File file = fc.getSelectedFile();
187 newEngine.
load(file);
188 startPlaying(newEngine, file);
193 e.printStackTrace(System.err);
194 JOptionPane.showMessageDialog(
this, e, strings.getString(
"main.error_caption"),
195 JOptionPane.ERROR_MESSAGE);
199 void startPlaying(
Micropolis newEngine, File file)
203 win.currentFile = file;
212 startPlaying(engine, null);
227 for (
int lev : levelBtns.keySet())
229 if (levelBtns.get(lev).isSelected()) {
238 for (
int lev : levelBtns.keySet())
240 levelBtns.get(lev).setSelected(lev == level);