Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
CheatWindow.java
Go to the documentation of this file.
1 package micropolisj.gui;
2 
3 import java.awt.FlowLayout;
4 import java.awt.KeyEventDispatcher;
5 import java.awt.KeyboardFocusManager;
6 import java.awt.event.KeyEvent;
7 import java.util.Map;
8 
9 import javax.swing.JFrame;
10 import javax.swing.JTextField;
11 import javax.swing.JToggleButton;
12 import javax.swing.WindowConstants;
13 
14 import micropolisj.engine.Micropolis;
15 import micropolisj.engine.MicropolisTool;
16 import micropolisj.research.ResearchState;
17 
18 public class CheatWindow extends JFrame implements KeyEventDispatcher{
19 
20  private static final String MORE_MONEY_CHEAT = "gimme";
21  private static final int AMOUNT_GIVEN = 5000;
22 
23  private static final String MORE_RESEARCH_CHEAT = "research";
24  private static final int AMOUNT_RESEARCH = 1000;
25 
26 
27  private static String lastInput;
28 
29  private Micropolis engine;
30 
31  private KeyboardFocusManager manager;
32 
33  private JTextField textField;
34 
35  private Map<MicropolisTool, JToggleButton> toolBtns;
36 
37  public CheatWindow(Micropolis engine, Map<MicropolisTool, JToggleButton> toolBtns) {
38  super("Let's cheat!");
39  this.engine = engine;
40  this.toolBtns = toolBtns;
41  textField = new JTextField(10);
42  textField.setText(lastInput);
43  add(textField);
44  setLayout(new FlowLayout());
45  pack();
46  setLocationRelativeTo(null);
47  manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
48  manager.addKeyEventDispatcher(this);
49  setVisible(true);
50  setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
51  }
52 
53  @Override
54  public boolean dispatchKeyEvent(KeyEvent e) {
55  if(e.getID() == KeyEvent.KEY_PRESSED) {
56  if(e.getKeyCode() == KeyEvent.VK_ENTER) {
57  if(textField.getText().equals(MORE_MONEY_CHEAT)) {
60  }
61  if(textField.getText().equals(MORE_RESEARCH_CHEAT)) {
64  }
65  lastInput = textField.getText();
66  manager.removeKeyEventDispatcher(this);
67  this.dispose();
68  return true;
69  }
70  }
71  return false;
72  }
73 
74 }