}
@Override
public void render()
{
final WithPart part = part();
final WithDef def = part.definition();
final ElementHandle<?> property = part.property();
final Composite composite = new Composite( composite(), SWT.NONE );
composite.setLayoutData( gdhspan( ( part.getScaleVertically() ? gdfill() : gdhfill() ), 2 ) );
composite.setLayout( glayout( 1, 0, 0 ) );
register( composite );
final PossibleTypesService possibleTypesService = property.service( PossibleTypesService.class );
final Composite typeSelectorComposite = new Composite( composite, SWT.NONE );
typeSelectorComposite.setLayoutData( gdhfill() );
final Runnable renderTypeSelectorOp = new Runnable()
{
public void run()
{
for( Control control : typeSelectorComposite.getChildren() )
{
control.dispose();
}
final SortedSet<ElementType> allPossibleTypes = possibleTypesService.types();
final int allPossibleTypesCount = allPossibleTypes.size();
final Runnable updateUserInterfaceOp;
final Style defaultStyle;
if( allPossibleTypesCount == 1 )
{
defaultStyle = Style.CHECKBOX;
}
else if( allPossibleTypesCount <= 3 )
{
defaultStyle = Style.RADIO_BUTTONS;
}
else
{
defaultStyle = Style.DROP_DOWN_LIST;
}
Style style = Style.decode( def.getHint( WithDef.HINT_STYLE ) );
if( style == null || ( style == Style.CHECKBOX && allPossibleTypesCount != 1 ) )
{
style = defaultStyle;
}
final SapphireActionGroup actions = part.getActions();
final SapphireActionPresentationManager actionPresentationManager = new SapphireActionPresentationManager( WithPresentation.this, actions );
final SapphireKeyboardActionPresentation actionPresentationKeyboard = new SapphireKeyboardActionPresentation( actionPresentationManager );
final boolean showLabel = ( part.label() != null );
if( style == Style.CHECKBOX )
{
typeSelectorComposite.setLayout( glspacing( glayout( 2, 0, 0 ), 2 ) );
final PropertyEditorAssistDecorator decorator = new PropertyEditorAssistDecorator( part, property, typeSelectorComposite );
decorator.control().setLayoutData( gd() );
final Button masterCheckBox = new Button( typeSelectorComposite, SWT.CHECK );
masterCheckBox.setLayoutData( gd() );
decorator.addEditorControl( masterCheckBox );
actionPresentationKeyboard.attach( masterCheckBox );
attachHelp( masterCheckBox, property );
if( showLabel )
{
masterCheckBox.setText( part.label( CapitalizationType.FIRST_WORD_ONLY, true ) );
attachPartListener
(
new FilteredListener<LabelChangedEvent>()
{
@Override
protected void handleTypedEvent( final LabelChangedEvent event )
{
masterCheckBox.setText( part.label( CapitalizationType.FIRST_WORD_ONLY, true ) );
layout();
}
}
);
}
updateUserInterfaceOp = new Runnable()
{
public void run()
{
if( Display.getCurrent() == null )
{
masterCheckBox.getDisplay().asyncExec( this );
return;
}
final Element subModelElement = ( (ElementHandle<?>) property ).content();
masterCheckBox.setSelection( subModelElement != null );
masterCheckBox.setEnabled( property.enabled() );
}
};
masterCheckBox.addSelectionListener
(
new SelectionAdapter()
{
@Override
public void widgetSelected( final SelectionEvent event )
{
try
{
final ElementHandle<?> handle = (ElementHandle<?>) property;
if( masterCheckBox.getSelection() == true )
{
handle.content( true );
}
else
{
handle.clear();
}
}
catch( Exception e )
{
// Note that the EditFailedException is ignored here because the user has already
// been notified and likely has taken action that led to the exception (such as
// declining to make a file writable).
final EditFailedException editFailedException = EditFailedException.findAsCause( e );
if( editFailedException == null )
{
Sapphire.service( LoggingService.class ).log( e );
}
}
}
}
);
}
else
{
typeSelectorComposite.setLayout( glspacing( glayout( 3, 0, 0 ), 2 ) );
if( showLabel )
{
final Label label = new Label( typeSelectorComposite, SWT.NONE );
label.setLayoutData( gdhindent( gd(), 9 ) );
label.setText( part.label( CapitalizationType.FIRST_WORD_ONLY, true ) + ":" );
attachPartListener
(
new FilteredListener<LabelChangedEvent>()
{
@Override
protected void handleTypedEvent( final LabelChangedEvent event )
{
label.setText( part.label( CapitalizationType.FIRST_WORD_ONLY, true ) + ":" );
layout();
}
}
);
}