Package com.cedarsoft.utils.springrcp.selection

Source Code of com.cedarsoft.utils.springrcp.selection.SelectionManager$WindowSelectionManager

package com.cedarsoft.utils.springrcp.selection;

import org.apache.commons.collections.MultiHashMap;
import org.apache.commons.collections.MultiMap;
import org.jetbrains.annotations.NotNull;
import org.springframework.richclient.application.Application;
import org.springframework.richclient.application.ApplicationWindow;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

/**
* <p/>
* Date: 22.08.2006<br>
* Time: 20:19:05<br>
*
* @author <a href="http://johannes-schneider.info">Johannes Schneider</a> -
*         <a href="http://www.xore.de">Xore Systems</a>
*/
public class SelectionManager {
  @NotNull
  public static SelectionManager getInstance() {
    return ( SelectionManager ) Application.instance().getApplicationContext().getBean( "selectionManager" );
  }

  private Map<ApplicationWindow, WindowSelectionManager> windowSelectionManagers = new HashMap<ApplicationWindow, WindowSelectionManager>();

  /**
   * Returns the selection for the currently active window
   *
   * @param selectionType the type of the selection
   * @return the selection
   */
  @NotNull
  public <T> Selection<T> getSelection( @NotNull Class<T> selectionType ) {
    return getActiveWindowSelectionManager().getSelection( selectionType );
  }

  @NotNull
  public WindowSelectionManager getWindowSelectionManager( @NotNull ApplicationWindow activeWindow ) {
    WindowSelectionManager selectionManager = windowSelectionManagers.get( activeWindow );
    if ( selectionManager == null ) {
      selectionManager = new WindowSelectionManager();
      windowSelectionManagers.put( activeWindow, selectionManager );
    }
    return selectionManager;
  }

  @NotNull
  public WindowSelectionManager getActiveWindowSelectionManager() {
    ApplicationWindow activeWindow = Application.instance().getActiveWindow();
    return getWindowSelectionManager( activeWindow );
  }

  public static class WindowSelectionManager {
    private Map<Class<?>, Selection<?>> selections; //todo listener notify when removed!?
    private MultiMap selectionListeners = new MultiHashMap();

    public WindowSelectionManager() {
      selections = new WeakHashMap<Class<?>, Selection<?>>();
    }

    @NotNull
    public <T> Selection<T> getSelection( @NotNull Class<T> selectionType ) {
      Selection<?> selection = selections.get( selectionType );
      if ( selection == null ) {
        selection = new DefaultSelection<T>( selectionType );
        selections.put( selectionType, selection );
      }
      return ( Selection<T> ) selection;
    }

    public <T> void setSelection( @NotNull Selection<T> selection ) {
      selections.put( selection.getType(), selection );


      Collection<?> listeners = ( Collection<?> ) selectionListeners.get( selection.getType() );
      if ( listeners != null && !listeners.isEmpty() ) {
        for ( Object listener : listeners ) {
          ( ( SelectionListener<T> ) listener ).notifySelectionChanged( selection );
        }
      }

      System.out.println( "new selection " + selection );
    }

    public <T> void addSelectionListener( Class<T> beanType, SelectionListener<T> listener ) {
      selectionListeners.put( beanType, listener );
    }
  }

  public interface SelectionListener<T> {
    void notifySelectionChanged( @NotNull Selection<T> selection );
  }
}
TOP

Related Classes of com.cedarsoft.utils.springrcp.selection.SelectionManager$WindowSelectionManager

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.