Java Code Examples for javax.swing.RowSorter

The following examples show how to use javax.swing.RowSorter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: constellation   Source File: TableState.java    License: Apache License 2.0 6 votes vote down vote up
public static TableState createMetaState(final JTable table, final boolean selectedOnly) {
    final TableState state = new TableState(table, selectedOnly);

    final GraphTableModel tm = (GraphTableModel) table.getModel();
    for (int i = 0; i < table.getColumnCount(); i++) {
        final TableColumn tc = table.getColumnModel().getColumn(i);
        final int modelIndex = tc.getModelIndex();
        final Attribute attr = tm.getAttribute(modelIndex);
        final String label = attr.getName();

        final ColumnState cs = new ColumnState(label, tm.getSegment(modelIndex), tc.getWidth());
        state.columns.add(cs);
    }

    final RowSorter<? extends TableModel> sorter = table.getRowSorter();
    for (final RowSorter.SortKey sk : sorter.getSortKeys()) {
        // TODO: should really store the column label + segment here.
        state.sortOrder.add(String.format("%d,%s", sk.getColumn(), sk.getSortOrder()));
    }

    return state;
}
 
Example 2
Source Project: jdal   Source File: PageableTable.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Configure sort and order in page from sorter
 */
private void configurePage() {
	Page.Order order = Page.Order.ASC;
	String sortPropertyName = null;
	List<? extends SortKey> keys = sorter.getSortKeys();
	// If sorting, get values to set in page
	if (keys.size() > 0) {
		RowSorter.SortKey key = sorter.getSortKeys().get(0);
		if (tableModel.isPropertyColumn(key.getColumn())) {
			sortPropertyName = tableModel.getSortPropertyName(key.getColumn());
			order = converSortOrder(key);
		}
		
	}
	page.setSortName(sortPropertyName);
	page.setOrder(order);
}
 
Example 3
Source Project: keystore-explorer   Source File: KseFrame.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Re-draw all keystore tables
 * @param applicationSettings
 */
public void redrawKeyStores(ApplicationSettings applicationSettings) {
	if (keyStoreTables != null) {

		keyStoreTableColumns = applicationSettings.getKeyStoreTableColumns();

		for (JTable keyStoreTable : keyStoreTables) {
			KeyStoreHistory history = ((KeyStoreTableModel) keyStoreTable.getModel()).getHistory();
			KeyStoreTableModel ksModel = new KeyStoreTableModel(keyStoreTableColumns);
			try {
				ksModel.load(history);
				keyStoreTable.setModel(ksModel);

				RowSorter<KeyStoreTableModel> sorter = new TableRowSorter<>(ksModel);
				keyStoreTable.setRowSorter(sorter);

				setColumnsToIconSize(keyStoreTable, 0, 1, 2);
				colAdjust(keyStoreTable);
			} catch (GeneralSecurityException | CryptoException e) {
				DError.displayError(frame, e);
			}
		}
	}
}
 
Example 4
Source Project: azure-devops-intellij   Source File: CheckoutForm.java    License: MIT License 6 votes vote down vote up
public void setRepositoryTable(final ServerContextTableModel tableModel, final ListSelectionModel selectionModel) {
    repositoryTable.setModel(tableModel);
    repositoryTable.setSelectionModel(selectionModel);

    // Setup table sorter
    RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);
    repositoryTable.setRowSorter(sorter);

    // Attach an index converter to fix the indexes if the user sorts the list
    tableModel.setSelectionConverter(new TableModelSelectionConverter() {
        @Override
        public int convertRowIndexToModel(int viewRowIndex) {
            if (viewRowIndex >= 0) {
                return repositoryTable.convertRowIndexToModel(viewRowIndex);
            }

            return viewRowIndex;
        }
    });
}
 
Example 5
Source Project: seaglass   Source File: SeaGlassTableHeaderUI.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * DOCUMENT ME!
 *
 * @param  table  DOCUMENT ME!
 * @param  column DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
