-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
426 lines (345 loc) · 15.1 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import griddler.importImage.ImportImage;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import javax.swing.AbstractAction;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import utils.*;
/**
*
* @author zeroos
**/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (args.length > 0) {
createAndShowGUI(args[0]);
} else {
createAndShowGUI();
}
}
});
}
private static File file;
private static griddler.GriddlerBoard board;
private static griddler.GriddlerSolver solver;
private static JFrame f;
private static void save(Component parent) {
if (file == null) {
saveAs(parent);
return;
}
try {
String data;
try {
data = board.getData().toXML();
} catch (Exception e) {
JOptionPane.showMessageDialog(parent, TR.t("There are too many colours in this picture, cannot save."),
TR.t("Error"), JOptionPane.ERROR_MESSAGE);
throw e;
}
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
osw.write(data, 0, data.length());
osw.close();
} catch (Exception e) {
//FileNotFound, UnsupportedEncoding
JOptionPane.showMessageDialog(parent, TR.t("An error occured and the file could not be saved."),
TR.t("Error"), JOptionPane.ERROR_MESSAGE);
System.err.println(e.getMessage());
}
}
private static void saveAs(Component parent) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(TR.t("Griddlers"), "grd", "grid", "xml");
chooser.setFileFilter(filter);
if (chooser.showSaveDialog(parent) != JFileChooser.APPROVE_OPTION) {
return;
}
File newFile = chooser.getSelectedFile();
if (!filter.accept(newFile)) {
newFile = new File(newFile.getPath() + ".grd");
}
file = newFile;
save(parent);
}
private static void createAndShowGUI() {
createAndShowGUI(null);
}
private static void createAndShowGUI(String file) {
f = new JFrame(TR.t("Griddler tester"));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// f.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
// f.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
if (file != null) {
board = new griddler.GriddlerBoard(new griddler.GriddlerStaticData(file));
board.setEditData(null);
} else {
board = new griddler.GriddlerBoard();
}
solver = new griddler.GriddlerSolver(board);
solver.setPrintResult(true);
solver.setAssumptionsLimit(-1);
f.setJMenuBar(getMenuBar());
final MyButton editModeButton = new MyButton(TR.t("Edit mode"));
editModeButton.addActionListener(new ActionListener() {
boolean editMode = false;
public void actionPerformed(ActionEvent e) {
editMode = !editMode;
board.setEditMode(editMode);
if (!editMode) {
editModeButton.setLabel(TR.t("Edit mode"));
} else {
editModeButton.setLabel(TR.t("Normal mode"));
}
}
});
f.setContentPane(new griddler.GriddlerPanel(board, new MyButton[]{editModeButton}));
f.setSize(350, 300);
f.setVisible(true);
}
private static JMenuBar getMenuBar() {
//menu bar
//todo: mnemonics i18n
JMenuBar menu = new JMenuBar();
JMenu menuFile = new JMenu(TR.t("File"));
menuFile.setMnemonic(KeyEvent.VK_F);
JMenuItem menuItemNew = new JMenuItem(new AbstractAction(TR.t("New")) {
public void actionPerformed(ActionEvent e) {
board.setData(new griddler.GriddlerStaticData());
f.setSize(f.getWidth() + 1, f.getHeight());
solver.reinit();
file = null;
}
});
menuItemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK));
menuItemNew.setMnemonic(KeyEvent.VK_N);
menuFile.add(menuItemNew);
JMenuItem menuItemOpen = new JMenuItem(new AbstractAction(TR.t("Open")) {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(TR.t("Griddlers"), "grd", "grid", "xml");
chooser.setFileFilter(filter);
if (chooser.showOpenDialog(f) != JFileChooser.APPROVE_OPTION) {
return;
}
File newFile = chooser.getSelectedFile();
board.setData(new griddler.GriddlerStaticData(newFile.toURI().toString()));
file = newFile;
f.setSize(f.getWidth() + 1, f.getHeight());
solver.reinit();
}
});
menuItemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
menuItemOpen.setMnemonic(KeyEvent.VK_O);
menuFile.add(menuItemOpen);
JMenuItem menuItemSave = new JMenuItem(new AbstractAction(TR.t("Save")) {
public void actionPerformed(ActionEvent e) {
save(f);
}
});
menuItemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
menuItemSave.setMnemonic(KeyEvent.VK_S);
menuFile.add(menuItemSave);
JMenuItem menuItemSaveAs = new JMenuItem(new AbstractAction(TR.t("Save as...")) {
public void actionPerformed(ActionEvent e) {
saveAs(f);
}
});
menuItemSaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
menuItemSaveAs.setMnemonic(KeyEvent.VK_A);
menuFile.add(menuItemSaveAs);
menuFile.addSeparator();
JMenuItem menuItemImportImage = new JMenuItem(new AbstractAction(TR.t("Import image")) {
@Override
public void actionPerformed(ActionEvent e) {
new ImportImage(f, board.getData());
}
});
menuItemSaveAs.setMnemonic(KeyEvent.VK_I);
menuFile.add(menuItemImportImage);
menuFile.addSeparator();
JMenuItem menuItemExit = new JMenuItem(new AbstractAction(TR.t("Exit")) {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menuItemExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_DOWN_MASK));
menuItemExit.setMnemonic(KeyEvent.VK_X);
menuFile.add(menuItemExit);
JMenu menuEdit = new JMenu(TR.t("Edit"));
menuEdit.setMnemonic(KeyEvent.VK_E);
JMenuItem menuItemCrop = new JMenuItem(new AbstractAction(TR.t("Crop board")) {
public void actionPerformed(ActionEvent e) {
board.getData().crop();
}
});
menuItemCrop.setMnemonic(KeyEvent.VK_C);
menuEdit.add(menuItemCrop);
JMenuItem menuItemGenDesc = new JMenuItem(new AbstractAction(TR.t("Generate descriptions")) {
public void actionPerformed(ActionEvent e) {
board.getData().genDesc();
}
});
menuItemGenDesc.setMnemonic(KeyEvent.VK_G);
menuEdit.add(menuItemGenDesc);
JMenuItem menuItemDelUnusedColors = new JMenuItem(new AbstractAction(TR.t("Delete unused fields")) {
public void actionPerformed(ActionEvent e) {
board.getData().delUnusedFields();
}
});
menuItemDelUnusedColors.setMnemonic(KeyEvent.VK_D);
menuEdit.add(menuItemDelUnusedColors);
JMenuItem menuItemNormalize = new JMenuItem(new AbstractAction(TR.t("Normalize")) {
public void actionPerformed(ActionEvent e) {
board.getData().delUnusedFields();
board.getData().crop();
board.getData().genDesc();
}
});
menuItemNormalize.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_DOWN_MASK));
menuItemNormalize.setMnemonic(KeyEvent.VK_N);
menuEdit.add(menuItemNormalize);
menuEdit.addSeparator();
final JMenuItem menuItemUndo = new JMenuItem();
final JMenuItem menuItemRedo = new JMenuItem();
menuItemUndo.setAction(new AbstractAction(TR.t("Undo")) {
public void actionPerformed(ActionEvent e) {
board.undo();
menuItemRedo.setEnabled(board.canRedo());
menuItemUndo.setEnabled(board.canUndo());
}
});
menuItemUndo.setEnabled(board.canUndo());
menuItemUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_DOWN_MASK));
menuItemUndo.setMnemonic(KeyEvent.VK_U);
menuEdit.add(menuItemUndo);
menuItemRedo.setAction(new AbstractAction(TR.t("Redo")) {
public void actionPerformed(ActionEvent e) {
board.redo();
menuItemUndo.setEnabled(board.canUndo());
menuItemRedo.setEnabled(board.canRedo());
}
});
menuItemRedo.setEnabled(board.canRedo());
menuItemRedo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
menuItemRedo.setMnemonic(KeyEvent.VK_R);
menuEdit.add(menuItemRedo);
board.addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
menuItemUndo.setEnabled(board.canUndo());
menuItemRedo.setEnabled(board.canRedo());
}
});
menuEdit.addSeparator();
JMenuItem menuItemSelectModeSingle = new JMenuItem(new AbstractAction(TR.t("Single select mode")) {
public void actionPerformed(ActionEvent e) {
board.setSelectMode(board.SINGLE);
}
});
menuItemSelectModeSingle.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, InputEvent.CTRL_DOWN_MASK));
menuItemSelectModeSingle.setMnemonic(KeyEvent.VK_S);
menuEdit.add(menuItemSelectModeSingle);
JMenuItem menuItemSelectModeBlock = new JMenuItem(new AbstractAction(TR.t("Block select mode")) {
public void actionPerformed(ActionEvent e) {
board.setSelectMode(board.BLOCK);
}
});
menuItemSelectModeBlock.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, InputEvent.CTRL_DOWN_MASK));
menuItemSelectModeBlock.setMnemonic(KeyEvent.VK_B);
menuEdit.add(menuItemSelectModeBlock);
JMenu menuWindows = new JMenu(TR.t("Windows"));
menuWindows.setMnemonic(KeyEvent.VK_W);
JMenuItem menuItemFieldsManager = new JMenuItem(new AbstractAction(TR.t("Fields manager")) {
public void actionPerformed(ActionEvent e) {
griddler.FieldsManager.getInstance(f, board.getData());
}
});
menuItemFieldsManager.setMnemonic(KeyEvent.VK_F);
menuWindows.add(menuItemFieldsManager);
JMenuItem menuItemColorsManager = new JMenuItem(new AbstractAction(TR.t("Colors manager")) {
public void actionPerformed(ActionEvent e) {
griddler.ColorsManager.getInstance(f);
}
});
menuItemFieldsManager.setMnemonic(KeyEvent.VK_C);
menuWindows.add(menuItemColorsManager);
JMenu menuSolve = new JMenu(TR.t("Solve"));
menuSolve.setMnemonic(KeyEvent.VK_S);
JMenuItem menuItemNextStep = new JMenuItem(new AbstractAction(TR.t("Next step")) {
public void actionPerformed(ActionEvent e) {
Object[] options = {TR.t("Cancel")};
JOptionPane optionPane = new JOptionPane(TR.t("Looking for a next step..."), JOptionPane.INFORMATION_MESSAGE,
JOptionPane.DEFAULT_OPTION, null, options, options[0]);
final JDialog dialog = optionPane.createDialog(f, TR.t("Working..."));
class MyWorker extends SwingWorker<Object, Void> {
@Override
public Object doInBackground() {
System.out.println(solver.descNextStep());
return new Object();
}
@Override
public void done() {
dialog.dispose();
}
};
MyWorker bgTask = new MyWorker();
bgTask.execute();
dialog.setVisible(true);
bgTask.cancel(true);
}
});
menuItemNextStep.setMnemonic(KeyEvent.VK_N);
menuSolve.add(menuItemNextStep);
JMenuItem menuItemIsSolvable = new JMenuItem(new AbstractAction(TR.t("Is solvable?")) {
public void actionPerformed(ActionEvent e) {
Object[] options = {TR.t("Cancel")};
JOptionPane optionPane = new JOptionPane(TR.t("Solving this puzzle for you..."), JOptionPane.INFORMATION_MESSAGE,
JOptionPane.DEFAULT_OPTION, null, options, options[0]);
final JDialog dialog = optionPane.createDialog(f, TR.t("Working..."));
class myWorker extends SwingWorker<Object, Void> {
@Override
public Object doInBackground() {
solver.setPrintResult(true);
solver.isSolvable();
return new Object();
}
@Override
public void done() {
dialog.dispose();
}
};
(new myWorker()).execute();
dialog.setVisible(true);
}
});
menuItemIsSolvable.setMnemonic(KeyEvent.VK_S);
menuSolve.add(menuItemIsSolvable);
menu.add(menuFile);
menu.add(menuEdit);
menu.add(menuWindows);
menu.add(menuSolve);
return menu;
}
}