/*
 Copyright (c) 2006, 2007, The Cytoscape Consortium (www.cytoscape.org)

 The Cytoscape Consortium is:
 - Institute for Systems Biology
 - University of California San Diego
 - Memorial Sloan-Kettering Cancer Center
 - Institut Pasteur
 - Agilent Technologies

 This library 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
 any later version.

 This 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.  The software and
 documentation provided hereunder is on an "as is" basis, and the
 Institute for Systems Biology and the Whitehead Institute
 have no obligations to provide maintenance, support,
 updates, enhancements or modifications.  In no event shall the
 Institute for Systems Biology and the Whitehead Institute
 be liable to any party for direct, indirect, special,
 incidental or consequential damages, including lost profits, arising
 out of the use of this software and its documentation, even if the
 Institute for Systems Biology and the Whitehead Institute
 have been advised of the possibility of such damage.  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 library; if not, write to the Free Software Foundation,
 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
package cytoscape.util.swing;

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.util.HashMap;


/**
 *
 * Automatically resize column based on the objects in the cell.<br>
 *
 * <p>
 * From <i>Swing Hacks</i> by Joshua Marinacci and Chris Adamson.<br>
 * 2005 Oreilly & Associates Inc. ISBN: 0-596-00907-0<br>
 * </p>
 * Customized by Keiichiro Ono
 *
 * @since Cytoscape 2.4
 * @version 1.0
 * @author Joshua Marinacci, Chris Adamson, Keiichiro Ono
 *
 */
public class ColumnResizer {
	private static final int DEFLMAX_WIDTH = 280;

	/**
	 *  DOCUMENT ME!
	 *
	 * @param table DOCUMENT ME!
	 */
	public static void adjustColumnPreferredWidths(JTable table) {
		// strategy - get max width for cells in column and
		// make that the preferred width
		TableColumnModel columnModel = table.getColumnModel();

		for (int col = 0; col < table.getColumnCount(); col++) {
			int maxwidth = 0;

			for (int row = 0; row < table.getRowCount(); row++) {
				TableCellRenderer rend = table.getCellRenderer(row, col);
				Object value = table.getValueAt(row, col);
				Component comp = rend.getTableCellRendererComponent(table, value, false, false,
				                                                    row, col);
				maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
			} // for row

			/*
			 * this version of the width set considers the column header's
			 * preferred width too
			 */
			TableColumn column = columnModel.getColumn(col);
			TableCellRenderer headerRenderer = column.getHeaderRenderer();

			if (headerRenderer == null)
				headerRenderer = table.getTableHeader().getDefaultRenderer();

			Object headerValue = column.getHeaderValue();
			Component headerComp = headerRenderer.getTableCellRendererComponent(table, headerValue,
			                                                                    false, false, 0, col);
			maxwidth = Math.max(maxwidth, headerComp.getPreferredSize().width);

			/*
			 * If the value is too big, adjust to fixed maximum val.
			 */
			if (DEFLMAX_WIDTH < maxwidth) {
				maxwidth = DEFLMAX_WIDTH;
			}

			column.setPreferredWidth(maxwidth + 20);
		} // for col
	}

	/**
	 *  DOCUMENT ME!
	 *
	 * @param table DOCUMENT ME!
	 */
	public static HashMap<String, Integer> getColumnPreferredWidths(JTable table) {
		
		HashMap<String, Integer> retMap = new HashMap<String, Integer>();
		
		// strategy - get max width for cells in column and
		// make that the preferred width
		TableColumnModel columnModel = table.getColumnModel();

		for (int col = 0; col < table.getColumnCount(); col++) {
			int maxwidth = 0;

			for (int row = 0; row < table.getRowCount(); row++) {
				TableCellRenderer rend = table.getCellRenderer(row, col);
				Object value = table.getValueAt(row, col);
				Component comp = rend.getTableCellRendererComponent(table, value, false, false,
				                                                    row, col);
				maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
			} // for row

			/*
			 * this version of the width set considers the column header's
			 * preferred width too
			 */
			TableColumn column = columnModel.getColumn(col);
			TableCellRenderer headerRenderer = column.getHeaderRenderer();

			if (headerRenderer == null)
				headerRenderer = table.getTableHeader().getDefaultRenderer();

			Object headerValue = column.getHeaderValue();
			Component headerComp = headerRenderer.getTableCellRendererComponent(table, headerValue,
			                                                                    false, false, 0, col);
			maxwidth = Math.max(maxwidth, headerComp.getPreferredSize().width);

			/*
			 * If the value is too big, adjust to fixed maximum val.
			 */
			if (DEFLMAX_WIDTH < maxwidth) {
				maxwidth = DEFLMAX_WIDTH;
			}

			retMap.put(headerValue.toString(), new Integer(maxwidth + 20));			
		} // for col
		
		return retMap;
	}


}
