Java Code Examples for javax.swing.RowSorter.SortKey

The following examples show how to use javax.swing.RowSorter.SortKey. 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
private boolean isSameSorter(List<? extends SortKey> sortKeys1,
        List<? extends SortKey> sortKeys2) {
    int size1 = (sortKeys1 == null) ? 0 : sortKeys1.size();
    int size2 = (sortKeys2 == null) ? 0 : sortKeys2.size();
    if ((size1 == 0) && (size2 == 0)) {
        return true;
    }
    if ((sortKeys1 == null) && (sortKeys2 == null))
        return true;
    if ((sortKeys1 == null) || (sortKeys2 == null))
        return false;
    if (size1 != size2)
        return false;
    for (int i = 0; i < size1; i++) {
        SortKey sortKey1 = sortKeys1.get(i);
        SortKey sortKey2 = sortKeys2.get(i);
        if ((sortKey1.getColumn() != sortKey2.getColumn())
                || (sortKey1.getSortOrder() != sortKey2.getSortOrder())) {
            return false;
        }
    }
    return true;
}
 
Example 2
@Override
public void valueChanged(final ListSelectionEvent e) {
    // fix for issue 478 - no animations when sorter has changed
    List<? extends SortKey> sortKeys = (table.getRowSorter() == null) ? null
            : table.getRowSorter().getSortKeys();
    boolean isDifferentSorter = !isSameSorter(sortKeys, oldSortKeys);
    if (e.getValueIsAdjusting() && isDifferentSorter)
        return;
    if (sortKeys == null) {
        oldSortKeys = null;
    } else {
        oldSortKeys = new ArrayList<>();
        for (SortKey sortKey : sortKeys) {
            SortKey copy = new SortKey(sortKey.getColumn(), sortKey.getSortOrder());
            oldSortKeys.add(copy);
        }
    }
    syncSelection(isDifferentSorter);
}
 
Example 3
Source Project: scelight   Source File: JavaSysPropsPage.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public JComponent createPageComp() {
	final BorderPanel p = new BorderPanel();
	
	final XTable table = new XTable();
	
	final Vector< Vector< Object > > data = new Vector<>();
	
	final Properties props = System.getProperties();
	// Use name enumeration which properly returns names from the default properties of a property
	// (while HashTable.entrySet() does not!).
	final Enumeration< ? > nameEnum = props.propertyNames();
	while ( nameEnum.hasMoreElements() ) {
		final Object name = nameEnum.nextElement();
		data.add( Utils.vector( name, props.getProperty( name.toString() ) ) );
	}
	
	table.getXTableModel().setDataVector( data, Utils.vector( "Property name", "Property value" ) );
	table.getRowSorter().setSortKeys( Arrays.asList( new SortKey( 0, SortOrder.ASCENDING ) ) );
	table.packColumnsExceptLast();
	
	p.addCenter( table.createWrapperBox( true, table.createToolBarParams( p ) ) );
	
	return p;
}
 
Example 4
Source Project: dsworkbench   Source File: SortableTableHeaderRenderer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    c.setBackground(Constants.DS_BACK);
    DefaultTableCellRenderer r = ((DefaultTableCellRenderer) c);
    r.setText("<html><b>" + r.getText() + "</b></html>");
    try {
        List<? extends SortKey> sortKeys = table.getRowSorter().getSortKeys();
        SortKey key = sortKeys.get(0);
        if (column == key.getColumn()) {
            r.setIcon(key.getSortOrder() == SortOrder.ASCENDING ? ascIcon : descIcon);
        } else {
            r.setIcon(null);
        }
    } catch (Exception e) {
        r.setIcon(null);
    }
    return r;
}
 
Example 5
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 6
Source Project: scelight   Source File: RepListColumnSetupDialog.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Rebuilds the table.
 */
