2
2
3
3
import com .luox6 .conway .Cell ;
4
4
import com .luox6 .conway .ConwayMap ;
5
+ import com .luox6 .conway .gui .factories .Dialog ;
6
+ import com .luox6 .conway .gui .factories .FileChooser ;
5
7
import com .luox6 .conway .gui .models .MapModel ;
8
+ import com .luox6 .conway .gui .models .UserSetting ;
9
+ import com .luox6 .conway .utils .ConveyFileParser ;
6
10
7
11
import javax .swing .*;
12
+ import java .io .*;
8
13
9
14
public class GUIController {
10
15
protected MapModel mapModel ;
@@ -20,8 +25,7 @@ public void setGuiViewer(GUIViewer guiViewer) {
20
25
}
21
26
22
27
protected void start () {
23
- guiViewer .gameBoard .setConwayMap (mapModel .getLatestMap ());
24
- guiViewer .stepActionPanel .updateStep (mapModel .getCurrentIndex ());
28
+ updateMapView ();
25
29
26
30
// Setting the frame visibility to true
27
31
guiViewer .pack ();
@@ -32,53 +36,42 @@ public void cellButtonPressed(int row, int col) {
32
36
if (!mapModel .isStarted ()) {
33
37
Cell c = mapModel .getLatestMap ().getCell (row , col );
34
38
c .flip ();
35
- guiViewer .gameBoard .setState (row , col , c );
39
+ guiViewer .gameBoard .setState (row , col , c , mapModel . getCurrentIndex () );
36
40
} else {
37
- JOptionPane .showMessageDialog (guiViewer ,
38
- "Current Map has started simulation and cannot be changed. Use 'Set as Begin' to edit from current map." ,
39
- "Warning" ,
40
- JOptionPane .WARNING_MESSAGE );
41
+ Dialog .genericWarningDialog (guiViewer ,
42
+ new Exception ("Current Map has started simulation and cannot be changed. " +
43
+ "Use 'Set as Begin' to edit from current map." ));
41
44
}
42
45
}
43
46
44
47
public void nextStepButtonPressed () {
45
48
try {
46
49
mapModel .runOnce ();
47
- guiViewer .gameBoard .setConwayMap (mapModel .getCurrentConwayMap ());
48
- guiViewer .stepActionPanel .updateStep (mapModel .getCurrentIndex ());
50
+ updateMapView ();
49
51
guiViewer .statusPanel .updateCalculatedSimulation (mapModel .getLatestIndex ());
50
52
} catch (Exception e ) {
51
- JOptionPane .showMessageDialog (guiViewer ,
52
- e .getMessage (),
53
- "Warning" ,
54
- JOptionPane .WARNING_MESSAGE );
53
+ Dialog .genericWarningDialog (guiViewer , e );
55
54
}
56
55
}
57
56
58
57
public void backStepButtonPressed () {
59
58
try {
60
59
mapModel .goBack ();
61
- guiViewer .gameBoard .setConwayMap (mapModel .getCurrentConwayMap ());
62
- guiViewer .stepActionPanel .updateStep (mapModel .getCurrentIndex ());
60
+ updateMapView ();
63
61
} catch (Exception e ) {
64
- JOptionPane .showMessageDialog (guiViewer ,
65
- e .getMessage (),
66
- "Warning" ,
67
- JOptionPane .WARNING_MESSAGE );
62
+ Dialog .genericWarningDialog (guiViewer , e );
68
63
}
69
64
}
70
65
71
66
public void stepValueSet (String s ) {
72
- int step = Integer .parseInt (s );
73
67
try {
68
+ int step = Integer .parseInt (s );
74
69
mapModel .setCurrentIndex (step );
75
- guiViewer .gameBoard .setConwayMap (mapModel .getCurrentConwayMap ());
76
- guiViewer .statusPanel .updateCalculatedSimulation (mapModel .getLatestIndex ());
70
+ updateMapView ();
71
+ } catch (NumberFormatException e ) {
72
+ Dialog .numberParseDialog (guiViewer , e );
77
73
} catch (Exception e ) {
78
- JOptionPane .showMessageDialog (guiViewer ,
79
- e .getMessage (),
80
- "Warning" ,
81
- JOptionPane .WARNING_MESSAGE );
74
+ Dialog .genericWarningDialog (guiViewer , e );
82
75
} finally {
83
76
guiViewer .stepActionPanel .updateStep (mapModel .getCurrentIndex ());
84
77
}
@@ -89,18 +82,141 @@ public void exitButtonPressed() {
89
82
}
90
83
91
84
public void resetButtonPressed () {
92
- mapModel .reset ();
93
- guiViewer .gameBoard .setConwayMap (mapModel .getCurrentConwayMap ());
94
- guiViewer .stepActionPanel .updateStep (mapModel .getCurrentIndex ());
95
- guiViewer .statusPanel .resetSimulation ();
85
+ setNewMap (new ConwayMap (mapModel .getRow (), mapModel .getCol ()));
96
86
}
97
87
98
88
public void setBeginButtonPressed () {
99
89
ConwayMap m = mapModel .getCurrentConwayMap ();
100
90
m .clearTicks ();
101
- mapModel = new MapModel (m );
102
- guiViewer .gameBoard .setConwayMap (mapModel .getCurrentConwayMap ());
103
- guiViewer .stepActionPanel .updateStep (mapModel .getCurrentIndex ());
91
+ setNewMap (m );
92
+ }
93
+
94
+ public void openFile () {
95
+ try {
96
+ JFileChooser fc = FileChooser .conwayMapDialog ("Open Map File" );
97
+ if (fc .showOpenDialog (guiViewer ) == JFileChooser .APPROVE_OPTION ) {
98
+ File selectedFile = fc .getSelectedFile ();
99
+ ConwayMap c = ConveyFileParser .parse (new BufferedReader (new FileReader (selectedFile )));
100
+ setNewMap (c );
101
+ }
102
+ } catch (Exception e ) {
103
+ Dialog .genericWarningDialog (guiViewer , e );
104
+ }
105
+ }
106
+
107
+ public void saveFile () {
108
+ try {
109
+ JFileChooser fc = FileChooser .conwayMapDialog ("Save Map as" );
110
+ if (fc .showSaveDialog (guiViewer ) == JFileChooser .APPROVE_OPTION ) {
111
+ File selectedFile = fc .getSelectedFile ();
112
+ BufferedWriter outputFile = new BufferedWriter (new FileWriter (selectedFile ));
113
+ outputFile .append (mapModel .getCurrentConwayMap ().toString ());
114
+ outputFile .close ();
115
+ }
116
+ JOptionPane .showMessageDialog (guiViewer ,
117
+ "File saved successfully!" ,
118
+ "Success" ,
119
+ JOptionPane .INFORMATION_MESSAGE );
120
+ } catch (Exception e ) {
121
+ Dialog .genericWarningDialog (guiViewer , e );
122
+ }
123
+ }
124
+
125
+ public void saveMultipleFiles (String startIndex , String endIndex , String fileFormat ) {
126
+ try {
127
+ int si = Integer .parseInt (startIndex );
128
+ if (si < 0 || si > mapModel .getLatestIndex ()) {
129
+ throw new Exception ("Start Index out of range" );
130
+ }
131
+ int ei = Integer .parseInt (endIndex );
132
+ if (ei < 0 || ei > mapModel .getLatestIndex ()) {
133
+ throw new Exception ("End Index out of range" );
134
+ }
135
+ if (si > ei ) {
136
+ throw new Exception ("Start Index is larger than end Index" );
137
+ }
138
+ JFileChooser fc = FileChooser .conwayMapMultipleFileDialog ("Save map Files to" );
139
+ if (fc .showSaveDialog (guiViewer ) == JFileChooser .APPROVE_OPTION ) {
140
+ File selectedDirectory = fc .getSelectedFile ();
141
+ for (int i = si ; i <= ei ; i ++) {
142
+ String filename = fileFormat .formatted (i );
143
+ BufferedWriter outputFile = new BufferedWriter (new FileWriter (new File (selectedDirectory , filename )));
144
+ outputFile .append (mapModel .getSpecificMap (i ).toString ());
145
+ outputFile .close ();
146
+ }
147
+ UserSetting .setOutputFilesFormat (fileFormat );
148
+ JOptionPane .showMessageDialog (guiViewer ,
149
+ "Files saved successfully!" ,
150
+ "Success" ,
151
+ JOptionPane .INFORMATION_MESSAGE );
152
+ }
153
+ } catch (NumberFormatException e ) {
154
+ Dialog .numberParseDialog (guiViewer , e );
155
+ } catch (Exception e ) {
156
+ Dialog .genericWarningDialog (guiViewer , e );
157
+ }
158
+ }
159
+
160
+ public void openConfigurationDialog () {
161
+ guiViewer .configurationPanel .setVisible (true );
162
+ }
163
+
164
+ public void setLiveCellColor () {
165
+ UserSetting .setAliveColor (JColorChooser .showDialog (guiViewer , "Set Live Cell Color" , UserSetting .getAliveColor ()));
166
+ guiViewer .configurationPanel .updateSettings ();
167
+ updateMapView ();
168
+ }
169
+
170
+ public void setDeadCellColor () {
171
+ UserSetting .setDeadColor (JColorChooser .showDialog (guiViewer , "Set Dead Cell Color" , UserSetting .getDeadColor ()));
172
+ guiViewer .configurationPanel .updateSettings ();
173
+ updateMapView ();
174
+ }
175
+
176
+ public void setTextColor () {
177
+ UserSetting .setTextColor (JColorChooser .showDialog (guiViewer , "Set Text Color" , UserSetting .getTextColor ()));
178
+ guiViewer .configurationPanel .updateSettings ();
179
+ updateMapView ();
180
+ }
181
+
182
+ public void setSurvivalStatus (boolean status ) {
183
+ UserSetting .setShowSurvivalTimes (status );
184
+ guiViewer .configurationPanel .updateSettings ();
185
+ updateMapView ();
186
+ }
187
+
188
+ public void showMultipleFilesPanel () {
189
+ guiViewer .rangeSelectionPanel .updateEndIndex (mapModel .getLatestIndex ());
190
+ guiViewer .rangeSelectionPanel .setVisible (true );
191
+ }
192
+
193
+ public void setMaxShadeLevel (String text ) {
194
+ try {
195
+ int i = Integer .parseInt (text );
196
+ if (i <= 0 ) {
197
+ throw new Exception ("Shade level must be larger than 0" );
198
+ }
199
+ UserSetting .setMaxLevelShade (i );
200
+
201
+ updateMapView ();
202
+ } catch (NumberFormatException e ) {
203
+ Dialog .numberParseDialog (guiViewer , e );
204
+ } catch (Exception e ) {
205
+ Dialog .genericWarningDialog (guiViewer , e );
206
+ }
207
+ }
208
+
209
+
210
+ private void setNewMap (ConwayMap c ) {
211
+ mapModel = new MapModel (c );
212
+ updateMapView ();
104
213
guiViewer .statusPanel .resetSimulation ();
105
214
}
215
+
216
+ private void updateMapView () {
217
+ ConwayMap c = mapModel .getCurrentConwayMap ();
218
+ guiViewer .gameBoard .setConwayMap (c , mapModel .getCurrentIndex ());
219
+ guiViewer .stepActionPanel .updateStep (mapModel .getCurrentIndex ());
220
+ guiViewer .statusPanel .updateCellStatus (c .getAliveCellNum (), c .getDeadCellNum (), c .getTotalCellNum (), c .getMapRowLength (), c .getMapColLength ());
221
+ }
106
222
}
0 commit comments