1: package ste.pendu;
3: import java.awt.*;
4: import java.awt.event.*;
5: import javax.swing.*;
6: import java.beans.PropertyChangeEvent;
7: import java.beans.PropertyChangeListener;
8: import java.beans.PropertyVetoException;
10: /**
11: * Le PenduBean!
12: *
13: * @author <a href="mailto:arnaud.vandyck@ulg.ac.be">Arnaud Vandyck</a>
14: * @version 1.0
15: */
16: public class PenduBean extends JPanel
17: implements PropertyChangeListener
18: {
20: public static final String MOT_EN_COURS = "Mot en cours: ";
21: public static final String LETTRES_ENTREES = "Lettres Entrées: ";
23: private PenduModele modele;
24:
25: private PenduPerduVeto perdu = null;
27: private class MonEcouteur implements ActionListener
28: {
29:
30: public MonEcouteur()
31: {
32: }
34: public void actionPerformed (ActionEvent action)
35: {
36: String s = ( (JButton)action.getSource() ).getText();
37: PenduBean.this.modele.addLetter ( s.charAt (0) );
38: ( (JButton)action.getSource() ).setEnabled(false);
39: }
40:
41: }
42:
43: public PenduBean()
44: {
45: super (new BorderLayout ());
46: this.modele = new PenduModele();
47: this.modele.addVetoableChangeListener(new PenduGagneVeto());
48: this.perdu = new PenduPerduVeto (7);
49: this.modele.addVetoableChangeListener(this.perdu);
50: this.add (this.getMotEnCours(), BorderLayout.NORTH);
51: this.add (this.getBoutons(), BorderLayout.CENTER);
52: this.add (this.getLettresEntrees(), BorderLayout.SOUTH);
53: this.modele.addPropertyChangeListener(PenduModele.STATE, this);
54: }
56: public void propertyChange (PropertyChangeEvent evt)
57: {
58: Container parent = this.getParent();
59: if (((Integer)evt.getOldValue()).intValue()==PenduModele.WON)
60: {
61: //JOptionPane.showMessageDialog(null, "Gagné!", "Pendu!", JOptionPane.INFORMATION_MESSAGE);
62: JOptionPane.showInternalMessageDialog(parent, "Gagné!", "Pendu!", JOptionPane.INFORMATION_MESSAGE);
63: }
64: else
65: {
66: //JOptionPane.showMessageDialog(null, "Perdu!", "Pendu!", JOptionPane.INFORMATION_MESSAGE);
67: JOptionPane.showInternalMessageDialog(parent, "Perdu!", "Pendu!", JOptionPane.INFORMATION_MESSAGE);
68: }
69: }
71: protected Component getMotEnCours()
72: {
73: JPanel panel = new JPanel ();
74: final JLabel motEnCours = new JLabel (PenduBean.MOT_EN_COURS,
75: SwingConstants.CENTER);
76: this.modele.addPropertyChangeListener
77: (new PropertyChangeListener()
78: {
79: public void propertyChange(PropertyChangeEvent evt)
80: {
81: motEnCours.setText
82: (PenduBean.MOT_EN_COURS + PenduBean.this.modele.getWord());
83: }
84: });
85: panel.add (motEnCours);
86: return panel;
87: }
89: protected Component getBoutons()
90: {
91: MonEcouteur me = new MonEcouteur();
92: JPanel panelPrinc = new JPanel();
93: JPanel panel = new JPanel(new GridLayout(5,6));
94: for (char i = 'a'; i<= 'z'; i++)
95: {
96: JButton b = new JButton ( String.valueOf(i) );
97: b.addActionListener (me);
98: b.setEnabled(false);
99: panel.add (b);
100: }
101: panelPrinc.add(panel);
102: return panelPrinc;
103: }
105: protected Component getLettresEntrees()
106: {
107: JPanel panel = new JPanel ();
108: final JLabel lettresEntrees = new JLabel (PenduBean.LETTRES_ENTREES,
109: SwingConstants.CENTER);
110: this.modele.addPropertyChangeListener
111: (new PropertyChangeListener()
112: {
113: public void propertyChange(PropertyChangeEvent evt)
114: {
115: lettresEntrees.setText
116: (PenduBean.LETTRES_ENTREES +
117: new String(PenduBean.this.modele.getLetters()));
118: }
119: });
120: panel.add (lettresEntrees);
121: return panel;
122: }
124: /**
125: * Get the Word value.
126: * @return the Word value.
127: */
128: public String getWord()
129: {
130: return this.modele.getWord();
131: }
133: /**
134: * Set the Word value the game can begin.
135: * @param newWord The new Word value.
136: */
137: public void setWord (String newWord)
138: {
139: this.modele.setWord(newWord);
140: parseComponents(this.getComponents());
141: }
143: private void parseComponents(Component[] c)
144: {
145: if (c!=null && c.length>0)
146: {
147: for (int i=0; i<c.length; i++)
148: {
149: if (c[i] instanceof Container)
150: {
151: c[i].setEnabled(true);
152: parseComponents(((Container)c[i]).getComponents());
153: }
154: else
155: {
156: c[i].setEnabled(true);
157: }
158: }
159: }
160: }
162: /**
163: * Get the invalid letters.
164: * @return the Letter value.
165: */
166: public char[] getLetters()
167: {
168: return this.modele.getLetters();
169: }
171: /**
172: * Add a letter.
173: * @param newLetter The new Letter value.
174: */
175: public void addLetter (char newLetter)
176: {
177: this.modele.addLetter(newLetter);
178: }
180: /**
181: * Number of bad try.
182: *
183: * @return Number of bad try.
184: */
185: public int getBadTry()
186: {
187: return this.modele.getBadTry();
188: }
190: /**
191: * Number of good try.
192: *
193: * @return Number of good try.
194: */
195: public int getGoodTry()
196: {
197: return this.modele.getGoodTry();
198: }
200: /**
201: * Number of already tried Tries.
202: *
203: * @return Number of already Try.
204: */
205: public int getAlreadyTry()
206: {
207: return this.modele.getAlreadyTry();
208: }
210:
212: }