private void rebuildTable() {
	table.saveSelection( classColIdx );
	
	final XTableModel model = table.getXTableModel();
	
	model.getDataVector().clear();
	model.fireTableDataChanged();
	
	for ( final Class< ? extends IColumn< ? > > colClass : RepListColumnRegistry.COLUMN_LIST ) {
		final boolean isCustom = BaseCustomColumn.class.isAssignableFrom( colClass );
		
		// Only show custom columns if skill level is met
		if ( isCustom && Settings.REP_LIST_CUST_COL_1_NAME.skillLevel.isBelow() )
			continue;
		
		final IColumn< ? > column = RepListColumnRegistry.getColumnInstance( colClass );
		if ( column == null )
			continue;
		
		final int pos = rlcBean.getColumnClassList().indexOf( colClass ) + 1;
		final Vector< Object > row = Utils.< Object > asNewVector( colClass, pos > 0 ? pos : -1, column.getRicon(), column.getDisplayName(), pos > 0,
		        isCustom, column.getDescription() );
		model.addRow( row );
	}
	
	table.getRowSorter().setSortKeys( Arrays.asList( new SortKey( posColIdx, SortOrder.ASCENDING ) ) );
	
	// Pack all columns except the last Description column
	for ( int i = table.getColumnCount() - 1; i >= 0; i-- )
		if ( i != table.convertColumnIndexToView( descColIdx ) )
			table.packColumns( i );
	
	table.restoreSelection( classColIdx );
}
 
Example 7
@Override
public Icon getIcon() {
	int modelCol = header.getTable().convertColumnIndexToModel(curCol);
	TableModel model = header.getTable().getModel();
	if (model instanceof ExtendedJTableSorterModel) {
		ExtendedJTableSorterModel sortModel = (ExtendedJTableSorterModel) model;
		switch (sortModel.getSortingStatus(modelCol)) {
			case ExtendedJTableSorterModel.ASCENDING:
				return UIManager.getIcon("Table.ascendingSortIcon");
			case ExtendedJTableSorterModel.DESCENDING:
				return UIManager.getIcon("Table.descendingSortIcon");
			case ExtendedJTableSorterModel.NOT_SORTED:
			default:
				return null;
		}
	} else {
		SortKey sortKey = getSortKey(header.getTable().getRowSorter(), modelCol);
		SortOrder sortOrder = sortKey != null ? sortKey.getSortOrder() : SortOrder.UNSORTED;
		switch (sortOrder) {
			case ASCENDING:
				return UIManager.getIcon("Table.ascendingSortIcon");
			case DESCENDING:
				return UIManager.getIcon("Table.descendingSortIcon");
			case UNSORTED:
			default:
				return null;
		}
	}
}
 
Example 8
/**
 * 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 9
/**
 * Overloaded to return an icon suitable to the primary sorted column, or null if
 * the column is not the primary sort key.
 *
 * @param table the <code>JTable</code>.
 * @param column the column index.
 * @return the sort icon, or null if the column is unsorted.
 */
protected Icon getIcon(JTable table, int column) {
  SortKey sortKey = getSortKey(table, column);
  if (sortKey != null && table.convertColumnIndexToView(sortKey.getColumn()) == column) {
    switch (sortKey.getSortOrder()) {
      case ASCENDING:
        return UIManager.getIcon("Table.ascendingSortIcon");
      case DESCENDING:
        return UIManager.getIcon("Table.descendingSortIcon");
    }
  }
  return null;
}
 
Example 10
/**
 * 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 11
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 12
Source Project: arcusplatform   Source File: ProductListView.java    License: Apache License 2.0 4 votes vote down vote up
protected Component createListSelector() {
   TableModel<ProductModel> tableModel = createTableModel();
   TableRowSorter<TableModel<ProductModel>> sorter = new TableRowSorter<>(tableModel);
   sorter.setSortKeys(ImmutableList.<SortKey>of(
         new RowSorter.SortKey(0, SortOrder.UNSORTED),
         new RowSorter.SortKey(1, SortOrder.UNSORTED)
   ));
   Table<ProductModel> table = new Table<>(tableModel);
   table.setRowSorter(sorter);
   table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
      @Override
      public void valueChanged(ListSelectionEvent e) {
         if(e.getValueIsAdjusting()) {
            return;
         }
         int selected = table.getSelectedRow();
         if(selected == -1) {
            selectionModel.clearSelection();
         }
         else {
            selected = table.getRowSorter().convertRowIndexToModel(selected);
            selectionModel.setSelection(table.getModel().getValue(selected));
         }
      }
   });
   // TODO wrap this into a selection listener
   table.addMouseListener(new MouseAdapter() {
      @Override
      public void mousePressed(MouseEvent me) {
         if (me.getClickCount() == 2) {
            Point p = me.getPoint();
            int row = table.rowAtPoint(p);
            int offset = table.convertRowIndexToModel(row);
            ProductModel model = table.getModel().getValue(offset);
            controller.getProductSelection().setSelection(model);
         }
     }
   });
   return new JScrollPane(table);
}
 
Example 13
Source Project: scelight   Source File: EnvVarsPage.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public JComponent createPageComp() {
	final BorderPanel p = new BorderPanel();
	
	final XTable table = new XTable();
	
	final Vector< Vector< Object > > data = new Vector<>();
	
	for ( final Entry< String, String > entry : System.getenv().entrySet() )
		data.add( Utils.vector( entry.getKey(), entry.getValue() ) );
	
	table.getXTableModel().setDataVector( data, Utils.vector( "Property name", "Property value" ) );
	table.getRowSorter().setSortKeys( Arrays.asList( new SortKey( 0, SortOrder.ASCENDING ) ) );
	table.packColumnsExceptLast();
	
	p.addCenter( table.createWrapperBox( true, table.createToolBarParams( p ) ) );
	
	return p;
}
 
Example 14
Source Project: scelight   Source File: RepFoldersComp.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Rebuilds the table.
 * 
 * @param rebuildHeader tells if column headers are also to be rebuilt
 */
private void rebuildTable( final boolean rebuildHeader ) {
	table.saveSelection( beanColIdx );
	
	// Model cannot be updated while editing, so stop editing if it is in progress!
	if ( table.isEditing() )
		table.getCellEditor().stopCellEditing();
	
	final XTableModel model = table.getXTableModel();
	
	final boolean showRepsCount = Env.APP_SETTINGS.get( Settings.SHOW_REPLAYS_COUNT );
	
	if ( rebuildHeader ) {
		// Re-set column headers
		final Vector< String > columns = Utils.asNewVector( "Bean", "I", "Folder", "Include sub-folders?", "Monitored?", "Filters", "Position", "Comment",
		        "# of Replays" );
		final List< Class< ? > > columnClasses = Utils.< Class< ? > > asNewList( RepFolderBean.class, TableIcon.class, Path.class, Boolean.class,
		        Boolean.class, Integer.class, Integer.class, String.class, Integer.class );
		beanColIdx = 0;
		pathColIdx = 2;
		recursiveColIdx = 3;
		monitoredColIdx = 4;
		filtersColIdx = 5;
		positionColIdx = 6;
		commentColIdx = 7;
		countColIdx = 8;
		table.setEditableColModelIndices( pathColIdx, recursiveColIdx, monitoredColIdx, commentColIdx );
		table.getXTableRowSorter().setColumnDefaultDescs( true, recursiveColIdx, monitoredColIdx, filtersColIdx, countColIdx );
		
		model.setColumnIdentifiers( columns );
		model.setColumnClasses( columnClasses );
		if ( !showRepsCount )
			table.getColumnModel().removeColumn( table.getColumnModel().getColumn( countColIdx ) );
		table.getColumnModel().removeColumn( table.getColumnModel().getColumn( beanColIdx ) );
		table.getRowSorter().setSortKeys( Arrays.asList( new SortKey( positionColIdx, SortOrder.ASCENDING ) ) );
	}
	
	// Build table data
	model.getDataVector().clear();
	model.fireTableDataChanged();
	final RepFoldersBean rfsBean = Env.APP_SETTINGS.get( Settings.REPLAY_FOLDERS_BEAN );
	int pos = 1;
	for ( final RepFolderBean rfBean : rfsBean.getReplayFolderBeanList() ) {
		final Vector< Object > row = Utils.< Object > asNewVector( rfBean, rfBean.getOrigin().tableIcon, rfBean.getPath(), rfBean.getRecursive(),
		        rfBean.getMonitored(), rfBean.getActiveFilterCount(), pos++, rfBean.getComment(), rfBean.getReplaysCount() );
		model.addRow( row );
	}
	
	// Pack all columns except the path and comment columns (leave remaining space for them)
	for ( int i = table.getColumnCount() - 1; i >= 0; i-- )
		if ( i != table.convertColumnIndexToView( pathColIdx ) && i != table.convertColumnIndexToView( commentColIdx ) )
			table.packColumns( i );
	
	table.restoreSelection( beanColIdx );
}
 
Example 15
Source Project: megamek   Source File: UnitSelectorDialog.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void run() {
    // Loading mechs can take a while, so it will have its own thread.
    // This prevents the UI from freezing, and allows the
    // "Please wait..." dialog to behave properly on various Java VMs.
    MechSummaryCache mscInstance = MechSummaryCache.getInstance();
    mechs = mscInstance.getAllMechs();

    // break out if there are no units to filter
    if (mechs == null) {
        System.err.println("No units to filter!");
    } else {
        unitModel.setData(mechs);
    }
    filterUnits();

    //initialize with the units sorted alphabetically by chassis
    ArrayList<SortKey> sortlist = new ArrayList<>();
    sortlist.add(new SortKey(MechTableModel.COL_CHASSIS,SortOrder.ASCENDING));
    //sortlist.add(new RowSorter.SortKey(MechTableModel.COL_MODEL,SortOrder.ASCENDING));
    tableUnits.getRowSorter().setSortKeys(sortlist);
    ((DefaultRowSorter<?, ?>)tableUnits.getRowSorter()).sort();

    tableUnits.invalidate(); // force re-layout of window
    pack();
    //setLocation(computeDesiredLocation());

    unitLoadingDialog.setVisible(false);

    // In some cases, it's possible to get here without an initialized
    // instance (loading a saved game without a cache).  In these cases,
    // we dn't care about the failed loads.
    if (mscInstance.isInitialized() && !useAlternate)
    {
        final Map<String, String> hFailedFiles =
            MechSummaryCache.getInstance().getFailedFiles();
        if ((hFailedFiles != null) && (hFailedFiles.size() > 0)) {
            // self-showing dialog
            new UnitFailureDialog(frame, hFailedFiles);
        }
    }
    GUIPreferences guip = GUIPreferences.getInstance();
    int width = guip.getMechSelectorSizeWidth();
    int height = guip.getMechSelectorSizeHeight();
    setSize(width,height);
}
 
Example 16
Source Project: dsworkbench   Source File: RankTableTab.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Creates new form AttackTablePanel
 */
public RankTableTab(RANK_TYPE pType, final ActionListener pActionListener) {
    eType = pType;
    initComponents();
    switch (eType) {
        case TRIBE: {
            buildTribeModel();
            break;
        }
        case ALLY: {
            buildAllyModel();
            break;
        }
        case TRIBE_BASH: {
            buildTribeBashModel();
            break;
        }
        case ALLY_BASH: {
            buildAllyBashModel();
            break;
        }
    }
    jScrollPane1.setViewportView(jxRankTable);
    jxRankTable.setRowHeight(24);
    jxRankTable.setHighlighters(HighlighterFactory.createAlternateStriping(Constants.DS_ROW_A, Constants.DS_ROW_B));
    jxRankTable.setColumnControlVisible(true);
    jxRankTable.setModel(theModel);
    List<SortKey> keys = new LinkedList<>();
    keys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
    jxRankTable.getRowSorter().setSortKeys(keys);
    jxRankTable.setDefaultRenderer(Integer.class, new NumberFormatCellRenderer());
    jxRankTable.setDefaultRenderer(Double.class, new NumberFormatCellRenderer());

    jxRankTable.getActionMap().put("find", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            pActionListener.actionPerformed(new ActionEvent(jxRankTable, 0, "Find"));
        }
    });

    jxRankTable.getSelectionModel().addListSelectionListener(RankTableTab.this);
}