OSDN Git Service

Fixed OpenGrid check for grid file correctness.
authorSylvain Vedrenne <sylvain@jankenpoi.net>
Mon, 30 Apr 2012 23:03:35 +0000 (01:03 +0200)
committerSylvain Vedrenne <sylvain@jankenpoi.net>
Mon, 30 Apr 2012 23:03:35 +0000 (01:03 +0200)
src/classes/net/jankenpoi/sudokuki/controller/GridController.java
src/classes/net/jankenpoi/sudokuki/model/GridModel.java
src/classes/net/jankenpoi/sudokuki/ui/swing/OpenGridAction.java

index a02b54c..ce09e96 100644 (file)
@@ -100,17 +100,12 @@ public class GridController {
                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() {
index 0e4892b..f230ded 100644 (file)
@@ -77,24 +77,11 @@ public class GridModel implements Cloneable {
                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() {
@@ -554,15 +541,10 @@ public class GridModel implements Cloneable {
                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);
index 45d5951..5dd1c1c 100644 (file)
@@ -31,7 +31,7 @@ import javax.swing.JFrame;
 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")
@@ -104,31 +104,34 @@ public class OpenGridAction extends AbstractAction {
                }
                
                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()");
+                       }
                }
        }