001    package swingui;
002    
003    import java.awt.BorderLayout;
004    import java.awt.GridLayout;
005    import java.awt.event.ActionEvent;
006    import java.awt.event.ActionListener;
007    import java.awt.event.KeyEvent;
008    import java.awt.event.KeyListener;
009    
010    import javax.swing.JButton;
011    import javax.swing.JFrame;
012    import javax.swing.JLabel;
013    import javax.swing.JPanel;
014    import javax.swing.JTextField;
015    
016    import components.GameObject;
017    import components.GameSpace;
018    
019    /** AddKeyWindow is a mutable class that extends JFrame to make a window that allows the user to add a key trigger 
020     * <p>
021     * Unfortunately, AddKeyWindow is not modal, so if the user wished it, she could ignore it and
022     * continue to use the main program. This would not actually cause any problems with gameplay, however.
023     * AddKeyWindow does at least request focus, so hopefully it will not be ignored.
024     * <p>
025     * This window listens for key presses and updates two (uneditable) textfields with the key name
026     * and key code of whatever key is pressed. When the user clicks "Select", the chosen key is added as a
027     * trigger to the object that started this window. 
028     * <p>
029     * It might have been nicer to use JDialog, but JDialog cannot use KeyListeners for reasons that
030     * are not entirely clear. 
031     * 
032     * @author ruthdhan
033     *
034     */
035    public class AddKeyWindow extends JFrame implements KeyListener, ActionListener {
036            private static final long serialVersionUID = 1776L;
037            //fields
038            private TriggerPanel parent;
039            private GameSpace gs;
040            private GameObject obj;
041            private boolean down;
042            
043            //components
044            private JButton okay;
045            private JTextField keyText;
046            private JTextField keyCode;
047            
048            /** construct an AddKeyWindow, with references to its parent, its GameSpace and Object,
049             * and which direction we want to add a key trigger in (key press or release). 
050             * @param parent - the TriggerPanel that this was caled from 
051             * @param gs     - the current GameSpace
052             * @param obj    - the object to add a trigger to
053             * @param down   - true if we want to add a key press, false for key release
054             */
055            public AddKeyWindow(TriggerPanel parent, GameSpace gs, GameObject obj, boolean down) {
056                    super();
057                    this.parent = parent;
058                    this.gs = gs;
059                    this.obj = obj;
060                    this.down = down;
061                    
062                    //components
063                    okay = new JButton("Select");
064                    okay.setEnabled(false);
065                    okay.setFocusable(false); //no focus! how shall i listen to keys if you do that?
066                    keyText = new JTextField("");
067                    keyText.setEditable(false);
068                    keyCode = new JTextField("");
069                    keyCode.setEditable(false);
070                    
071                    //panels + layout
072                    JPanel keypicker = new JPanel();
073                    keypicker.setLayout(new GridLayout(2, 2));
074                    keypicker.add(new JLabel (" KEY: "));
075                    keypicker.add(keyText);
076                    keypicker.add(new JLabel("CODE: "));
077                    keypicker.add(keyCode);
078                    add(keypicker, BorderLayout.CENTER);
079                    add(okay, BorderLayout.SOUTH);
080                    
081                    //key listener
082                    addKeyListener(this);
083                    
084                    //okay button
085                    okay.addActionListener(this);
086                    
087                    //check layout
088                    validate();
089                    
090                    //finish up
091                    setSize(200, 100);
092                    setVisible(true);
093                    requestFocus(); 
094            }
095            
096            /** when a key is pressed, update the textfields 
097             * @effects textfields **/
098            public void keyPressed(KeyEvent arg0) {
099                    System.err.println("key pressed!");
100                    okay.setEnabled(true);
101                    keyText.setText(KeyEvent.getKeyText(arg0.getKeyCode()));
102                    keyCode.setText(""+arg0.getKeyCode());
103            }
104            public void keyReleased(KeyEvent arg0) {}
105            public void keyTyped(KeyEvent arg0) {}
106    
107            /** when select button is clicked, add key trigger and dispose of dialog **/
108            public void actionPerformed(ActionEvent arg0) {
109                    if (arg0.getSource().equals(okay)) {
110                            System.err.println("clicked OKAY");
111                            int code = Integer.valueOf(keyCode.getText());
112                            if (code > 0) {
113                                    if (down) 
114                                            gs.addDownKey(code, obj);
115                                    else
116                                            gs.addUpKey(code, obj);
117                            }
118                            parent.updateTriggerList();
119                            this.dispose();
120                    } 
121            }
122    }