program | Design 1, Introduction
· Eclipse form is a new feature of Eclipse 3.0
· The Eclipse form is a set of custom widgets and support classes that were used internally by the PDE and update components and have become public APIs in Eclipse 3.0
· The Eclipse form provides:
* the "Form" concept for inclusion in the content area (editor or view)
* A toolkit for managing colors, hyperlink groups, and other form appearance like SWT controls
* New layout manager layout like HTML table
* Custom controls designed for form (hyperlinks, image links, scrollable composite, etc.)
* Each page is a multiple-page editor for a form (just like PDE)
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 ();
}
}
• First create a Formtoolkit object instance
• Create a Form object from Formtoolkit (this is scrolledform)
• Invoke the Scrolledform SetText () method to set the caption content at the top of the form
• Note: The last Formtoolkit object to dispose of management resources
• To run in Workbench, you need to add org.eclipse.ui.forms to the list of required plug-ins in Plugin.xml, and register the view
>
Id= "Formsamples"
Name= "Formsamples Plug-in"
version= "1.0.0"
Provider-name= "Nelson_tu"
Class= "Org.xqtu.samples.FormSamplesPlugin" >
Point= "Org.eclipse.ui.views" >
Class= "Org.xqtu.samples.views.FormView"
Name= "Form Sample"
Id= "FormView"/>
(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 get the body content of the form, which is a composite object
• Set its layout to GridLayout
• Create a hyperlink (Hyperlink) control through Formtoolkit
• Add a hyperlink event listener that responds to a hyperlink click
(3) Adding common controls
• Allows an SWT control to be created because the body content of the form is a composite object
• But SWT controls are designed to fit into Windows, dialog boxes, so using in form is problematic
• In form, use Formtoolkit to create the corresponding generic 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 example above adds three common controls: Label, text, and checkbox
• Because the text control created by default is 3D in appearance, you need to do some extra work to achieve a flat appearance like PDE:
1) Call the SetData () method to add additional information about the redraw border
2) Call the Formtoolkit Paintbordersfor () method to redraw the border of the flat skin