/* * This file is part of WebLookAndFeel library. * * WebLookAndFeel library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * WebLookAndFeel library 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 General Public License * along with WebLookAndFeel library. If not, see . */ package com.alee.extended.filechooser; import com.alee.managers.language.LanguageManager; import com.alee.utils.CollectionUtils; import com.alee.utils.FileUtils; import javax.swing.table.AbstractTableModel; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; /** * Data model for WebFileTable. * * @author Mikle Garin */ public class WebFileTableModel extends AbstractTableModel implements WebFileTableColumns { /** * List of displayed column ids. */ private List columns; /** * List of displayed files. */ private List files; /** * Constructs empty model with default displayed columns. */ public WebFileTableModel () { this ( new ArrayList (), CollectionUtils.copy ( DEFAULT_COLUMNS ) ); } /** * Constructs empty model with specified displayed columns. * * @param columns columns to display */ public WebFileTableModel ( String... columns ) { this ( new ArrayList (), Arrays.asList ( columns ) ); } /** * Constructs empty model with specified displayed columns. * * @param columns columns to display */ public WebFileTableModel ( List columns ) { this ( new ArrayList (), columns ); } /** * Constructs model with specified displayed columns and files. * * @param files files to display * @param columns columns to display */ public WebFileTableModel ( Collection files, String... columns ) { this ( files, Arrays.asList ( columns ) ); } /** * Constructs model with specified displayed columns and files. * * @param files files to display * @param columns columns to display */ public WebFileTableModel ( Collection files, List columns ) { super (); this.columns = columns; this.files = new ArrayList ( files ); } /** * Sets displayed files. * * @param files files to display */ public void setFiles ( Collection files ) { this.files.clear (); this.files.addAll ( files ); fireTableDataChanged (); } /** * Adds displayed files. * * @param files files to display */ public void addFiles ( Collection files ) { if ( files.size () > 0 ) { int lastIndex = this.files.size (); this.files.addAll ( files ); fireTableRowsInserted ( lastIndex, lastIndex + files.size () - 1 ); } } /** * Returns index of row with the specified file. * * @param file file to find * @return index of row with the specified file */ public int getFileRow ( File file ) { return files.indexOf ( file ); } /** * Returns file for the specified row. * * @param row row to process * @return file for the specified row */ public File getFileAtRow ( int row ) { return files.get ( row ); } /** * Sets displayed column ids list. * * @param columns displayed column ids list */ public void setColumns ( List columns ) { this.columns = columns; fireTableStructureChanged (); } /** * Returns column id at the specified index. * * @param column column index * @return column id at the specified index */ public String getColumnId ( int column ) { return columns.get ( column ); } /** * Returns whether the specified column is displayed or not. * * @param column column to process * @return true if the specified column is displayed, false otherwise */ public boolean isColumnDisplayed ( String column ) { return columns.contains ( column ); } /** * Returns index of the specified column. * * @param column column to process * @return index of the specified column */ public int getColumnIndex ( String column ) { return columns.indexOf ( column ); } /** * {@inheritDoc} */ @Override public String getColumnName ( int column ) { return LanguageManager.get ( columns.get ( column ) ); } /** * {@inheritDoc} */ @Override public int getRowCount () { return files.size (); } /** * {@inheritDoc} */ @Override public int getColumnCount () { return columns.size (); } /** * {@inheritDoc} */ @Override public Object getValueAt ( int rowIndex, int columnIndex ) { return files.get ( rowIndex ); } /** * {@inheritDoc} */ @Override public void setValueAt ( Object aValue, int rowIndex, int columnIndex ) { files.set ( rowIndex, ( File ) aValue ); } /** * {@inheritDoc} */ @Override public Class getColumnClass ( int columnIndex ) { return File.class; } /** * {@inheritDoc} */ @Override public boolean isCellEditable ( int rowIndex, int columnIndex ) { return getColumnId ( columnIndex ).equals ( NAME_COLUMN ) && FileUtils.isNameEditable ( getFileAtRow ( rowIndex ) ); } }