summaryrefslogtreecommitdiffstats
path: root/watchmaker/examples/src/java/main/org/uncommons/watchmaker/examples/sudoku/SudokuTableModel.java
blob: 0249f7f8c2ba77b4d7a9b17b0d35fe7808f54e2c (plain)
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
//=============================================================================
// Copyright 2006-2010 Daniel W. Dyer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//=============================================================================
package org.uncommons.watchmaker.examples.sudoku;

import javax.swing.table.AbstractTableModel;

/**
 * {@link javax.swing.table.TableModel} for displaying a Sudoku
 * grid in a {@link javax.swing.JTable}.
 * @author Daniel Dyer
 */
class SudokuTableModel extends AbstractTableModel
{
    // In puzzle mode, the user can edit the given cells and all other
    // cells are blank.  In solution mode, all cells have values and are
    // uneditable.
    private boolean puzzleMode = true;

    // Solution mode data.
    private Sudoku sudoku;

    // Puzzle mode data.
    private final Character[][] cells = new Character[Sudoku.SIZE][Sudoku.SIZE];


    /**
     * Sets the Sudoku grid represented by this table model.
     */
    public void setSudoku(Sudoku sudoku)
    {
        this.sudoku = sudoku;
        puzzleMode = false;
        fireTableRowsUpdated(0, getRowCount() - 1);
    }


    /**
     * @return The Sudoku grid represented by this table model.
     */
    public Sudoku getSudoku()
    {
        return sudoku;
    }

    
    public int getRowCount()
    {
        return Sudoku.SIZE;
    }


    public int getColumnCount()
    {
        return Sudoku.SIZE;
    }


    public Object getValueAt(int row, int column)
    {
        if (puzzleMode)
        {
            return cells[row][column];
        }
        else
        {
            return sudoku == null ? null : sudoku.getValue(row, column);
        }
    }


    @Override
    public boolean isCellEditable(int row, int column)
    {
        return puzzleMode;
    }


    @Override
    public void setValueAt(Object object, int row, int column)
    {
        Character value = (Character) object;
        if (!(value == null || (value >= '1' && value <= '9')))
        {
            throw new IllegalArgumentException("Invalid character: " + value);
        }
        cells[row][column] = value;
        fireTableCellUpdated(row, column);
    }


    /**
     * Sets all cells at once using the same patterns as supported by
     * {@link SudokuFactory}.
     * @param pattern A String representation of a Sudoku puzzle.  Each element
     * in the array represents a single row.  There are 9 elements and each has 9
     * characters, one per cell.  Number cells are represented by the characters
     * '0' to '9' and blank cells are represented by dots.
     */
    public void setPattern(String[] pattern)
    {
        if (pattern.length != Sudoku.SIZE)
        {
            throw new IllegalArgumentException("Pattern must have " + Sudoku.SIZE + " rows.");
        }
        for (int row = 0; row < pattern.length; row++)
        {
            String patternRow = pattern[row];
            if (patternRow.length() != Sudoku.SIZE)
            {
                throw new IllegalArgumentException("Row must have " + Sudoku.SIZE + " columns.");
            }
            for (int column = 0; column < patternRow.toCharArray().length; column++)
            {
                char c = patternRow.toCharArray()[column];
                cells[row][column] = c == '.' ? null : c;
            }
        }
        sudoku = null;
        puzzleMode = true;
        fireTableRowsUpdated(0, getRowCount() - 1);
    }


    /**
     * @return A Sudoku puzzle in the pattern format used by {@link SudokuFactory}.
     */
    public String[] getPattern()
    {
        String[] pattern = new String[Sudoku.SIZE];
        for (int i = 0; i < cells.length; i++)
        {
            Character[] row = cells[i];
            StringBuilder rowString = new StringBuilder();
            for (Character c : row)
            {
                rowString.append(c == null ? '.' : c);
            }
            pattern[i] = rowString.toString();
        }
        return pattern;
    }

}