}
}
public Object[] getElements( final Object inputElement )
{
final MasterDetailsContentNodeList nodes = outline.getRoot().nodes();
attach( nodes );
return nodes.visible().toArray();
}
public Object[] getChildren( final Object parentElement )
{
final MasterDetailsContentNodeList nodes = ( (MasterDetailsContentNodePart) parentElement ).nodes();
attach( nodes );
return nodes.visible().toArray();
}
public Object getParent( final Object element )
{
return ( (MasterDetailsContentNodePart) element ).getParentNode();
}
public boolean hasChildren( final Object parentElement )
{
final MasterDetailsContentNodeList nodes = ( (MasterDetailsContentNodePart) parentElement ).nodes();
attach( nodes );
return ! nodes.visible().isEmpty();
}
public void inputChanged( final Viewer viewer,
final Object oldInput,
final Object newInput )
{
}
public void dispose()
{
detach( outline.getRoot().nodes() );
}
};
final StyledCellLabelProvider labelProvider = new StyledCellLabelProvider ()
{
private final Map<ImageDescriptor,Image> images = new HashMap<ImageDescriptor,Image>();
private final Map<org.eclipse.sapphire.Color,Color> colors = new HashMap<org.eclipse.sapphire.Color,Color>();
public void update( final ViewerCell cell )
{
final MasterDetailsContentNodePart node = (MasterDetailsContentNodePart) cell.getElement();
final StyledString styledString = new StyledString( node.getLabel() );
for( final TextDecoration decoration : node.decorations() )
{
String text = decoration.text();
if( text != null )
{
text = text.trim();
if( text.length() > 0 )
{
final Color color = color( decoration.color() );
styledString.append
(
" " + text,
new Styler()
{
@Override
public void applyStyles( final TextStyle style )
{
style.foreground = color;
}
}
);
}
}
}
cell.setText( styledString.toString() );
cell.setStyleRanges( styledString.getStyleRanges() );
cell.setImage( image( node.getImage() ) );
super.update( cell );
}
private Color color( final org.eclipse.sapphire.Color c )
{
Color color = this.colors.get( c );
if( color == null )
{
color = new Color( tree.getDisplay(), c.red(), c.green(), c.blue() );
this.colors.put( c, color );
}
return color;
}
private Image image( final ImageDescriptor imageDescriptor )
{
Image image = this.images.get( imageDescriptor );
if( image == null )
{
image = imageDescriptor.createImage();
this.images.put( imageDescriptor, image );
}
return image;
}
@Override
public void dispose()
{
for( final Image image : this.images.values() )
{
image.dispose();
}
}
};
new SapphireToolTip( tree )
{
protected Object getToolTipArea( final Event event )
{
return treeViewer.getCell( new Point( event.x, event.y ) );
}
protected boolean shouldCreateToolTip(Event event)
{
if( ! super.shouldCreateToolTip( event ) )
{
return false;
}
setShift( new Point( 0, 20 ) );
tree.setToolTipText( "" );
boolean res = false;
final MasterDetailsContentNodePart node = getNode( event );
if( node != null )
{
res = ! node.validation().ok();
}
return res;
}
private MasterDetailsContentNodePart getNode( final Event event )
{
final TreeItem item = tree.getItem( new Point(event.x, event.y) );
if( item == null )
{
return null;
}
else
{
return (MasterDetailsContentNodePart) item.getData();
}
}
protected void afterHideToolTip(Event event) {
super.afterHideToolTip(event);
// Clear the restored value else this could be a source of a leak
if (event != null && event.widget != treeViewer.getControl()) {
treeViewer.getControl().setFocus();
}
}
@Override
protected void createContent( final Event event,
final Composite parent )
{
final MasterDetailsContentNodePart node = getNode( event );
parent.setLayout( glayout( 1 ) );
SapphireFormText text = new SapphireFormText( parent, SWT.NO_FOCUS );
text.setLayoutData( gdfill() );
final org.eclipse.sapphire.modeling.Status validation = node.validation();
final List<org.eclipse.sapphire.modeling.Status> items = gather( validation );
final StringBuffer buffer = new StringBuffer();
buffer.append( "<form>" );
final int count = items.size();
int i = 0;
for( org.eclipse.sapphire.modeling.Status item : items )
{
final String imageKey = ( item.severity() == org.eclipse.sapphire.modeling.Status.Severity.ERROR ? "error" : "warning" );
buffer.append( "<li style=\"image\" value=\"" + imageKey + "\">" + escapeForXml( item.message() ) + "</li>" );
i++;
if( count > 10 && i == 9 )
{
break;
}
}
if( count > 10 )
{
final String msg = problemsOverflowMessage.format( numberToString( count - 9 ) );
final String imageKey = ( validation.severity() == org.eclipse.sapphire.modeling.Status.Severity.ERROR ? "error" : "warning" );
buffer.append( "<br/><li style=\"image\" value=\"" + imageKey + "\">" + msg + "</li>" );
}
buffer.append( "</form>" );
text.setText( buffer.toString(), true, false );
text.setImage( "error", ImageData.readFromClassLoader( SwtResourceCache.class, "Error.png" ).required() );
text.setImage( "warning", ImageData.readFromClassLoader( SwtResourceCache.class, "Warning.png" ).required() );
}
private String numberToString( final int number )
{
switch( number )
{
case 2 : return two.text();
case 3 : return three.text();
case 4 : return four.text();
case 5 : return five.text();
case 6 : return six.text();
case 7 : return seven.text();
case 8 : return eight.text();
case 9 : return nine.text();
default : return String.valueOf( number );
}
}
private List<org.eclipse.sapphire.modeling.Status> gather( final org.eclipse.sapphire.modeling.Status status )
{
final List<org.eclipse.sapphire.modeling.Status> items = new ArrayList<org.eclipse.sapphire.modeling.Status>();
gather( status, items );
return items;
}
private void gather( final org.eclipse.sapphire.modeling.Status status,
final List<org.eclipse.sapphire.modeling.Status> items )
{
if( status.children().isEmpty() )
{
items.add( status );
}
else
{
for( org.eclipse.sapphire.modeling.Status child : status.children() )
{
gather( child, items );
}
}
}
};
treeViewer.setContentProvider( contentProvider );
treeViewer.setLabelProvider( labelProvider );
treeViewer.setInput( new Object() );
final MutableReference<Boolean> ignoreSelectionChange = new MutableReference<Boolean>( false );
final MutableReference<Boolean> ignoreExpandedStateChange = new MutableReference<Boolean>( false );
final Listener contentTreeListener = new Listener()
{
@Override
public void handle( final org.eclipse.sapphire.Event event )
{
if( event instanceof MasterDetailsContentOutline.SelectionChangedEvent )
{
if( ignoreSelectionChange.get() == true )
{
return;
}
final MasterDetailsContentOutline.SelectionChangedEvent evt = (MasterDetailsContentOutline.SelectionChangedEvent) event;
final List<MasterDetailsContentNodePart> selection = evt.selection();
final IStructuredSelection sel;
if( selection.isEmpty() )
{
sel = StructuredSelection.EMPTY;
}
else
{
sel = new StructuredSelection( selection );
}
if( ! treeViewer.getSelection().equals( sel ) )
{
for( MasterDetailsContentNodePart node : selection )
{
treeViewer.reveal( node );
}
treeViewer.setSelection( sel );
}
}
else if( event instanceof MasterDetailsContentOutline.NodeExpandedStateChangedEvent )
{
if( ignoreExpandedStateChange.get() == true )
{
return;
}
final MasterDetailsContentOutline.NodeExpandedStateChangedEvent evt = (MasterDetailsContentOutline.NodeExpandedStateChangedEvent) event;
final MasterDetailsContentNodePart node = evt.node();
final boolean expandedState = node.isExpanded();
if( treeViewer.getExpandedState( node ) != expandedState )
{
treeViewer.setExpandedState( node, expandedState );
}
}
else if( event instanceof MasterDetailsContentOutline.FilterChangedEvent )
{
final MasterDetailsContentOutline.FilterChangedEvent evt = (MasterDetailsContentOutline.FilterChangedEvent) event;
filteredTree.changeFilterText( evt.filter() );
}
}
};
outline.attach( contentTreeListener );
treeViewer.addSelectionChangedListener
(
new ISelectionChangedListener()
{
public void selectionChanged( final SelectionChangedEvent event )
{
ignoreSelectionChange.set( true );
try
{
final IStructuredSelection selection = (IStructuredSelection) event.getSelection();
final List<MasterDetailsContentNodePart> nodes = new ArrayList<MasterDetailsContentNodePart>();
for( Iterator<?> itr = selection.iterator(); itr.hasNext(); )
{
nodes.add( (MasterDetailsContentNodePart) itr.next() );
}
outline.setSelectedNodes( nodes );
}
finally