Search in sources :

Example 1 with SubMenuManager

use of org.eclipse.jface.action.SubMenuManager in project egit by eclipse.

the class SpellcheckableMessageArea method configureContextMenu.

private void configureContextMenu() {
    final boolean editable = isEditable(sourceViewer);
    TextViewerAction cutAction;
    TextViewerAction undoAction;
    TextViewerAction redoAction;
    TextViewerAction pasteAction;
    IAction quickFixAction;
    if (editable) {
        cutAction = createFromActionFactory(ActionFactory.CUT, ITextOperationTarget.CUT);
        undoAction = createFromActionFactory(ActionFactory.UNDO, ITextOperationTarget.UNDO);
        redoAction = createFromActionFactory(ActionFactory.REDO, ITextOperationTarget.REDO);
        pasteAction = createFromActionFactory(ActionFactory.PASTE, ITextOperationTarget.PASTE);
        quickFixAction = new QuickfixAction(sourceViewer);
    } else {
        cutAction = null;
        undoAction = null;
        redoAction = null;
        pasteAction = null;
        quickFixAction = null;
    }
    TextViewerAction copyAction = createFromActionFactory(ActionFactory.COPY, ITextOperationTarget.COPY);
    TextViewerAction selectAllAction = createFromActionFactory(ActionFactory.SELECT_ALL, ITextOperationTarget.SELECT_ALL);
    final TextEditorPropertyAction showWhitespaceAction = new TextEditorPropertyAction(UIText.SpellcheckableMessageArea_showWhitespace, sourceViewer, AbstractTextEditor.PREFERENCE_SHOW_WHITESPACE_CHARACTERS) {

        private IPainter whitespaceCharPainter;

        @Override
        public void propertyChange(PropertyChangeEvent event) {
            String property = event.getProperty();
            if (property.equals(getPreferenceKey()) || AbstractTextEditor.PREFERENCE_SHOW_LEADING_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_TRAILING_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_LEADING_IDEOGRAPHIC_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_IDEOGRAPHIC_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_TRAILING_IDEOGRAPHIC_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_LEADING_TABS.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_TABS.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_TRAILING_TABS.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_CARRIAGE_RETURN.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_LINE_FEED.equals(property) || AbstractTextEditor.PREFERENCE_WHITESPACE_CHARACTER_ALPHA_VALUE.equals(property)) {
                synchronizeWithPreference();
            }
        }

        @Override
        protected void toggleState(boolean checked) {
            if (checked)
                installPainter();
            else
                uninstallPainter();
        }

        /**
         * Installs the painter on the viewer.
         */
        private void installPainter() {
            Assert.isTrue(whitespaceCharPainter == null);
            ITextViewer v = getTextViewer();
            if (v instanceof ITextViewerExtension2) {
                IPreferenceStore store = getStore();
                whitespaceCharPainter = new WhitespaceCharacterPainter(v, store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LEADING_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_TRAILING_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LEADING_IDEOGRAPHIC_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_IDEOGRAPHIC_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_TRAILING_IDEOGRAPHIC_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LEADING_TABS), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_TABS), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_TRAILING_TABS), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_CARRIAGE_RETURN), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LINE_FEED), store.getInt(AbstractTextEditor.PREFERENCE_WHITESPACE_CHARACTER_ALPHA_VALUE));
                ((ITextViewerExtension2) v).addPainter(whitespaceCharPainter);
            }
        }

        /**
         * Remove the painter from the viewer.
         */
        private void uninstallPainter() {
            if (whitespaceCharPainter == null)
                return;
            ITextViewer v = getTextViewer();
            if (v instanceof ITextViewerExtension2)
                ((ITextViewerExtension2) v).removePainter(whitespaceCharPainter);
            whitespaceCharPainter.deactivate(true);
            whitespaceCharPainter = null;
        }
    };
    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());
    contextMenu.add(showWhitespaceAction);
    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();
    List<IAction> globalActions = new ArrayList<>();
    if (editable) {
        globalActions.add(cutAction);
        globalActions.add(pasteAction);
        globalActions.add(undoAction);
        globalActions.add(redoAction);
        globalActions.add(quickFixAction);
    }
    globalActions.add(copyAction);
    globalActions.add(selectAllAction);
    if (contentAssistAction != null) {
        globalActions.add(contentAssistAction);
    }
    ActionUtils.setGlobalActions(textWidget, globalActions, getHandlerService());
    textWidget.setMenu(contextMenu.createContextMenu(textWidget));
    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();
            }
        });
    }
    textWidget.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent disposeEvent) {
            showWhitespaceAction.dispose();
        }
    });
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) ITextListener(org.eclipse.jface.text.ITextListener) ArrayList(java.util.ArrayList) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) DisposeEvent(org.eclipse.swt.events.DisposeEvent) WhitespaceCharacterPainter(org.eclipse.jface.text.WhitespaceCharacterPainter) TextEvent(org.eclipse.jface.text.TextEvent) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) ITextViewerExtension2(org.eclipse.jface.text.ITextViewerExtension2) StyledText(org.eclipse.swt.custom.StyledText) IAction(org.eclipse.jface.action.IAction) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) IPainter(org.eclipse.jface.text.IPainter) IMenuListener(org.eclipse.jface.action.IMenuListener) ITextViewer(org.eclipse.jface.text.ITextViewer) SubMenuManager(org.eclipse.jface.action.SubMenuManager) SubMenuManager(org.eclipse.jface.action.SubMenuManager) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) Separator(org.eclipse.jface.action.Separator)

Aggregations

ArrayList (java.util.ArrayList)1 IAction (org.eclipse.jface.action.IAction)1 IMenuListener (org.eclipse.jface.action.IMenuListener)1 IMenuManager (org.eclipse.jface.action.IMenuManager)1 MenuManager (org.eclipse.jface.action.MenuManager)1 Separator (org.eclipse.jface.action.Separator)1 SubMenuManager (org.eclipse.jface.action.SubMenuManager)1 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)1 IPainter (org.eclipse.jface.text.IPainter)1 ITextListener (org.eclipse.jface.text.ITextListener)1 ITextViewer (org.eclipse.jface.text.ITextViewer)1 ITextViewerExtension2 (org.eclipse.jface.text.ITextViewerExtension2)1 TextEvent (org.eclipse.jface.text.TextEvent)1 WhitespaceCharacterPainter (org.eclipse.jface.text.WhitespaceCharacterPainter)1 PropertyChangeEvent (org.eclipse.jface.util.PropertyChangeEvent)1 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)1 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)1 StyledText (org.eclipse.swt.custom.StyledText)1 DisposeEvent (org.eclipse.swt.events.DisposeEvent)1 DisposeListener (org.eclipse.swt.events.DisposeListener)1