package com.cedarsoft.spring.rcp.tree;
import com.cedarsoft.spring.rcp.selection.SelectionModel;
import com.cedarsoft.lookup.DynamicLookup;
import com.cedarsoft.lookup.Lookups;
import org.jetbrains.annotations.NotNull;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
/**
* Updates the selection model on tree selection events
*/
public class SelectionModelBridge {
private SelectionModelBridge() {
}
public static void connect( @NotNull final SelectionModel selectionModel, @NotNull final TreeSelectionModel treeSelectionModel ) {
treeSelectionModel.addTreeSelectionListener( new TreeSelectionListener() {
@Override
public void valueChanged( @NotNull TreeSelectionEvent e ) {
DynamicLookup selects = Lookups.dynamicLookup();
DynamicLookup unselects = Lookups.dynamicLookup();
TreePath[] paths = e.getPaths();
for ( TreePath path : paths ) {
if ( e.isAddedPath( path ) ) {
selects.addValues( TreeUtils.unwrap( path.getPath() ) );
} else {
unselects.removeValues( TreeUtils.unwrap( path.getPath() ) );
}
}
selectionModel.selectionChanged( unselects, selects );
}
} );
}
}