/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program 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 2.1 of the License, or (at your option) any later version.
*
* This program 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 this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2005, by :
* Corporate:
* EADS Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
*
* $Id: ReportingDialog.java,v 1.4 2008/04/08 11:53:41 ogor Exp $
*
* Changes
* -------
* 14 avr. 2006 : Initial public release (CC);
*
*/
package simtools.ui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
/**
* Dialog box displaying a report in a JTable.
* @author zxpletran007
*
*/
public class ReportingDialog extends JPanel{
/**
* Create an monodimensional array that displays a list of errors in a Jtable
* @param reason
* @param errors
*/
public ReportingDialog(String reason, List errors) {
this.setLayout(new BorderLayout());
add(new JLabel(reason), BorderLayout.NORTH);
String[] titles = new String[1];
titles[0] = "Errors";
String[][] errorsList = new String[errors.size()][1];
for(int i=0;i<errors.size();i++){
errorsList[i][0] = (String)errors.get(i);
}
add(createTablePanel(titles, errorsList), BorderLayout.CENTER);
}
/**
* Create an bidimensional array that displays a list of errors in a Jtable
* @param reportText
* @param titles
* @param errors
*/
public ReportingDialog(String reportText, String[] titles, String[][] errors) {
this.setLayout(new BorderLayout());
add(new JLabel(reportText), BorderLayout.NORTH);
add(createTablePanel(titles, errors), BorderLayout.CENTER);
}
protected JPanel createTablePanel(String[] titles, String[][] errors){
JPanel p=new JPanel();
JTable table = new JTable(new ErrorsReportingTableModel(errors, titles));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// Fit columns size to all text values
int tableWidth = setColumnSize(table);
table.setPreferredScrollableViewportSize(new Dimension(tableWidth, 100));
p.add(new JScrollPane(table));
return p;
}
/**
* Compute for each columm the prefered width
* @param table
* @return total width of the table
*/
private int setColumnSize(JTable table){
int ret =0;
FontMetrics fm = table.getFontMetrics(table.getFont());
for (int i = 0 ; i < table.getColumnCount() ; i++){
int max = 0;
for (int j = 0 ; j < table.getRowCount() ; j++){
String value = (String)table.getValueAt(j,i);
if (value!=null){
int taille = fm.stringWidth(value);
if (taille > max)
max = taille;
}
}
String nom = (String)table.getColumnModel().getColumn(i).getIdentifier();
int taille = fm.stringWidth(nom);
if (taille > max)
max = taille;
table.getColumnModel().getColumn(i).setPreferredWidth(max+10);
ret+=max+10;
}
return ret;
}
public class ErrorsReportingTableModel extends AbstractTableModel {
Object donnees[][];
String titres[];
public ErrorsReportingTableModel(Object donnees[][], String titres[]) {
this.donnees = donnees;
this.titres = titres;
}
public int getColumnCount() {
return donnees[0].length;
}
public Object getValueAt(int parm1, int parm2) {
return donnees[parm1][parm2];
}
public int getRowCount() {
return donnees.length;
}
public String getColumnName(int col){
return titres[col];
}
public boolean isCellEditable(int row, int col) {
return false;
}
}
}