Package org.eclipse.sapphire.services

Examples of org.eclipse.sapphire.services.PossibleTypesService


                {
                    for( MasterDetailsContentNodePart.NodeFactory factory : node.factories() )
                    {
                        factory.attach( OutlineNodeAddActionHandlerFactory.this.nodeFactoryListener );
                       
                        final PossibleTypesService possibleTypesService = factory.property().service( PossibleTypesService.class );
                        possibleTypesService.attach( OutlineNodeAddActionHandlerFactory.this.possibleTypesServiceListener );
                    }
                   
                    broadcast( new Event() );
                }
            }
View Full Code Here


        {
            final Property property = factory.property();
           
            if( factory.visible() && ! property.definition().isReadOnly() )
            {
                final PossibleTypesService possibleTypesService = property.service( PossibleTypesService.class );
   
                if( property instanceof ElementList )
                {
                    for( final ElementType memberType : possibleTypesService.types() )
                    {
                        final ListPropertyActionHandler handler = new ListPropertyActionHandler( (ElementList<?>) property, memberType );
                        handlers.add( handler );
                    }
                }
                else if( property instanceof ElementHandle && ! ( property.definition() instanceof ImpliedElementProperty ) )
                {
                    for( final ElementType memberType : possibleTypesService.types() )
                    {
                        final ElementPropertyActionHandler handler = new ElementPropertyActionHandler( (ElementHandle<?>) property, memberType );
                        handlers.add( handler );
                    }
                }
View Full Code Here

        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();
                                }
                            }
                        );
                    }
                   
                    final PropertyEditorAssistDecorator decorator = new PropertyEditorAssistDecorator( part, property, typeSelectorComposite );
                    decorator.control().setLayoutData( gdhindent( gdvalign( gd(), ( style == Style.DROP_DOWN_LIST ? SWT.TOP : SWT.CENTER ) ), ( showLabel ? 3 : 0 ) ) );
                   
                    if( style == Style.RADIO_BUTTONS )
                    {
                        final RadioButtonsGroup radioButtonsGroup = new RadioButtonsGroup( typeSelectorComposite, false );
                        radioButtonsGroup.setLayoutData( gdhfill() );
                       
                        final Map<ElementType,Button> typeToButton = new HashMap<ElementType,Button>();
                        final Map<Button,ElementType> buttonToType = new HashMap<Button,ElementType>();
                       
                        for( final ElementType type : allPossibleTypes )
                        {
                            final String label = type.getLabel( true, CapitalizationType.FIRST_WORD_ONLY, false );
                            final Button button = radioButtonsGroup.addRadioButton( label );
                            typeToButton.put( type, button );
                            buttonToType.put( button, type );
                            decorator.addEditorControl( button );
                            actionPresentationKeyboard.attach( button );
                            attachHelp( button, property );
                        }
                       
                        final Button noneButton = radioButtonsGroup.addRadioButton( noneSelection.text() );
                        noneButton.setVisible( false );
                        decorator.addEditorControl( noneButton );
                        actionPresentationKeyboard.attach( noneButton );
                        attachHelp( noneButton, property );
                       
                        updateUserInterfaceOp = new Runnable()
                        {
                            public void run()
                            {
                                if( Display.getCurrent() == null )
                                {
                                    radioButtonsGroup.getDisplay().asyncExec( this );
                                    return;
                                }
                               
                                final Element subModelElement = ( (ElementHandle<?>) property ).content();
                                final Button button;
                               
                                if( subModelElement == null )
                                {
                                    button = noneButton;
                                    noneButton.setVisible( true );
                                }
                                else
                                {
                                    button = typeToButton.get( subModelElement.type() );
                                    noneButton.setVisible( false );
                                }
                               
                                if( radioButtonsGroup.getSelection() != button )
                                {
                                    radioButtonsGroup.setSelection( button );
                                }
                               
                                radioButtonsGroup.setEnabled( property.enabled() );
                            }
                        };
                               
                        radioButtonsGroup.addSelectionListener
                        (
                            new SelectionAdapter()
                            {
                                @Override
                                public void widgetSelected( final SelectionEvent event )
                                {
                                    try
                                    {
                                        final ElementHandle<?> handle = (ElementHandle<?>) property;
                                        final Button button = radioButtonsGroup.getSelection();
                                       
                                        if( button == noneButton )
                                        {
                                            handle.clear();
                                        }
                                        else
                                        {
                                            final ElementType type = buttonToType.get( button );
                                            handle.content( true, type );
                                        }
                                    }
                                    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 if( style == Style.DROP_DOWN_LIST )
                    {
                        final Combo combo = new Combo( typeSelectorComposite, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY );
                        combo.setLayoutData( gdhfill() );
                        decorator.addEditorControl( combo );
                        actionPresentationKeyboard.attach( combo );
                        attachHelp( combo, property );
                       
                        final Map<ElementType,Integer> typeToIndex = new HashMap<ElementType,Integer>();
                        final Map<Integer,ElementType> indexToType = new HashMap<Integer,ElementType>();
                       
                        int index = 0;
                       
                        for( ElementType type : allPossibleTypes )
                        {
                            final String label = type.getLabel( true, CapitalizationType.FIRST_WORD_ONLY, false );
                            combo.add( label );
                            typeToIndex.put( type, index );
                            indexToType.put( index, type );
                           
                            index++;
                        }
                       
                        updateUserInterfaceOp = new Runnable()
                        {
                            public void run()
                            {
                                if( Display.getCurrent() == null )
                                {
                                    combo.getDisplay().asyncExec( this );
                                    return;
                                }
                               
                                final Element subModelElement = ( (ElementHandle<?>) property ).content();
                                final int index;
                               
                                if( subModelElement == null )
                                {
                                    index = -1;
                                }
                                else
                                {
                                    index = typeToIndex.get( subModelElement.type() );
                                }
                               
                                if( combo.getSelectionIndex() != index )
                                {
                                    if( index == -1 )
                                    {
                                        combo.deselectAll();
                                    }
                                    else
                                    {
                                        combo.select( index );
                                    }
                                }
                               
                                combo.setEnabled( property.enabled() );
                            }
                        };
   
                        combo.addSelectionListener
                        (
                            new SelectionAdapter()
                            {
                                @Override
                                public void widgetSelected( final SelectionEvent event )
                                {
                                    try
                                    {
                                        final ElementHandle<?> handle = (ElementHandle<?>) property;
                                        final int index = combo.getSelectionIndex();
                                       
                                        if( index == -1 )
                                        {
                                            handle.clear();
                                        }
                                        else
                                        {
                                            final ElementType type = indexToType.get( index );
                                            handle.content( true, type );
                                        }
                                    }
                                    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
                    {
                        throw new IllegalStateException();
                    }
                }
               
                actionPresentationKeyboard.render();
               
                updateUserInterfaceOp.run();
               
                final Listener modelPropertyListener = new FilteredListener<PropertyEvent>()
                {
                    @Override
                    protected void handleTypedEvent( final PropertyEvent event )
                    {
                        updateUserInterfaceOp.run();
                    }
                };
               
                property.attach( modelPropertyListener );
               
                typeSelectorComposite.layout( true, true );
               
                typeSelectorComposite.getChildren()[ 0 ].addDisposeListener
                (
                    new DisposeListener()
                    {
                        public void widgetDisposed( final DisposeEvent event )
                        {
                            property.detach( modelPropertyListener );
                            actionPresentationManager.dispose();
                            actionPresentationKeyboard.dispose();
                        }
                    }
                );
            }
        };
       
        renderTypeSelectorOp.run();
       
        final Listener possibleTypesServiceListener = new Listener()
        {
            @Override
            public void handle( final Event event )
            {
                renderTypeSelectorOp.run();
            }
        };
       
        possibleTypesService.attach( possibleTypesServiceListener );
       
        typeSelectorComposite.addDisposeListener
        (
            new DisposeListener()
            {
                public void widgetDisposed( final DisposeEvent event )
                {
                    possibleTypesService.detach( possibleTypesServiceListener );
                }
            }
        );
   
        final Composite separatorComposite = new Composite( composite, SWT.NONE );
View Full Code Here

                            }
                        }
                    }
                };
               
                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;
                                }
                            }
View Full Code Here

           
            final Property property = factory.property();
           
            if( ! property.disposed() )
            {
                final PossibleTypesService possibleTypesService = property.service( PossibleTypesService.class );
                possibleTypesService.detach( this.possibleTypesServiceListener );
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.eclipse.sapphire.services.PossibleTypesService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.