toolBarActionsPresentation.render();
addControl( toolbar );
decorator.addEditorControl( toolbar );
}
final PossibleValuesService possibleValuesService = property.service( PossibleValuesService.class );
final ValueNormalizationService valueNormalizationService = property.service( ValueNormalizationService.class );
final MutableReference<List<PossibleValue>> possibleValuesRef = new MutableReference<List<PossibleValue>>();
final Runnable updateComboSelectionOp = new Runnable()
{
public void run()
{
if( PopUpListFieldPropertyEditorPresentation.this.updatingModel || combo.isDisposed() )
{
return;
}
final String text = valueNormalizationService.normalize( property.text() );
try
{
PopUpListFieldPropertyEditorPresentation.this.updatingEditor = true;
combo.setData( DATA_DEFAULT_VALUE, property.empty() );
if( text == null )
{
combo.deselectAll();
combo.setText( EMPTY_STRING );
}
else
{
final List<PossibleValue> possibleValues = possibleValuesRef.get();
final int possibleValuesCount = possibleValues.size();
int possibleValueIndex = -1;
for( int i = 0; i < possibleValuesCount && possibleValueIndex == -1; i++ )
{
if( equal( possibleValues.get( i ).value(), text ) )
{
possibleValueIndex = i;
}
}
if( PopUpListFieldPropertyEditorPresentation.this.style == PopUpListFieldStyle.STRICT )
{
if( possibleValueIndex == -1 )
{
if( possibleValues.size() == combo.getItemCount() )
{
combo.add( text );
}
else
{
final String existingNonConformingValue = combo.getItem( possibleValuesCount );
if( ! existingNonConformingValue.equals( text ) )
{
combo.setItem( possibleValuesCount, text );
}
}
possibleValueIndex = possibleValuesCount;
}
else if( possibleValuesCount < combo.getItemCount() )
{
combo.remove( possibleValuesCount );
}
}
if( possibleValueIndex != -1 )
{
if( combo.getSelectionIndex() != possibleValueIndex )
{
combo.setText( EMPTY_STRING );
combo.select( possibleValueIndex );
}
}
else
{
if( ! equal( valueNormalizationService.normalize( combo.getText() ), text ) )
{
combo.deselectAll();
combo.setText( text );
}
}
}
}
finally
{
PopUpListFieldPropertyEditorPresentation.this.updatingEditor = false;
}
}
};
final Runnable updateComboContentOp = new Runnable()
{
private final PossibleValue.Factory factory = PossibleValue.factory( property );
public void run()
{
if( PopUpListFieldPropertyEditorPresentation.this.updatingModel || combo.isDisposed() )
{
return;
}
try
{
PopUpListFieldPropertyEditorPresentation.this.updatingEditor = true;
final List<PossibleValue> possibleValues = this.factory.entries();
possibleValuesRef.set( possibleValues );
final String[] contentForCombo = new String[ possibleValues.size() ];
for( int i = 0, n = possibleValues.size(); i < n; i++ )
{
contentForCombo[ i ] = possibleValues.get( i ).label();
}
combo.setItems( contentForCombo );
}
finally
{
PopUpListFieldPropertyEditorPresentation.this.updatingEditor = false;
}
updateComboSelectionOp.run();
}
};
updateComboContentOp.run();
final Listener possibleValuesServiceListener = new Listener()
{
@Override
public void handle( final Event event )
{
runOnDisplayThread( updateComboContentOp );
}
};
possibleValuesService.attach( possibleValuesServiceListener );
final Listener propertyListener = new FilteredListener<PropertyContentEvent>()
{
@Override
protected void handleTypedEvent( final PropertyContentEvent event )
{
runOnDisplayThread( updateComboSelectionOp );
}
};
property.attach( propertyListener );
combo.addModifyListener
(
new ModifyListener()
{
public void modifyText( final ModifyEvent e )
{
if( PopUpListFieldPropertyEditorPresentation.this.updatingEditor )
{
return;
}
try
{
PopUpListFieldPropertyEditorPresentation.this.updatingModel = true;
String value = null;
final int index = combo.getSelectionIndex();
if( index != -1 )
{
final List<PossibleValue> possible = possibleValuesRef.get();
if( index < possible.size() )
{
value = possible.get( index ).value();
}
}
if( value == null )
{
value = combo.getText().trim();
}
if( value != null && value.length() == 0 )
{
value = null;
}
setPropertyValue( value );
}
finally
{
PopUpListFieldPropertyEditorPresentation.this.updatingModel = false;
}
}
}
);
if( this.style == PopUpListFieldStyle.EDITABLE )
{
combo.addFocusListener
(
new FocusAdapter()
{
@Override
public void focusLost( final FocusEvent event )
{
// If an editable pop-up list was presenting the default value and user clears it, there is
// no change in the model, but we need to restore the display of the default value in the UI.
updateComboSelectionOp.run();
}
}
);
}
addOnDisposeOperation
(
new Runnable()
{
public void run()
{
possibleValuesService.detach( possibleValuesServiceListener );
property.detach( propertyListener );
}
}
);
}