Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
ResearchState.java
Go to the documentation of this file.
1 package micropolisj.research;
2 
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.awt.Rectangle;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.io.Serializable;
11 import java.util.HashSet;
12 import java.util.Map;
13 import java.util.ResourceBundle;
14 
15 import javax.swing.JButton;
16 import javax.swing.JFrame;
17 import javax.swing.JLabel;
18 import javax.swing.JOptionPane;
19 import javax.swing.JPanel;
20 import javax.swing.JScrollPane;
21 import javax.swing.JToggleButton;
22 
23 import micropolisj.engine.Micropolis;
24 import micropolisj.engine.MicropolisTool;
25 import micropolisj.gui.MainWindow;
26 
27 public class ResearchState extends JFrame implements ActionListener, Serializable {
28  private static final long serialVersionUID = 1L;
29 
30  static ResourceBundle strings = ResourceBundle.getBundle("micropolisj.GuiStrings"); //changeswp
31 
32  public int policeResearch = 0;
33 
34  public int getPoliceResearchState() {
35  return policeResearch;
36  }
37 
38  public double getPoliceStationRange() {
39  return 1000.0 + policeResearch * 500.0;
40  }
41 
42  public int firemanResearch = 0;
43 
44  public int getFiremanResearchState() {
45  return firemanResearch;
46  }
47 
48  public double getFireStationRange() {
49  return 1000.0 + firemanResearch * 500.0;
50  }
51 
52  public int environmentResearch = 0;
53 
55  return environmentResearch;
56  }
57 
58  public int rocketResearch = 0;
59 
60  public int getRocketResearchState() {
61  return rocketResearch;
62  }
63 
64  int buttonWidth = 250;
65  int buttonHeight = 100;
66  int maxWidth = 0;
67  int maxHeight = 0;
68 
69  public ResearchTree tree;// = new ResearchTree();
70 
71  HashSet<Integer> reached_nodes;// = new HashSet<Integer>();
72 
73  public int researchPoints = 0;
74 
75  private Micropolis city;
76 
77  public Map<MicropolisTool, JToggleButton> toolBtns;
78  public void setToolBtns(Map<MicropolisTool, JToggleButton> toolBtns){
79  if(toolBtns == null)
80  System.out.println("no but");
81  this.toolBtns = toolBtns;
82  }
83 
84  JScrollPane scrollPane;
85  JPanel insidePane;
86  JPanel subMenu;
87  JLabel researchPointsLabel;
88  JButton closeButton;
89  JButton[] buttons;
90 
91  // SINGLETON PATTERN
92  private static ResearchState instance = null;
93 
94 // public static ResearchState getInstance() {
95 // if (instance == null) {
96 // return new ResearchState();
97 // } else {
98 // return instance;
99 // }
100 // }
101 
102  // when loading a *.cty file this method will be called
103  public static ResearchState createFromResearchData(Micropolis engine, ResearchData data, Map<MicropolisTool, JToggleButton> toolBtns) {
104  ResearchState state = new ResearchState(engine);
105  state.setToolBtns(toolBtns);
107  state.firemanResearch = data.fireResearch;
108  state.policeResearch = data.policeResearch;
109  state.rocketResearch = data.rocketResearch;
110  state.researchPoints = data.researchPoints;
111  return state;
112  }
113 
114 
116  ResearchData data = new ResearchData();
122  return data;
123  }
124 
125  // TODO Separate Tree from Frame!!!
127  this(city, new ResearchTree());
128  }
129 
130  // CONSTRUCTOR
132  city = engine;
133  this.tree = tree;
134 
135  reached_nodes = new HashSet<Integer>();
136 
137  setName("Research Tree");
138  setLayout(new BorderLayout());
139 
140  insidePane = new JPanel();
141  insidePane.setLayout(null);
142 
143  scrollPane = new JScrollPane(insidePane);
144  // scrollPane.setLayout(null);
145  add(scrollPane, BorderLayout.CENTER);
146 
147  subMenu = new JPanel();
148  subMenu.setLayout(new BorderLayout());
149  add(subMenu, BorderLayout.SOUTH);
150 
151  researchPointsLabel = new JLabel(Integer.toString(researchPoints) + " " + strings.getString("research.POINTS_NAME") + " ");
152  subMenu.add(researchPointsLabel, BorderLayout.EAST);
153 
154  JPanel ppanel = new JPanel();
155  ppanel.setLayout(new FlowLayout(FlowLayout.CENTER));
156  subMenu.add(ppanel, BorderLayout.CENTER);
157 
158  closeButton = new JButton(strings.getString("research.CLOSE_BUTTON"));
159  closeButton.addActionListener(new ActionListener() {
160  @Override
161  public void actionPerformed(ActionEvent arg0) {
162  setVisible(false);
163  }
164  });
165  ppanel.add(closeButton);
166 
167  int n = ResearchTree.possible_nodes.length;
168  buttons = new JButton[n];
169 
170  for(int node_id = 0; node_id < n; node_id++) {
171  ResearchNode node = ResearchTree.possible_nodes[node_id];
172 
173  buttons[node_id] = new JButton();
174  buttons[node_id].setBounds(new Rectangle(ResearchTree.positionsX[node_id], ResearchTree.positionsY[node_id],
175  buttonWidth, buttonHeight));
176 
177  buttons[node_id].setActionCommand(Integer.toString(node_id));
178  buttons[node_id].addActionListener(this);
179 
180 
181  maxWidth = Math.max(maxWidth, ResearchTree.positionsX[node_id] + buttonWidth);
182  maxHeight = Math.max(maxHeight, ResearchTree.positionsY[node_id] + buttonHeight);
183 
184  insidePane.add(buttons[node_id]);
185  }
186 
187  insidePane.setPreferredSize(new Dimension(maxWidth, maxHeight));
188 
189  refreshPanel();
190 
191  setVisible(false);
192  }
193 
194 /*
195  // CONSTRUCTOR
196  public ResearchState(Micropolis engine, ResearchTree tree) {
197  city = engine;
198  this.tree = tree;
199  reached_nodes = new HashSet<Integer>();
200 
201  setName("Research Tree");
202  setLayout(new BorderLayout());
203 
204  insidePane = new JPanel();
205  insidePane.setLayout(null);
206 
207  scrollPane = new JScrollPane(insidePane);
208  // scrollPane.setLayout(null);
209  add(scrollPane, BorderLayout.CENTER);
210 
211  subMenu = new JPanel();
212  subMenu.setLayout(new BorderLayout());
213  add(subMenu, BorderLayout.SOUTH);
214 
215  researchPointsLabel = new JLabel(Integer.toString(researchPoints) + " research points");
216  subMenu.add(researchPointsLabel, BorderLayout.EAST);
217 
218  JPanel ppanel = new JPanel();
219  ppanel.setLayout(new FlowLayout(FlowLayout.CENTER));
220  subMenu.add(ppanel, BorderLayout.CENTER);
221 
222  closeButton = new JButton("close");
223  closeButton.addActionListener(new ActionListener() {
224  @Override
225  public void actionPerformed(ActionEvent arg0) {
226  setVisible(false);
227  }
228  });
229  ppanel.add(closeButton);
230 
231  int n = ResearchTree.possible_nodes.length;
232  buttons = new JButton[n];
233 
234  for (int node_id = 0; node_id < n; node_id++) {
235  ResearchNode node = ResearchTree.possible_nodes[node_id];
236 
237  buttons[node_id] = new JButton("<html>" + node.getDesc() + "</html>");
238  buttons[node_id].setBounds(new Rectangle(ResearchTree.positionsX[node_id], ResearchTree.positionsY[node_id], buttonWidth, buttonHeight));
239 
240  buttons[node_id].setActionCommand(Integer.toString(node_id));
241  buttons[node_id].addActionListener(this);
242 
243  maxWidth = Math.max(maxWidth, ResearchTree.positionsX[node_id] + buttonWidth);
244  maxHeight = Math.max(maxHeight, ResearchTree.positionsY[node_id] + buttonHeight);
245 
246  insidePane.add(buttons[node_id]);
247  }
248 
249  insidePane.setPreferredSize(new Dimension(maxWidth, maxHeight));
250 
251  refreshPanel();
252 
253  setVisible(false);
254  }
255 */
256  public void showResearchPanel() {
257  setBounds(200, 200, maxWidth + 40, maxHeight + 100); //changeswp
258 
259  refreshPanel();
260 
261  setVisible(true);
262  }
263 
264  public void refreshPanel() {
265 
266  researchPointsLabel.setText(Integer.toString(researchPoints) + " " + strings.getString("research.POINTS_NAME") + " ");
267 
268  for(int node_id = 0; node_id < ResearchTree.possible_nodes.length; node_id++) {
269  ResearchNode node = ResearchTree.possible_nodes[node_id];
270 
271  if(reached_nodes.contains(node_id)) {
272  buttons[node_id].setEnabled(false);
273  buttons[node_id].setText("<html>" + node.getName() + node.getDesc() +"</html>");
274  buttons[node_id].setBackground(Color.blue);
275  }
276  else if(!tree.isReachable(reached_nodes, node_id)) {
277  buttons[node_id].setEnabled(false);
278  buttons[node_id].setText("<html>"+node.getName() + node.getDesc() + "</html>");
279  }
280  else {
281 
282  buttons[node_id].setEnabled(true);
283  buttons[node_id].setText("<html>" + node.getName() + " (" + Integer.toString(node.getCost()) + "pts)" + node.getDesc() +"</html>");
284  }
285  }
286 
287  revalidate();
288  }
289 
290  @Override
291  public void actionPerformed(ActionEvent ev) {
292  int node_id = Integer.parseInt(ev.getActionCommand());
293  ResearchNode node = ResearchTree.possible_nodes[node_id];
294  int cost = node.getCost();
295 
296  if (researchPoints >= cost) {
297 
298  reached_nodes.add(node_id);
299  node.makeChanges(this);
300 
301  researchPoints -= cost;
302 
303  refreshPanel();
304 
306  } else {
307  JOptionPane.showMessageDialog(this, strings.getString("research.POINTS_MISSING"));
308  }
309  }
310 }