Archive

Archive for December, 2011

Eclipse RCP Preferences – Storing & Retrieving values using preferences

December 30, 2011 Leave a comment

Often in the IDE development, we require to store some application specific values. We can make use of eclipse preferences to store and retrieve values.

The Eclipse has three different types of scopes.
Configuration scope
Default scope
Instance scope

The below code snippet shows how we can store and retrieve values using configuration scope:

 
Preferences preferences = ConfigurationScope.INSTANCE.getNode("com.mycomp.client");
Preferences pref = preferences.node("node1");
pref.put("key", "val");
 try
  {
  // Forcing the application to save the preferences
  preferences.flush();
  }
  catch (BackingStoreException e)
  {
    e.printStackTrace();
  }

Retrieving the stored value:


 Preferences preferences = ConfigurationScope.INSTANCE.getNode("com.mycomp.client");
 Preferences pref  = preferences.node("node1");
 String val = pref.get("key", "default");

For further reference you can refer here

Note: Preferences are workspace specific. So If the workspace is changed or deleted then the stored values will be lost.

Happy coding.. 🙂

Categories: eclipse rcp Tags: ,

Eclipse RCP – AutoCompleteCombo/Text control

December 12, 2011 Leave a comment

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… 🙂

Categories: eclipse rcp, Jface Tags: ,

Eclipse RCP – Placing shell/dialog in the center or near cursor position

December 9, 2011 Leave a comment

One of the common requirement in day to day rcp development is to place the dialogs/shells we show in the center of the screen or near to the cursor position.

The following snippet will help you for the same.

Placing at the center of the screen:


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);

Placing the shell near cursor:

Point pt = display.getCursorLocation();
 shell.setLocation(pt.x, pt.y);

Happy coding.. 🙂

Categories: eclipse rcp Tags: ,