TextWidget catches SWT.CR key event when complement a word [message #1714681] |
Sun, 15 November 2015 18:09 |
Missing name Mising name Messages: 43 Registered: July 2011 |
Member |
|
|
I don't want to catch key event when Enter pressed on the completion popup shell to select completion proposal.
But StyledText catch key event and I cannot distinguish Enter key to just complement a word or to enter a line break.
I think StyledText should not catch Enter key event when completion popup shell exists.
Is there a way to distinguish trying to enter a line break or not?
Here is a SSCCE.
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
//CTRL + SPACE to activate completion proposal
//SWT.CR key event cannot distinguish
public class CRKeyEventSample {
static StyledText styledText;
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
createTextViewer(shell);
addCRKeyListener();
shell.open();
while( !shell.isDisposed() ) {
if( !display.readAndDispatch() ) {
display.sleep();
}
}
display.dispose();
}
public static void createTextViewer(Shell shell) {
TextViewer viewer = new TextViewer(shell, SWT.MULTI);
styledText = viewer.getTextWidget();
viewer.setDocument(new Document());
ContentAssistant assistant = new ContentAssistant();
AssistProcessor processor = new AssistProcessor();
assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
assistant.install(viewer);
styledText.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
// CTRL + SPACE to complement
if( e.keyCode == SWT.SPACE && e.stateMask == SWT.CTRL) {
assistant.showPossibleCompletions();
}
}
});
}
public static void addCRKeyListener() {
styledText.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if( e.keyCode == SWT.CR ) {
//cannot distinguish CR to complement a word or CR to enter a line break.
System.out.println(e);
}
}
});
}
}
class AssistProcessor implements IContentAssistProcessor {
public AssistProcessor() {
super();
}
public ICompletionProposal[] computeCompletionProposals(ITextViewer textViewer, int documentOffset) {
String[] list = {"orange", "apple", "pine"};
ICompletionProposal[] proposals = new ICompletionProposal[list.length];
for(int i=0; i<list.length; i++) {
String s = list[i];
proposals[i] = new CompletionProposal(s, 0, 0, s.length());
}
return proposals;
}
public IContextInformation[] computeContextInformation(ITextViewer textViewer, int documentOffset) {
return null;
}
public char[] getCompletionProposalAutoActivationCharacters() {
return null;
}
public char[] getContextInformationAutoActivationCharacters() {
return null;
}
public String getErrorMessage() {
return null;
}
public IContextInformationValidator getContextInformationValidator() {
return null;
}
}
|
|
|
Re: TextWidget catches SWT.CR key event when complement a word [message #1714785 is a reply to message #1714681] |
Mon, 16 November 2015 16:43 |
Eclipse User |
|
|
|
Remember that the IDE and JDT are prime examples of the use of JFace and the Eclipse Platform. The JDT JavaCorrectionAssistant (org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionAssistant in org.eclipse.jdt.ui) installs a completion listener and maintains a boolean to track whether the completions are shown or not.
And two other minor points: you should mask e.stateMask with SWT.MODIFIER_MASK as it may have non-modifier information.
// CTRL + SPACE to complement
if( e.keyCode == SWT.SPACE && (e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) {
And in your addCRKeyListener() you want to check that the stateMask is SWT.NONE.
Brian.
|
|
|
Re: TextWidget catches SWT.CR key event when complement a word [message #1714823 is a reply to message #1714785] |
Tue, 17 November 2015 05:37 |
Missing name Mising name Messages: 43 Registered: July 2011 |
Member |
|
|
Thank you for the reply. But it didn't work out in my case.
Even with the flag, I can't distinguish the two case.
In the styledText key listener, flag is always false. Because ICompletionListener#assistSessionEnded is called before KeyListener#keyPressed when I press the Enter key.
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ContentAssistEvent;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.ICompletionListener;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
//CTRL + SPACE to activate completion proposal
//SWT.CR key event cannot distinguish
public class CRKeyEventSample {
static StyledText styledText;
static boolean completionActive;
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
createTextViewer(shell);
addCRKeyListener();
shell.open();
while( !shell.isDisposed() ) {
if( !display.readAndDispatch() ) {
display.sleep();
}
}
display.dispose();
}
public static void createTextViewer(Shell shell) {
TextViewer viewer = new TextViewer(shell, SWT.MULTI);
styledText = viewer.getTextWidget();
viewer.setDocument(new Document());
ContentAssistant assistant = new ContentAssistant();
AssistProcessor processor = new AssistProcessor();
assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
assistant.addCompletionListener(new ICompletionListener(){
@Override
public void assistSessionEnded(ContentAssistEvent event) {
completionActive= false;
}
@Override
public void assistSessionStarted(ContentAssistEvent event) {
completionActive= true;
}
@Override
public void selectionChanged(ICompletionProposal proposal, boolean smartToggle) {}
});
assistant.install(viewer);
styledText.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
// CTRL + SPACE to complement
if( e.keyCode == SWT.SPACE && (e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) {
assistant.showPossibleCompletions();
}
}
});
}
public static void addCRKeyListener() {
styledText.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if( e.keyCode == SWT.CR && (e.stateMask & SWT.MODIFIER_MASK) == SWT.NONE) {
//cannot distinguish CR to complement a word or CR to enter a line break.
// this flag is always false
System.out.println( "completionActive : " + completionActive);
}
}
});
}
}
class AssistProcessor implements IContentAssistProcessor {
public AssistProcessor() {
super();
}
public ICompletionProposal[] computeCompletionProposals(ITextViewer textViewer, int documentOffset) {
String[] list = {"orange", "apple", "pine"};
ICompletionProposal[] proposals = new ICompletionProposal[list.length];
for(int i=0; i<list.length; i++) {
String s = list[i];
proposals[i] = new CompletionProposal(s, 0, 0, s.length());
}
return proposals;
}
public IContextInformation[] computeContextInformation(ITextViewer textViewer, int documentOffset) {
return null;
}
public char[] getCompletionProposalAutoActivationCharacters() {
return null;
}
public char[] getContextInformationAutoActivationCharacters() {
return null;
}
public String getErrorMessage() {
return null;
}
public IContextInformationValidator getContextInformationValidator() {
return null;
}
}
[Updated on: Tue, 17 November 2015 05:42] Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03394 seconds