9 package micropolisj.gui;
12 import java.awt.event.*;
14 import javax.swing.event.*;
15 import java.text.NumberFormat;
18 import micropolisj.engine.*;
19 import static micropolisj.gui.MainWindow.formatFunds;
20 import static micropolisj.gui.MainWindow.formatGameDate;
26 JSpinner taxRateEntry;
31 double origResearchPct;
34 JLabel roadFundRequest =
new JLabel();
35 JLabel roadFundAlloc =
new JLabel();
36 JSlider roadFundEntry;
38 JLabel policeFundRequest =
new JLabel();
39 JLabel policeFundAlloc =
new JLabel();
40 JSlider policeFundEntry;
42 JLabel fireFundRequest =
new JLabel();
43 JLabel fireFundAlloc =
new JLabel();
44 JSlider fireFundEntry;
46 JLabel researchFundRequest =
new JLabel();
47 JLabel researchFundAlloc =
new JLabel();
48 JSlider researchFundEntry;
50 JLabel taxRevenueLbl =
new JLabel();
52 static ResourceBundle strings =
MainWindow.strings;
54 JCheckBox autoBudgetBtn =
new JCheckBox(strings.getString(
"budgetdlg.auto_budget"));
55 JCheckBox pauseBtn =
new JCheckBox(strings.getString(
"budgetdlg.pause_game"));
59 int newTaxRate = ((Number) taxRateEntry.getValue()).intValue();
60 int newRoadPct = ((Number) roadFundEntry.getValue()).intValue();
61 int newPolicePct = ((Number) policeFundEntry.getValue()).intValue();
62 int newFirePct = ((Number) fireFundEntry.getValue()).intValue();
63 int newResearchPct = ((Number) researchFundEntry.getValue()).intValue();
65 engine.
setBudgetNumbers(newTaxRate, (
double)newRoadPct / 100.0, (
double)newRoadPct / 100.0,
66 (
double)newPolicePct / 100.0, (
double)newFirePct / 100.0, (
double)newResearchPct/100.0);
76 taxRateEntry.setValue(b.taxRate);
77 roadFundEntry.setValue((
int)Math.round(b.roadPercent*100.0));
78 policeFundEntry.setValue((
int)Math.round(b.policePercent*100.0));
79 fireFundEntry.setValue((
int)Math.round(b.firePercent*100.0));
80 researchFundEntry.setValue((
int)Math.round(b.researchPercent*100.0));
83 taxRevenueLbl.setText(formatFunds(b.taxIncome));
85 roadFundRequest.setText(formatFunds(b.roadRequest));
86 roadFundAlloc.setText(formatFunds(b.roadFunded));
88 policeFundRequest.setText(formatFunds(b.policeRequest));
89 policeFundAlloc.setText(formatFunds(b.policeFunded));
91 fireFundRequest.setText(formatFunds(b.fireRequest));
92 fireFundAlloc.setText(formatFunds(b.fireFunded));
94 researchFundRequest.setText(formatFunds(b.researchRequest));
95 researchFundAlloc.setText(formatFunds(b.researchFunded));
98 static void adjustSliderSize(JSlider slider)
100 Dimension sz = slider.getPreferredSize();
101 slider.setPreferredSize(
102 new Dimension(80, sz.height)
109 setTitle(strings.getString(
"budgetdlg.title"));
111 this.engine = engine;
112 this.origTaxRate = engine.getPlayerInfo().cityTax;
113 this.origRoadPct = engine.getPlayerInfo().roadPercent;
114 this.origFirePct = engine.getPlayerInfo().firePercent;
115 this.origPolicePct = engine.getPlayerInfo().policePercent;
116 this.origResearchPct = engine.getPlayerInfo().researchPercent;
119 taxRateEntry =
new JSpinner(
new SpinnerNumberModel(7,0,20,1));
122 roadFundEntry =
new JSlider(JSlider.HORIZONTAL, 0, 100, 100);
123 adjustSliderSize(roadFundEntry);
124 fireFundEntry =
new JSlider(JSlider.HORIZONTAL, 0, 100, 100);
125 adjustSliderSize(fireFundEntry);
126 policeFundEntry =
new JSlider(JSlider.HORIZONTAL, 0, 100, 100);
127 adjustSliderSize(policeFundEntry);
128 researchFundEntry =
new JSlider(JSlider.HORIZONTAL, 0, 100, 100);
129 adjustSliderSize(researchFundEntry);
131 ChangeListener change =
new ChangeListener() {
132 public void stateChanged(ChangeEvent ev) {
136 taxRateEntry.addChangeListener(change);
137 roadFundEntry.addChangeListener(change);
138 fireFundEntry.addChangeListener(change);
139 policeFundEntry.addChangeListener(change);
140 researchFundEntry.addChangeListener(change);
142 Box mainBox =
new Box(BoxLayout.Y_AXIS);
143 mainBox.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
144 add(mainBox, BorderLayout.CENTER);
148 JSeparator sep =
new JSeparator(SwingConstants.HORIZONTAL);
153 JSeparator sep1 =
new JSeparator(SwingConstants.HORIZONTAL);
158 JSeparator sep2 =
new JSeparator(SwingConstants.HORIZONTAL);
163 JPanel buttonPane =
new JPanel();
164 add(buttonPane, BorderLayout.SOUTH);
166 JButton continueBtn =
new JButton(strings.getString(
"budgetdlg.continue"));
167 continueBtn.addActionListener(
new ActionListener() {
168 public void actionPerformed(ActionEvent ev) {
171 buttonPane.add(continueBtn);
173 JButton resetBtn =
new JButton(strings.getString(
"budgetdlg.reset"));
174 resetBtn.addActionListener(
new ActionListener() {
175 public void actionPerformed(ActionEvent ev) {
178 buttonPane.add(resetBtn);
183 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
184 setLocationRelativeTo(owner);
185 getRootPane().registerKeyboardAction(
new ActionListener() {
186 public void actionPerformed(ActionEvent evt) {
189 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
190 JComponent.WHEN_IN_FOCUSED_WINDOW);
197 if (super.getClass().getMethod(
"setAutoRequestFocus",
boolean.
class) != null) {
198 super.setAutoRequestFocus(v);
201 catch (NoSuchMethodException e) {
208 JPanel fundingRatesPane =
new JPanel(
new GridBagLayout());
209 fundingRatesPane.setBorder(BorderFactory.createEmptyBorder(8,0,8,0));
211 GridBagConstraints c0 =
new GridBagConstraints();
214 c0.anchor = GridBagConstraints.WEST;
215 GridBagConstraints c1 =
new GridBagConstraints();
218 c1.anchor = GridBagConstraints.EAST;
219 GridBagConstraints c2 =
new GridBagConstraints();
222 c2.anchor = GridBagConstraints.EAST;
223 GridBagConstraints c3 =
new GridBagConstraints();
226 c3.anchor = GridBagConstraints.EAST;
228 c1.gridy = c2.gridy = c3.gridy = 0;
229 fundingRatesPane.add(
new JLabel(strings.getString(
"budgetdlg.funding_level_hdr")), c1);
230 fundingRatesPane.add(
new JLabel(strings.getString(
"budgetdlg.requested_hdr")), c2);
231 fundingRatesPane.add(
new JLabel(strings.getString(
"budgetdlg.allocation_hdr")), c3);
233 c0.gridy = c1.gridy = c2.gridy = c3.gridy = 1;
234 fundingRatesPane.add(
new JLabel(strings.getString(
"budgetdlg.road_fund")), c0);
235 fundingRatesPane.add(roadFundEntry, c1);
236 fundingRatesPane.add(roadFundRequest, c2);
237 fundingRatesPane.add(roadFundAlloc, c3);
239 c0.gridy = c1.gridy = c2.gridy = c3.gridy = 2;
240 fundingRatesPane.add(
new JLabel(strings.getString(
"budgetdlg.police_fund")), c0);
241 fundingRatesPane.add(policeFundEntry, c1);
242 fundingRatesPane.add(policeFundRequest, c2);
243 fundingRatesPane.add(policeFundAlloc, c3);
245 c0.gridy = c1.gridy = c2.gridy = c3.gridy = 3;
246 fundingRatesPane.add(
new JLabel(strings.getString(
"budgetdlg.fire_fund")), c0);
247 fundingRatesPane.add(fireFundEntry, c1);
248 fundingRatesPane.add(fireFundRequest, c2);
249 fundingRatesPane.add(fireFundAlloc, c3);
251 c0.gridy = c1.gridy = c2.gridy = c3.gridy = 4;
252 fundingRatesPane.add(
new JLabel(strings.getString(
"budgetdlg.research_fund")), c0);
253 fundingRatesPane.add(researchFundEntry, c1);
254 fundingRatesPane.add(researchFundRequest, c2);
255 fundingRatesPane.add(researchFundAlloc, c3);
258 return fundingRatesPane;
263 JPanel optionsPane =
new JPanel(
new GridBagLayout());
264 optionsPane.setBorder(BorderFactory.createEmptyBorder(8,0,0,0));
266 GridBagConstraints c0 =
new GridBagConstraints();
267 GridBagConstraints c1 =
new GridBagConstraints();
271 c0.anchor = c1.anchor = GridBagConstraints.WEST;
272 c0.gridy = c1.gridy = 0;
273 c0.weightx = c1.weightx = 0.5;
274 optionsPane.add(autoBudgetBtn, c0);
275 optionsPane.add(pauseBtn, c1);
285 JPanel pane =
new JPanel(
new GridBagLayout());
286 pane.setBorder(BorderFactory.createEmptyBorder(0,0,8,0));
288 GridBagConstraints c0 =
new GridBagConstraints();
289 GridBagConstraints c1 =
new GridBagConstraints();
290 GridBagConstraints c2 =
new GridBagConstraints();
293 c0.anchor = GridBagConstraints.WEST;
296 c1.anchor = GridBagConstraints.EAST;
299 c2.anchor = GridBagConstraints.EAST;
302 c0.gridy = c1.gridy = c2.gridy = 0;
303 pane.add(
new JLabel(strings.getString(
"budgetdlg.tax_rate_hdr")), c1);
304 pane.add(
new JLabel(strings.getString(
"budgetdlg.annual_receipts_hdr")), c2);
306 c0.gridy = c1.gridy = c2.gridy = 1;
307 pane.add(
new JLabel(strings.getString(
"budgetdlg.tax_revenue")), c0);
308 pane.add(taxRateEntry, c1);
309 pane.add(taxRevenueLbl, c2);
316 if (autoBudgetBtn.isSelected() != engine.
autoBudget) {
341 JPanel balancePane =
new JPanel(
new GridBagLayout());
342 balancePane.setBorder(BorderFactory.createEmptyBorder(8,24,8,24));
344 GridBagConstraints c0 =
new GridBagConstraints();
345 GridBagConstraints c1 =
new GridBagConstraints();
347 c0.anchor = GridBagConstraints.WEST;
352 JLabel thLbl =
new JLabel(strings.getString(
"budgetdlg.period_ending"));
353 Font origFont = thLbl.getFont();
354 Font headFont = origFont.deriveFont(Font.ITALIC);
355 thLbl.setFont(headFont);
356 thLbl.setForeground(Color.MAGENTA);
357 balancePane.add(thLbl, c0);
362 balancePane.add(
new JLabel(strings.getString(
"budgetdlg.taxes_collected")), c0);
364 balancePane.add(
new JLabel(strings.getString(
"budgetdlg.capital_expenses")), c0);
366 balancePane.add(
new JLabel(strings.getString(
"budgetdlg.operating_expenses")), c0);
370 balancePane.add(
new JLabel(strings.getString(
"budgetdlg.cash_income")), c0);
372 balancePane.add(
new JLabel(strings.getString(
"budgetdlg.cash_income_w/o")), c0);
374 c1.anchor = GridBagConstraints.EAST;
378 for (
int i = 0; i < 2; i++) {
386 int cashFlow = f.totalFunds - fPrior.totalFunds;
387 int capExpenses = -(cashFlow - f.taxIncome + f.operatingExpenses);
389 int cashIncome = cashFlow;
390 int cashIncomeWO = f.taxIncome - f.operatingExpenses;
397 thLbl =
new JLabel(formatGameDate(f.cityTime-1));
398 thLbl.setFont(headFont);
399 thLbl.setForeground(Color.MAGENTA);
400 balancePane.add(thLbl, c1);
408 JLabel taxIncomeLbl =
new JLabel();
409 taxIncomeLbl.setText(formatFunds(f.taxIncome));
410 balancePane.add(taxIncomeLbl, c1);
413 JLabel capExpensesLbl =
new JLabel();
414 capExpensesLbl.setText(formatFunds(capExpenses));
415 balancePane.add(capExpensesLbl, c1);
418 JLabel opExpensesLbl =
new JLabel();
419 opExpensesLbl.setText(formatFunds(f.operatingExpenses));
420 balancePane.add(opExpensesLbl, c1);
428 JLabel newCashIncomeLbl =
new JLabel();
429 newCashIncomeLbl.setText(formatFunds(cashIncome));
430 balancePane.add(newCashIncomeLbl, c1);
433 JLabel newCashIncomeWOLbl =
new JLabel();
434 newCashIncomeWOLbl.setText(formatFunds(cashIncomeWO));
435 balancePane.add(newCashIncomeWOLbl, c1);