/*
 * Created on 29.02.2008
 *
 */
package org.jdesktop.swingx.util;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.TableModel;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.renderer.StringValue;
import org.jdesktop.swingx.table.ColumnFactory;
import org.jdesktop.swingx.table.TableColumnExt;

/**
 * A ColumnFactory is used by JXTable to create and configure all columns.
 * It can be set per-application (before creating an JXTable) or per-table
 * (before setting the model). 

* * Additional features: *

*/ public class CustomColumnFactory extends ColumnFactory { List excludes; List hidden; private StringValue titleStringValue; /** * * @param names the identifiers to exclude. */ public void addExcludeNames(Object... names) { for (Object object : names) { getExcludeNames().add(object); } } /** * * @param names the identifiers to exclude. */ public void addHiddenNames(Object... names) { for (Object object : names) { getHiddenNames().add(object); } } /** * * @param sv the StringValue to use for column header "beautification". */ public void setTitleStringValue(StringValue sv) { this.titleStringValue = sv; } /** * {@inheritDoc}

* Overridden to not create columns for the model columns which are * listed in the excludes. */ @Override public TableColumnExt createAndConfigureTableColumn(TableModel model, int modelIndex) { if (getExcludeNames().contains(model.getColumnName(modelIndex))) { return null; } return super.createAndConfigureTableColumn(model, modelIndex); } /** * {@inheritDoc}

* * Overridden to set the column's identifier, beautify the display title. */ @Override public void configureTableColumn(TableModel model, TableColumnExt columnExt) { super.configureTableColumn(model, columnExt); columnExt.setIdentifier(model.getColumnName(columnExt.getModelIndex())); beautifyColumnTitle(columnExt); if (getHiddenNames().contains(columnExt.getIdentifier())) { columnExt.setVisible(false); } } /** * {@inheritDoc}

* Overridden to check if the wants to limit the count of rows when * measuring. */ @Override protected int getRowCount(JXTable table) { if (table.getClientProperty("ColumnFactory.maxRowCount") instanceof Integer) { return Math.min(table.getRowCount(), (Integer) table.getClientProperty("ColumnFactory.maxRowCount")); } return super.getRowCount(table); } private void beautifyColumnTitle(TableColumnExt columnExt) { if (titleStringValue != null) { columnExt.setTitle(titleStringValue.getString(columnExt.getTitle())); } } private List getExcludeNames() { if (excludes == null) { excludes = new ArrayList(); } return excludes; } private List getHiddenNames() { if (hidden == null) { hidden = new ArrayList(); } return hidden; } }

Ads help maintain this website.