1
2
3
4
5 package spellcast.ui;
6
7 import java.awt.*;
8 import java.awt.event.*;
9 import java.util.*;
10 import javax.swing.*;
11 import spellcast.gestures.*;
12
13 /***
14 * This PopupMenu displays the eight spellcast gestures that can be chosen.
15 * When a gesture is selected a <code>gestureSelected</code> event is fired
16 * to all <code>GestureListeners</code> with which gesture was chosen.
17 *
18 * @author Barrie Treloar
19 */
20 public abstract class GesturePopupMenu extends JPopupMenu
21 {
22 private ArrayList listeners = new ArrayList( 1 );
23 private Gesture selectedGesture;
24 private GridBagLayout gridbag = new GridBagLayout();
25
26 public GesturePopupMenu()
27 {
28 setLayout( gridbag );
29 addFirstFourGestures();
30 addSecondFourGestures();
31 }
32
33 public synchronized void addGestureListener( GestureListener l )
34 {
35 if ( ! listeners.contains( l ) )
36 {
37 listeners.add( l );
38 }
39 }
40
41 public synchronized void removeGestureListener( GestureListener l )
42 {
43 listeners.remove( l );
44 }
45
46 private void fireSelectedGesture( Gesture g )
47 {
48 ArrayList copy;
49 synchronized ( listeners )
50 {
51 copy = new ArrayList( listeners );
52 }
53 Iterator i = copy.iterator();
54 while ( i.hasNext() )
55 {
56 GestureListener l = (GestureListener)i.next();
57 l.gestureSelected( g );
58 }
59 }
60
61 abstract Icon getPalmIcon();
62 abstract Icon getDigitIcon();
63 abstract Icon getFingersIcon();
64 abstract Icon getWaveIcon();
65 abstract Icon getClapIcon();
66 abstract Icon getSnapIcon();
67 abstract Icon getKnifeIcon();
68
69 /***
70 * Creates a MenuItem for the specified gesture and icon using the GridBagConstraints.
71 * A GestureSelector is attached as the action listener for this MenuItem so that
72 * the correct gesture can be sent when a gesture is chosen from the PopupMenu.
73 */
74 private void createMenuItem( Gesture theGesture, Icon gestureIcon, GridBagConstraints c )
75 {
76 JMenuItem button = new JMenuItem( gestureIcon );
77 button.setUI( new GestureMenuItemUI() );
78 GestureSelector gs = new GestureSelector( theGesture );
79 button.addActionListener( gs );
80 gridbag.setConstraints( button, c );
81 add( button );
82 }
83
84 private void addFirstFourGestures()
85 {
86 GridBagConstraints c = new GridBagConstraints();
87
88 createMenuItem( GestureFactory.instance().getEmptyHand(), GestureIcon.getEmptyHandIcon(), c );
89 createMenuItem( GestureFactory.instance().getPalm(), getPalmIcon(), c );
90 createMenuItem( GestureFactory.instance().getDigit(), getDigitIcon(), c );
91 c.gridwidth = GridBagConstraints.REMAINDER;
92 createMenuItem( GestureFactory.instance().getFingers(), getFingersIcon(), c );
93 }
94
95 private void addSecondFourGestures()
96 {
97 GridBagConstraints c = new GridBagConstraints();
98
99 createMenuItem( GestureFactory.instance().getWave(), getWaveIcon(), c );
100 createMenuItem( GestureFactory.instance().getClap(), getClapIcon(), c );
101 createMenuItem( GestureFactory.instance().getSnap(), getSnapIcon(), c );
102 c.gridwidth = GridBagConstraints.REMAINDER;
103 createMenuItem( GestureFactory.instance().getKnife(), getKnifeIcon(), c );
104 }
105
106 class GestureSelector implements ActionListener
107 {
108 Gesture gesture;
109
110 GestureSelector( Gesture gesture )
111 {
112 this.gesture = gesture;
113 }
114
115 public void actionPerformed( ActionEvent e )
116 {
117 fireSelectedGesture( gesture );
118 }
119 }
120 }