Search in sources :

Example 1 with ITextListener

use of org.eclipse.jface.text.ITextListener in project xtext-eclipse by eclipse.

the class XtextSourceViewer method updateTextListeners.

/**
 * Informs all registered text listeners about the change specified by the
 * widget command. This method does not use a robust iterator.
 *
 * @param cmd the widget command translated into a text event sent to all text listeners
 */
@SuppressWarnings("unchecked")
@Override
protected void updateTextListeners(WidgetCommand cmd) {
    if (cmd.event == null && cmd.length == 0 && cmd.start == 0 && cmd.text == null) {
        // handle plain redraw changes
        super.updateTextListeners(cmd);
    } else {
        List<ITextListener> textListeners = fTextListeners;
        if (textListeners != null) {
            textListeners = new ArrayList<ITextListener>(textListeners);
            DocumentEvent event = cmd.event;
            if (event instanceof SlaveDocumentEvent)
                event = ((SlaveDocumentEvent) event).getMasterEvent();
            int usedDiff = 0;
            if (event == null) {
                if (lengthDiff > 0) {
                    usedDiff = lengthDiff;
                }
                lengthDiff = Integer.MIN_VALUE;
            } else {
                lengthDiff = event.fText.length() - event.fLength;
            }
            int length = cmd.length + usedDiff;
            String text = cmd.text;
            if (usedDiff != 0) {
                try {
                    IRegion model = getModelCoverage();
                    length = Math.min(cmd.start + length, model.getLength()) - cmd.start;
                    text = getDocument().get(cmd.start + model.getOffset(), length);
                } catch (BadLocationException e) {
                    length = cmd.length;
                    log.debug("Ignored BadLocationException when fixing document events", e);
                }
            }
            TextEvent e = new TextEvent(cmd.start, length, text, cmd.preservedText, event, redraws()) {
            };
            for (int i = 0; i < textListeners.size(); i++) {
                ITextListener l = textListeners.get(i);
                try {
                    l.textChanged(e);
                } catch (NullPointerException exception) {
                    // in 3.8 this throws NPEs (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=369244)
                    log.info(e);
                }
            }
        }
    }
}
Also used : TextEvent(org.eclipse.jface.text.TextEvent) ITextListener(org.eclipse.jface.text.ITextListener) SlaveDocumentEvent(org.eclipse.jface.text.SlaveDocumentEvent) SlaveDocumentEvent(org.eclipse.jface.text.SlaveDocumentEvent) DocumentEvent(org.eclipse.jface.text.DocumentEvent) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with ITextListener

use of org.eclipse.jface.text.ITextListener in project eclipse.platform.text by eclipse.

the class TestTextViewer method fireTextChanged.

protected void fireTextChanged(TextEvent event) {
    Iterator<ITextListener> e = new ArrayList<>(fTextListeners).iterator();
    while (e.hasNext()) {
        ITextListener l = e.next();
        l.textChanged(event);
    }
}
Also used : ITextListener(org.eclipse.jface.text.ITextListener)

Example 3 with ITextListener

use of org.eclipse.jface.text.ITextListener 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)

Example 4 with ITextListener

use of org.eclipse.jface.text.ITextListener in project statecharts by Yakindu.

the class XtextStyledTextCellEditor method createControl.

/**
 * Creates an {@link SourceViewer} and returns the {@link StyledText} widget
 * of the viewer as the cell editors control. Some code is copied from
 * {@link XtextEditor}.
 */
@Override
protected Control createControl(Composite parent) {
    StyledText styledText = (StyledText) super.createControl(parent);
    styledText.addFocusListener(new FocusAdapter() {

        public void focusLost(FocusEvent e) {
            XtextStyledTextCellEditor.this.focusLost();
        }
    });
    // adapt to xtext
    this.xtextAdapter = createXtextAdapter();
    getXtextAdapter().adapt(styledText);
    // configure content assist
    final IContentAssistant contentAssistant = getXtextAdapter().getContentAssistant();
    this.completionProposalAdapter = createCompletionProposalAdapter(styledText, contentAssistant);
    // This listener notifies the modification, when text is selected via
    // proposal. A ModifyEvent is not thrown by the StyledText in this case.
    getXtextAdapter().getXtextSourceviewer().addTextListener(new ITextListener() {

        public void textChanged(TextEvent event) {
            editOccured(null);
        }
    });
    if ((styledText.getStyle() & SWT.SINGLE) != 0) {
        // The regular key down event is too late (after popup is closed
        // again).
        // when using the StyledText.VerifyKey event (3005), we get the
        // event early enough!
        styledText.addListener(3005, new Listener() {

            public void handleEvent(Event event) {
                if (event.character == SWT.CR && !getCompletionProposalAdapter().isProposalPopupOpen()) {
                    focusLost();
                }
            }
        });
    }
    styledText.addListener(3005, new Listener() {

        public void handleEvent(Event event) {
            if (// ESC
            event.character == '\u001b' && !getCompletionProposalAdapter().isProposalPopupOpen()) {
                XtextStyledTextCellEditor.this.fireCancelEditor();
            }
        }
    });
    initContextMenu(styledText);
    return styledText;
}
Also used : FocusAdapter(org.eclipse.swt.events.FocusAdapter) TextEvent(org.eclipse.jface.text.TextEvent) ITextListener(org.eclipse.jface.text.ITextListener) StyledText(org.eclipse.swt.custom.StyledText) ITextListener(org.eclipse.jface.text.ITextListener) Listener(org.eclipse.swt.widgets.Listener) IContentAssistant(org.eclipse.jface.text.contentassist.IContentAssistant) FocusEvent(org.eclipse.swt.events.FocusEvent) Event(org.eclipse.swt.widgets.Event) KeyEvent(org.eclipse.swt.events.KeyEvent) TextEvent(org.eclipse.jface.text.TextEvent) FocusEvent(org.eclipse.swt.events.FocusEvent)

Example 5 with ITextListener

use of org.eclipse.jface.text.ITextListener in project dsl-devkit by dsldevkit.

the class FixedXtextSourceViewer method updateTextListeners.

/**
 * Informs all registered text listeners about the change specified by the
 * widget command. This method does not use a robust iterator.
 *
 * @param cmd
 *          the widget command translated into a text event sent to all text listeners
 */
@Override
protected void updateTextListeners(final WidgetCommand cmd) {
    List<ITextListener> textListeners = fTextListeners;
    if (textListeners != null) {
        textListeners = new ArrayList<ITextListener>(textListeners);
        DocumentEvent event = cmd.event;
        if (event instanceof SlaveDocumentEvent) {
            event = ((SlaveDocumentEvent) event).getMasterEvent();
        }
        TextEvent e = new TextEvent(cmd.start, cmd.length, cmd.text, cmd.preservedText, event, redraws()) {
        };
        for (int i = 0; i < textListeners.size(); i++) {
            ITextListener l = textListeners.get(i);
            l.textChanged(e);
        }
    }
}
Also used : TextEvent(org.eclipse.jface.text.TextEvent) ITextListener(org.eclipse.jface.text.ITextListener) SlaveDocumentEvent(org.eclipse.jface.text.SlaveDocumentEvent) SlaveDocumentEvent(org.eclipse.jface.text.SlaveDocumentEvent) DocumentEvent(org.eclipse.jface.text.DocumentEvent)

Aggregations

ITextListener (org.eclipse.jface.text.ITextListener)10 TextEvent (org.eclipse.jface.text.TextEvent)8 StyledText (org.eclipse.swt.custom.StyledText)5 ArrayList (java.util.ArrayList)3 KeyEvent (org.eclipse.swt.events.KeyEvent)3 GridData (org.eclipse.swt.layout.GridData)3 GridLayout (org.eclipse.swt.layout.GridLayout)3 Iterator (java.util.Iterator)2 DocumentEvent (org.eclipse.jface.text.DocumentEvent)2 SlaveDocumentEvent (org.eclipse.jface.text.SlaveDocumentEvent)2 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)2 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)2 DisposeEvent (org.eclipse.swt.events.DisposeEvent)2 DisposeListener (org.eclipse.swt.events.DisposeListener)2 FocusEvent (org.eclipse.swt.events.FocusEvent)2 KeyAdapter (org.eclipse.swt.events.KeyAdapter)2 MouseAdapter (org.eclipse.swt.events.MouseAdapter)2 MouseEvent (org.eclipse.swt.events.MouseEvent)2 Control (org.eclipse.swt.widgets.Control)2 List (java.util.List)1