Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
EvaluationPane.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.text.*;
14 import java.util.*;
15 import javax.swing.*;
16 
17 import micropolisj.engine.*;
18 import static micropolisj.gui.MainWindow.formatFunds;
19 
20 public class EvaluationPane extends JPanel implements Micropolis.Listener {
21  Micropolis engine;
22 
23  JLabel yesLbl;
24  JLabel noLbl;
25  JLabel[] voterProblemLbl;
26  JLabel[] voterCountLbl;
27  JLabel popLbl;
28  JLabel deltaLbl;
29  JLabel assessLbl;
30  JLabel cityClassLbl;
31  JLabel gameLevelLbl;
32  JLabel scoreLbl;
33  JLabel scoreDeltaLbl;
34 
35  static ResourceBundle cstrings = ResourceBundle.getBundle("micropolisj.CityStrings");
36  static ResourceBundle gstrings = MainWindow.strings;
37 
38  public EvaluationPane(Micropolis _engine) {
39  super(new BorderLayout());
40 
41  JButton dismissBtn = new JButton(gstrings.getString("dismiss-evaluation"));
42  dismissBtn.addActionListener(new ActionListener() {
43  public void actionPerformed(ActionEvent evt) {
45  }
46  });
47  add(dismissBtn, BorderLayout.SOUTH);
48 
49  Box b1 = new Box(BoxLayout.X_AXIS);
50  add(b1, BorderLayout.CENTER);
51 
52  b1.add(makePublicOpinionPane());
53  b1.add(new JSeparator(SwingConstants.VERTICAL));
54  b1.add(makeStatisticsPane());
55 
56  assert _engine != null;
57  setEngine(_engine);
58  }
59 
60  public void setEngine(Micropolis newEngine) {
61  if(engine != null) { // old engine
62  engine.removeListener(this);
63  }
64  engine = newEngine;
65  if(engine != null) { // new engine
66  engine.addListener(this);
68  }
69  }
70 
71  private void onDismissClicked() {
72  setVisible(false);
73  }
74 
75  private JComponent makePublicOpinionPane() {
76  JPanel me = new JPanel(new GridBagLayout());
77  GridBagConstraints c1 = new GridBagConstraints();
78  GridBagConstraints c2 = new GridBagConstraints();
79  GridBagConstraints c3 = new GridBagConstraints();
80 
81  // c1 is for the full-width headers
82  c1.gridx = c1.gridy = 0;
83  c1.gridwidth = 2;
84  c1.gridheight = 1;
85  c1.weightx = 1.0;
86  c1.fill = GridBagConstraints.NONE;
87  c1.anchor = GridBagConstraints.NORTH;
88 
89  JLabel headerLbl = new JLabel(gstrings.getString("public-opinion"));
90  Font curFont = headerLbl.getFont();
91  headerLbl.setFont(curFont.deriveFont(curFont.getStyle() | Font.BOLD, (float) (curFont.getSize() * 1.2)));
92  me.add(headerLbl, c1);
93 
94  c1.gridy = 1;
95  c1.insets = new Insets(3, 0, 3, 0);
96  me.add(new JLabel(gstrings.getString("public-opinion-1")), c1);
97 
98  c1.gridy = 4;
99  me.add(new JLabel(gstrings.getString("public-opinion-2")), c1);
100 
101  c2.gridx = 0;
102  c2.gridy = 2;
103  c2.gridwidth = c2.gridheight = 1;
104  c2.weightx = 1.0;
105  c2.anchor = GridBagConstraints.EAST;
106  c2.insets = new Insets(0, 0, 0, 4);
107 
108  me.add(new JLabel(gstrings.getString("public-opinion-yes")), c2);
109 
110  c2.gridy = 3;
111  me.add(new JLabel(gstrings.getString("public-opinion-no")), c2);
112 
113  c3.gridx = 1;
114  c3.gridwidth = c3.gridheight = 1;
115  c3.weightx = 1.0;
116  c3.anchor = GridBagConstraints.WEST;
117  c3.insets = new Insets(0, 4, 0, 0);
118 
119  c3.gridy = 2;
120  yesLbl = new JLabel();
121  me.add(yesLbl, c3);
122 
123  c3.gridy = 3;
124  noLbl = new JLabel();
125  me.add(noLbl, c3);
126 
127  c2.gridy = c3.gridy = 5;
128 
129  final int NUM_PROBS = 4;
130  voterProblemLbl = new JLabel[NUM_PROBS];
131  voterCountLbl = new JLabel[NUM_PROBS];
132  for(int i = 0; i < NUM_PROBS; i++) {
133  voterProblemLbl[i] = new JLabel();
134  me.add(voterProblemLbl[i], c2);
135 
136  voterCountLbl[i] = new JLabel();
137  me.add(voterCountLbl[i], c3);
138 
139  c2.gridy = ++c3.gridy;
140  }
141 
142  // add glue so that everything will align towards the top
143  c1.gridy = 999;
144  c1.weighty = 1.0;
145  me.add(new JLabel(), c1);
146 
147  return me;
148  }
149 
150  private JComponent makeStatisticsPane() {
151  JPanel me = new JPanel(new GridBagLayout());
152  GridBagConstraints c1 = new GridBagConstraints();
153  GridBagConstraints c2 = new GridBagConstraints();
154  GridBagConstraints c3 = new GridBagConstraints();
155 
156  c1.gridx = c1.gridy = 0;
157  c1.gridwidth = 2;
158  c1.gridheight = 1;
159  c1.weightx = 1.0;
160  c1.fill = GridBagConstraints.NONE;
161  c1.anchor = GridBagConstraints.NORTH;
162  c1.insets = new Insets(0, 0, 3, 0);
163 
164  JLabel headerLbl = new JLabel(gstrings.getString("statistics-head"));
165  Font curFont = headerLbl.getFont();
166  headerLbl.setFont(curFont.deriveFont(curFont.getStyle() | Font.BOLD, (float) (curFont.getSize() * 1.2)));
167  me.add(headerLbl, c1);
168 
169  c1.gridy = 20;
170  c1.insets = new Insets(9, 0, 3, 0);
171  c1.fill = GridBagConstraints.VERTICAL;
172  JLabel header2Lbl = new JLabel(gstrings.getString("city-score-head"));
173  me.add(header2Lbl, c1);
174 
175  c2.gridx = 0;
176  c2.gridwidth = c2.gridheight = 1;
177  c2.weightx = 0.5;
178  c2.anchor = GridBagConstraints.EAST;
179  c2.insets = new Insets(0, 0, 0, 4);
180 
181  c3.gridx = 1;
182  c3.gridwidth = c3.gridheight = 1;
183  c3.weightx = 0.5;
184  c3.anchor = GridBagConstraints.WEST;
185  c3.insets = new Insets(0, 4, 0, 0);
186 
187  c2.gridy = c3.gridy = 1;
188  me.add(new JLabel(gstrings.getString("stats-population")), c2);
189  popLbl = new JLabel();
190  me.add(popLbl, c3);
191 
192  c2.gridy = ++c3.gridy;
193  me.add(new JLabel(gstrings.getString("stats-net-migration")), c2);
194  deltaLbl = new JLabel();
195  me.add(deltaLbl, c3);
196 
197  c2.gridy = ++c3.gridy;
198  me.add(new JLabel(gstrings.getString("stats-last-year")), c2);
199 
200  c2.gridy = ++c3.gridy;
201  me.add(new JLabel(gstrings.getString("stats-assessed-value")), c2);
202  assessLbl = new JLabel();
203  me.add(assessLbl, c3);
204 
205  c2.gridy = ++c3.gridy;
206  me.add(new JLabel(gstrings.getString("stats-category")), c2);
207  cityClassLbl = new JLabel();
208  me.add(cityClassLbl, c3);
209 
210  c2.gridy = ++c3.gridy;
211  me.add(new JLabel(gstrings.getString("stats-game-level")), c2);
212  gameLevelLbl = new JLabel();
213  me.add(gameLevelLbl, c3);
214 
215  c2.gridy = c3.gridy = 21;
216  me.add(new JLabel(gstrings.getString("city-score-current")), c2);
217  scoreLbl = new JLabel();
218  me.add(scoreLbl, c3);
219 
220  c2.gridy = ++c3.gridy;
221  me.add(new JLabel(gstrings.getString("city-score-change")), c2);
222  scoreDeltaLbl = new JLabel();
223  me.add(scoreDeltaLbl, c3);
224 
225  // add glue so that everything will align towards the top
226  c1.gridy = 999;
227  c1.weighty = 1.0;
228  c1.insets = new Insets(0, 0, 0, 0);
229  me.add(new JLabel(), c1);
230 
231  return me;
232  }
233 
234  // implements Micropolis.Listener
235  public void cityMessage(MicropolisMessage message, CityLocation loc) {
236  }
237 
238  public void citySound(Sound sound, CityLocation loc) {
239  }
240 
241  public void censusChanged() {
242  }
243 
244  public void demandChanged() {
245  }
246 
247  public void fundsChanged() {
248  }
249 
250  public void optionsChanged() {
251  }
252 
253  // implements Micropolis.Listener
254  public void evaluationChanged() {
255  loadEvaluation();
256  }
257 
258  private void loadEvaluation() {
259  NumberFormat pctFmt = NumberFormat.getPercentInstance();
260  PlayerInfo info = engine.getPlayerInfo();
261  yesLbl.setText(pctFmt.format(0.01 * info.evaluation.cityYes));
262  noLbl.setText(pctFmt.format(0.01 * info.evaluation.cityNo));
263 
264  for (int i = 0; i < voterProblemLbl.length; i++) {
265  CityProblem p = i < info.evaluation.problemOrder.length ? info.evaluation.problemOrder[i] : null;
266  int numVotes = p != null ? info.evaluation.problemVotes.get(p) : 0;
267 
268  if(numVotes != 0) {
269  voterProblemLbl[i].setText(cstrings.getString("problem." + p.name()));
270  voterCountLbl[i].setText(pctFmt.format(0.01 * numVotes));
271  voterProblemLbl[i].setVisible(true);
272  voterCountLbl[i].setVisible(true);
273  }
274  else {
275  voterProblemLbl[i].setVisible(false);
276  voterCountLbl[i].setVisible(false);
277  }
278  }
279 
280  NumberFormat nf = NumberFormat.getInstance();
281  popLbl.setText(nf.format(info.evaluation.cityPop));
282  deltaLbl.setText(nf.format(info.evaluation.deltaCityPop));
283  assessLbl.setText(formatFunds(info.evaluation.cityAssValue));
284  cityClassLbl.setText(getCityClassName(info.evaluation.cityClass));
285  gameLevelLbl.setText(getGameLevelName(engine.gameLevel));
286  scoreLbl.setText(nf.format(info.evaluation.cityScore));
287  scoreDeltaLbl.setText(nf.format(info.evaluation.deltaCityScore));
288  }
289 
290  static String getCityClassName(int cityClass) {
291  return cstrings.getString("class." + cityClass);
292  }
293 
294  static String getGameLevelName(int gameLevel) {
295  return cstrings.getString("level." + gameLevel);
296  }
297 }