ColumnFactory.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:5k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: ColumnFactory.java,v 1.4 2005/10/10 18:03:03 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.table;
  22. import java.awt.Component;
  23. import java.awt.Dimension;
  24. import javax.swing.table.JTableHeader;
  25. import javax.swing.table.TableCellRenderer;
  26. import javax.swing.table.TableModel;
  27. import org.jdesktop.swingx.JXTable;
  28. /**
  29.  * Creates and configures TableColumns.
  30.  * 
  31.  * @author Jeanette Winzenburg
  32.  */
  33. public class ColumnFactory {
  34.     
  35.     private static ColumnFactory columnFactory;
  36.     
  37.     public static synchronized ColumnFactory getInstance() {
  38.         if (columnFactory == null) {
  39.             columnFactory = new ColumnFactory();
  40.         }
  41.         return columnFactory;
  42.     }
  43.     public static synchronized void  setInstance(ColumnFactory factory) {
  44.         columnFactory = factory;
  45.     }
  46.     
  47.     public TableColumnExt createTableColumn(int modelIndex) {
  48.         return new TableColumnExt(modelIndex);
  49.     }
  50.     
  51.     /**
  52.      * Configure column properties from TableModel.
  53.      * 
  54.      * @param model
  55.      * @param column
  56.      * @throws NPE if model or column == null
  57.      * @throws IllegalStateException if column does not have valid modelIndex
  58.      *   (in coordinate space of the tablemodel)
  59.      */
  60.     public void configureTableColumn(TableModel model, TableColumnExt column) {
  61.         if ((column.getModelIndex() < 0) 
  62.                 || (column.getModelIndex() >= model.getColumnCount())) 
  63.             throw new IllegalStateException("column must have valid modelIndex");
  64.         column.setHeaderValue(model.getColumnName(column.getModelIndex()));
  65.     }
  66.     
  67.     public TableColumnExt createAndConfigureTableColumn(TableModel model, int modelIndex) {
  68.         TableColumnExt column = createTableColumn(modelIndex);
  69.         configureTableColumn(model, column);
  70.         return column;
  71.     }
  72.     public void configureColumnWidths(JXTable table, TableColumnExt columnx) {
  73.         Dimension cellSpacing = table.getIntercellSpacing();
  74.         Object prototypeValue = columnx.getPrototypeValue();
  75.         if (prototypeValue != null) {
  76.             // calculate how much room the prototypeValue requires
  77.             TableCellRenderer renderer = table.getCellRenderer(0, table
  78.                     .convertColumnIndexToView(columnx.getModelIndex()));
  79.             Component comp = renderer.getTableCellRendererComponent(table,
  80.                     prototypeValue, false, false, 0, 0);
  81.             int prefWidth = comp.getPreferredSize().width + cellSpacing.width;
  82.             // now calculate how much room the column header wants
  83.             renderer = columnx.getHeaderRenderer();
  84.             if (renderer == null) {
  85.                 JTableHeader header = table.getTableHeader();
  86.                 if (header != null) {
  87.                     renderer = header.getDefaultRenderer();
  88.                 }
  89.             }
  90.             if (renderer != null) {
  91.                 comp = renderer.getTableCellRendererComponent(table, columnx
  92.                         .getHeaderValue(), false, false, 0, table
  93.                         .convertColumnIndexToView(columnx.getModelIndex()));
  94.                 prefWidth = Math.max(comp.getPreferredSize().width, prefWidth);
  95.             }
  96.             prefWidth += table.getColumnModel().getColumnMargin();
  97.             columnx.setPreferredWidth(prefWidth);
  98.         }
  99.     }
  100.     public void packColumn(JXTable table, TableColumnExt col, int margin, int max) {
  101.         /* Get width of column header */
  102.         TableCellRenderer renderer = col.getHeaderRenderer();
  103.         if (renderer == null) 
  104.             renderer = table.getTableHeader().getDefaultRenderer();
  105.         
  106.         int width = 0;
  107.         
  108.         Component comp = renderer.getTableCellRendererComponent(table, col
  109.                 .getHeaderValue(), false, false, 0, 0);
  110.         width = comp.getPreferredSize().width;
  111.         
  112.         int column = table.convertColumnIndexToView(col.getModelIndex());
  113.         if(table.getRowCount() > 0)
  114.             renderer = table.getCellRenderer(0, column);
  115.         for (int r = 0; r < table.getRowCount(); r++) {
  116.             comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r,
  117.                     column), false, false, r, column);
  118.             width = Math.max(width, comp.getPreferredSize().width);
  119.         }
  120.         width += 2 * margin;
  121.         /* Check if the width exceeds the max */
  122.         if( max != -1 && width > max )
  123.             width = max;
  124.         
  125.         col.setPreferredWidth(width);
  126.         
  127.     }
  128. }