Example usage for org.eclipse.jface.action SubMenuManager add

List of usage examples for org.eclipse.jface.action SubMenuManager add

Introduction

In this page you can find the example usage for org.eclipse.jface.action SubMenuManager add.

Prototype

@Override
    public void add(IAction action) 

Source Link

Usage

From source file:mesfavoris.internal.views.comment.SpellcheckableMessageArea.java

License:Open Source License

private void addProposals(final SubMenuManager quickFixMenu) {
    IAnnotationModel sourceModel = sourceViewer.getAnnotationModel();
    Iterator annotationIterator = sourceModel.getAnnotationIterator();
    while (annotationIterator.hasNext()) {
        Annotation annotation = (Annotation) annotationIterator.next();
        boolean isDeleted = annotation.isMarkedDeleted();
        boolean isIncluded = includes(sourceModel.getPosition(annotation), getTextWidget().getCaretOffset());
        boolean isFixable = sourceViewer.getQuickAssistAssistant().canFix(annotation);
        if (!isDeleted && isIncluded && isFixable) {
            IQuickAssistProcessor processor = sourceViewer.getQuickAssistAssistant().getQuickAssistProcessor();
            IQuickAssistInvocationContext context = sourceViewer.getQuickAssistInvocationContext();
            ICompletionProposal[] proposals = processor.computeQuickAssistProposals(context);

            for (ICompletionProposal proposal : proposals)
                quickFixMenu.add(createQuickFixAction(proposal));
        }/*  w  w w.j av a  2  s.c  o m*/
    }
}

From source file:org.eclipse.papyrus.documentation.view.SpellingTextComposite.java

License:Open Source License

/**
 * @param composite the parent of this Composite
 * @param style the style of the text control
 * @param colored tells if the widget has a colored line where the cursor is set
 *//*  w  w  w.  ja va 2  s .com*/
public SpellingTextComposite(final Composite composite, int style, boolean colored) {
    super(composite, SWT.BORDER);
    this.setLayout(new FillLayout());
    AnnotationModel annotationModel = new AnnotationModel();
    IAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess();

    sourceViewer = new SourceViewer(this, null, null, true, style);
    fTextField = sourceViewer.getTextWidget();

    final SourceViewerDecorationSupport support = new SourceViewerDecorationSupport(sourceViewer, null,
            annotationAccess, EditorsUI.getSharedTextColors());
    // display or not a colored line where the field is edited
    if (colored) {
        support.setCursorLinePainterPreferenceKeys(CURRENT_LINE, CURRENT_LINE_COLOR);
    }
    Iterator<?> e = new MarkerAnnotationPreferences().getAnnotationPreferences().iterator();
    while (e.hasNext())
        support.setAnnotationPreference((AnnotationPreference) e.next());

    support.install(EditorsUI.getPreferenceStore());

    final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench()
            .getService(IHandlerService.class);

    final ActionHandler quickFixActionHandler = createQuickFixActionHandler(sourceViewer);

    final TextViewerAction cutAction = new TextViewerAction(sourceViewer, ITextOperationTarget.CUT);
    cutAction.setText(Messages.SpellingTextComposite_cut);
    cutAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_CUT);

    final TextViewerAction copyAction = new TextViewerAction(sourceViewer, ITextOperationTarget.COPY);
    copyAction.setText(Messages.SpellingTextComposite_copy);
    copyAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_COPY);

    final TextViewerAction pasteAction = new TextViewerAction(sourceViewer, ITextOperationTarget.PASTE);
    pasteAction.setText(Messages.SpellingTextComposite_paste);
    pasteAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_PASTE);

    final TextViewerAction selectAllAction = new TextViewerAction(sourceViewer,
            ITextOperationTarget.SELECT_ALL);
    selectAllAction.setText(Messages.SpellingTextComposite_selectAll);
    selectAllAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_SELECT_ALL);

    final MenuManager contextMenu = new MenuManager();
    contextMenu.add(cutAction);
    contextMenu.add(copyAction);
    contextMenu.add(pasteAction);
    contextMenu.add(selectAllAction);
    contextMenu.add(new Separator());
    final SubMenuManager quickFixMenu = new SubMenuManager(contextMenu);
    quickFixMenu.setVisible(true);
    quickFixMenu.addMenuListener(new IMenuListener() {

        public void menuAboutToShow(IMenuManager manager) {
            quickFixMenu.removeAll();
            if (fTextField != null && fTextField.getEditable()) {
                IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
                Iterator<?> annotationIterator = annotationModel.getAnnotationIterator();
                while (annotationIterator.hasNext()) {
                    Annotation annotation = (Annotation) annotationIterator.next();
                    if (!annotation.isMarkedDeleted()
                            && includes(annotationModel.getPosition(annotation),
                                    sourceViewer.getTextWidget().getCaretOffset())
                            && sourceViewer.getQuickAssistAssistant().canFix(annotation)) {
                        ICompletionProposal[] computeQuickAssistProposals = sourceViewer
                                .getQuickAssistAssistant().getQuickAssistProcessor()
                                .computeQuickAssistProposals(sourceViewer.getQuickAssistInvocationContext());
                        for (int i = 0; i < computeQuickAssistProposals.length; i++) {
                            final ICompletionProposal proposal = computeQuickAssistProposals[i];
                            quickFixMenu.add(new Action(proposal.getDisplayString()) {

                                /*
                                 * (non-Javadoc)
                                 * 
                                 * @see org.eclipse.jface.action.Action#run()
                                 */
                                public void run() {
                                    proposal.apply(sourceViewer.getDocument());
                                }

                                /*
                                 * (non-Javadoc)
                                 * 
                                 * @see org.eclipse.jface.action.Action#getImageDescriptor()
                                 */
                                public ImageDescriptor getImageDescriptor() {
                                    if (proposal.getImage() != null) {
                                        return ImageDescriptor.createFromImage(proposal.getImage());
                                    }
                                    return null;
                                }
                            });
                        }
                    }
                }
            }
        }

    });

    fTextField.addFocusListener(new FocusListener() {
        private IHandlerActivation cutHandlerActivation;
        private IHandlerActivation copyHandlerActivation;
        private IHandlerActivation pasteHandlerActivation;
        private IHandlerActivation selectAllHandlerActivation;

        public void focusGained(FocusEvent e) {
            cutAction.update();
            copyAction.update();
            IHandlerService service = (IHandlerService) PlatformUI.getWorkbench()
                    .getService(IHandlerService.class);
            this.cutHandlerActivation = service.activateHandler(IWorkbenchCommandConstants.EDIT_CUT,
                    new ActionHandler(cutAction), new ActiveShellExpression(getShell()));
            this.copyHandlerActivation = service.activateHandler(IWorkbenchCommandConstants.EDIT_COPY,
                    new ActionHandler(copyAction), new ActiveShellExpression(getShell()));
            this.pasteHandlerActivation = service.activateHandler(IWorkbenchCommandConstants.EDIT_PASTE,
                    new ActionHandler(pasteAction), new ActiveShellExpression(getShell()));
            this.selectAllHandlerActivation = service.activateHandler(
                    IWorkbenchCommandConstants.EDIT_SELECT_ALL, new ActionHandler(selectAllAction),
                    new ActiveShellExpression(getShell()));
            quickFixhandlerActivation = installQuickFixActionHandler(handlerService, sourceViewer,
                    quickFixActionHandler);

        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.swt.events.FocusAdapter#focusLost(org.eclipse.swt.events.FocusEvent)
         */
        public void focusLost(FocusEvent e) {
            IHandlerService service = (IHandlerService) PlatformUI.getWorkbench()
                    .getService(IHandlerService.class);
            if (quickFixhandlerActivation != null) {
                service.deactivateHandler(quickFixhandlerActivation);
            }

            if (cutHandlerActivation != null) {
                service.deactivateHandler(cutHandlerActivation);
            }

            if (copyHandlerActivation != null) {
                service.deactivateHandler(copyHandlerActivation);
            }

            if (pasteHandlerActivation != null) {
                service.deactivateHandler(pasteHandlerActivation);
            }

            if (selectAllHandlerActivation != null) {
                service.deactivateHandler(selectAllHandlerActivation);
            }
        }

    });

    sourceViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            cutAction.update();
            copyAction.update();
        }

    });

    sourceViewer.getTextWidget().addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent e) {
            support.uninstall();
            if (quickFixhandlerActivation != null) {
                handlerService.deactivateHandler(quickFixhandlerActivation);
            }
        }

    });

    document = new Document();

    // NOTE: Configuration must be applied before the document is set in order for
    // Hyperlink coloring to work. (Presenter needs document object up front)
    sourceViewer.configure(new TextSourceViewerConfiguration(EditorsUI.getPreferenceStore()));
    sourceViewer.setDocument(document, annotationModel);
    fTextField.setMenu(contextMenu.createContextMenu(fTextField));
}