public static SortOrder getColumnSortOrder(JTable table, int column) {
    SortOrder rv = null;

    if (table == null || table.getRowSorter() == null) {
        return rv;
    }

    java.util.List<? extends RowSorter.SortKey> sortKeys = table.getRowSorter().getSortKeys();

    if (sortKeys.size() > 0 && sortKeys.get(0).getColumn() == table.convertColumnIndexToModel(column)) {
        rv = sortKeys.get(0).getSortOrder();
    }

    return rv;
}
 
Example 6
private void sort()
{
	SortableTableModel m = getModel();
	if (m == null)
	{
		return;
	}
	int columnCount = m.getColumnCount();
	Comparator<?>[] comparators = new Comparator[columnCount];
	for (int i = 0; i < columnCount; i++)
	{
		comparators[i] = Comparators.getComparatorFor(m.getColumnClass(i));
	}
	RowSorter.SortKey[] keys = sortKeys.toArray(new RowSorter.SortKey[0]);

	m.sortModel(new RowComparator(keys, comparators));
}
 
Example 7
Source Project: azure-devops-intellij   Source File: ImportForm.java    License: MIT License 6 votes vote down vote up
public void setTeamProjectTable(final ServerContextTableModel tableModel, final ListSelectionModel selectionModel) {
    teamProjectTable.setModel(tableModel);
    teamProjectTable.setSelectionModel(selectionModel);

    // Setup table sorter
    final RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);
    teamProjectTable.setRowSorter(sorter);

    // Attach an index converter to fix the indexes if the user sorts the list
    tableModel.setSelectionConverter(new TableModelSelectionConverter() {
        @Override
        public int convertRowIndexToModel(int viewRowIndex) {
            if (viewRowIndex >= 0) {
                return teamProjectTable.convertRowIndexToModel(viewRowIndex);
            }

            return viewRowIndex;
        }
    });
}
 
Example 8
Source Project: constellation   Source File: TableViewTopComponent.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Assign a new table model and filter while keeping the sort keys.
 */
private void setNewModel() {
    final RowSorter<? extends TableModel> oldSorter = dataTable.getRowSorter();
    final GraphTableModel gtm = new GraphTableModel(graphNode.getGraph(), currentElementType);
    final TableRowSorter<GraphTableModel> sorter = new TableRowSorter<>(gtm);
    sorter.setSortKeys(oldSorter.getSortKeys());
    if (selectedOnlyButton.isSelected()) {
        sorter.setRowFilter(new SelectionRowFilter(graphNode.getGraph(), currentElementType));
    }
    dataTable.setModel(gtm);
    dataTable.setRowSorter(sorter);
}
 
Example 9
/**
 * Initializes the model
 */
private void init() {
    if (initialized.compareAndSet(false, true)) {
        final RowSorter<? extends TableModel> rowSorter = table.getRowSorter();
        rowSorter.toggleSortOrder(1); // sort by date
        rowSorter.toggleSortOrder(1); // descending
        final TableColumnModel columnModel = table.getColumnModel();
        columnModel.getColumn(1).setCellRenderer(dateRenderer);
        columnModel.getColumn(2).setCellRenderer(sizeRenderer);
    }
}
 
Example 10
Source Project: netbeans   Source File: ETable.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * When ETable has ETableColumnModel set, only <code>null</code> sorter
 * is accepted, which turns off sorting. Otherwise UnsupportedOperationException is thrown.
 * RowSorter can be used when a different TableColumnModel is set.
 * 
 * @param sorter {@inheritDoc}
 */
@Override
public void setRowSorter(RowSorter<? extends TableModel> sorter) {
    if (getColumnModel() instanceof ETableColumnModel) {
        if (sorter == null) {
            sortable = false;
            ((ETableColumnModel) getColumnModel()).clearSortedColumns();
        } else {
            throw new UnsupportedOperationException(
                    "ETable with ETableColumnModel has it's own sorting mechanism. Use ETableColumnModel to define sorting, or set a different TableColumnModel.");
        }
    } else {
        super.setRowSorter(sorter);
    }
}
 
