Package com.cedarsoft.spring.rcp

Source Code of com.cedarsoft.spring.rcp.UserInteraction

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

Related Classes of com.cedarsoft.spring.rcp.UserInteraction

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.