2 * Sudokuki - essential sudoku game
3 * Copyright (C) 2007-2012 Sylvain Vedrenne
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.
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.
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/>.
18 package net.jankenpoi.sudokuki.ui.swing;
20 import static net.jankenpoi.i18n.I18n._;
22 import java.awt.BorderLayout;
24 import javax.swing.Action;
25 import javax.swing.JFrame;
26 import javax.swing.JOptionPane;
27 import javax.swing.JToolBar;
29 import net.jankenpoi.sudokuki.model.GridChangedEvent;
30 import net.jankenpoi.sudokuki.model.GridModel;
31 import net.jankenpoi.sudokuki.view.GridView;
33 public class SwingView extends GridView {
35 private SwingGrid grid;
38 private ActionsRepository actions;
39 private CheatMenu cheatMenu;
40 private LevelMenu levelMenu;
42 public SwingView(GridModel model) {
44 javax.swing.SwingUtilities.invokeLater(new Runnable() {
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);
52 frame.setIconImage(Images.ICON_APPLICATION.getImage());
54 MenuBar menuBar = new MenuBar(frame, grid, SwingView.this);
55 levelMenu = menuBar.getLevelMenu();
56 cheatMenu = menuBar.getCheatMenu();
57 frame.setJMenuBar(menuBar);
59 JToolBar toolbar = new ToolBar(frame, menuBar.getActions());
60 frame.getContentPane().add(toolbar, BorderLayout.PAGE_START);
61 frame.getContentPane().add(grid, BorderLayout.CENTER);
63 actions = menuBar.getActions();
66 frame.setLocationRelativeTo(null);
67 grid.requestFocusInWindow();
69 * Avoid TAB key presses to cause the focus to go out of the
72 grid.setFocusTraversalKeysEnabled(false);
78 public void display() {
79 javax.swing.SwingUtilities.invokeLater(new Runnable() {
81 frame.setVisible(true);
82 frame.setSize(frame.getPreferredSize());
87 public void gridChanged(final GridChangedEvent event) {
89 javax.swing.SwingUtilities.invokeLater(new Runnable() {
93 GridModel model = (GridModel) event.getSource();
94 refreshSaveAsAction(model);
95 refreshPrintGridAction(model);
96 refreshClearAllMovesAction(model);
98 refreshClearAllMemosAction(model);
99 refreshLevelMenu(model);
100 refreshSolutionMenu(model);
102 refreshResolveAction(model);
103 refreshCustomGridAction(model);
104 refreshPlayCustomGridAction(model);
111 public void gridComplete() {
112 JOptionPane.showMessageDialog(frame,
114 + "<table border=\"0\">"
116 + "<td align=\"center\"><b>"
117 + _("Congratulations!")
123 + "<td align=\"center\">"
124 + _("Grid complete!") + "</td>"
125 + "</tr>" + "</table>" + "</html>", "Sudokuki", JOptionPane.PLAIN_MESSAGE);
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);
135 private void refreshSaveAsAction(GridModel model) {
138 Action saveAsAction = actions.get("SaveAs");
139 saveAsAction.setEnabled(!model.getCustomGridMode());
142 private void refreshPrintGridAction(GridModel model) {
145 Action printGridAction = actions.get("Print");
146 printGridAction.setEnabled(!model.getCustomGridMode());
149 private void refreshClearAllMemosAction(GridModel model) {
152 Action eraseAllMemosAction = actions.get("EraseAllMemos");
153 eraseAllMemosAction.setEnabled(model.areSomeMemosSet());
156 private void refreshClearAllMovesAction(GridModel model) {
157 if (actions == null) {
160 Action clearAllMovesAction = actions.get("ClearAllMoves");
161 clearAllMovesAction.setEnabled(!model.isGridComplete() && model.areSomeCellsFilled());
164 private void refreshLevelMenu(GridModel model) {
165 levelMenu.setEnabled(!model.getCustomGridMode());
168 private void refreshSolutionMenu(GridModel model) {
169 cheatMenu.setEnabled(!model.getCustomGridMode() && model.areSomeCellsEmpty());
172 protected void refreshResolveAction(GridModel model) {
173 if (actions == null) {
176 Action resolveAction = actions.get("ResolveGrid");
177 resolveAction.setEnabled(!model.getCustomGridMode() && model.areSomeCellsEmpty());
180 private void refreshCustomGridAction(GridModel model) {
181 if (actions == null) {
184 Action customGridAction = actions.get("CustomGrid");
185 customGridAction.setEnabled(!model.isGridComplete() && !model.getCustomGridMode() && !model.isGridFull());
188 private void refreshPlayCustomGridAction(GridModel model) {
189 if (actions == null) {
192 Action playCustomGridAction = actions.get("PlayCustomGrid");
193 playCustomGridAction.setEnabled(model.getCustomGridMode() && model.getGridValidity().isGridValid());
197 public void close() {