Example 11
Source Project: netbeans   Source File: ETable.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get the RowSorter in case that the ETable does not have ETableColumnModel set.
 * @return {@inheritDoc}
 */
@Override
public RowSorter<? extends TableModel> getRowSorter() {
    if (getColumnModel() instanceof ETableColumnModel) {
        return null;
    } else {
        return super.getRowSorter();
    }
}
 
Example 12
Source Project: jdal   Source File: PageableTable.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Convert the Order from SortKey to Page.Order
 * @param key the SortKey
 * @return  the Page order
 */
private Page.Order converSortOrder(RowSorter.SortKey key) {
	Page.Order order = Order.ASC;
	if (key.getSortOrder() == SortOrder.DESCENDING) {
		order = Order.DESC;
	}
	return order;
}
 
Example 13
Source Project: netbeans   Source File: ProfilerRowSorter.java    License: Apache License 2.0 5 votes vote down vote up
void saveToStorage(Properties properties, ProfilerTable table) {
    RowSorter.SortKey key = getSortKey();
    if (key == null) {
        properties.remove(SORT_COLUMN_KEY);
        properties.remove(SORT_ORDER_KEY);
    } else {
        int column = key.getColumn();
        SortOrder order = key.getSortOrder();
        properties.setProperty(SORT_COLUMN_KEY, Integer.toString(column));
        properties.setProperty(SORT_ORDER_KEY, order.toString());
    }
}
 
Example 14
public Component getTableCellRendererComponent( final JTable table,
                                                final Object value,
                                                final boolean isSelected,
                                                final boolean hasFocus,
                                                final int row,
                                                final int column ) {
  final JLabel component =
    (JLabel) super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
  try {
    if ( column == 0 ) {
      final RepositoryTableModel tableModel = (RepositoryTableModel) table.getModel();
      final RowSorter rowSorter = table.getRowSorter();
      final FileObject e;
      if ( rowSorter != null ) {
        e = tableModel.getElementForRow( rowSorter.convertRowIndexToModel( row ) );
      } else {
        e = tableModel.getElementForRow( row );
      }

      if ( e.getType() == FileType.FOLDER ) {
        component.setIcon( closedIcon );
      } else {
        component.setIcon( leafIcon );
      }
    } else {
      component.setIcon( null );
    }
  } catch ( FileSystemException fse ) {
    // ok, ugly, but not fatal.
  }
  return component;
}
 
Example 15
public void setClassifiers(String[] cs) {
    classifiers = cs;
    TableModel model = new ClassifierTableModel(cs);
    RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    classifierTable.setRowSorter(sorter);
    classifierTable.setModel(model);
}
 
Example 16
Source Project: pcgen   Source File: JTableEx.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
public void sortModel()
{
	RowSorter<?> rowSorter = getRowSorter();
	if (rowSorter != null)
	{
		rowSorter.setSortKeys(getRowSorter().getSortKeys());
	}
}
 
Example 17
private RowSorter.SortKey toggle(RowSorter.SortKey key)
{
	if (key.getSortOrder() == SortOrder.ASCENDING)
	{
		return new RowSorter.SortKey(key.getColumn(), SortOrder.DESCENDING);
	}
	return new RowSorter.SortKey(key.getColumn(), SortOrder.ASCENDING);
}
 
Example 18
public FilteredTreeViewTable()
{
	RowSorter<SortableTableModel> rowSorter = new SortableTableRowSorter()
	{

		@Override
		public SortableTableModel getModel()
		{
			return (SortableTableModel) FilteredTreeViewTable.this.getModel();
		}

	};
	setRowSorter(rowSorter);
	rowSorter.toggleSortOrder(0);
}
 
