eu.cedarsoft.utils.springrcp.selection.SelectionManager Maven / Gradle / Ivy
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;
/**
*
* Date: 22.08.2006
* Time: 20:19:05
*
* @author Johannes Schneider -
* Xore Systems
*/
public class SelectionManager {
@NotNull
public static SelectionManager getInstance() {
return ( SelectionManager ) Application.instance().getApplicationContext().getBean( "selectionManager" );
}
private Map windowSelectionManagers = new HashMap();
/**
* Returns the selection for the currently active window
*
* @param selectionType the type of the selection
* @return the selection
*/
@NotNull
public Selection getSelection( @NotNull Class 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, Selection>> selections; //todo listener notify when removed!?
private MultiMap selectionListeners = new MultiHashMap();
public WindowSelectionManager() {
selections = new WeakHashMap, Selection>>();
}
@NotNull
public Selection getSelection( @NotNull Class selectionType ) {
Selection> selection = selections.get( selectionType );
if ( selection == null ) {
selection = new DefaultSelection( selectionType );
selections.put( selectionType, selection );
}
return ( Selection ) selection;
}
public void setSelection( @NotNull Selection selection ) {
selections.put( selection.getType(), selection );
Collection> listeners = ( Collection> ) selectionListeners.get( selection.getType() );
if ( listeners != null && !listeners.isEmpty() ) {
for ( Object listener : listeners ) {
( ( SelectionListener ) listener ).notifySelectionChanged( selection );
}
}
System.out.println( "new selection " + selection );
}
public void addSelectionListener( Class beanType, SelectionListener listener ) {
selectionListeners.put( beanType, listener );
}
}
public interface SelectionListener {
void notifySelectionChanged( @NotNull Selection selection );
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy