if( ! isReadOnly )
{
if( this.exposeAddAction )
{
final SapphireAction addAction = actions.getAction( ACTION_ADD );
final List<SapphireActionHandler> addActionHandlers = new ArrayList<SapphireActionHandler>();
final org.eclipse.sapphire.Listener addActionHandlerListener = new org.eclipse.sapphire.Listener()
{
@Override
public void handle( final org.eclipse.sapphire.Event event )
{
if( event instanceof PostExecuteEvent )
{
if( TablePropertyEditorPresentation.this.table.isDisposed() )
{
return;
}
final Element newListElement = (Element) ( (PostExecuteEvent) event ).getResult();
if( newListElement != null )
{
TablePropertyEditorPresentation.this.refreshOperation.run();
final TableRow row = findTableRow( newListElement );
TablePropertyEditorPresentation.this.tableViewer.setSelection( new StructuredSelection( row ), true );
if( TablePropertyEditorPresentation.this.table.isDisposed() )
{
return;
}
TablePropertyEditorPresentation.this.tableViewer.editElement( row, 0 );
TablePropertyEditorPresentation.this.table.notifyListeners( SWT.Selection, null );
}
}
}
};
final PossibleTypesService possibleTypesService = property.service( PossibleTypesService.class );
final Runnable refreshAddActionHandlersOp = new Runnable()
{
public void run()
{
addAction.removeHandlers( addActionHandlers );
for( SapphireActionHandler addActionHandler : addActionHandlers )
{
addActionHandler.dispose();
}
for( ElementType memberType : possibleTypesService.types() )
{
final SapphireActionHandler addActionHandler = new AddActionHandler( memberType );
addActionHandler.init( addAction, null );
addActionHandler.attach( addActionHandlerListener );
addActionHandlers.add( addActionHandler );
addAction.addHandler( addActionHandler );
}
}
};
refreshAddActionHandlersOp.run();
final org.eclipse.sapphire.Listener possibleTypesServiceListener = new org.eclipse.sapphire.Listener()
{
@Override
public void handle( final org.eclipse.sapphire.Event event )
{
refreshAddActionHandlersOp.run();
}
};
possibleTypesService.attach( possibleTypesServiceListener );
addOnDisposeOperation
(
new Runnable()
{
public void run()
{
addAction.removeHandlers( addActionHandlers );
for( SapphireActionHandler addActionHandler : addActionHandlers )
{
addActionHandler.dispose();
}
possibleTypesService.detach( possibleTypesServiceListener );
}
}
);
}
if( this.exposeDeleteAction )
{
final SapphireAction deleteAction = actions.getAction( ACTION_DELETE );
final SapphireActionHandler deleteActionHandler = new DeleteActionHandler();
deleteActionHandler.init( deleteAction, null );
deleteAction.addHandler( deleteActionHandler );
addOnDisposeOperation
(
new Runnable()
{
public void run()
{
deleteAction.removeHandler( deleteActionHandler );
}
}
);
}
if( ! property.definition().hasAnnotation( FixedOrderList.class ) )
{
final SapphireAction moveUpAction = actions.getAction( ACTION_MOVE_UP );
final SapphireActionHandler moveUpActionHandler = new MoveUpActionHandler();
moveUpActionHandler.init( moveUpAction, null );
moveUpAction.addHandler( moveUpActionHandler );
addOnDisposeOperation
(
new Runnable()
{
public void run()
{
moveUpAction.removeHandler( moveUpActionHandler );
}
}
);
final SapphireAction moveDownAction = actions.getAction( ACTION_MOVE_DOWN );
final SapphireActionHandler moveDownActionHandler = new MoveDownActionHandler();
moveDownActionHandler.init( moveDownAction, null );
moveDownAction.addHandler( moveDownActionHandler );
addOnDisposeOperation
(
new Runnable()
{
public void run()
{
moveDownAction.removeHandler( moveDownActionHandler );
}
}
);
final org.eclipse.sapphire.Listener moveActionHandlerListener = new org.eclipse.sapphire.Listener()
{
@Override
public void handle( final org.eclipse.sapphire.Event event )
{
if( event instanceof PostExecuteEvent )
{
TablePropertyEditorPresentation.this.refreshOperation.run();
// This is a workaround for a weird problem on SWT on Windows. If modifier keys are pressed
// when the list is re-ordered (as in when issuing move up or move down command from the
// keyboard), the focused row can detached from selected row.
final Element element = getSelectedElement();
final TableItem[] items = TablePropertyEditorPresentation.this.table.getItems();
for( int i = 0; i < items.length; i++ )
{
if( items[ i ].getData() == element )
{
TablePropertyEditorPresentation.this.table.setSelection( i );
break;
}
}
}
}
};
moveUpAction.attach( moveActionHandlerListener );
moveDownAction.attach( moveActionHandlerListener );
final ElementsTransfer transfer = new ElementsTransfer( element().type().getModelElementClass().getClassLoader() );
final Transfer[] transfers = new Transfer[] { transfer };
final DragSource dragSource = new DragSource( this.table, DND.DROP_COPY | DND.DROP_MOVE );