OSDN Git Service

Support Polish language thanks to Monika Viste
[sudokuki/sudokuki.git] / src / classes / net / jankenpoi / sudokuki / controller / GridController.java
1 /*
2  * Sudokuki - essential sudoku game
3  * Copyright (C) 2007-2016 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.controller;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import net.jankenpoi.sudokuki.model.GridChangedEvent;
24 import net.jankenpoi.sudokuki.model.GridModel;
25 import net.jankenpoi.sudokuki.model.GridModel.GridValidity;
26 import net.jankenpoi.sudokuki.view.GridView;
27
28 public class GridController {
29
30         private GridModel model;
31
32         List<GridView> views = new ArrayList<GridView>();
33
34         public GridController(GridModel model) {
35                 this.model = model;
36         }
37
38         public void displayViews() {
39                 for (GridView view : views) {
40                         view.display();
41                 }
42         }
43
44         public void closeViews() {
45                 for (GridView view : views) {
46                         view.close();
47                 }
48         }
49
50         public void notifyGridChanged() {
51                 model.fireGridChanged(new GridChangedEvent(model, 0, 0, (short)0));
52         }
53         
54         public void notifyGridValueChanged(int li, int co, int value, boolean silent) {
55                 model.setCellValue(li, co, value, silent);
56         }
57
58         public void notifyGridMemosChanged(int li, int co, byte[] memos) {
59                 model.clearCellMemos(li, co);
60                 model.setCellMemos(li, co, memos);
61         }
62
63         public void addView(GridView view) {
64                 views.add(view);
65                 view.setController(this);
66                 view.gridChanged(new GridChangedEvent(model, 0, 0, (short)0));
67                 model.addGridListener(view);
68                 view.display();
69         }
70
71         public void notifySetAllMemosRequested() {
72                 model.setMemosForAllCells();
73         }
74
75         public void notifyClearAllMovesRequested() {
76                 model.clearAllUserMoves();
77         }
78         
79         public void notifyClearAllMemosRequested() {
80                 model.clearAllUserMemos();
81         }
82         
83         public void notifyEnterCustomGridMode() {
84                 model.enterCustomGridMode();
85         }
86         
87         public void notifyExitCustomGridModeRequested() {
88                 GridValidity validity = model.getGridValidity();
89                 if (validity.isGridValid()) {
90                         model.exitCustomGridMode();
91                 }
92                 model.fireGridChanged(new GridChangedEvent(model, 0, 0, (short)0));
93         }
94         
95         public void notifyNewGridRequested() {
96                 if (model.getCustomGridMode()) {
97                         model.exitCustomGridMode();
98                 }
99                 model.requestNewGrid();
100                 model.fireGridChanged(new GridChangedEvent(model, 0, 0, (short)0));
101         }
102
103         public void notifyResetGridFromShorts(short[] externalCellInfos) {
104                 if (model.getCustomGridMode()) {
105                         model.exitCustomGridMode();
106                 }
107                 model.resetGridModelFromShorts(externalCellInfos);
108                 model.fireGridChanged(new GridChangedEvent(model, 0, 0, (short)0));
109         }
110
111         public int[] getCellInfosFromModel() {
112                 return model.asIntArray();
113         }
114
115         public void notifyGridResolutionSuccess() {
116                 notifyGridChanged();
117                 model.setGridResolved();
118         }
119
120         public void notifyGridComplete() {
121                 notifyGridChanged();
122                 model.setGridComplete();
123         }
124         
125         private int lastLI = 4;
126         private int lastCO = 4;
127
128         public void notifyFocusPositionChanged(int li, int co) {
129                 lastLI = li;
130                 lastCO = co;
131         }
132
133         public void notifySetMemosHere() {
134                 model.setMemosForThisCell(lastLI, lastCO);
135         }
136 }