package com.onaro.util.jfc.grouping; import javax.swing.*; import javax.swing.tree.TreePath; import javax.swing.event.TreeSelectionListener; import javax.swing.event.TreeSelectionEvent; /** * A selection model that selects only rows from the selection model of the * {@link GroupingTable} (the selection of the GroupingTable may contain * groups and rows, yet this model will select only the rows). */ public class SourceTableRowSelectionModel extends DefaultListSelectionModel implements TreeSelectionListener { private static final long serialVersionUID = 1L; /** * Initialize the selection model for the given {@link GroupingTable}. Register * for selection model changes in the GroupingTable. * @param groupingTable the GroupingTable who's selection model * is being translated */ public SourceTableRowSelectionModel(GroupingTable groupingTable) { groupingTable.getTree().getSelectionModel().addTreeSelectionListener(this); } /** * Whenever the selection model of the {@link GroupingTable} changes, update * this selection model with the rows in all the selected groups and theire * sub-groups. * @param e describe the change */ public void valueChanged(TreeSelectionEvent e) { setValueIsAdjusting(true); TreePath[] paths = e.getPaths(); for (int i = 0; i < paths.length; ++i) { Object lastPathComponent = paths[i].getLastPathComponent(); if (lastPathComponent instanceof GroupingModel.State.Row) { GroupingModel.State.Row row = (GroupingModel.State.Row) lastPathComponent; if (e.isAddedPath(i)) { if (!isSelectedIndex(row.row)) { addSelectionInterval(row.row, row.row); } } else { if (isSelectedIndex(row.row)) { removeSelectionInterval(row.row, row.row); } } } } setValueIsAdjusting(false); } }