Example 19
/**
 * Reverses the sort order from ascending to descending (or descending
 * to ascending) if the specified column is already the primary sorted
 * column; otherwise, makes the specified column the primary sorted
 * column, with an ascending sort order. If the specified column is not
 * sortable, this method has no effect.
 *
 * @param column index of the column to make the primary sorted column,
 * in terms of the underlying model
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
@Override
public void toggleSortOrder(int column)
{
	List<RowSorter.SortKey> keys = new ArrayList<>(getSortKeys());
	RowSorter.SortKey sortKey;
	int sortIndex;
	for (sortIndex = keys.size() - 1; sortIndex >= 0; sortIndex--)
	{
		if (keys.get(sortIndex).getColumn() == column)
		{
			break;
		}
	}
	if (sortIndex == -1)
	{
		// Key doesn't exist
		sortKey = new RowSorter.SortKey(column, SortOrder.ASCENDING);
		keys.add(0, sortKey);
	}
	else if (sortIndex == 0)
	{
		// It's the primary sorting key, toggle it
		keys.set(0, toggle(keys.get(0)));
	}
	else
	{
		// It's not the first, but was sorted on, remove old
		// entry, insert as first with ascending.
		keys.remove(sortIndex);
		keys.add(0, new RowSorter.SortKey(column, SortOrder.ASCENDING));
	}
	if (keys.size() > 2)
	{
		keys = keys.subList(0, 2);
	}
	setSortKeys(keys);
}
 
Example 20
Source Project: visualvm   Source File: ProfilerRowSorter.java    License: GNU General Public License v2.0 5 votes vote down vote up
void setSortKey(RowSorter.SortKey key) {
    RowSorter.SortKey secondaryKey = secondarySortColumn == -1 ||
                      secondarySortColumn == key.getColumn() ? null :
                      new RowSorter.SortKey(secondarySortColumn,
                      getDefaultSortOrder(secondarySortColumn));
    setSortKeysImpl(secondaryKey == null ? Arrays.asList(key) :
                      Arrays.asList(key, secondaryKey));
}
 
Example 21
public FilteredTreeViewTable()
{
	RowSorter<SortableTableModel> rowSorter = new SortableTableRowSorter()
	{

		@Override
		public SortableTableModel getModel()
		{
			return (SortableTableModel) FilteredTreeViewTable.this.getModel();
		}

	};
	setRowSorter(rowSorter);
	rowSorter.toggleSortOrder(0);
}
 
Example 22
Source Project: visualvm   Source File: ProfilerRowSorter.java    License: GNU General Public License v2.0 5 votes vote down vote up
void saveToStorage(Properties properties, ProfilerTable table) {
    RowSorter.SortKey key = getSortKey();
    if (key == null) {
        properties.remove(SORT_COLUMN_KEY);
        properties.remove(SORT_ORDER_KEY);
    } else {
        int column = key.getColumn();
        SortOrder order = key.getSortOrder();
        properties.setProperty(SORT_COLUMN_KEY, Integer.toString(column));
        properties.setProperty(SORT_ORDER_KEY, order.toString());
    }
}
 
Example 23
/**
 * Returns the current sort key, or null if the column is unsorted.
 *
 * @param table the table
 * @param column the column index
 * @return the SortKey, or null if the column is unsorted
 */
protected SortKey getSortKey(JTable table, int column) {
  RowSorter<?> rowSorter = table.getRowSorter();
  if (rowSorter == null) {
    return null;
  }

  List<?> sortedColumns = rowSorter.getSortKeys();
  if (sortedColumns.size() > 0) {
    return (SortKey) sortedColumns.get(0);
  }
  return null;
}
 
Example 24
private RowSorter.SortKey toggle(RowSorter.SortKey key)
{
	if (key.getSortOrder() == SortOrder.ASCENDING)
	{
		return new RowSorter.SortKey(key.getColumn(), SortOrder.DESCENDING);
	}
	return new RowSorter.SortKey(key.getColumn(), SortOrder.ASCENDING);
}
 
Example 25
Source Project: pcgen   Source File: JTableEx.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
public void sortModel()
{
	RowSorter<?> rowSorter = getRowSorter();
	if (rowSorter != null)
	{
		rowSorter.setSortKeys(getRowSorter().getSortKeys());
	}
}
 
Example 26
/**
 * Tries to return the sort key for the given column.
 *
 * @param sorter
 * @param column
 * @return the sort key or {@code null}
 */
