1. Introduction
Eclipse Form is a new feature of Eclipse 3.0.
Eclipse Form is a set of custom widgets and support classes. It was previously used internally by the pdpa and Update components and has become a public API in Eclipse 3.0.
Eclipse Form provides:
· Applicable to the concept of "Form" in the content area (editor or view)
· Toolkit used to manage colors, hyperlink groups, and other Form appearances like SWT controls
· New layout manager with the same layout as HTML tables
· Custom controls (hyperlinks, image links, and scrollable composite) designed for Form)
· Each page is a Form multi-page editor (just like the partial code)
2. Quick Start
(1) HelloWorld example
The following example creates an empty Form in the view.
public class FormView extends ViewPart {
private FormToolkit toolkit;
private ScrolledForm form;
public void createPartControl(Composite parent) {
toolkit = new FormToolkit(parent.getDisplay());
form = toolkit.createScrolledForm(parent);
form.setText("Hello, Eclipse Forms");
}
public void setFocus() {
form.setFocus();
}
public void dispose() {
toolkit.dispose();
super.dispose();
}
}
· Create a FormToolkit object instance
· Create a Form object by FormToolkit (ScrolledForm)
· Call the setText () method of ScrolledForm to set the title content at the top of Form
· Note: The final FormToolkit object for dispose Resource Management
· To run in Workbench, add org. eclipse. ui. forms to the list of required plug-ins and register the view in plugin. xml.
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin
id="FormSamples"
name="FormSamples Plug-in"
version="1.0.0"
provider-name="nelson_tu"
class="org.xqtu.samples.FormSamplesPlugin">
<runtime>
<library name="FormSamples.jar">
<export name="*"/>
</library>
</runtime>
<requires>
<import plugin="org.eclipse.ui"/>
<import plugin="org.eclipse.core.runtime"/>
<import plugin="org.eclipse.ui.forms"/>
</requires>
<extension
point="org.eclipse.ui.views">
<view
class="org.xqtu.samples.views.FormView"
name="Form Sample"
id="FormView"/>
</extension>
</plugin>
(2) Add content
public void createPartControl(Composite parent) {
toolkit = new FormToolkit(parent.getDisplay());
form = toolkit.createScrolledForm(parent);
form.setText("Hello, Eclipse Forms");
Composite body = form.getBody();
GridLayout layout = new GridLayout();
body.setLayout(layout);
Hyperlink link = toolkit.createHyperlink(body, "Click here.",SWT.WRAP);
link.addHyperlinkListener(new HyperlinkAdapter() {
public void linkActivated(HyperlinkEvent e) {
System.out.println("Link activated!");
}
});
}
· First obtain the Form body content, which is a Composite object
· Set its layout to GridLayout.
· Create a Hyperlink Control Using FormToolkit
· Add a hyperlink event listener to respond to hyperlink clicks
(3) Add a universal control
· Because the Form body content is a Composite object, you can create a SWT control in it.
· However, the SWT control is designed to be suitable for Windows and dialog boxes, so it is problematic to use it in Form.
In Form, use FormToolkit to create the corresponding universal control
public void createPartControl(Composite parent) {
toolkit = new FormToolkit(parent.getDisplay());
form = toolkit.createScrolledForm(parent);
form.setText("Hello, Eclipse Forms");
Composite body = form.getBody();
GridLayout layout = new GridLayout();
body.setLayout(layout);
Hyperlink link = toolkit.createHyperlink(body, "Click here.",SWT.WRAP);
link.addHyperlinkListener(new HyperlinkAdapter() {
public void linkActivated(HyperlinkEvent e) {
System.out.println("Link activated!");
}
});
layout.numColumns = 2;
GridData gd = new GridData();
gd.horizontalSpan = 2;
link.setLayoutData(gd);
Label label = toolkit.createLabel(body, "Text field label:");
Text text = toolkit.createText(body, "");
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
text.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
Button button = toolkit.createButton(body,"An example of a checkbox in a form", SWT.CHECK);
gd = new GridData();
gd.horizontalSpan = 2;
button.setLayoutData(gd);
toolkit.paintBordersFor(body);
}
The preceding example adds three general controls: Label, Text, and CheckBox.
Since the default Text control is 3D, to achieve the same FLAT appearance as the pdat, you need to do some additional work:
· Call the setData () method to add additional information for the redraw border
· Call the paintBordersFor () method of FormToolkit to re-draw the FLAT appearance border