OSDN Git Service

8f4e9a5b155b6a0e85dd4c1637c706be98ffb686
[sudokuki/sudokuki.git] / src / classes / net / jankenpoi / sudokuki / ui / swing / SwingView.java
1 /*
2  * Sudokuki - essential sudoku game
3  * Copyright (C) 2007-2012 Sylvain Vedrenne
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package net.jankenpoi.sudokuki.ui.swing;
19
20 import static net.jankenpoi.i18n.I18n._;
21
22 import java.awt.BorderLayout;
23
24 import javax.swing.Action;
25 import javax.swing.JFrame;
26 import javax.swing.JOptionPane;
27 import javax.swing.JToolBar;
28
29 import net.jankenpoi.sudokuki.model.GridChangedEvent;
30 import net.jankenpoi.sudokuki.model.GridModel;
31 import net.jankenpoi.sudokuki.view.GridView;
32
33 public class SwingView extends GridView {
34
35         private SwingGrid grid;
36         private JFrame frame;
37
38         private ActionsRepository actions;
39         private CheatMenu cheatMenu;
40         private LevelMenu levelMenu;
41         
42         public SwingView(GridModel model) {
43                 super(model);
44                 javax.swing.SwingUtilities.invokeLater(new Runnable() {
45                         public void run() {
46                                 frame = new JFrame("Sudokuki");
47                                 frame.setLayout(new BorderLayout());
48                                 grid = new SwingGrid(SwingView.this, frame);
49                                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
50                                 frame.setResizable(true);
51                                 
52                                 frame.setIconImage(Images.ICON_APPLICATION.getImage());
53                                 
54                                 MenuBar menuBar = new MenuBar(frame, grid, SwingView.this);
55                                 levelMenu = menuBar.getLevelMenu();
56                                 cheatMenu = menuBar.getCheatMenu();
57                                 frame.setJMenuBar(menuBar);
58                                 
59                                 JToolBar toolbar = new ToolBar(frame, menuBar.getActions());
60                                 frame.getContentPane().add(toolbar, BorderLayout.PAGE_START);
61                                 frame.getContentPane().add(grid, BorderLayout.CENTER);
62
63                                 actions = menuBar.getActions();
64                                 
65                                 frame.pack();
66                                 frame.setLocationRelativeTo(null);
67                                 grid.requestFocusInWindow();
68                                 /*
69                                  * Avoid TAB key presses to cause the focus to go out of the
70                                  * grid
71                                  */
72                                 grid.setFocusTraversalKeysEnabled(false);
73                         }
74                 });
75         }
76
77         @Override
78         public void display() {
79                 javax.swing.SwingUtilities.invokeLater(new Runnable() {
80                         public void run() {
81                                 frame.setVisible(true);
82                                 frame.setSize(frame.getPreferredSize());
83                         }
84                 });
85         }
86
87         public void gridChanged(final GridChangedEvent event) {
88
89                 javax.swing.SwingUtilities.invokeLater(new Runnable() {
90                         public void run() {
91                                 grid.repaint();
92                                 
93                                 GridModel model = (GridModel) event.getSource();
94                                 refreshSaveAsAction(model);
95                                 refreshPrintGridAction(model);
96                                 refreshClearAllMovesAction(model);
97                                 
98                                 refreshClearAllMemosAction(model);
99                                 refreshLevelMenu(model);
100                                 refreshSolutionMenu(model);
101                                 
102                                 refreshResolveAction(model);
103                                 refreshCustomGridAction(model);
104                                 refreshPlayCustomGridAction(model);
105                         }
106
107                 });
108         }
109         
110         @Override
111         public void gridComplete() {
112                 JOptionPane.showMessageDialog(frame, 
113                 "<html>"
114         + "<table border=\"0\">"
115         + "<tr>"
116         + "<td align=\"center\"><b>"
117         + _("Congratulations!")
118         + "</b></td>"
119         + "</tr>"
120         + "<tr>"
121         + "</tr>"
122         + "<tr>"
123         + "<td align=\"center\">"
124         + _("Grid complete!") + "</td>"
125         + "</tr>" + "</table>" + "</html>", "Sudokuki", JOptionPane.PLAIN_MESSAGE);
126         }
127         
128         @Override
129         public void gridResolved() {
130                 JOptionPane.showMessageDialog(frame, "<html>" + "<table border=\"0\">"
131                                 + "<tr>" + _("Grid resolved with success.") + "</tr>"
132                                 + "</html>", "Sudokuki", JOptionPane.PLAIN_MESSAGE);
133         }
134         
135         private void refreshSaveAsAction(GridModel model) {
136                 if (actions == null)
137                         return;
138                 Action saveAsAction = actions.get("SaveAs");
139                 saveAsAction.setEnabled(!model.getCustomGridMode());
140         }
141         
142         private void refreshPrintGridAction(GridModel model) {
143                 if (actions == null)
144                         return;
145                 Action printGridAction = actions.get("Print");
146                 printGridAction.setEnabled(!model.getCustomGridMode());
147         }
148         
149         private void refreshClearAllMemosAction(GridModel model) {
150                 if (actions == null)
151                         return;
152                 Action eraseAllMemosAction = actions.get("EraseAllMemos");
153                 eraseAllMemosAction.setEnabled(model.areSomeMemosSet());
154         }
155
156         private void refreshClearAllMovesAction(GridModel model) {
157                 if (actions == null) {
158                         return;
159                 }
160                 Action clearAllMovesAction = actions.get("ClearAllMoves"); 
161                 clearAllMovesAction.setEnabled(!model.isGridComplete() && model.areSomeCellsFilled());
162         }
163         
164         private void refreshLevelMenu(GridModel model) {
165                 levelMenu.setEnabled(!model.getCustomGridMode());
166         }
167
168         private void refreshSolutionMenu(GridModel model) {
169                 cheatMenu.setEnabled(!model.getCustomGridMode() && model.areSomeCellsEmpty());
170         }
171
172         protected void refreshResolveAction(GridModel model) {
173                 if (actions == null) {
174                         return;
175                 }
176                 Action resolveAction = actions.get("ResolveGrid"); 
177                 resolveAction.setEnabled(!model.getCustomGridMode() && model.areSomeCellsEmpty());
178         }
179         
180         private void refreshCustomGridAction(GridModel model) {
181                 if (actions == null) {
182                         return;
183                 }
184                 Action customGridAction = actions.get("CustomGrid"); 
185                 customGridAction.setEnabled(!model.isGridComplete() && !model.getCustomGridMode() && !model.isGridFull());
186         }
187
188         private void refreshPlayCustomGridAction(GridModel model) {
189                 if (actions == null) {
190                         return;
191                 }
192                 Action playCustomGridAction = actions.get("PlayCustomGrid"); 
193                 playCustomGridAction.setEnabled(model.getCustomGridMode() && model.getGridValidity().isGridValid());
194         }
195
196         @Override
197         public void close() {
198         }
199
200 }