@SuppressWarnings( "unchecked" ) // TreeViewer is parameterized since Eclipse 4.4
protected Control createContents( final Composite parent, final boolean embedded )
{
final PropertyEditorPart part = part();
final Property property = part.property();
final boolean isReadOnly = part.isReadOnly();
final boolean showHeader = part.getRenderingHint( PropertyEditorDef.HINT_SHOW_HEADER, true );
final SapphireActionGroup actions = getActions();
final SapphireActionPresentationManager actionPresentationManager = getActionPresentationManager();
final SapphireToolBarActionPresentation toolBarActionsPresentation = new SapphireToolBarActionPresentation( actionPresentationManager );
toolBarActionsPresentation.addFilter( createFilterByActionId( ACTION_ASSIST ) );
toolBarActionsPresentation.addFilter( createFilterByActionId( ACTION_JUMP ) );
final SapphireMenuActionPresentation menuActionsPresentation = new SapphireMenuActionPresentation( actionPresentationManager );
menuActionsPresentation.addFilter( createFilterByActionId( ACTION_ASSIST ) );
menuActionsPresentation.addFilter( createFilterByActionId( ACTION_JUMP ) );
addOnDisposeOperation
(
new Runnable()
{
public void run()
{
actionPresentationManager.dispose();
}
}
);
final Composite mainComposite = createMainComposite
(
parent,
new CreateMainCompositeDelegate( part )
{
@Override
public boolean getShowLabel()
{
return ( embedded ? false : super.getShowLabel() );
}
@Override
public int getLeftMargin()
{
return ( embedded ? 0 : super.getLeftMargin() );
}
@Override
public boolean getSpanBothColumns()
{
return ( embedded ? true : super.getSpanBothColumns() );
}
}
);
final Composite tableComposite;
if( this.decorator == null )
{
tableComposite = new Composite( mainComposite, SWT.NULL );
tableComposite.setLayoutData( gdfill() );
tableComposite.setLayout( glspacing( glayout( 2, 0, 0 ), 2 ) );
this.decorator = createDecorator( tableComposite );
this.decorator.control().setLayoutData( gdvalign( gd(), SWT.TOP ) );
this.decorator.addEditorControl( tableComposite );
}
else
{
tableComposite = mainComposite;
}
this.decorator.addEditorControl( mainComposite );
// Setting the whint in the following code is a hacky workaround for the problem
// tracked by the following JFace bug:
//
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=215997
//
final Composite tableParentComposite = new Composite( tableComposite, SWT.NULL );
tableParentComposite.setLayoutData( gdwhint( gdfill(), 1 ) );
final TableColumnLayout tableColumnLayout = new TableColumnLayout();
tableParentComposite.setLayout( tableColumnLayout );
this.tableViewer = new CustomTableViewer( tableParentComposite, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI );
this.table = this.tableViewer.getTable();
this.decorator.addEditorControl( this.table );
final List<Control> relatedControls = new ArrayList<Control>();
this.table.setData( RELATED_CONTROLS, relatedControls );
this.table.addListener
(
SWT.MeasureItem,
new org.eclipse.swt.widgets.Listener()
{
public void handleEvent( final org.eclipse.swt.widgets.Event event )
{
// The rows should be 18 pixels at minimum to allow sufficient
// room for the cell editors.
event.height = Math.max( event.height, 18 );
}
}
);
this.columnHandlers = new ArrayList<ColumnHandler>();
final ColumnViewerEditorActivationStrategy activationStrategy = new ColumnViewerEditorActivationStrategy( this.tableViewer )
{
protected boolean isEditorActivationEvent( final ColumnViewerEditorActivationEvent event )
{
final int columnIndex = ( (ViewerCell) event.getSource() ).getColumnIndex();
final ColumnHandler columnHandler = getColumnHandler( columnIndex );
return columnHandler.isEditorActivationEvent( event );
}
};
TableViewerEditor.create( this.tableViewer, null, activationStrategy, ColumnViewerEditor.TABBING_HORIZONTAL | ColumnViewerEditor.TABBING_CYCLE_IN_ROW );
this.table.setHeaderVisible( showHeader );
this.selectionProvider = new SelectionProvider( this.tableViewer );
this.table.addFocusListener
(
new FocusAdapter()
{
@Override
public void focusGained( final FocusEvent event )
{
handleTableFocusGainedEvent();
}
}
);
this.refreshOperation = new Runnable()
{
boolean running = false;
public void run()
{
if( TablePropertyEditorPresentation.this.table.isDisposed() )
{
return;
}
if( this.running == true )
{
return;
}
this.running = true;
try
{
TablePropertyEditorPresentation.this.tableViewer.refresh();
if( TablePropertyEditorPresentation.this.table.isDisposed() )
{
return;
}
TablePropertyEditorPresentation.this.table.notifyListeners( SWT.Selection, null );
tableParentComposite.layout();
}
finally
{
this.running = false;
}
}
};
final Listener listener = new FilteredListener<PropertyContentEvent>()
{
@Override
protected void handleTypedEvent( final PropertyContentEvent event )
{
TablePropertyEditorPresentation.this.refreshOperation.run();
}
};
property.attach( listener );
addOnDisposeOperation
(
new Runnable()
{
public void run()
{
property.detach( listener );
}
}
);
boolean showImages = true;
final String columnWidthsHint = part.getRenderingHint( PropertyEditorDef.HINT_COLUMN_WIDTHS, "" );
final StringTokenizer columnWidthsHintTokenizer = new StringTokenizer( columnWidthsHint, "," );
for( final ModelPath childPropertyPath : part.getChildProperties() )
{
final PropertyDef childProperty = property.definition().getType().property( childPropertyPath );
final PropertyEditorDef childPropertyEditorDef = part.definition().getChildPropertyEditor( childPropertyPath );
final TableViewerColumn tableViewerColumn = new TableViewerColumn( this.tableViewer, SWT.NONE );
if( childPropertyEditorDef == null )
{
final String label = childProperty.getLabel( false, CapitalizationType.TITLE_STYLE, false );
tableViewerColumn.getColumn().setText( label );
}
else
{
final MutableReference<FunctionResult> labelFunctionResultRef = new MutableReference<FunctionResult>();
final Runnable updateLabelOp = new Runnable()
{
public void run()
{
String label = (String) labelFunctionResultRef.get().value();
label = LabelTransformer.transform( label, CapitalizationType.TITLE_STYLE, false );
tableViewerColumn.getColumn().setText( label );
}
};
final FunctionResult labelFunctionResult = part.initExpression
(
childPropertyEditorDef.getLabel().content(),
String.class,
Literal.create( childProperty.getLabel( false, CapitalizationType.NO_CAPS, true ) ),
updateLabelOp
);
labelFunctionResultRef.set( labelFunctionResult );
updateLabelOp.run();
addOnDisposeOperation
(
new Runnable()
{
public void run()
{
labelFunctionResult.dispose();
}
}
);
}
ColumnWeightData columnWeightData = null;
if( columnWidthsHintTokenizer.hasMoreTokens() )
{
final String columnWidthHint = columnWidthsHintTokenizer.nextToken();
final String[] columnWidthHintSplit = columnWidthHint.split( ":" );
if( columnWidthHintSplit.length == 1 || columnWidthHintSplit.length == 2 )
{
try
{
final int minColumnWidth = Integer.parseInt( columnWidthHintSplit[ 0 ].trim() );
final int columnWeight;
if( columnWidthHintSplit.length == 2 )
{
columnWeight = Integer.parseInt( columnWidthHintSplit[ 1 ].trim() );
}
else
{
columnWeight = 0;
}
columnWeightData = new ColumnWeightData( columnWeight, minColumnWidth, true );
}
catch( NumberFormatException e ) {}
}
}
if( columnWeightData == null )
{
columnWeightData = new ColumnWeightData( 1, 100, true );
}
tableColumnLayout.setColumnData( tableViewerColumn.getColumn(), columnWeightData );
final ColumnHandler columnHandler = createColumnHandler( this.columnHandlers, childPropertyPath, showImages, childPropertyEditorDef );
showImages = false; // Only the first column should ever show the image.
tableViewerColumn.setLabelProvider( columnHandler.getLabelProvider() );
tableViewerColumn.setEditingSupport( columnHandler.getEditingSupport() );
final TableColumn tableColumn = tableViewerColumn.getColumn();
tableColumn.addSelectionListener
(
new SelectionAdapter()
{
@Override
public void widgetSelected( final SelectionEvent event )
{
final TableColumn currentSortColumn = TablePropertyEditorPresentation.this.table.getSortColumn();
if( currentSortColumn != tableColumn )
{
TablePropertyEditorPresentation.this.table.setSortColumn( tableColumn );
TablePropertyEditorPresentation.this.table.setSortDirection( SWT.DOWN );
TablePropertyEditorPresentation.this.tableViewer.setComparator( new TableSorter( columnHandler, SWT.DOWN ) );
}
else
{
final int currentSortDirection = TablePropertyEditorPresentation.this.table.getSortDirection();
if( currentSortDirection == SWT.DOWN )
{
TablePropertyEditorPresentation.this.table.setSortDirection( SWT.UP );
TablePropertyEditorPresentation.this.tableViewer.setComparator( new TableSorter( columnHandler, SWT.UP ) );
}
else
{
TablePropertyEditorPresentation.this.table.setSortColumn( null );
TablePropertyEditorPresentation.this.tableViewer.setComparator( null );
}
}
for( SapphireAction action : actions.getActions() )
{
for( SapphireActionHandler handler : action.getActiveHandlers() )
{
if( handler instanceof PropertyEditorActionHandler )
{
( (PropertyEditorActionHandler) handler ).refreshEnablementState();
}
}
}
}
}
);
}
final IStructuredContentProvider contentProvider = new IStructuredContentProvider()
{
public Object[] getElements( final Object inputElement )
{
final ElementList<?> list = property();
final Map<Element,TableRow> rows = new LinkedHashMap<Element,TableRow>();
for( Element element : list )
{
TableRow row = null;
if( TablePropertyEditorPresentation.this.rows != null )
{
row = TablePropertyEditorPresentation.this.rows.remove( element );
}
if( row == null )
{
ImageProvider imageProvider = null;
final ImageService imageService = element.service( ImageService.class );
if( imageService != null )
{
imageProvider = new ImageProvider()
{
private Listener imageServiceListener;
@Override
public ImageData image()
{
if( this.imageServiceListener == null )
{
this.imageServiceListener = new Listener()
{
@Override
public void handle( final Event event )
{
update( row() );
}
};
imageService.attach( this.imageServiceListener );
}
return imageService.image();
}
@Override
public void dispose()
{
if( this.imageServiceListener != null )
{
imageService.detach( this.imageServiceListener );
}
}
};
}
else if( getColumnCount() == 1 )
{
final Value<?> value = (Value<?>) element.property( getColumnHandler( 0 ).property() );
final ValueImageService valueImageService = value.service( ValueImageService.class );
if( valueImageService != null )
{
imageProvider = new ImageProvider()
{
@Override
public ImageData image()
{
return valueImageService.provide( value.text() );
}
};
}
}
row = new TableRow( element, imageProvider );
}
rows.put( element, row );
}
if( TablePropertyEditorPresentation.this.rows != null )
{
for( TableRow row : TablePropertyEditorPresentation.this.rows.values() )
{
row.dispose();
}
}
TablePropertyEditorPresentation.this.rows = rows;
return rows.values().toArray();
}
public void inputChanged( final Viewer viewer,
final Object oldInput,
final Object newInput )
{
}
public void dispose()
{
for( TableRow row : TablePropertyEditorPresentation.this.rows.values() )
{
row.dispose();
}
}
};
this.tableViewer.setContentProvider( contentProvider );
this.tableViewer.setInput( contentProvider );
this.table.addTraverseListener
(
new TraverseListener()
{
public void keyTraversed( final TraverseEvent event )
{
handleTableTraverseEvent( event );
}
}
);
final ListSelectionService selectionService = part.service( ListSelectionService.class );
this.selectionProvider.addSelectionChangedListener
(
new ISelectionChangedListener()
{
public void selectionChanged( SelectionChangedEvent event )
{
selectionService.select( getSelectedElements() );
}
}
);
setSelectedElements( selectionService.selection() );
final org.eclipse.sapphire.Listener selectionServiceListener = new FilteredListener<ListSelectionChangedEvent>()
{
@Override
protected void handleTypedEvent( final ListSelectionChangedEvent event )
{
setSelectedElements( event.after() );
}
};
selectionService.attach( selectionServiceListener );
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 );
dragSource.setTransfer( transfers );
final List<Element> dragElements = new ArrayList<Element>();
dragSource.addDragListener
(
new DragSourceListener()
{
public void dragStart( final DragSourceEvent event )
{
if( TablePropertyEditorPresentation.this.tableViewer.getComparator() == null )
{
dragElements.addAll( getSelectedElements() );
event.doit = true;
}
else
{
event.doit = false;
}
}
public void dragSetData( final DragSourceEvent event )
{
event.data = dragElements;
}
public void dragFinished( final DragSourceEvent event )
{
if( event.detail == DND.DROP_MOVE )
{
// When drop target is the same editor as drag source, the drop handler takes care of removing
// elements from their original location. The following block of code accounts for the case when
// dropping into another editor.
boolean droppedIntoAnotherEditor = false;
for( Element dragElement : dragElements )
{
if( ! dragElement.disposed() )
{
droppedIntoAnotherEditor = true;
break;
}
}
if( droppedIntoAnotherEditor )
{
try
{
final Element selectionPostDelete = findSelectionPostDelete( property(), dragElements );
for( Element dragElement : dragElements )
{
final ElementList<?> dragElementContainer = (ElementList<?>) dragElement.parent();
dragElementContainer.remove( dragElement );
}
setSelectedElement( selectionPostDelete );
}
catch( Exception e )
{
// Log this exception unless the cause is EditFailedException. These exception
// are the result of the user declining a particular action that is necessary
// before the edit can happen (such as making a file writable).
final EditFailedException editFailedException = EditFailedException.findAsCause( e );
if( editFailedException == null )
{
Sapphire.service( LoggingService.class ).log( e );
}
}
}
}
dragElements.clear();
}
}
);
final DropTarget target = new DropTarget( this.table, DND.DROP_COPY | DND.DROP_MOVE );
target.setTransfer( transfers );
target.addDropListener
(
new DropTargetAdapter()
{
public void dragOver( final DropTargetEvent event )
{
if( event.item != null )
{
final TableItem dragOverItem = (TableItem) event.item;
final Point pt = dragOverItem.getDisplay().map( null, TablePropertyEditorPresentation.this.table, event.x, event.y );
final Rectangle bounds = dragOverItem.getBounds();
if( pt.y < bounds.y + bounds.height / 2 )
{
event.feedback = DND.FEEDBACK_INSERT_BEFORE;
}
else
{
event.feedback = DND.FEEDBACK_INSERT_AFTER;
}
}
event.feedback |= DND.FEEDBACK_SCROLL;
}
public void drop( final DropTargetEvent event )
{
if( event.data == null )
{
event.detail = DND.DROP_NONE;
return;
}
final List<ElementData> droppedElements = (List<ElementData>) event.data;
final Set<ElementType> possibleTypesService = property.service( PossibleTypesService.class ).types();
for( final ElementData droppedElement : droppedElements )
{
if( ! possibleTypesService.contains( droppedElement.type() ) )
{
event.detail = DND.DROP_NONE;
return;
}
}
final ElementList<?> list = property();
int position;
if( event.item == null )
{
position = list.size();
}
else
{
final TableItem dropTargetItem = (TableItem) event.item;
final TableRow dropTargetRow = (TableRow) dropTargetItem.getData();
final Element dropTargetElement = dropTargetRow.element();
final Point pt = TablePropertyEditorPresentation.this.table.getDisplay().map( null, TablePropertyEditorPresentation.this.table, event.x, event.y );
final Rectangle bounds = dropTargetItem.getBounds();
position = list.indexOf( dropTargetElement );
if( pt.y >= bounds.y + bounds.height / 2 )
{
position++;
}
}
try
{
if( event.detail == DND.DROP_MOVE )
{
for( Element dragElement : dragElements )
{
final ElementList<?> dragElementContainer = (ElementList<?>) dragElement.parent();
if( dragElementContainer == list && dragElementContainer.indexOf( dragElement ) < position )
{
position--;
}
dragElementContainer.remove( dragElement );
}
}
final List<Element> newSelection = new ArrayList<Element>();
for( final ElementData droppedElement : droppedElements )
{
final Element insertedElement = list.insert( droppedElement.type(), position );
insertedElement.copy( droppedElement );
newSelection.add( insertedElement );
position++;
}
if( TablePropertyEditorPresentation.this.table.isDisposed() )
{
return;
}
TablePropertyEditorPresentation.this.tableViewer.refresh();
setSelectedElements( newSelection );
}
catch( Exception e )
{
// Log this exception unless the cause is EditFailedException. These exception
// are the result of the user declining a particular action that is necessary
// before the edit can happen (such as making a file writable).
final EditFailedException editFailedException = EditFailedException.findAsCause( e );
if( editFailedException == null )
{
Sapphire.service( LoggingService.class ).log( e );
}
event.detail = DND.DROP_NONE;
}
}
}
);
}
}
final boolean toolBarNeeded = toolBarActionsPresentation.hasActions();
mainComposite.setLayout( glayout( ( toolBarNeeded ? 2 : 1 ), 0, 0, 0, 0 ) );
if( toolBarNeeded )
{
final ToolBar toolbar = new ToolBar( mainComposite, SWT.FLAT | SWT.VERTICAL );
toolbar.setLayoutData( gdvfill() );
toolBarActionsPresentation.setToolBar( toolbar );
toolBarActionsPresentation.render();
addControl( toolbar );
this.decorator.addEditorControl( toolbar );
}
if( menuActionsPresentation.hasActions() )
{
final Menu menu = new Menu( this.table );
this.table.setMenu( menu );
menuActionsPresentation.setMenu( menu );
menuActionsPresentation.render();
}
final HyperlinkTable hyperlinkTable = new HyperlinkTable( this.table, actions );
hyperlinkTable.setController
(
new HyperlinkTable.Controller()
{
@Override
public boolean isHyperlinkEnabled( final TableItem item,
final int column )
{
final SapphireActionHandler jumpHandler = getJumpHandler( item, column );
if( jumpHandler != null )
{
return jumpHandler.isEnabled();
}
return false;
}
@Override
public void handleHyperlinkEvent( final TableItem item,
final int column )
{
final SapphireActionHandler jumpHandler = getJumpHandler( item, column );
if( jumpHandler != null )
{
jumpHandler.execute( TablePropertyEditorPresentation.this );
}
}
private SapphireActionHandler getJumpHandler( final TableItem item,
final int column )
{
final Element element = ( (TableRow) item.getData() ).element();
final ModelPath property = part.getChildProperties().get( column );
final PropertyEditorPart propertyEditor = part.getChildPropertyEditor( element, property );
final SapphireActionGroup actions = propertyEditor.getActions();
return actions.getAction( ACTION_JUMP ).getFirstActiveHandler();
}
}
);