Home > eclipse rcp, Jface > Eclipse RCP – AutoCompleteCombo/Text control

Eclipse RCP – AutoCompleteCombo/Text control

In the recent project there was a requirement from the client where they want some sort of filtering in the drop down combo box as they felt its really difficult to choose from the combo if they have lots of items to choose.

So , we decided to work on that , and we found content proposal will be a good idea to get rid of this.

Here I am sharing the code snippet using that we can enable content proposals for combo and text controls.


import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.fieldassist.ComboContentAdapter;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class AutoCompleteComboTextControls {

	private static final String LCL = "abcdefghijklmnopqrstuvwxyz";
	private static final String UCL = LCL.toUpperCase();
	private static final String NUMS = "0123456789";
	private static final String[] items = new String[] { "Alpha", "Beta", "gaama", "pie",
		"alge","bata" };


	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout(1, false));
		shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		shell.setSize(200, 200);
		shell.setText("Auto Complete Controls");
		
		Monitor primaryMonitor = display.getPrimaryMonitor ();
		Rectangle bounds = primaryMonitor.getBounds ();
		Rectangle rect = shell.getBounds ();
		int x = bounds.x + (bounds.width - rect.width) / 2 ;
		int y = bounds.y + (bounds.height - rect.height) / 2 ;
		shell.setLocation (x, y);
		
		Combo combo = new Combo(shell, SWT.NONE);
		combo.setItems(items);
		combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		combo.addSelectionListener(new SelectionAdapter() {
			
			// called for keyboard enter press
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		
//		Have a look at Jface AutoCompleteField class also
		enableContentProposal(combo);
		
		Text text = new Text(shell, SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		enableContentProposal(text);
		
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
	
	static  void enableContentProposal(Control control)
	    {
	        
	        SimpleContentProposalProvider proposalProvider = null;
	        ContentProposalAdapter proposalAdapter = null;
	        if (control instanceof Combo)
	        {
	            Combo combo = (Combo) control;
	            proposalProvider = new SimpleContentProposalProvider(combo.getItems());
	            proposalAdapter = new ContentProposalAdapter(
	                combo,
	                new ComboContentAdapter(),
	                proposalProvider,
	                getActivationKeystroke(),
	                getAutoactivationChars());
	        }
	        else if (control instanceof Text)
	        {
	            
	            Text text = (Text) control;
	            proposalProvider = new SimpleContentProposalProvider(items);
	            proposalAdapter = new ContentProposalAdapter(
	                text,
	                new TextContentAdapter(),
	                proposalProvider,
	                getActivationKeystroke(),
	                getAutoactivationChars());
	        }
	        proposalProvider.setFiltering(true);
	        proposalAdapter.setPropagateKeys(true);
	        proposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

	    }
	

	
	// this logic is  from swt addons project
	 static char[] getAutoactivationChars() {

		// To enable content proposal on deleting a char
		
		String delete = new String(new char[] { 8 });
		String allChars = LCL + UCL + NUMS + delete;
		return allChars.toCharArray();
	}

	 static KeyStroke getActivationKeystroke() {
		KeyStroke instance = KeyStroke.getInstance(
				new Integer(SWT.CTRL).intValue(), new Integer(' ').intValue());
		return instance;
	}

}

As the content proposal adapter of the jface does not handle the deletion of char, we have to add it in our logic which you can see in getAutoactivationChars() method. This logic is from SWT Addons project .Have a look into SWT Add-ons project.

Note: One annoying thing which I found while implementing this is , after selecting any items from the list via enter press in the keyboard. The control is jus transferred from the content dialog to the combo or text. So if you are handling anything on enter key press for combo or text then user may have to press the enter button twice.

I Hope this is useful.
Happy coding… :)

About these ads
Categories: eclipse rcp, Jface Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: