model.fireGridChanged(new GridChangedEvent(model, 0, 0, (short)0));
}
- public boolean notifyResetGridFromShorts(short[] externalCellInfos) {
- final boolean wasCustomGridModeOn = model.getCustomGridMode();
- if (wasCustomGridModeOn) {
+ public void notifyResetGridFromShorts(short[] externalCellInfos) {
+ if (model.getCustomGridMode()) {
model.exitCustomGridMode();
}
- boolean result = model.resetGridModelFromShorts(externalCellInfos);
- if (wasCustomGridModeOn && result == false) {
- model.enterCustomGridMode();
- }
+ model.resetGridModelFromShorts(externalCellInfos);
model.fireGridChanged(new GridChangedEvent(model, 0, 0, (short)0));
- return result;
}
public int[] getCellInfosFromModel() {
newGrid();
}
- public boolean resetGridModelFromShorts(short[] externalCellInfos) {
- short[] oldCellInfos = new short[81];
- for (int i=0; i<81; i++) {
- oldCellInfos[i] = cellInfos[i];
- }
-
+ public void resetGridModelFromShorts(short[] externalCellInfos) {
cellInfos = new short[81];
for (int i=0; i<81; i++) {
cellInfos[i] = externalCellInfos[i];
}
- if (checkGridValidity().isValid) {
- return true;
- } else {
- for (int i=0; i<81; i++) {
- cellInfos[i] = oldCellInfos[i];
- }
- return false;
- }
}
private void newGrid() {
Integer squareWithErrorY = null;
// Check validity of all lines
- lines:
for (int li = 0; isValid && li < 9; li++) {
byte[] numbers = new byte[10];
for (int co = 0; co < 9; co++) {
byte value = getValueAt(li, co);
- if (10 <= value) {
- isValid = false;
- break lines;
- }
if (numbers[value] != 0) {
isValid = false;
lineWithError = Integer.valueOf(li);
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
-import net.jankenpoi.sudokuki.model.GridModel.GridValidity;
+import net.jankenpoi.sudokuki.model.GridModel;
import net.jankenpoi.sudokuki.view.GridView;
@SuppressWarnings("serial")
}
short[] externalCellInfos = new short[81];
- for (int i=0; i<81; i++) {
- try {
+ try {
+ for (int i = 0; i < 81; i++) {
int lo = fis.read();
int hi = fis.read();
short together = (short) (hi << 8 | lo);
- externalCellInfos[i] = (short) together;
- } catch (IOException e1) {
- JOptionPane.showMessageDialog(frame, "<html>"
- + "<table border=\"0\">" + "<tr>"
- + _("Failed to open the grid<br/>at the selected location.") + "</tr>"
- + "</html>", "Sudokuki", JOptionPane.ERROR_MESSAGE);
+ if ((together & ~(GridModel.FLAG_CELL_READ_ONLY
+ | GridModel.FLAG_GRID_COMPLETE
+ | GridModel.MASK_CELL_MEMOS | GridModel.MASK_CELL_VALUES)) != 0) {
+ throw new IOException("Invalid cell info");
+ }
+ if (9 < (together & GridModel.MASK_CELL_VALUES)) {
+ throw new IOException("Cell value out of range");
+ }
+ externalCellInfos[i] = together;
}
- }
- boolean success = view.getController().notifyResetGridFromShorts(externalCellInfos);
- if (!success) {
- JOptionPane.showMessageDialog(frame, "<html>"
- + "<table border=\"0\">" + "<tr>"
- + _("This file is not a valid Sudokuki grid.") + "</tr>"
- + "</html>", "Sudokuki", JOptionPane.ERROR_MESSAGE);
- }
- try {
- fis.close();
+ view.getController().notifyResetGridFromShorts(externalCellInfos);
} catch (IOException e1) {
- System.out.println("An error occured upon FileInputStream close()");
+ JOptionPane.showMessageDialog(frame, "<html>"
+ + "<table border=\"0\">" + "<tr>"
+ + _("This file is not a Sudokuki grid.") + "</tr>"
+ + "</html>", "Sudokuki", JOptionPane.ERROR_MESSAGE);
+ } finally {
+ try {
+ fis.close();
+ } catch (IOException e1) {
+ System.err.println("An error occured upon FileInputStream close()");
+ }
}
}