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");
}
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();
}
Aggregations