001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.gui.menu.search;
020
021import icy.search.SearchResult;
022import icy.search.SearchResultProducer;
023
024import java.util.ArrayList;
025import java.util.List;
026
027import javax.swing.table.AbstractTableModel;
028
029public class SearchResultTableModel extends AbstractTableModel
030{
031    /**
032     * 
033     */
034    private static final long serialVersionUID = -6476031165522752303L;
035
036    public static final int COL_RESULT_OBJECT = 1;
037
038    private List<SearchResult> results;
039
040    public SearchResultTableModel(int maxRowCount)
041    {
042        super();
043
044        results = new ArrayList<SearchResult>();
045    }
046
047    @Override
048    public boolean isCellEditable(int rowIndex, int columnIndex)
049    {
050        return false;
051    }
052
053    @Override
054    public int getRowCount()
055    {
056        return results.size();
057    }
058
059    @Override
060    public int getColumnCount()
061    {
062        return 2;
063    }
064
065    @Override
066    public Object getValueAt(int rowIndex, int columnIndex)
067    {
068        if (rowIndex < getRowCount())
069        {
070            final SearchResult element = results.get(rowIndex);
071
072            switch (columnIndex)
073            {
074                case 0:
075                    final SearchResultProducer producer = element.getProducer();
076
077                    // display producer on first producer result
078                    if (rowIndex == 0)
079                        return producer;
080                    else if (results.get(rowIndex - 1).getProducer() != producer)
081                        return producer;
082
083                    return null;
084
085                case 1:
086                    return element;
087            }
088        }
089
090        return null;
091    }
092
093    public void setResults(List<SearchResult> results)
094    {
095        this.results = results;
096    }
097}