Package ui.models

Source Code of ui.models.ResultTableModel

/*
* This file is part of TextScout.
*
* TextScout 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.
*
* TextScout 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with TextScout. If not, see <http://www.gnu.org/licenses/>.
*/

package ui.models;

import data.SearchResult;
import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;
import logic.Constraints;
import static logic.logger.Logger.*;
import static logic.logger.Logger.LogLevel.*;

public class ResultTableModel extends AbstractTableModel {
    //Column constants
    public static final int COL_FILENAME = 0;
    public static final int COL_FILEPATH = 1;
    public static final int COL_FILESIZE = 2;
    public static final int COL_FILETYPE = 3;
    public static final int COL_LASTCHANGE = 4;
   
    private static final String[] COLUMN_NAMES = {"File", "Directory", "Size", "Type", "Last Change"};
    private List<SearchResult> results = null;

    public ResultTableModel() {
        results = new ArrayList<>();
    }
   
    @Override
    public int getRowCount() {
        return results.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMN_NAMES.length;
    }

    @Override
    public String getColumnName(int column) {
        return COLUMN_NAMES[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rowIndex >= results.size() || rowIndex < 0) {
            throw new IllegalArgumentException(String.format("Row out of bounds! (Items: %d, Row: %d)", results.size(), rowIndex));
        }

        SearchResult searchResult = get(rowIndex);
        Object value = null;
        switch (columnIndex) {
            case COL_FILENAME:
                value = searchResult.getFileName();
                break;
            case COL_FILEPATH:
                value = searchResult.getPath();
                break;
            case COL_FILESIZE:
                value = searchResult.getSize();
                break;
            case COL_FILETYPE:
                value = searchResult.getFileType();
                break;
            case COL_LASTCHANGE:
                value = searchResult.getLastChange();
                break;
            default:
                log(ERROR, "Invalid column-id <" + columnIndex + ">!");
                throw new IllegalArgumentException(String.format("Invalid column %d!", columnIndex));
        }
        return value;
    }

    public boolean isEmpty() {
        return results.isEmpty();
    }

    public void add(SearchResult e) {
        Constraints.ensureArgumentNotNull(e);
        results.add(e);
        fireTableDataChanged();
    }

    public void addRange(List<SearchResult> items) {
        if (items == null) {
            return;
        }

        for (SearchResult sr : items) {
            results.add(sr);
        }
        fireTableDataChanged();
    }

    public void remove(Object o) {
        Constraints.ensureArgumentNotNull(o);
        results.remove(o);
        fireTableDataChanged();
    }

    public void clear() {
        if (results.size() > 0) {
            results.clear();
            fireTableDataChanged();
        }
    }

    public SearchResult get(int index) {
        return results.get(index);
    }

    public void remove(int index) {
        Constraints.ensureIndexInRange(results, index);
        results.remove(index);
        fireTableDataChanged();
    }

    public int indexOf(Object o) {
        Constraints.ensureArgumentNotNull(o);
        return results.indexOf(o);
    }
}
TOP

Related Classes of ui.models.ResultTableModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.