Friday, June 09, 2006

Decorated Fields in Eclipse

With Eclipse 3.2 a new JFace API is released addressing decorated fields. Everyone knows the little yellow light bulb that comes up if you want to create a new Java type and do not specify any package name.


Also a content assistant is provide that helps you choosing a package name. In Eclipse 3.2 this is now API and developers can use the feature to improve the user experience, especially in the area of validating user input.

I have put some sample code together that simply creates a decorated text field with a message. The user should provide a name. The name must not start with a blank though. In case it does, I show a little error sign that is augmented with a message.


If the user presses F1 in the field a content assitant pops up and proposes funny old german names that even google does not know :)

Here is the code snippet:

// a new shell
Shell shell = new Shell();
shell.setLayout(new FillLayout());
shell.setText("Name");
shell.setBounds(200, 200, 200, 200);

// a parent
Composite parentComposite = new Composite(shell, SWT.NONE);
parentComposite.setLayout(new GridLayout(2, false));

// a label
Label nameLabel = new Label(parentComposite, SWT.NONE);
nameLabel.setText("Type a name:");

// a field
final DecoratedField field = new DecoratedField(parentComposite,
SWT.NONE, new IControlCreator() {

public Control createControl(Composite parent, int style) {
// create a simple text control
Text nameText = new Text(parent, SWT.BORDER);
nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
return nameText;
}
});
final Text text = (Text) field.getControl();

// create an error decoration
final FieldDecoration decoration = FieldDecorationRegistry.getDefault()
.getFieldDecoration(FieldDecorationRegistry.DEC_ERROR);
decoration.setDescription("A name must not start with a blank.");
field.addFieldDecoration(decoration, SWT.LEFT | SWT.TOP, true);

// create a content proposal provider that simply provides some funny old German
// names
SimpleContentProposalProvider sampleNames = new SimpleContentProposalProvider(
new String[] { "Wernfried", "Irmhelm", "Hernmut", "Gottlob" });
// create a F1 keystroke, this can probably be done more elegant
KeyStroke f1Stroke = null;
try {
f1Stroke = KeyStroke.getInstance("F1");
} catch (ParseException e1) {
// hope the KeyStroke.getInstance() works :)
}

// add a proposal adapter to our text field
ContentProposalAdapter adapter = new ContentProposalAdapter(text,
new TextContentAdapter(), sampleNames, f1Stroke, null);

// react on text changes and show error if needed
text.addModifyListener(new ModifyListener() {

public void modifyText(ModifyEvent e) {
if (text.getText().startsWith(" ")) {
field.showDecoration(decoration);
return;
}
field.hideDecoration(decoration);

}
});
field.hideDecoration(decoration);
shell.open();

The code above really just covers a simple implementation. There is much more behind the concept of decorated fields and much more flexibility. I am planing on writing a more in detail article or HOWTO about decorated fields.

3 Comments:

Blogger Neil Bartlett said...

Looks very cool. Do you know if I can use this with the Forms API as well?

2:28 PM

 
Blogger Philipp said...

Hi Neil,

I haven't tried it yet, but I am almost 100 % sure it works. My insight of the Forms API is not the deepest but my understanding is that the controls are the same SWT controls, only produced by a toolkit that cares for the right flat look, background, and foreground colour. So yes, it should work.

Philipp

7:08 AM

 
Blogger xiaonanok said...

Welcome to our website for you World of Warcraft Gold,Wow Gold,Cheap World of Warcraft Gold,cheap wow gold,buy cheap wow gold,real wow gold,sell wow gold, ...Here wow gold of 1000 gold at $68.99-$80.99 ,World Of Warcraft Gold,world of warcraft gold buy wow gold,sell world of warcraft gold(wow gold),buy euro gold wow Cheap wow gold,cheapest wow gold store ... buy euro gold wow wow gold--buy cheap wow gold,sell wow gold.welcome to buy cheap wow gold--cheap, easy,world of warcraft gold wow gold purchasing.World of Warcraft,wow gold Super ...
We can have your wow gold,buy wow gold,wow gold game,world of warcraft gold, wow Gold Cheap wow, Cheap wow gold,world of warcraft gold deal,Cheap WOW Gold ...

Welcome to our website for you World of Warcraft Gold,Wow Gold,Cheap World of Warcraft Gold,wow gold,buy cheap wow gold,real wow gold,sell wow gold, ...
Here wow gold of 1000 gold at $68.99-$80.99,World Of Warcraft Gold,gold wow buy wow gold,sell world of warcraft gold(wow gold),buy gold wow lightninghoof instock Cheap wow gold,cheapest wow gold store ...
wow gold--buy cheap wow gold,sell wow gold.welcome to buy cheap wow gold--cheap, easy, wow gold purchasing.World of Warcraft,wow gold Super ...
Wow gold- Gold for buy gold wow lightninghoof instock EU-Server: ...wow Gold EU: starting from 84,99?; 3000 WoW Gold EU: starting from 119,99?. wow Gold- Leveling Services: ...
We can have your wow Gold,buy wow Gold,gold wow wow Gold game,wow gold, Cheap wow Gold, Cheap World of Warcraft Gold,world of warcraft gold deal,buy cheap wow gold,Cheap WOW Gold ...

Here wow Gold of 1000 gold at $68.99-$80.99,World Of Warcraft Gold,buy wow Gold,sell world of warcraft gold(wow gold),Cheap wow gold,cheapest World of Warcraft Gold store ...

9:00 AM

 

Post a Comment

Links to this post:

Create a Link

<< Home