Search in sources :

Example 46 with KeyListener

use of org.eclipse.swt.events.KeyListener in project eclipse.platform.text by eclipse.

the class AbstractControlContentAssistSubjectAdapter method installControlListener.

/**
 * Installs <code>fControlListener</code>, which handles VerifyEvents and KeyEvents by
 * passing them to {@link #fVerifyKeyListeners} and {@link #fKeyListeners}.
 */
private void installControlListener() {
    if (DEBUG)
        // $NON-NLS-1$ //$NON-NLS-2$
        System.out.println("AbstractControlContentAssistSubjectAdapter#installControlListener() -> k: " + fKeyListeners.size() + ", v: " + fVerifyKeyListeners.size());
    if (fControlListener != null)
        return;
    fControlListener = new Listener() {

        @Override
        public void handleEvent(Event e) {
            if (!getControl().isFocusControl())
                // SWT.TRAVERSE_MNEMONIC events can also come in to inactive widgets
                return;
            VerifyEvent verifyEvent = new VerifyEvent(e);
            KeyEvent keyEvent = new KeyEvent(e);
            switch(e.type) {
                case SWT.Traverse:
                    if (DEBUG)
                        // $NON-NLS-1$
                        dump("before traverse", e, verifyEvent);
                    verifyEvent.doit = true;
                    for (VerifyKeyListener verifyKeyListener : fVerifyKeyListeners) {
                        verifyKeyListener.verifyKey(verifyEvent);
                        if (!verifyEvent.doit) {
                            e.detail = SWT.TRAVERSE_NONE;
                            e.doit = true;
                            if (DEBUG)
                                // $NON-NLS-1$
                                dump("traverse eaten by verify", e, verifyEvent);
                            return;
                        }
                        if (DEBUG)
                            // $NON-NLS-1$
                            dump("traverse OK", e, verifyEvent);
                    }
                    break;
                case SWT.KeyDown:
                    for (VerifyKeyListener verifyKeyListener : fVerifyKeyListeners) {
                        verifyKeyListener.verifyKey(verifyEvent);
                        if (!verifyEvent.doit) {
                            e.doit = verifyEvent.doit;
                            if (DEBUG)
                                // $NON-NLS-1$
                                dump("keyDown eaten by verify", e, verifyEvent);
                            return;
                        }
                    }
                    if (DEBUG)
                        // $NON-NLS-1$
                        dump("keyDown OK", e, verifyEvent);
                    for (KeyListener keyListener : fKeyListeners) {
                        keyListener.keyPressed(keyEvent);
                    }
                    break;
                default:
                    Assert.isTrue(false);
            }
        }

        /**
         * Dump the given events to "standard" output.
         *
         * @param who who dump's
         * @param e the event
         * @param ve the verify event
         */
        private void dump(String who, Event e, VerifyEvent ve) {
            // $NON-NLS-1$
            StringBuilder sb = new StringBuilder("--- [AbstractControlContentAssistSubjectAdapter]\n");
            sb.append(who);
            // $NON-NLS-1$
            sb.append(" - e: keyCode=" + e.keyCode + hex(e.keyCode));
            // $NON-NLS-1$
            sb.append("; character=" + e.character + hex(e.character));
            // $NON-NLS-1$
            sb.append("; stateMask=" + e.stateMask + hex(e.stateMask));
            // $NON-NLS-1$
            sb.append("; doit=" + e.doit);
            // $NON-NLS-1$
            sb.append("; detail=" + e.detail + hex(e.detail));
            // $NON-NLS-1$
            sb.append("; widget=" + e.widget);
            // $NON-NLS-1$
            sb.append("\n");
            // $NON-NLS-1$
            sb.append("  verifyEvent keyCode=" + e.keyCode + hex(e.keyCode));
            // $NON-NLS-1$
            sb.append("; character=" + e.character + hex(e.character));
            // $NON-NLS-1$
            sb.append("; stateMask=" + e.stateMask + hex(e.stateMask));
            // $NON-NLS-1$
            sb.append("; doit=" + ve.doit);
            // $NON-NLS-1$
            sb.append("; widget=" + e.widget);
            System.out.println(sb);
        }

        private String hex(int i) {
            // $NON-NLS-1$
            return "[0x" + Integer.toHexString(i) + ']';
        }
    };
    getControl().addListener(SWT.Traverse, fControlListener);
    getControl().addListener(SWT.KeyDown, fControlListener);
    if (DEBUG)
        // $NON-NLS-1$
        System.out.println("AbstractControlContentAssistSubjectAdapter#installControlListener() - installed");
}
Also used : KeyEvent(org.eclipse.swt.events.KeyEvent) VerifyKeyListener(org.eclipse.swt.custom.VerifyKeyListener) DisposeListener(org.eclipse.swt.events.DisposeListener) ILabelProviderListener(org.eclipse.jface.viewers.ILabelProviderListener) Listener(org.eclipse.swt.widgets.Listener) KeyListener(org.eclipse.swt.events.KeyListener) VerifyKeyListener(org.eclipse.swt.custom.VerifyKeyListener) DisposeEvent(org.eclipse.swt.events.DisposeEvent) LabelProviderChangedEvent(org.eclipse.jface.viewers.LabelProviderChangedEvent) Event(org.eclipse.swt.widgets.Event) VerifyEvent(org.eclipse.swt.events.VerifyEvent) KeyEvent(org.eclipse.swt.events.KeyEvent) VerifyKeyListener(org.eclipse.swt.custom.VerifyKeyListener) KeyListener(org.eclipse.swt.events.KeyListener) VerifyEvent(org.eclipse.swt.events.VerifyEvent)

Example 47 with KeyListener

use of org.eclipse.swt.events.KeyListener in project eclipse.platform.text by eclipse.

the class CompletionProposalPopup2 method showProposals.

/**
 * Computes and presents completion proposals. The flag indicates whether this call has
 * be made out of an auto activation context.
 *
 * @param autoActivated <code>true</code> if auto activation context
 * @return an error message or <code>null</code> in case of no error
 */
public String showProposals(final boolean autoActivated) {
    if (fKeyListener == null) {
        fKeyListener = new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (!Helper2.okToUse(fProposalShell))
                    return;
                if (e.character == 0 && e.keyCode == SWT.CTRL) {
                    // http://dev.eclipse.org/bugs/show_bug.cgi?id=34754
                    int index = fProposalTable.getSelectionIndex();
                    if (index >= 0)
                        selectProposal(index, true);
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (!Helper2.okToUse(fProposalShell))
                    return;
                if (e.character == 0 && e.keyCode == SWT.CTRL) {
                    // http://dev.eclipse.org/bugs/show_bug.cgi?id=34754
                    int index = fProposalTable.getSelectionIndex();
                    if (index >= 0)
                        selectProposal(index, false);
                }
            }
        };
    }
    final StyledText styledText = fViewer.getTextWidget();
    if (styledText != null && !styledText.isDisposed())
        styledText.addKeyListener(fKeyListener);
    // BusyIndicator.showWhile(styledText.getDisplay(), new Runnable() {
    // public void run() {
    fInvocationOffset = fViewer.getSelectedRange().x;
    // lazily compute proposals
    // if (fComputedProposals == null)	fComputedProposals= computeProposals(fContentAssistant.getCompletionPosition());
    fComputedProposals = computeProposals(fInvocationOffset);
    int count = (fComputedProposals == null ? 0 : fComputedProposals.length);
    if (count == 0) {
        if (!autoActivated)
            styledText.getDisplay().beep();
    } else {
        if (count == 1 && !autoActivated && fContentAssistant.isAutoInserting())
            insertProposal(fComputedProposals[0], (char) 0, 0, fInvocationOffset);
        else {
            if (fLineDelimiter == null)
                fLineDelimiter = styledText.getLineDelimiter();
            createProposalSelector();
            setProposals(fComputedProposals);
            resizeProposalSelector(true);
            displayProposals();
        }
    }
    return getErrorMessage();
}
Also used : KeyEvent(org.eclipse.swt.events.KeyEvent) StyledText(org.eclipse.swt.custom.StyledText) KeyListener(org.eclipse.swt.events.KeyListener) Point(org.eclipse.swt.graphics.Point)

Aggregations

KeyListener (org.eclipse.swt.events.KeyListener)47 KeyEvent (org.eclipse.swt.events.KeyEvent)44 MouseEvent (org.eclipse.swt.events.MouseEvent)14 SelectionEvent (org.eclipse.swt.events.SelectionEvent)14 Point (org.eclipse.swt.graphics.Point)14 GridData (org.eclipse.swt.layout.GridData)13 Button (org.eclipse.swt.widgets.Button)11 FocusEvent (org.eclipse.swt.events.FocusEvent)10 MouseListener (org.eclipse.swt.events.MouseListener)9 Composite (org.eclipse.swt.widgets.Composite)9 ModifyEvent (org.eclipse.swt.events.ModifyEvent)8 ModifyListener (org.eclipse.swt.events.ModifyListener)8 GridLayout (org.eclipse.swt.layout.GridLayout)8 Event (org.eclipse.swt.widgets.Event)8 Text (org.eclipse.swt.widgets.Text)8 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)7 SelectionListener (org.eclipse.swt.events.SelectionListener)7 StyledText (org.eclipse.swt.custom.StyledText)6 ControlEvent (org.eclipse.swt.events.ControlEvent)6 Label (org.eclipse.swt.widgets.Label)6