Java Code Examples for javax.swing.JList#getListSelectionListeners()

The following examples show how to use javax.swing.JList#getListSelectionListeners() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListParameterComponentTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void testInitialize() throws Exception {
  ParameterValues paramValues = mock( ParameterValues.class );
  Calendar cal = Calendar.getInstance();
  cal.set( 2015, 5, 10 );
  Set<Object> keys = new HashSet<Object>();
  keys.add( "key_0" );
  keys.add( new BigDecimal( 1.0 ) );
  keys.add( cal.getTime() );
  doReturn( keys ).when( updateContext ).getParameterValue( PARAM_NAME );
  doReturn( paramValues ).when( listParameter ).getValues( parameterContext );
  doReturn( 3 ).when( paramValues ).getRowCount();
  doReturn( "key_0" ).when( paramValues ).getKeyValue( 0 );
  doReturn( new BigDecimal( 1.1 ) ).when( paramValues ).getKeyValue( 1 );
  cal.add( Calendar.DAY_OF_MONTH, 2 );
  doReturn( cal.getTime() ).when( paramValues ).getKeyValue( 2 );
  doReturn( "val_0" ).when( paramValues ).getTextValue( 0 );
  doReturn( "val_1" ).when( paramValues ).getTextValue( 1 );
  doReturn( "val_2" ).when( paramValues ).getTextValue( 2 );

  testCreateListComponent();
  comp.initialize();

  JList list = findView( comp );
  assertThat( list, is( notNullValue() ) );
  assertThat( list.getModel(), is( instanceOf( KeyedComboBoxModel.class ) ) );
  assertThat( (String) list.getSelectedValue(), is( equalTo( "val_0" ) ) );

  ListSelectionEvent evt = mock( ListSelectionEvent.class );
  doReturn( true ).when( evt ).getValueIsAdjusting();

  ListSelectionListener listener = list.getListSelectionListeners()[0];
  listener.valueChanged( evt );
  verify( updateContext, never() ).setParameterValue( anyString(), anyObject() );

  doReturn( false ).when( evt ).getValueIsAdjusting();

  listener.valueChanged( evt );
  verify( updateContext ).setParameterValue( PARAM_NAME, "key_0" );

  doReturn( "key_incorrect" ).when( paramValues ).getKeyValue( 0 );
  comp.initialize();
  list = findView( comp );
  assertThat( list, is( notNullValue() ) );
  assertThat( list.getModel(), is( instanceOf( KeyedComboBoxModel.class ) ) );
  assertThat( (String) list.getSelectedValue(), is( nullValue() ) );
  listener = list.getListSelectionListeners()[0];
  listener.valueChanged( evt );
  verify( updateContext ).setParameterValue( PARAM_NAME, null );
}
 
Example 2
Source File: ListParameterComponentTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void testInitializeMultiple() throws Exception {
  ParameterValues paramValues = mock( ParameterValues.class );
  Calendar cal = Calendar.getInstance();
  cal.set( 2015, 5, 10 );
  Set<Object> keys = new HashSet<Object>();
  keys.add( "key_0" );
  keys.add( new BigDecimal( 1.0 ) );
  keys.add( cal.getTime() );
  keys.add( "key_3" );
  doReturn( keys ).when( updateContext ).getParameterValue( PARAM_NAME );
  doReturn( paramValues ).when( listParameter ).getValues( parameterContext );
  doReturn( 3 ).when( paramValues ).getRowCount();
  doReturn( "key_0" ).when( paramValues ).getKeyValue( 0 );
  doReturn( new BigDecimal( 1.1 ) ).when( paramValues ).getKeyValue( 1 );
  cal.add( Calendar.DAY_OF_MONTH, 2 );
  doReturn( cal.getTime() ).when( paramValues ).getKeyValue( 2 );
  doReturn( "val_0" ).when( paramValues ).getTextValue( 0 );
  doReturn( "val_1" ).when( paramValues ).getTextValue( 1 );
  doReturn( "val_2" ).when( paramValues ).getTextValue( 2 );

  testCreateMultipleListComponent();
  comp.initialize();

  JList list = findView( comp );
  assertThat( list, is( notNullValue() ) );
  assertThat( list.getModel(), is( instanceOf( KeyedComboBoxModel.class ) ) );
  assertThat( list.getSelectedValuesList().size(), is( equalTo( 1 ) ) );
  assertThat( (String) list.getSelectedValue(), is( equalTo( "val_0" ) ) );

  ListSelectionEvent evt = mock( ListSelectionEvent.class );
  doReturn( true ).when( evt ).getValueIsAdjusting();
  ListSelectionListener listener = list.getListSelectionListeners()[0];
  listener.valueChanged( evt );
  verify( updateContext, never() ).setParameterValue( anyString(), anyObject() );

  doReturn( false ).when( evt ).getValueIsAdjusting();

  listener.valueChanged( evt );
  verify( updateContext ).setParameterValue( PARAM_NAME, new String[] { "key_0" } );
}