Free tutorials for Java, Eclipse and Web programming



11. Field Assist

Field Assit can be used to provide information about the possible inputs and status of a simple field, e.g. text field or combo box.

The org.eclipse.jface.fieldassist package provides assistance in two ways. Control decorations allow you to place image decorations to show the status of a field. Content proposal allows to provide a popup that which provides possible choices for this field. user. The following will demonstrate the content proposal.

Create a new project "de.vogella.rcp.intro.fieldassist". Use "RCP application with a view" as an example.

In our example the content proposal should get activated via certain keys ("." and "#") as well as the key combination "Cntrl+Space".

Change the View.java to the following.

		
package de.vogella.rcp.intro.fieldassist;

import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.ParseException;
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.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

public class View extends ViewPart {
	public static final String ID = "de.vogella.rcp.intro.fieldassist.view";

	public void createPartControl(Composite parent) {
		Text text = new Text(parent, SWT.BORDER);
		text.setText("Example");

		// "." and "#" will also activate the content proposals
		char[] autoActivationCharacters = new char[] { '.', '#' };
		KeyStroke keyStroke;
		try {
			keyStroke = KeyStroke.getInstance("Ctrl+Space");
			// assume that myTextControl has already been created in some way
			ContentProposalAdapter adapter = new ContentProposalAdapter(text,
					new TextContentAdapter(),
					new SimpleContentProposalProvider(new String[] {
							"ProposalOne", "ProposalTwo", "ProposalThree" }),
					keyStroke, autoActivationCharacters);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public void setFocus() {
	}
}
	

Run the application and check the content proposal works.