Package com.cedarsoft.spring.rcp.tree

Source Code of com.cedarsoft.spring.rcp.tree.SelectionModelBridge

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 );
      }
    } );
  }
}
TOP

Related Classes of com.cedarsoft.spring.rcp.tree.SelectionModelBridge

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.