/**
* Copyright 1999-2009 The Pegadi Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pegadi.articlelist;
import org.pegadi.model.Article;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.table.AbstractTableModel;
import java.util.ResourceBundle;
/**
* A table-model for the ArticleList
*/
public class ArticleTableModel extends AbstractTableModel {
/**
* this array holds all the articles
*/
protected Article[] articleList;
/**
* An array containg the current names of the columns
*/
protected String[] currentColumnNames;
/**
* the array of ints, showing the current order of the columns
*/
protected int[] currentColumnOrder;
/**
* this array contains all the names of the columns for a
*/
protected static String[] allColumnNames = {
"column_name",
"column_type",
"column_journalist",
"column_photographer",
"column_description",
"column_characters",
"column_pages",
"column_status",
"column_issue"
};
/**
* Translatable strings.
*/
protected ResourceBundle str;
/**
* an array of int's for all columns
*/
protected int[] allColumnIndexes = {0, 1, 2, 3, 4, 5, 6, 7, 8};
private final Logger log = LoggerFactory.getLogger(getClass());
/**
* Constructor.
* Creates a tablemodel with columns specified by the array <code>whichColumns</code>
*
* @param whichColumns Array of int's determining which columns to show
* @param articleList Array of articles for the table.
*/
public ArticleTableModel(Article[] articleList, int[] whichColumns) {
init(articleList, whichColumns);
}
/**
* Constructor.
* Called without the array specifying which columns to show
* and therefore displays all available columns
*/
public ArticleTableModel(Article[] articleList) {
str = ResourceBundle.getBundle("org.pegadi.articlelist.ArticleListStrings");
init(articleList, allColumnIndexes);
}
/**
* This method build the array <code>currentColumnNames</code> and
* <code>currentColumnOrder</code>, based on the input array of int's
*
* @param whichColumns Array of int's determining which columns to show
* @param articleList Array of articles for the table.
*/
private void init(Article[] articleList, int[] whichColumns) {
currentColumnOrder = whichColumns;
currentColumnNames = new String[whichColumns.length];
for (int i = 0; i < whichColumns.length; i++) {
currentColumnNames[i] = str.getString(allColumnNames[whichColumns[i]]);
}
if (articleList == null) {
} else {
this.articleList = articleList;
}
}
/**
* Updates the table with new articles. Old articles are deleted
*
* @param articles The new array of articles for the table
*/
public void update(Article[] articles) {
articleList = articles;
fireTableDataChanged();
}
public void updateColumns(int[] whichNewColumns) {
init(this.articleList, whichNewColumns);
fireTableStructureChanged();
}
/**
* Returns the number of rows in the list.
*/
public int getRowCount() {
if (articleList == null)
return 0;
else
return articleList.length;
}
/**
* Returns the number of columns for this list
*/
public int getColumnCount() {
return currentColumnNames.length;
}
/**
* Returns the name of the given column
*/
public String getColumnName(int col) {
return currentColumnNames[col];
}
/**
* Returns the order of the given column
*/
public int getColumnOrder(int col) {
return currentColumnOrder[col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public Article getArticleAtRow(int row) {
return articleList[row];
}
/**
* Returns the value at a given cell in the table
* Heeds the current order of columns.
*/
public Object getValueAt(int row, int column) {
if (articleList[row] == null) {
return "<NULL value>";
}
if (column >= currentColumnOrder.length) {
// FIXME: Remove or translate
log.error("Warning: Column index too large (" + column + " >= " + currentColumnOrder.length);
return "<unknown column>";
} else {
switch (currentColumnOrder[column]) {
case 0: // Name
return articleList[row].getName();
case 1: // ArticleType
if (articleList[row].getArticleType() == null) {
return "";
} else {
return articleList[row].getArticleType().getName();
}
case 2: // Journalist
if (articleList[row].getJournalist() == null) {
return "";
} else {
return articleList[row].getJournalist().getName();
}
case 3: // Photographer
if (articleList[row].getPhotographer() == null) {
return "";
} else {
return articleList[row].getPhotographer().getName();
}
case 4: // Description
return articleList[row].getDescription();
case 5:// antal teikn
int wanted = articleList[row].getWantedNumberOfCharacters();
if (wanted == 0) {
return String.valueOf(articleList[row].getCurrentNumberOfCharacters());
} else {
return String.valueOf(articleList[row].getCurrentNumberOfCharacters())
+ " / " + wanted;
}
case 6: //number of pages
return articleList[row].getWantedNumberOfPages();
case 7: // Status
if (articleList[row].getArticleStatus() == null) {
return "";
} else {
return articleList[row].getArticleStatus();
}
case 8: // Publication
if (articleList[row].getPublication() == null) {
return "";
} else {
return articleList[row].getPublication().getName();
}
default:
return articleList[row].getName();
}
} // else
}
}