Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
NotificationPane.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.util.*;
14 import javax.swing.*;
15 
16 import micropolisj.engine.*;
17 import static micropolisj.gui.ColorParser.parseColor;
18 
19 public class NotificationPane extends JPanel
20 {
21  JLabel headerLbl;
22  JViewport mapViewport;
23  MicropolisDrawingArea mapView;
24  JPanel mainPane;
25  JComponent infoPane;
26 
27  static final Dimension VIEWPORT_SIZE = new Dimension(160,160);
28  static final Color QUERY_COLOR = new Color(255,165,0);
29  static final ResourceBundle strings = MainWindow.strings;
30  static final ResourceBundle mstrings = ResourceBundle.getBundle("micropolisj.CityMessages");
31  static final ResourceBundle s_strings = ResourceBundle.getBundle("micropolisj.StatusMessages");
32 
34  {
35  super(new BorderLayout());
36  setVisible(false);
37 
38  headerLbl = new JLabel();
39  headerLbl.setOpaque(true);
40  headerLbl.setHorizontalAlignment(SwingConstants.CENTER);
41  headerLbl.setBorder(BorderFactory.createRaisedBevelBorder());
42  add(headerLbl, BorderLayout.NORTH);
43 
44  JButton dismissBtn = new JButton(strings.getString("notification.dismiss"));
45  dismissBtn.addActionListener(new ActionListener() {
46  public void actionPerformed(ActionEvent evt) {
48  }});
49  add(dismissBtn, BorderLayout.SOUTH);
50 
51  mainPane = new JPanel(new BorderLayout());
52  add(mainPane, BorderLayout.CENTER);
53 
54  JPanel viewportContainer = new JPanel(new BorderLayout());
55  viewportContainer.setBorder(
56  BorderFactory.createCompoundBorder(
57  BorderFactory.createEmptyBorder(8,4,8,4),
58  BorderFactory.createLineBorder(Color.BLACK)
59  ));
60  mainPane.add(viewportContainer, BorderLayout.WEST);
61 
62  mapViewport = new JViewport();
63  mapViewport.setPreferredSize(VIEWPORT_SIZE);
64  mapViewport.setMaximumSize(VIEWPORT_SIZE);
65  mapViewport.setMinimumSize(VIEWPORT_SIZE);
66  viewportContainer.add(mapViewport, BorderLayout.CENTER);
67 
68  mapView = new MicropolisDrawingArea(engine);
69  mapViewport.setView(mapView);
70  }
71 
72  private void onDismissClicked()
73  {
74  setVisible(false);
75  }
76 
77  void setPicture(Micropolis engine, int xpos, int ypos)
78  {
79  Dimension sz = VIEWPORT_SIZE;
80 
81  mapView.setEngine(engine);
82  Rectangle r = mapView.getTileBounds(xpos,ypos);
83 
84  mapViewport.setViewPosition(new Point(
85  r.x + r.width/2 - sz.width/2,
86  r.y + r.height/2 - sz.height/2
87  ));
88  }
89 
90  public void showMessage(Micropolis engine, MicropolisMessage msg, int xpos, int ypos)
91  {
92  setPicture(engine, xpos, ypos);
93 
94  if (infoPane != null) {
95  mainPane.remove(infoPane);
96  infoPane = null;
97  }
98 
99  headerLbl.setText(mstrings.getString(msg.name()+".title"));
100  headerLbl.setBackground(parseColor(mstrings.getString(msg.name()+".color")));
101 
102  JLabel myLabel = new JLabel("<html><p>"+
103  mstrings.getString(msg.name()+".detail") + "</p></html>");
104  myLabel.setPreferredSize(new Dimension(1,1));
105 
106  infoPane = myLabel;
107  mainPane.add(myLabel, BorderLayout.CENTER);
108 
109  setVisible(true);
110  }
111 
112  public void showZoneStatus(Micropolis engine, int xpos, int ypos, ZoneStatus zone)
113  {
114  headerLbl.setText(strings.getString("notification.query_hdr"));
115  headerLbl.setBackground(QUERY_COLOR);
116 
117  String buildingStr = zone.building != -1 ? s_strings.getString("zone."+zone.building) : "";
118  String popDensityStr = s_strings.getString("status."+zone.popDensity);
119  String landValueStr = s_strings.getString("status."+zone.landValue);
120  String crimeLevelStr = s_strings.getString("status."+zone.crimeLevel);
121  String pollutionStr = s_strings.getString("status."+zone.pollution);
122  String growthRateStr = s_strings.getString("status."+zone.growthRate);
123 
124  setPicture(engine, xpos, ypos);
125 
126  if (infoPane != null) {
127  mainPane.remove(infoPane);
128  infoPane = null;
129  }
130 
131  JPanel p = new JPanel(new GridBagLayout());
132  mainPane.add(p, BorderLayout.CENTER);
133  infoPane = p;
134 
135  GridBagConstraints c1 = new GridBagConstraints();
136  GridBagConstraints c2 = new GridBagConstraints();
137 
138  c1.gridx = 0;
139  c2.gridx = 1;
140  c1.gridy = c2.gridy = 0;
141  c1.anchor = GridBagConstraints.WEST;
142  c2.anchor = GridBagConstraints.WEST;
143  c1.insets = new Insets(0,0,0,8);
144  c2.weightx = 1.0;
145 
146  p.add(new JLabel(strings.getString("notification.zone_lbl")), c1);
147  p.add(new JLabel(buildingStr), c2);
148 
149  c1.gridy = ++c2.gridy;
150  p.add(new JLabel(strings.getString("notification.density_lbl")), c1);
151  p.add(new JLabel(popDensityStr), c2);
152 
153  c1.gridy = ++c2.gridy;
154  p.add(new JLabel(strings.getString("notification.value_lbl")), c1);
155  p.add(new JLabel(landValueStr), c2);
156 
157  c1.gridy = ++c2.gridy;
158  p.add(new JLabel(strings.getString("notification.crime_lbl")), c1);
159  p.add(new JLabel(crimeLevelStr), c2);
160 
161  c1.gridy = ++c2.gridy;
162  p.add(new JLabel(strings.getString("notification.pollution_lbl")), c1);
163  p.add(new JLabel(pollutionStr), c2);
164 
165  c1.gridy = ++c2.gridy;
166  p.add(new JLabel(strings.getString("notification.growth_lbl")), c1);
167  p.add(new JLabel(growthRateStr), c2);
168 
169  c1.gridy++;
170  c1.gridwidth = 2;
171  c1.weighty = 1.0;
172  p.add(new JLabel(), c1);
173 
174  setVisible(true);
175  }
176 }