View Javadoc

1   /**
2    * This program is free software: you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License as published by
4    * the Free Software Foundation, version 3.
5    *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program. If not, see <http://www.gnu.org/licenses/>.
13   */
14  
15  package net.curre.prefcount.event;
16  
17  import java.awt.event.ActionEvent;
18  import java.awt.event.ActionListener;
19  import java.awt.event.KeyEvent;
20  import java.awt.event.KeyListener;
21  import javax.swing.*;
22  
23  import net.curre.prefcount.PrefCountRegistry;
24  import net.curre.prefcount.gui.MainWindow;
25  import net.curre.prefcount.gui.LastInputPanel;
26  import net.curre.prefcount.service.ResultService;
27  
28  /**
29   * Object of this class represents a listener that implements
30   * the <code>ActionListener</code> and the <code>KeyListener</code>
31   * interfaces, to be used as an action and key listeners with the
32   * compute scores button.
33   * <p/>
34   * Created date: Jun 29, 2007
35   *
36   * @author Yevgeny Nyden
37   */
38  public class ComputeButtonActionListener extends AbstractAction
39      implements ActionListener, KeyListener {
40  
41    /** Constructor. */
42    public ComputeButtonActionListener() {
43    }
44  
45    /**
46     * Initiates computing and drawing the final scores.
47     * <p/>
48     * {@inheritDoc}
49     */
50    public void actionPerformed(ActionEvent actionEvent) {
51      computeResultsHelper();
52    }
53  
54    /** {@inheritDoc} */
55    public void keyTyped(KeyEvent keyEvent) {
56    }
57  
58    /** {@inheritDoc} */
59    public void keyPressed(KeyEvent keyEvent) {
60    }
61  
62    /** {@inheritDoc} */
63    public void keyReleased(KeyEvent keyEvent) {
64      if (KeyEvent.VK_ENTER == keyEvent.getKeyCode()) {
65        computeResultsHelper();
66      }
67    }
68  
69    /** Private methods ***********************/
70  
71    /** Helper method to generate final results. */
72    private void computeResultsHelper() {
73      LastInputPanel panel = PrefCountRegistry.getInstance().getLastInputPanel();
74      if (panel.validateFields()) {
75        panel.doOnLeave();
76        MainWindow mainWindow = PrefCountRegistry.getInstance().getMainWindow();
77        ResultService.generateFinalResults(mainWindow.playerResults);
78        mainWindow.repaint();
79      }
80    }
81  
82  }