Java Code Examples for javax.swing.RowSorter#SortKey

The following examples show how to use javax.swing.RowSorter#SortKey . 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 File: TableState.java    From constellation with 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 File: MultisortTableHeaderCellRenderer.java    From mars-sim with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Overridden to return an icon suitable to a sorted column, or null if the column is unsorted.
 * The icon for the primary sorted column is fully opaque, and the opacity is reduced by a
 * factor of <code>alpha</code> for each subsequent sort index.
 *
 * @param table the <code>JTable</code>.
 * @param column the column index.
 * @return the sort icon with appropriate opacity, or null if the column is unsorted.
 */
public Icon getIcon(JTable table, int column) {
  float computedAlpha = 1.0F;
  for (RowSorter.SortKey sortKey : table.getRowSorter().getSortKeys()) {
    if (table.convertColumnIndexToView(sortKey.getColumn()) == column) {
      switch (sortKey.getSortOrder()) {
        case ASCENDING:
          return new AlphaIcon(UIManager.getIcon("Table.ascendingSortIcon"), computedAlpha);
        case DESCENDING:
          return new AlphaIcon(UIManager.getIcon("Table.descendingSortIcon"), computedAlpha);
      }
    }
    computedAlpha *= alpha;
  }
  return null;
}
 
Example 3
Source File: LuckTableCellHeaderRenderer.java    From littleluck with Apache License 2.0 6 votes vote down vote up
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 4
Source File: SyncPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Adjust possible sort keys to new column order.
 */
private List<? extends RowSorter.SortKey> adjustSortKeys(List<? extends RowSorter.SortKey> sortKeys) {
    List<RowSorter.SortKey> currentKeys = new ArrayList<>(sortKeys);
    List<RowSorter.SortKey> newKeys = new ArrayList<>(currentKeys.size());
    for (RowSorter.SortKey sortKey : currentKeys) {
        int column = sortKey.getColumn();
        RowSorter.SortKey newSortKey;
        if (column == 1) {
            newSortKey = new RowSorter.SortKey(3, sortKey.getSortOrder());
        } else if (column == 3) {
            newSortKey = new RowSorter.SortKey(1, sortKey.getSortOrder());
        } else {
            newSortKey = sortKey;
        }
        newKeys.add(newSortKey);
    }
    return newKeys;
}
 
Example 5
Source File: ProfilerRowSorter.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public void setSortKeys(List newKeys) {
    if (newKeys == null || newKeys.isEmpty()) {
        setSortKeysImpl(newKeys);
        return;
    }
    
    RowSorter.SortKey oldKey = getSortKey();
    RowSorter.SortKey newKey = (RowSorter.SortKey)newKeys.get(0);
    
    if (oldKey == null || oldKey.getColumn() != newKey.getColumn()) {
        // Use defined initial SortOrder for a newly sorted column
        setSortColumn(newKey.getColumn());
    } else {
        setSortKeysImpl(newKeys);
    }
}
 
Example 6
Source File: ProfilerRowSorter.java    From netbeans with Apache License 2.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 7
Source File: PageableTable.java    From jdal with 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 8
Source File: SortableTableRowSorter.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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 9
Source File: SortableTableRowSorter.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 10
Source File: SortableTableRowSorter.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 11
Source File: SupportTableHeaderRenderer.java    From dsworkbench with 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 12
Source File: ProfilerTable.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public void setSorting(int column, SortOrder sortOrder) {
    if (isSortable()) {
        RowSorter.SortKey sortKey = new RowSorter.SortKey(column, sortOrder);
        _getRowSorter().setSortKeysImpl(Collections.singletonList(sortKey));
    }
}
 
Example 13
Source File: SeaGlassTableHeaderUI.java    From seaglass with Apache License 2.0 4 votes vote down vote up
/**
 * @see com.seaglasslookandfeel.ui.SeaGlassTableHeaderUI$DefaultTableCellHeaderRenderer#getTableCellRendererComponent(javax.swing.JTable,
 *      java.lang.Object, boolean, boolean, int, int)
 */
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
        int column) {
    boolean hasRollover = false; // (column == getRolloverColumn());

    if (isSelected || hasRollover || hasFocus) {
        SeaGlassLookAndFeel.setSelectedUI((SeaGlassLabelUI) SeaGlassLookAndFeel.getUIOfType(getUI(), SeaGlassLabelUI.class),
                                          isSelected, hasFocus, table.isEnabled(), hasRollover);
    } else {
        SeaGlassLookAndFeel.resetSelectedUI();
    }

    // Stuff a variable into the client property of this renderer
    // indicating the sort order, so that different rendering can be
    // done for the header based on sorted state.
    RowSorter                                   rs       = table == null ? null : table.getRowSorter();
    java.util.List<? extends RowSorter.SortKey> sortKeys = rs == null ? null : rs.getSortKeys();

    if (sortKeys != null && sortKeys.size() > 0 && sortKeys.get(0).getColumn() == table.convertColumnIndexToModel(column)) {
        switch (sortKeys.get(0).getSortOrder()) {

        case ASCENDING:
            putClientProperty("Table.sortOrder", "ASCENDING");
            break;

        case DESCENDING:
            putClientProperty("Table.sortOrder", "DESCENDING");
            break;

        case UNSORTED:
            putClientProperty("Table.sortOrder", "UNSORTED");
            break;

        default:
            throw new AssertionError("Cannot happen");
        }
    } else {
        putClientProperty("Table.sortOrder", "UNSORTED");
    }

    super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

    return this;
}
 
