001    package swingui;
002    
003    import java.awt.BorderLayout;
004    import java.awt.Dimension;
005    import java.awt.GridLayout;
006    import java.awt.event.ActionEvent;
007    import java.awt.event.ActionListener;
008    import java.awt.event.KeyEvent;
009    
010    import javax.swing.JButton;
011    import javax.swing.JLabel;
012    import javax.swing.JPanel;
013    import javax.swing.JScrollPane;
014    
015    import components.GameObject;
016    import components.GameSpace;
017    import components.KeyRegistry;
018    
019    public class TriggerPanel extends JPanel implements ActionListener {
020            /** the gamespace whose triggers we are interested in **/
021            private GameSpace gs;
022            /** the object in gamespace whose triggers we are interested in **/
023            private GameObject obj;
024            
025            //components
026            private JButton addUpButton;
027            private JButton addDownButton;
028            private JPanel triggerList;
029            private JScrollPane triggerScroll;
030            
031            public TriggerPanel() {
032                    super();
033                    
034                    //components
035                    //TRIGGERS
036                    //[box of]
037                    //[keys  ]
038                    //add add
039                    //////////////
040                    
041                    //title and display
042                    JLabel triggerLabel = new JLabel(" Triggers: ");
043                    triggerList = new JPanel(new GridLayout(0, 1)); //one column
044                    //triggerList.setPreferredSize(new Dimension(2, 4));
045                    triggerScroll = new JScrollPane(triggerList,
046                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
047                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
048                    
049                    //add/rm buttons
050                    addUpButton = new JButton("KeyUp");
051                    addUpButton.addActionListener(this);
052                    addDownButton = new JButton("KeyDown");
053                    addDownButton.addActionListener(this); 
054                    
055                    //layout
056                    JPanel triggerButtonPanel = new JPanel();
057                    triggerButtonPanel.add(addUpButton);
058                    triggerButtonPanel.add(addDownButton);
059                    
060                    this.setLayout(new BorderLayout());
061                add(triggerLabel, BorderLayout.NORTH);
062                add(triggerScroll, BorderLayout.CENTER);
063                add(triggerButtonPanel, BorderLayout.SOUTH);
064            }
065            
066            /** change the object abotu which we are displaying info **/
067            public void setObject(GameObject obj) {
068                    this.obj = obj;
069                    if (obj != null) {
070                            gs = obj.getGameSpace();
071                            updateTriggerList();
072                    } else {
073                            gs = null;
074                            clearTriggerList();
075                    }
076            }
077            
078            /** pop up a keyselect dialog **/
079            public void actionPerformed(ActionEvent e) {
080                    if (e.getSource().equals(addUpButton)) {
081                            AddKeyWindow d = new AddKeyWindow(this, gs, obj, false);
082                    } else if (e.getSource().equals(addDownButton)) {
083                            AddKeyWindow d = new AddKeyWindow(this, gs, obj, true);
084                    } else {  //delete key
085                            KeyTrigger src = (KeyTrigger) e.getSource();
086                            gs.getRegistry().remove(src.code(), src.down(), src.target());
087                            updateTriggerList();
088                    }
089            }
090            
091            /** update the triggerlist text area **/
092            public void updateTriggerList() {
093                    clearTriggerList();
094                    KeyRegistry keyreg = gs.getRegistry();
095                    for (Integer key : keyreg.getDowns(obj)) {
096                            KeyTrigger k = new KeyTrigger(key, KeyTrigger.DOWN, obj);
097                            triggerList.add(k);
098                            k.addActionListener(this);
099                    }
100                    for (Integer key : keyreg.getUps(obj)) {
101                            KeyTrigger k = new KeyTrigger(key, KeyTrigger.UP, obj);
102                            triggerList.add(k);
103                            k.addActionListener(this);
104                    }
105                    triggerList.validate();
106                    this.repaint();
107            }
108            
109            /** clear the triggerlist panel **/
110            public void clearTriggerList() {
111                    triggerList.removeAll();
112            }
113            
114            public String getName() {
115                    return "Triggers";
116            }
117            
118            /** A KeyTrigger is a JButton that represents a key trigger. The text of the
119             * button is the key + the direction, and the tool tip is the keyCode number. 
120             * @author ruthdhan
121             *
122             */
123            private class KeyTrigger extends JButton {
124                    /** the target of this KeyTrigger **/
125                    private GameObject tgt;
126                    /** the direction of this trigger **/
127                    private boolean down;
128                    
129                    public static final boolean DOWN = true;
130                    public static final boolean UP = false;
131                    
132                    /** construct a keyTrigger button based on the given key/direction/target **/
133                    public KeyTrigger(Integer key, boolean down, GameObject tgt) {
134                            super();
135                            this.down = down;
136                            this.tgt = tgt;
137                            this.setText(KeyEvent.getKeyText(key) + arrow(down) + "   \u2717");
138                            this.setToolTipText(key+"");
139                    }
140                    
141                    /** get the target of this trigger *
142                     * @return tgt - the GameObject targetted by the trigger */
143                    public GameObject target() {
144                            return tgt;
145                    }
146                    /** get the keycode of this trigger 
147                     * @return int - the code **/
148                    public int code() {
149                            return Integer.valueOf(this.getToolTipText());
150                    }
151                    /** get the direction of this trigger 
152                     * @return true if trigger is a key press (false if a key release) **/
153                    public boolean down() {
154                            return down;
155                    }
156                    
157                    /** return an up or down arrow (unicode) **/
158                    private String arrow(boolean down) {
159                            if (down)
160                                    return "\u2193";
161                            else
162                                    return "\u2191";
163                    }
164            }
165    }