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

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

Introduction

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

Prototype

@Override
    public void setVisible(boolean visible) 

Source Link

Usage

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

License:Open Source License

private void configureContextMenu() {
    final boolean editable = isEditable(sourceViewer);
    final TextViewerAction cutAction;
    final TextViewerAction undoAction;
    final TextViewerAction redoAction;
    final TextViewerAction pasteAction;
    if (editable) {
        cutAction = new TextViewerAction(sourceViewer, ITextOperationTarget.CUT);
        cutAction.setText("C&ut");
        cutAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_CUT);

        undoAction = new TextViewerAction(sourceViewer, ITextOperationTarget.UNDO);
        undoAction.setText("Undo");
        undoAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_UNDO);

        redoAction = new TextViewerAction(sourceViewer, ITextOperationTarget.REDO);
        redoAction.setText("Redo");
        redoAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_REDO);

        pasteAction = new TextViewerAction(sourceViewer, ITextOperationTarget.PASTE);
        pasteAction.setText("&Paste");
        pasteAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_PASTE);
    } else {//w ww.  j  a  v a 2  s.c om
        cutAction = null;
        undoAction = null;
        redoAction = null;
        pasteAction = null;
    }

    final TextViewerAction copyAction = new TextViewerAction(sourceViewer, ITextOperationTarget.COPY);
    copyAction.setText("&Copy");
    copyAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_COPY);

    final TextViewerAction selectAllAction = new TextViewerAction(sourceViewer,
            ITextOperationTarget.SELECT_ALL);
    selectAllAction.setText("Select &All");
    selectAllAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_SELECT_ALL);

    MenuManager contextMenu = new MenuManager();
    if (cutAction != null)
        contextMenu.add(cutAction);
    contextMenu.add(copyAction);
    if (pasteAction != null)
        contextMenu.add(pasteAction);
    contextMenu.add(selectAllAction);
    if (undoAction != null)
        contextMenu.add(undoAction);
    if (redoAction != null)
        contextMenu.add(redoAction);
    contextMenu.add(new Separator());

    if (editable) {
        final SubMenuManager quickFixMenu = new SubMenuManager(contextMenu);
        quickFixMenu.setVisible(true);
        quickFixMenu.addMenuListener(new IMenuListener() {
            @Override
            public void menuAboutToShow(IMenuManager manager) {
                quickFixMenu.removeAll();
                addProposals(quickFixMenu);
            }
        });
    }

    final StyledText textWidget = getTextWidget();
    textWidget.setMenu(contextMenu.createContextMenu(textWidget));

    textWidget.addFocusListener(new FocusListener() {

        private IHandlerActivation cutHandlerActivation;
        private IHandlerActivation copyHandlerActivation;
        private IHandlerActivation pasteHandlerActivation;
        private IHandlerActivation selectAllHandlerActivation;
        private IHandlerActivation undoHandlerActivation;
        private IHandlerActivation redoHandlerActivation;
        private IHandlerActivation quickFixHandlerActivation;
        private IHandlerActivation contentAssistHandlerActivation;

        @Override
        public void focusGained(FocusEvent e) {
            IHandlerService service = getHandlerService();
            if (service == null)
                return;

            if (cutAction != null) {
                cutAction.update();
                cutHandlerActivation = service.activateHandler(IWorkbenchCommandConstants.EDIT_CUT,
                        new ActionHandler(cutAction), new ActiveShellExpression(getParent().getShell()));
            }
            copyAction.update();

            copyHandlerActivation = service.activateHandler(IWorkbenchCommandConstants.EDIT_COPY,
                    new ActionHandler(copyAction), new ActiveShellExpression(getParent().getShell()));
            if (pasteAction != null)
                this.pasteHandlerActivation = service.activateHandler(IWorkbenchCommandConstants.EDIT_PASTE,
                        new ActionHandler(pasteAction), new ActiveShellExpression(getParent().getShell()));
            selectAllHandlerActivation = service.activateHandler(IWorkbenchCommandConstants.EDIT_SELECT_ALL,
                    new ActionHandler(selectAllAction), new ActiveShellExpression(getParent().getShell()));
            if (undoAction != null)
                undoHandlerActivation = service.activateHandler(IWorkbenchCommandConstants.EDIT_UNDO,
                        new ActionHandler(undoAction), new ActiveShellExpression(getParent().getShell()));
            if (redoAction != null)
                redoHandlerActivation = service.activateHandler(IWorkbenchCommandConstants.EDIT_REDO,
                        new ActionHandler(redoAction), new ActiveShellExpression(getParent().getShell()));
            if (quickFixActionHandler != null)
                quickFixHandlerActivation = getHandlerService().activateHandler(
                        quickFixActionHandler.getAction().getActionDefinitionId(), quickFixActionHandler,
                        new ActiveShellExpression(getParent().getShell()));
            if (contentAssistActionHandler != null)
                contentAssistHandlerActivation = getHandlerService().activateHandler(
                        contentAssistActionHandler.getAction().getActionDefinitionId(),
                        contentAssistActionHandler, new ActiveShellExpression(getParent().getShell()));
        }

        @Override
        public void focusLost(FocusEvent e) {
            IHandlerService service = getHandlerService();
            if (service == null)
                return;

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

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

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

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

            if (undoHandlerActivation != null)
                service.deactivateHandler(undoHandlerActivation);

            if (redoHandlerActivation != null)
                service.deactivateHandler(redoHandlerActivation);

            if (quickFixHandlerActivation != null)
                service.deactivateHandler(quickFixHandlerActivation);

            if (contentAssistHandlerActivation != null)
                service.deactivateHandler(contentAssistHandlerActivation);
        }

    });

    sourceViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            if (cutAction != null)
                cutAction.update();
            copyAction.update();
        }

    });

    if (editable) {
        sourceViewer.addTextListener(new ITextListener() {
            @Override
            public void textChanged(TextEvent event) {
                if (undoAction != null)
                    undoAction.update();
                if (redoAction != null)
                    redoAction.update();
            }
        });
    }
}

From source file:org.eclipse.egit.ui.internal.dialogs.SpellcheckableMessageArea.java

License:Open Source License

private void configureContextMenu() {
    final TextViewerAction cutAction = new TextViewerAction(sourceViewer, ITextOperationTarget.CUT);
    cutAction.setText(UIText.SpellCheckingMessageArea_cut);
    cutAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_CUT);

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

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

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

    MenuManager contextMenu = new MenuManager();
    contextMenu.add(cutAction);/*from   w  w w . j a v a2 s. c  o m*/
    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();
            addProposals(quickFixMenu);
        }
    });
    StyledText textWidget = getTextWidget();
    getTextWidget().setMenu(contextMenu.createContextMenu(textWidget));

    getTextWidget().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(getParent().getShell()));
            this.copyHandlerActivation = service.activateHandler(IWorkbenchCommandConstants.EDIT_COPY,
                    new ActionHandler(copyAction), new ActiveShellExpression(getParent().getShell()));
            this.pasteHandlerActivation = service.activateHandler(IWorkbenchCommandConstants.EDIT_PASTE,
                    new ActionHandler(pasteAction), new ActiveShellExpression(getParent().getShell()));
            this.selectAllHandlerActivation = service.activateHandler(
                    IWorkbenchCommandConstants.EDIT_SELECT_ALL, new ActionHandler(selectAllAction),
                    new ActiveShellExpression(getParent().getShell()));
        }

        /* (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 (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();
        }

    });
}

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 .j  ava2 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));
}