Example 14
Source File: SortableTableRowSorter.java    From pcgen with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public int compare(Row o1, Row o2)
{
	for (RowSorter.SortKey key : keys)
	{
		if (key.getSortOrder() == SortOrder.UNSORTED)
		{
			continue;
		}
		int column = key.getColumn();
		Comparator comparator = comparators[column];
		Object obj1 = o1.getValueAt(column);
		Object obj2 = o2.getValueAt(column);
		int ret;
		if (obj1 == null)
		{
			if (obj2 == null)
			{
				ret = 0;
			}
			else
			{
				ret = -1;
			}
		}
		else if (obj2 == null)
		{
			ret = 1;
		}
		else
		{
			ret = comparator.compare(obj1, obj2);
		}
		if (key.getSortOrder() == SortOrder.DESCENDING)
		{
			ret *= -1;
		}
		if (ret != 0)
		{
			return ret;
		}
	}

	return 0;
}
 
Example 15
Source File: HeapViewerTreeTable.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
HeapViewerTreeTable(ProfilerTreeTableModel model, List<? extends RowSorter.SortKey> sortKeys) {
        super(model, true, true, new int[] { 0 });
        
        setRootVisible(false);
        setShowsRootHandles(true);
        
        setShadeUnfocusedSelection(true);
        
        setForgetPreviouslyExpanded(true);
        
        setAllowsThreeStateColumns(true);
        getRowSorter().setSortKeys(sortKeys);
        
        getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            private HeapViewerNode currentSelected;
            private boolean currentAdjusting;
            private boolean adjustingNull;
            public void valueChanged(ListSelectionEvent e) {
//                System.err.println(">>> selected " + getSelectedNode() + " adjusting " + e.getValueIsAdjusting());
                
                // Ignore changes during sorting, will be handled separately in willBeSorted()
                if (sorting) return;
                
                HeapViewerNode node = getSelectedNode();
                boolean adjusting = e.getValueIsAdjusting();
                
                // workaround for noise created by restoring selection on internal model updates
                // leading nulls which are adjusting mean that following non-adjusting null should be skipped
                if (node == null) {
                    if (adjusting) {
                        adjustingNull = true;
                        return;
                    } else if (adjustingNull) {
                        adjustingNull = false;
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                // No node selected after leading adjusting nulls, selection not restored
                                if (getSelectedNode() == null) nodeSelected(null, false);
                            }
                        });
                        return;
                    }
                } else {
                    adjustingNull = false;
                }
                
                // workaround for noise created by restoring selection on internal model updates
                // ignore the same selection which is a result of clearing noise nulls
                if (Objects.equals(currentSelected, node) && currentAdjusting == adjusting) return;
                
                currentSelected = node;
                currentAdjusting = adjusting;
                
                nodeSelected(node, adjusting);
            }
        });
        
        initializing = false;
    }
 
Example 16
Source File: ProfilerTreeTable.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public List<? extends RowSorter.SortKey> getSortKeys() {
    return sortKeys;
}
 
Example 17
Source File: SortableTableRowSorter.java    From pcgen with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void setSortKeys(List<? extends RowSorter.SortKey> keys)
{
	sortKeys = keys;
	sort();
}
 
Example 18
Source File: LoggerViewPanel.java    From MtgDesktopCompanion with GNU General Public License v3.0 4 votes vote down vote up
public LoggerViewPanel() {
	model = new LogTableModel();
	cboChooseLevel = UITools.createCombobox(new Level[] {null, Level.INFO, Level.ERROR, Level.DEBUG, Level.TRACE });
	JPanel panel = new JPanel();
	table = new JXTable(model);
	btnRefresh = new JButton(MTGConstants.ICON_REFRESH);
	t = new Timer(1000, e -> model.fireTableDataChanged());
	chckbxAutorefresh = new JCheckBox("Auto-refresh");
	
	
	setLayout(new BorderLayout(0, 0));


	add(new JScrollPane(table), BorderLayout.CENTER);
	add(panel, BorderLayout.NORTH);
	panel.add(chckbxAutorefresh);
	panel.add(btnRefresh);
	panel.add(cboChooseLevel);
	
	datesorter = new TableRowSorter<>(table.getModel());
	List<RowSorter.SortKey> sortKeys = new ArrayList<>();
	int columnIndexToSort = 1;
	sortKeys.add(new RowSorter.SortKey(columnIndexToSort, SortOrder.DESCENDING));
	datesorter.setSortKeys(sortKeys);
	table.setRowSorter(datesorter);
	
	
	
	btnRefresh.addActionListener(ae -> model.fireTableDataChanged());
	
	chckbxAutorefresh.addItemListener(ie -> {

		if (chckbxAutorefresh.isSelected()) {
			t.start();
			btnRefresh.setEnabled(false);
		} else {
			t.stop();
			btnRefresh.setEnabled(true);
		}
	});
	
	cboChooseLevel.addActionListener(ae->{
		
		if(cboChooseLevel.getSelectedItem()!=null)
		{
			TableRowSorter<LogTableModel> sorter = new TableRowSorter<>(model);
			sorter.setRowFilter(RowFilter.regexFilter(cboChooseLevel.getSelectedItem().toString()));
			table.setRowSorter(sorter);
		}
		else
		{
			table.setRowSorter(datesorter);
			
		}
		
		
		
	});
	
	table.packAll();
}
 
Example 19
Source File: ProfilerRowSorter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
int getSortColumn() {
    RowSorter.SortKey key = getSortKey();
    return key == null ? -1 : key.getColumn();
}
 
Example 20
Source File: ProfilerTreeTable.java    From visualvm with GNU General Public License v2.0 votes vote down vote up
protected void willBeSorted(List<? extends RowSorter.SortKey> sortKeys) {}