private SortKey getSortKey(RowSorter<? extends TableModel> sorter, int column) {
	if (sorter == null) {
		return null;
	}

	for (Object sortObj : sorter.getSortKeys()) {
		SortKey key = (SortKey) sortObj;
		if (key.getColumn() == column) {
			return key;
		}
	}
	return null;
}
 
Example 27
@Override
public List<? extends RowSorter.SortKey> getSortKeys()
{
	return sortKeys;
}
 
Example 28
Source Project: dsworkbench   Source File: SupportTableHeaderRenderer.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table,
        Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    Icon sortIcon = null;
    boolean isPaintingForPrint = false;

    if (table != null) {
        JTableHeader header = table.getTableHeader();
        if (header != null) {
            Color fgColor = null;
            Color bgColor = null;
            if (hasFocus) {
                fgColor = UIManager.getColor("TableHeader.focusCellForeground");
                bgColor = UIManager.getColor("TableHeader.focusCellBackground");
            }
            if (fgColor == null) {
                fgColor = header.getForeground();
            }
            if (bgColor == null) {
                bgColor = header.getBackground();
            }
            setForeground(fgColor);
            setFont(header.getFont());
            isPaintingForPrint = header.isPaintingForPrint();
        }

        if (!isPaintingForPrint && table.getRowSorter() != null) {
            if (!horizontalTextPositionSet) {
                // There is a row sorter, and the developer hasn't
                // set a text position, change to leading.
                setHorizontalTextPosition(JLabel.LEADING);
            }
            java.util.List<? extends RowSorter.SortKey> sortKeys = table.getRowSorter().getSortKeys();
            if (sortKeys.size() > 0
                    && sortKeys.get(0).getColumn() == table.convertColumnIndexToModel(column)) {
                switch (sortKeys.get(0).getSortOrder()) {
                    case ASCENDING:
                        sortIcon = UIManager.getIcon("Table.ascendingSortIcon");
                        break;
                    case DESCENDING:
                        sortIcon = UIManager.getIcon("Table.descendingSortIcon");
                        break;
                    case UNSORTED:
                        sortIcon = UIManager.getIcon("Table.naturalSortIcon");
                        break;
                }
            }
        }
    }

    SupportTableModel model = (SupportTableModel) table.getModel();

    ImageIcon icon = model.getColumnIcon((String) value);
    BufferedImage i = ImageUtils.createCompatibleBufferedImage(18, 18, BufferedImage.BITMASK);
    Graphics2D g2d = i.createGraphics();
    // setIcon(sortIcon);
    if (icon != null) {
        icon.paintIcon(this, g2d, 0, 0);
        setText("");
        if (sortIcon != null) {
            g2d.setColor(getBackground());
            g2d.fillRect(18 - sortIcon.getIconWidth() - 2, 18 - sortIcon.getIconHeight() - 2, sortIcon.getIconWidth() + 2, sortIcon.getIconHeight() + 2);
            sortIcon.paintIcon(this, g2d, 18 - sortIcon.getIconWidth() - 1, 18 - sortIcon.getIconHeight() - 1);
        }
        setIcon(new ImageIcon(i));
    } else {
        setIcon(sortIcon);
        setText(value == null ? "" : value.toString());
    }


    Border border = null;
    if (hasFocus) {
        border = UIManager.getBorder("TableHeader.focusCellBorder");
    }
    if (border == null) {
        border = UIManager.getBorder("TableHeader.cellBorder");
    }
    setBorder(border);

    return this;
}
 
Example 29
Source Project: netbeans   Source File: UnitTable.java    License: Apache License 2.0 4 votes vote down vote up
void resortByDefault () {
    RowSorter sorter = getRowSorter();
    sorter.setSortKeys(Arrays.asList(new RowSorter.SortKey(2, SortOrder.ASCENDING)));
}
 
Example 30
@Override
public void setSortKeys(List<? extends RowSorter.SortKey> keys)
{
	sortKeys = keys;
	sort();
}