使用DecoratedField装饰SWT Control

DecoratedField 用来实现对 Control 的装饰,可以为 button,combo,text 等控件做出如下效果。
使用DecoratedField装饰SWT Control
控件边上提示的种类有4种,分别是 Proposal, Required, error, warning.
代码很简单:
package jface;

import org.eclipse.jface.fieldassist.DecoratedField;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.jface.fieldassist.IControlCreator;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/***/ /**
*ThisclassdemonstratesTableViewers
*/

public class FieldDecorationDemo ... {

publicvoidrun()...{
Displaydisplay
=newDisplay();
Shellshell
=newShell(display);
shell.setSize(
200,200);
shell.setText(
"ButtonExample");
shell.setLayout(
newGridLayout());

createFieldDecoration(shell);

shell.open();
while(!shell.isDisposed())...{
if(!display.readAndDispatch())
display.sleep();
}

display.dispose();
}


privatevoidcreateFieldDecoration(Shellshell)...{
createCombo(shell);
createButton(shell);
createText(shell);
}


privatevoidcreateText(Shellshell)...{
IControlCreatorcbCtrl
=newIControlCreator()...{

publicControlcreateControl(finalCompositeparent,finalintstyle)...{
Texttext
=newText(parent,style);
text.setText(
"text");
returntext;
}

}
;
DecoratedFielddField
=newDecoratedField(shell,SWT.BORDER,cbCtrl);
FieldDecorationdecoration
=FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_ERROR);
dField.addFieldDecoration(decoration,SWT.LEFT
|SWT.TOP,false);
}


privatevoidcreateButton(Shellshell)...{
IControlCreatorcbCtrl
=newIControlCreator()...{

publicControlcreateControl(finalCompositeparent,finalintstyle)...{
Buttonbutton
=newButton(parent,style);
button.setText(
"button");
returnbutton;
}

}
;
DecoratedFielddField
=newDecoratedField(shell,SWT.BORDER,cbCtrl);
FieldDecorationdecoration
=FieldDecorationRegistry.getDefault().getFieldDecoration(
FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
dField.addFieldDecoration(decoration,SWT.LEFT
|SWT.TOP,false);
}


privatevoidcreateCombo(Shellshell)...{
IControlCreatorcbCtrl
=newIControlCreator()...{

publicControlcreateControl(finalCompositeparent,finalintstyle)...{
Combocb
=newCombo(parent,style);
cb.setText(
"combo");
returncb;
}

}
;
DecoratedFielddField
=newDecoratedField(shell,SWT.BORDER,cbCtrl);
FieldDecorationdecoration
=FieldDecorationRegistry.getDefault()
.getFieldDecoration(FieldDecorationRegistry.DEC_REQUIRED);
dField.addFieldDecoration(decoration,SWT.RIGHT
|SWT.TOP,false);
}


publicstaticvoidmain(String[]args)...{
newFieldDecorationDemo().run();
}

}

你可能感兴趣的:(Field)