OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.openoffice.spreadsheet;
15
 
16
import org.openconcerto.openoffice.ODDocument;
25 ilm 17
import org.openconcerto.utils.CompareUtils;
17 ilm 18
 
19
import javax.swing.table.AbstractTableModel;
20
 
21
public class SheetTableModel<D extends ODDocument> extends AbstractTableModel {
22
 
23
    protected final Table<D> table;
24
    protected final int row;
25
    protected final int column;
26
    protected final int lastRow;
27
    protected final int lastCol;
28
 
29
    SheetTableModel(final Table<D> table, final int row, final int column) {
30
        this(table, row, column, table.getRowCount(), table.getColumnCount());
31
    }
32
 
33
    /**
34
     * Creates a new instance.
35
     *
36
     * @param table parent table.
37
     * @param row the first row, inclusive.
38
     * @param column the first column, inclusive.
39
     * @param lastRow the last row, exclusive.
40
     * @param lastCol the last column, exclusive.
41
     */
42
    SheetTableModel(final Table<D> table, final int row, final int column, final int lastRow, final int lastCol) {
43
        super();
44
        this.table = table;
45
        this.row = row;
46
        this.column = column;
47
        this.lastRow = lastRow;
48
        this.lastCol = lastCol;
49
    }
50
 
51
    @Override
52
    public int getColumnCount() {
53
        return this.lastCol - this.column;
54
    }
55
 
56
    @Override
57
    public int getRowCount() {
58
        return this.lastRow - this.row;
59
    }
60
 
61
    @Override
62
    public Object getValueAt(final int rowIndex, final int columnIndex) {
63
        check(rowIndex, columnIndex);
64
        return this.table.getValueAt(this.column + columnIndex, this.row + rowIndex);
65
    }
66
 
67
    public Cell<D> getImmutableCellAt(int rowIndex, int columnIndex) {
68
        check(rowIndex, columnIndex);
69
        return this.table.getImmutableCellAt(this.column + columnIndex, this.row + rowIndex);
70
    }
71
 
72
    // protect cells outside our range
73
    protected final void check(int rowIndex, int columnIndex) {
74
        if (rowIndex < 0 || rowIndex >= this.getRowCount())
75
            throw new IndexOutOfBoundsException("row :" + rowIndex + " not between 0 and " + (this.getRowCount() - 1));
76
        if (columnIndex < 0 || columnIndex >= this.getColumnCount())
77
            throw new IndexOutOfBoundsException("column: " + columnIndex + " not between 0 and " + (this.getColumnCount() - 1));
78
    }
79
 
25 ilm 80
    @Override
81
    public int hashCode() {
82
        final int rowCount = getRowCount();
83
        final int columnCount = getColumnCount();
84
        final int prime = 17;
85
        int result = 1;
86
        result = prime * result + rowCount;
87
        result = prime * result + columnCount;
88
        // use some of the values
89
        final int maxX = Math.min(4, columnCount);
90
        final int maxY = Math.min(8, rowCount);
91
        for (int y = 0; y < maxY; y++) {
92
            for (int x = 0; x < maxX; x++) {
93
                final Object v = this.getValueAt(x, y);
94
                result = prime * result + (v == null ? 0 : v.hashCode());
95
            }
96
        }
97
        return result;
98
    }
99
 
100
    @Override
101
    public boolean equals(Object obj) {
102
        if (this == obj)
103
            return true;
104
        if (obj == null)
105
            return false;
106
        if (!(obj instanceof SheetTableModel))
107
            return false;
108
        final SheetTableModel<?> other = (SheetTableModel<?>) obj;
109
 
110
        final int rowCount = this.getRowCount();
111
        final int columnCount = this.getColumnCount();
112
        if (other.getRowCount() != rowCount || other.getColumnCount() != columnCount)
113
            return false;
114
 
115
        for (int y = 0; y < rowCount; y++) {
116
            for (int x = 0; x < columnCount; x++) {
117
                if (!CompareUtils.equals(this.getValueAt(x, y), other.getValueAt(x, y)))
118
                    return false;
119
            }
120
        }
121
        return true;
122
    }
123
 
17 ilm 124
    static public final class MutableTableModel<D extends ODDocument> extends SheetTableModel<D> {
125
 
126
        MutableTableModel(final Table<D> table, final int row, final int column) {
127
            super(table, row, column);
128
        }
129
 
130
        MutableTableModel(final Table<D> table, final int row, final int column, final int lastRow, final int lastCol) {
131
            super(table, row, column, lastRow, lastCol);
132
        }
133
 
134
        @Override
135
        public void setValueAt(final Object obj, final int rowIndex, final int columnIndex) {
136
            check(rowIndex, columnIndex);
137
            this.table.setValueAt(obj, this.column + columnIndex, this.row + rowIndex);
138
        }
139
 
140
        public MutableCell<D> getCellAt(int rowIndex, int columnIndex) {
141
            check(rowIndex, columnIndex);
142
            return this.table.getCellAt(this.column + columnIndex, this.row + rowIndex);
143
        }
144
    }
145
}