All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jopendocument.dom.spreadsheet.SheetTableModel Maven / Gradle / Ivy

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2008 jOpenDocument, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU
 * General Public License Version 3 only ("GPL").  
 * You may not use this file except in compliance with the License. 
 * You can obtain a copy of the License at http://www.gnu.org/licenses/gpl-3.0.html
 * See the License for the specific language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 * 
 */

package org.jopendocument.dom.spreadsheet;

import org.jopendocument.dom.ODDocument;

import javax.swing.table.AbstractTableModel;

public class SheetTableModel extends AbstractTableModel {

    protected final Table table;
    protected final int row;
    protected final int column;
    protected final int lastRow;
    protected final int lastCol;

    SheetTableModel(final Table table, final int row, final int column) {
        this(table, row, column, table.getRowCount(), table.getColumnCount());
    }

    /**
     * Creates a new instance.
     * 
     * @param table parent table.
     * @param row the first row, inclusive.
     * @param column the first column, inclusive.
     * @param lastRow the last row, exclusive.
     * @param lastCol the last column, exclusive.
     */
    SheetTableModel(final Table table, final int row, final int column, final int lastRow, final int lastCol) {
        super();
        this.table = table;
        this.row = row;
        this.column = column;
        this.lastRow = lastRow;
        this.lastCol = lastCol;
    }

    @Override
    public int getColumnCount() {
        return this.lastCol - this.column;
    }

    @Override
    public int getRowCount() {
        return this.lastRow - this.row;
    }

    @Override
    public Object getValueAt(final int rowIndex, final int columnIndex) {
        check(rowIndex, columnIndex);
        return this.table.getValueAt(this.column + columnIndex, this.row + rowIndex);
    }

    public Cell getImmutableCellAt(int rowIndex, int columnIndex) {
        check(rowIndex, columnIndex);
        return this.table.getImmutableCellAt(this.column + columnIndex, this.row + rowIndex);
    }

    // protect cells outside our range
    protected final void check(int rowIndex, int columnIndex) {
        if (rowIndex < 0 || rowIndex >= this.getRowCount())
            throw new IndexOutOfBoundsException("row :" + rowIndex + " not between 0 and " + (this.getRowCount() - 1));
        if (columnIndex < 0 || columnIndex >= this.getColumnCount())
            throw new IndexOutOfBoundsException("column: " + columnIndex + " not between 0 and " + (this.getColumnCount() - 1));
    }

    static public final class MutableTableModel extends SheetTableModel {

        MutableTableModel(final Table table, final int row, final int column) {
            super(table, row, column);
        }

        MutableTableModel(final Table table, final int row, final int column, final int lastRow, final int lastCol) {
            super(table, row, column, lastRow, lastCol);
        }

        @Override
        public void setValueAt(final Object obj, final int rowIndex, final int columnIndex) {
            check(rowIndex, columnIndex);
            this.table.setValueAt(obj, this.column + columnIndex, this.row + rowIndex);
        }

        public MutableCell getCellAt(int rowIndex, int columnIndex) {
            check(rowIndex, columnIndex);
            return this.table.getCellAt(this.column + columnIndex, this.row + rowIndex);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy