Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » TextWidget catches SWT.CR key event when complement a word
TextWidget catches SWT.CR key event when complement a word [message #1714681] Sun, 15 November 2015 18:09 Go to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
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

Re: TextWidget catches SWT.CR key event when complement a word [message #1714911 is a reply to message #1714823] Tue, 17 November 2015 16:18 Go to previous messageGo to next message
Eclipse UserFriend
Perhaps take a look at ICompletionListenerExtension2?

Somebody has almost certainly had this problem before. Put a breakpoints in the JDT code and start examining what it does.
Re: TextWidget catches SWT.CR key event when complement a word [message #1714959 is a reply to message #1714911] Tue, 17 November 2015 22:50 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 43
Registered: July 2011
Member
Thank you for the reply.
I tried ICompletionListenerExtension2 but with no luck.
ICompletionListenerExtension2#applied is also called before StyledText's KeyListener#keyPressed.
I still cannot distinguish Enter key to complement a word or to enter a line break.
Re: TextWidget catches SWT.CR key event when complement a word [message #1715198 is a reply to message #1714959] Fri, 20 November 2015 06:55 Go to previous message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 43
Registered: July 2011
Member
I tried VerifyKeyListener instead of KeyListener, and it worked out.
I used ICompletionListener with the flag isCompletionActive.
Thank you.
Previous Topic:Using a table to display a hierarchy of objects
Next Topic:Is there a way to change fonts in Completion Popup?
Goto Forum:
  


Current Time: Tue Apr 23 10:01:20 GMT 2024

Powered by FUDForum. Page generated in 0.03394 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top