Oct 17, 2007

Exposing packages to selective plugins

C++ has a nice concept of friend - you can declare a method/class as a friend. The method/class has the privilege of accessing the private data as well. Java has the concept of default. If you don't specify public/private/protected, the member is accessible to the other classes in the same package. sort-of-friend functionality.

An Eclipse can expose selective packages to its dependencies. How to have the sort-of-friend functionality to few plugins? I means, what if it want to give access only to few pluings, say for the test plugin only and not to other plugins? You can set the package visibility accordingly. In the Runtime tab of the plugin.xml editor, select the exported package and in the right hand side, select the plugins to which you want to expose this package. Simple?


Oct 13, 2007

Wow shortcut!

Now that I've been accused as the evil friend, who will provide suggestions for your itching, let me try to do some damage control. How about letting you know about a cool shortcut? No, not the Ctrl+3, the whole world knows and loves that. This is about copying a Java file from other editor.

There are many times you would be using a TextPad or your favourite trivial editor because you double clicked a Java file from your local disk. How do you import that file into your Eclipse workspace?

Either you can locate the file in your disk, drag and drop to the Package Explorer view or simply Ctrl+A in TextPad, select the package in the Package Explorer view and do a Ctrl+V, you are done! A Java file is created for you in that package with right name and also your package statement in the file is correctly updated.

This is very helpful when you are viewing a web page with code (either some public CVS thru web or some nice tips on the web) you can do a copy and paste them in your Package Explorer view.

Cool isn't it?

Despite the usefulness, I haven't seen this in any documentation, does anyone knows a place with complete list of wonderful features like this?

Oct 11, 2007

Removing Widgets from a composite

One of the frequent questions from Swing-to-SWT developers is: What is the equivalent of the Container.remove method in SWT?

AFAIK, there is none. The Composite doesn't have any method to remove its children. But then we have a workaround. We can dispose the child widget!

Let me give a simple code to explain. The problem statement is simple: Have a Combo box for different UI elements and fill a composite based on the selection. Hope the code is clear and doesn't require any clarifications.

composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(2, false));

Label label = new Label(composite, SWT.None);
label.setText("Select Control:");

combo = new Combo(composite, SWT.READ_ONLY | SWT.DROP_DOWN);
combo.add("Text Box");
combo.add("Combo");
combo.add("Radio Button");

combo.addSelectionListener(new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {

if (control != null)
control.dispose();


switch (combo.getSelectionIndex()) {
case 0:
control = new Text(composite, SWT.BORDER);
break;
case 1:
control = new Combo(composite, SWT.DROP_DOWN);
break;
case 2:
control = new Button(composite, SWT.RADIO);
break;
}

GridData data = new GridData(SWT.FILL, SWT.NONE, false, false);
data.horizontalSpan = 2;
control.setLayoutData(data);

composite.layout(true);
}
});

Here is the result: