final Element element = part.getLocalModelElement();
if( element instanceof Item )
{
final Item item = (Item) element;
final MasterDetailsEditorPageState state = page.state();
return new FunctionResult( this, context )
{
private Listener listener;
@Override
protected void init()
{
this.listener = new FilteredListener<PropertyContentEvent>()
{
@Override
protected void handleTypedEvent( final PropertyContentEvent event )
{
refresh();
}
};
state.attach( this.listener, MasterDetailsEditorPageState.PROP_ATTRIBUTES.name() + "/*" );
element.property( Item.PROP_MANUFACTURER ).attach( this.listener );
}
@Override
protected Object evaluate()
{
final boolean color = state.getAttribute( ShowManufacturerColorActionHandler.ATTRIBUTE, false );
if( color )
{
final String manufacturer = item.getManufacturer().content();
final int hashCode = ( manufacturer == null ? 0 : manufacturer.hashCode() );
final int index = abs( hashCode ) % IMAGES.length;
return IMAGES[ index ];
}
else
{
return IMAGE_GENERIC;
}
}
@Override
public void dispose()
{
super.dispose();
if( this.listener != null )
{
state.detach( this.listener, MasterDetailsEditorPageState.PROP_ATTRIBUTES.name() + "/*" );
if( ! element.disposed() )
{
element.property( Item.PROP_MANUFACTURER ).detach( this.listener );
}