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

com.anrisoftware.prefdialog.csvimportdialog.previewpanel.PreviewDataTableModel Maven / Gradle / Ivy

/*
 * Copyright 2013-2015 Erwin Müller 
 *
 * This file is part of prefdialog-csvimportdialog.
 *
 * prefdialog-csvimportdialog is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * prefdialog-csvimportdialog is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with prefdialog-csvimportdialog. If not, see .
 */
package com.anrisoftware.prefdialog.csvimportdialog.previewpanel;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import com.anrisoftware.globalpom.dataimport.CsvImportException;
import com.anrisoftware.globalpom.dataimport.CsvImporter;
import com.anrisoftware.prefdialog.miscswing.awtcheck.OnAwt;

/**
 * Loads the preview data.
 *
 * @author Erwin Mueller, [email protected]
 * @since 1.0
 */
@SuppressWarnings("serial")
class PreviewDataTableModel extends AbstractTableModel {

    private final List> rows;

    private final List headers;

    private final int maxPrefiewRows;

    PreviewDataTableModel() {
        this.rows = new ArrayList>();
        this.headers = new ArrayList();
        this.maxPrefiewRows = 25;
    }

    /**
     * Updates the preview for the specified importer.
     *
     * 

AWT Thread

*

* Should be called in the AWT thread. * * @param importer * the {@link CsvImporter} or {@code null}. * * @throws CsvImportException * if there was an error loading the preview data. */ @OnAwt public void setImporter(CsvImporter importer) throws CsvImportException { if (importer == null) { this.rows.clear(); this.headers.clear(); fireTableStructureChanged(); } else { loadData(importer); fireTableStructureChanged(); } } private void loadData(CsvImporter importer) throws CsvImportException { int i = 0; int rowOffset = importer.getProperties().getStartRow(); int maxIndex = maxPrefiewRows + rowOffset; rows.clear(); headers.clear(); while (true) { List values = importer.call().getValues(); if (values == null || i > maxIndex) { return; } if (i == 0 && importer.getProperties().isHaveHeader()) { headers.addAll(values); } else { if (i >= rowOffset) { rows.add(values); } } i++; } } @Override public int getRowCount() { return rows.size(); } @Override public int getColumnCount() { if (rows.size() == 0) { return 0; } else { return rows.get(0).size(); } } @Override public String getColumnName(int column) { if (headers.size() > 0) { return headers.get(column); } else { return super.getColumnName(column); } } @Override public Object getValueAt(int rowIndex, int columnIndex) { return rows.get(rowIndex).get(columnIndex); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy