use of org.knime.core.node.tableview.TableSortOrder.TableSortKey in project knime-core by knime.
the class ColumnHeaderRenderer method getSortIcon.
/**
* @param table
* @param column
* @param sortIcon
* @return
*/
private Icon getSortIcon(final JTable table, final int column) {
if (table == null) {
return null;
}
TableModel model = table.getModel();
int colIndexInModel = -1;
TableSortOrder sortOrder = null;
if (model instanceof TableContentModel) {
TableContentModel cntModel = (TableContentModel) model;
sortOrder = cntModel.getTableSortOrder();
colIndexInModel = table.convertColumnIndexToModel(column);
} else if (model instanceof TableRowHeaderModel) {
TableRowHeaderModel rowHeaderModel = (TableRowHeaderModel) model;
TableContentInterface cntIface = rowHeaderModel.getTableContent();
if (cntIface instanceof TableContentModel) {
TableContentModel cntModel = (TableContentModel) cntIface;
sortOrder = cntModel.getTableSortOrder();
colIndexInModel = -1;
}
}
TableSortKey sortKey;
if (sortOrder == null) {
sortKey = TableSortKey.NONE;
} else {
sortKey = sortOrder.getSortKeyForColumn(colIndexInModel);
}
switch(sortKey) {
case PRIMARY_ASCENDING:
return ICON_PRIM_ASC;
case SECONDARY_ASCENDING:
return ICON_SEC_ASC;
case PRIMARY_DESCENDING:
return ICON_PRIM_DES;
case SECONDARY_DESCENDING:
return ICON_SEC_DES;
default:
return null;
}
}
use of org.knime.core.node.tableview.TableSortOrder.TableSortKey in project knime-core by knime.
the class TableContentView method createSortPopupMenu.
/**
* Create a custom popup menu when the mouse was clicked in a column header.
* This popup menu will contain possible sort options, ascending, descending, and clear sorting.
* @param contentModel The content model on which to call the sort routines.
* @param parentComp Component on which the sort-progress bar will be made modal
* @param columnInModel column (in model), which is going to be sorted.
* @param sortKey the new sort key
* @return a popup menu displaying these properties
* @see #onMouseClickInHeader(MouseEvent)
* @since 2.8
*/
static JPopupMenu createSortPopupMenu(final TableContentModel contentModel, final JComponent parentComp, final int columnInModel, final TableSortKey sortKey) {
/**
* Action listener for all sort actions.
*/
final class SortKeyActionListener implements ActionListener {
private final TableSortKey m_sortKey;
/**
* New sort key action listener.
*/
private SortKeyActionListener(final TableSortKey sk) {
m_sortKey = sk;
}
@Override
public void actionPerformed(final ActionEvent action) {
contentModel.requestSort(columnInModel, parentComp, m_sortKey);
}
}
final JPopupMenu popup = new JPopupMenu("Column Context Menu");
final JRadioButtonMenuItem menuItemDesc1 = new JRadioButtonMenuItem("Sort Descending");
menuItemDesc1.addActionListener(new SortKeyActionListener(TableSortKey.PRIMARY_DESCENDING));
final JRadioButtonMenuItem menuItemAsc1 = new JRadioButtonMenuItem("Sort Ascending");
menuItemAsc1.addActionListener(new SortKeyActionListener(TableSortKey.PRIMARY_ASCENDING));
final JRadioButtonMenuItem menuItemDesc2 = new JRadioButtonMenuItem("Sort Descending");
menuItemDesc2.addActionListener(new SortKeyActionListener(TableSortKey.SECONDARY_DESCENDING));
final JRadioButtonMenuItem menuItemAsc2 = new JRadioButtonMenuItem("Sort Ascending");
menuItemAsc2.addActionListener(new SortKeyActionListener(TableSortKey.SECONDARY_ASCENDING));
final JRadioButtonMenuItem menuItemNone = new JRadioButtonMenuItem("No Sorting");
menuItemNone.addActionListener(new SortKeyActionListener(TableSortKey.NONE));
popup.add(menuItemNone);
switch(sortKey) {
case NONE:
menuItemDesc1.setSelected(false);
popup.add(menuItemDesc1);
menuItemAsc1.setSelected(false);
popup.add(menuItemAsc1);
menuItemNone.setSelected(true);
popup.add(menuItemNone);
break;
case PRIMARY_DESCENDING:
menuItemDesc1.setSelected(true);
popup.add(menuItemDesc1);
menuItemAsc1.setSelected(false);
popup.add(menuItemAsc1);
menuItemNone.setSelected(false);
popup.add(menuItemNone);
break;
case PRIMARY_ASCENDING:
menuItemDesc1.setSelected(false);
popup.add(menuItemDesc1);
menuItemAsc1.setSelected(true);
popup.add(menuItemAsc1);
menuItemNone.setSelected(false);
popup.add(menuItemNone);
break;
case SECONDARY_DESCENDING:
menuItemDesc2.setSelected(true);
popup.add(menuItemDesc2);
menuItemAsc2.setSelected(false);
popup.add(menuItemAsc2);
menuItemNone.setSelected(false);
popup.add(menuItemNone);
break;
case SECONDARY_ASCENDING:
menuItemDesc2.setSelected(false);
popup.add(menuItemDesc2);
menuItemAsc2.setSelected(true);
popup.add(menuItemAsc1);
menuItemNone.setSelected(false);
popup.add(menuItemNone);
break;
default:
throw new IllegalArgumentException("TableSortKey '" + sortKey.name() + "' not implemented.");
}
return popup;
}
use of org.knime.core.node.tableview.TableSortOrder.TableSortKey in project knime-core by knime.
the class TableContentView method onMouseClickInHeader.
/**
* Invoked when a mouse event in the header occurs. This implementation
* will create a popup menu when there is more than one renderer for that
* column available and will also list all possible values in the column
* where the mouse was clicked (as it is provided in the column spec).
* The event's source is the table header
*
* @param e the mouse event in the table header
*/
protected void onMouseClickInHeader(final MouseEvent e) {
JTableHeader header = getTableHeader();
// get column in which event occurred
int columnInView = header.columnAtPoint(e.getPoint());
if (columnInView < 0) {
// outside columns
return;
}
Rectangle recOfColumn = header.getHeaderRect(columnInView);
int horizPos = e.getX() - recOfColumn.x;
assert (horizPos >= 0);
if (SwingUtilities.isRightMouseButton(e)) {
// right click in header.
final JPopupMenu popup = getPopUpMenu(columnInView);
if (popup.getSubElements().length > 0) {
// only if it has content
popup.show(header, e.getX(), e.getY());
}
} else if (e.isControlDown() && getContentModel().isSortingAllowed()) {
// control pressed.
onSortRequest(convertColumnIndexToModel(columnInView), null);
} else if (SwingUtilities.isLeftMouseButton(e) && getContentModel().isSortingAllowed()) {
// left click in header.
TableSortOrder sortOrder = null;
int colIndexInModel = -1;
TableModel model = getModel();
if (model instanceof TableContentModel) {
TableContentModel cntModel = (TableContentModel) model;
sortOrder = cntModel.getTableSortOrder();
colIndexInModel = convertColumnIndexToModel(columnInView);
} else if (model instanceof TableRowHeaderModel) {
TableRowHeaderModel rowHeaderModel = (TableRowHeaderModel) model;
TableContentInterface cntIface = rowHeaderModel.getTableContent();
if (cntIface instanceof TableContentModel) {
TableContentModel cntModel = (TableContentModel) cntIface;
sortOrder = cntModel.getTableSortOrder();
colIndexInModel = -1;
}
}
TableSortKey sortKey;
if (sortOrder == null) {
sortKey = TableSortKey.NONE;
} else {
sortKey = sortOrder.getSortKeyForColumn(colIndexInModel);
}
final JPopupMenu popup = createSortPopupMenu(columnInView, sortKey);
popup.show(header, e.getX(), e.getY());
}
}
Aggregations