Example usage for javax.swing.table DefaultTableColumnModel moveColumn

List of usage examples for javax.swing.table DefaultTableColumnModel moveColumn

Introduction

In this page you can find the example usage for javax.swing.table DefaultTableColumnModel moveColumn.

Prototype

public void moveColumn(int columnIndex, int newIndex) 

Source Link

Document

Moves the column and heading at columnIndex to newIndex.

Usage

From source file:forge.itemmanager.views.ItemListView.java

@Override
public void setup(final ItemManagerConfig config, final Map<ColumnDef, ItemTableColumn> colOverrides) {
    final Iterable<T> selectedItemsBefore = getSelectedItems();
    final DefaultTableColumnModel colmodel = new DefaultTableColumnModel();

    //ensure columns ordered properly
    final List<ItemTableColumn> columns = new LinkedList<ItemTableColumn>();
    for (final ItemColumnConfig colConfig : config.getCols().values()) {
        if (colOverrides == null || !colOverrides.containsKey(colConfig.getDef())) {
            columns.add(new ItemTableColumn(new ItemColumn(colConfig)));
        } else {//from w  ww.  j a  va 2 s .c  o m
            columns.add(colOverrides.get(colConfig.getDef()));
        }
    }
    Collections.sort(columns, new Comparator<ItemTableColumn>() {
        @Override
        public int compare(final ItemTableColumn arg0, final ItemTableColumn arg1) {
            return Integer.compare(arg0.getIndex(), arg1.getIndex());
        }
    });

    //hide table header if only showing single string column
    final boolean hideHeader = (config.getCols().size() == 1 && config.getCols().containsKey(ColumnDef.STRING));

    getPnlOptions().removeAll();

    if (config.getShowUniqueCardsOption()) {
        final FCheckBox chkBox = new FCheckBox("Unique Cards Only", this.itemManager.getWantUnique());
        chkBox.setFont(ROW_FONT);
        chkBox.setToolTipText("Toggle whether to show unique cards only");
        chkBox.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(final ChangeEvent arg0) {
                final boolean wantUnique = chkBox.isSelected();
                if (itemManager.getWantUnique() == wantUnique) {
                    return;
                }
                itemManager.setWantUnique(wantUnique);
                itemManager.refresh();

                if (itemManager.getConfig() != null) {
                    itemManager.getConfig().setUniqueCardsOnly(wantUnique);
                }
            }
        });
        getPnlOptions().add(chkBox);
    }

    int modelIndex = 0;
    for (final ItemTableColumn col : columns) {
        col.setModelIndex(modelIndex++);
        if (col.isVisible()) {
            colmodel.addColumn(col);
        }

        if (!hideHeader) {
            final FCheckBox chkBox = new FCheckBox(
                    StringUtils.isEmpty(col.getShortName()) ? col.getLongName() : col.getShortName(),
                    col.isVisible());
            chkBox.setFont(ROW_FONT);
            chkBox.setToolTipText(col.getLongName());
            chkBox.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(final ChangeEvent arg0) {
                    final boolean visible = chkBox.isSelected();
                    if (col.isVisible() == visible) {
                        return;
                    }
                    col.setVisible(visible);

                    if (col.isVisible()) {
                        colmodel.addColumn(col);

                        //move column into proper position
                        final int oldIndex = colmodel.getColumnCount() - 1;
                        int newIndex = col.getIndex();
                        for (int i = 0; i < col.getIndex(); i++) {
                            if (!columns.get(i).isVisible()) {
                                newIndex--;
                            }
                        }
                        if (newIndex < oldIndex) {
                            colmodel.moveColumn(oldIndex, newIndex);
                        }
                    } else {
                        colmodel.removeColumn(col);
                    }
                    ItemManagerConfig.save();
                }
            });
            getPnlOptions().add(chkBox);
        }
    }

    if (hideHeader) {
        this.table.getTableHeader().setPreferredSize(new Dimension(0, 0));
    } else {
        this.table.getTableHeader().setPreferredSize(new Dimension(0, ROW_HEIGHT));
    }

    this.tableModel.addListeners();
    this.table.setModel(this.tableModel);
    this.table.setColumnModel(colmodel);

    this.tableModel.setup();
    this.refresh(selectedItemsBefore, 0, 0);
}