Package com.cedarsoft.spring.rcp.hierarchy

Source Code of com.cedarsoft.spring.rcp.hierarchy.HierarchyTree$SelectionListener

package com.cedarsoft.spring.rcp.hierarchy;

import com.cedarsoft.spring.SpringSupport;
import com.cedarsoft.spring.rcp.events.DetailsTreeSelectionEvent;
import com.cedarsoft.spring.rcp.events.SelectionEvent;
import com.cedarsoft.lookup.Lookups;
import org.jetbrains.annotations.NotNull;

import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
*
*/
public class HierarchyTree extends JTree {
  @NotNull
  private final List<SelectionListener> selectionListeners = new ArrayList<SelectionListener>();

  private boolean updatingSelection;

  public HierarchyTree( @NotNull HierachyTreeModel hierachyTreeModel ) {
    setModel( hierachyTreeModel );
    setRootVisible( true );
    //    tree.setLargeModel( true );
    setShowsRootHandles( true );
    setCellRenderer( hierachyTreeModel.getRenderer() );

    getSelectionModel().addTreeSelectionListener( new TreeSelectionListener() {
      @Override
      public void valueChanged( TreeSelectionEvent e ) {
        if ( updatingSelection ) {
          return;
        }
        updatingSelection = true;
        try {
          TreePath oldSelection = e.getOldLeadSelectionPath();
          if ( oldSelection != null ) {
            Object object = oldSelection.getLastPathComponent();
            notifyUnselect( new DetailsTreeSelectionEvent( DetailsTreeSelectionEvent.Type.UNSELECTED, object ) );
            SpringSupport.INSTANCE.publishEvent( new SelectionEvent( SelectionEvent.Type.UNSELECT, Lookups.dynamicLookupFromList( Arrays.asList( oldSelection.getPath() ) ) ) );
          }

          TreePath newSelection = e.getNewLeadSelectionPath();
          if ( newSelection != null ) {
            Object object = newSelection.getLastPathComponent();
            notifySelect( new DetailsTreeSelectionEvent( DetailsTreeSelectionEvent.Type.SELECTED, object ) );
            SpringSupport.INSTANCE.publishEvent( new SelectionEvent( SelectionEvent.Type.SELECT, Lookups.dynamicLookupFromList( Arrays.asList( newSelection.getPath() ) ) ) );
          }
        } finally {
          updatingSelection = false;
        }
      }
    } );
  }

  protected void notifySelect( @NotNull DetailsTreeSelectionEvent event ) {
    for ( SelectionListener selectionListener : selectionListeners ) {
      selectionListener.select( event );
    }
  }

  protected void notifyUnselect( @NotNull DetailsTreeSelectionEvent event ) {
    for ( SelectionListener selectionListener : selectionListeners ) {
      selectionListener.unselect( event );
    }
  }

  public void addSelectionListener( @NotNull SelectionListener listener ) {
    this.selectionListeners.add( listener );
  }

  public interface SelectionListener {
    void unselect( @NotNull DetailsTreeSelectionEvent event );

    void select( @NotNull DetailsTreeSelectionEvent event );
  }
}
TOP

Related Classes of com.cedarsoft.spring.rcp.hierarchy.HierarchyTree$SelectionListener

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.