package com.cedarsoft.spring.rcp;
import com.cedarsoft.spring.SpringSupport;
import com.cedarsoft.spring.rcp.renderer.DefaultLabelProvider;
import com.cedarsoft.CanceledException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.richclient.selection.binding.support.LabelProviderListCellRenderer;
import org.springframework.richclient.selection.dialog.ListSelectionDialog;
import java.util.List;
/**
* Offers some (static) methods for user interaction
*/
public class UserInteraction {
private UserInteraction() {
}
/**
* Lets the user select a value fomr a given list
*
* @param <T> the type
* @param values the values
* @param titleKey the title key
* @param labelProvider the label provider
* @return the selected object
*
* @throws CanceledException
*/
@Nullable
public static <T> T userSelect( @NotNull List<? extends T> values, @NotNull @NonNls String titleKey, @NotNull DefaultLabelProvider<? super T> labelProvider ) throws CanceledException {
final Object[] selected = new Object[]{null};
ListSelectionDialog dialog = new ListSelectionDialog( SpringSupport.INSTANCE.getMessage( titleKey ), values ) {
@Override
protected void onSelect( Object selection ) {
selected[0] = selection;
}
};
dialog.setRenderer( new LabelProviderListCellRenderer( labelProvider ) );
dialog.showDialog();
if ( selected[0] == null ) {
throw new CanceledException();
}
return ( T ) selected[0];
}
}