Design
Add some content
Now we have a view that can run successfully. We can add some content to it. Eclipse forms has a body that we can create.
public void Createpartcontrol (composite parent) { toolkit = new Formtoolkit (Parent.getdisplay ()); Form = Toolkit.createform (parent); Form.settext ("Hello, Eclipse Forms"); GridLayout layout = new GridLayout (); Form.getbody (). setlayout (layout); Hyperlink link = toolkit.createhyperlink (form.getbody (), "Click here", SWT. WRAP); Link.addhyperlinklistener (New Hyperlinkadapter () {public void linkactivated (Hyperlinkevent e) { System.out.println ("Link activated!"); } );
The body of the form is the free space below the caption, because the space is an SWT composite that can be the parent of other components. In the code above, we set the layout for the body and then create a hyperlink. Hyperlinks are one of the few components provided by eclipse forms, and we can add listeners to hyperlinks to respond when the user clicks on them.
The upgraded view should look like this:
Figure 3: A simple form with a hyperlink.
Hyperlink Group (Hyperlink Groups)
Form Tookit has a hyperlink group object. Each created hyperlink is added to the Group object. Hyperlinks serve multiple roles. They define all the hyperlinks in this group in normal, hover, Activates colors in different states. They change the color according to the different state of the link in the group. They change the underline style according to the different state of the link in the group.
When you want to change the default settings for a hyperlink group object, you can use the Toolkit Gethyperlinkgroup () method to get the hyperlink group object.
To create a generic component
One of the design goals of Eclipse forms is to enable the creation of common SWT components in the editor/view. Because the body of form is an ordinary composite, you can use any layout and components inside it. But remember "native" The SWT component has a component background. We now create some components from their construction methods.
Layout.numcolumns = 2; Griddata gd = new Griddata (); Gd.horizontalspan = 2; Link.setlayoutdata (GD); Label label = new label (Form.getbody (), SWT. NULL); Label.settext ("Text field Label:"); Text text = new text (Form.getbody (), SWT. BORDER); Text.setlayoutdata (New Griddata (Griddata.fill_horizontal)); Button button = New button (Form.getbody (), SWT. CHECK); Button.settext ("A example of a checkbox in a form"); GD = new Griddata (); Gd.horizontalspan = 2; Button.setlayoutdata (GD);
We now use two columns, and we create a label, a text field, and a check box (checkbox). The results are as follows:
Figure 4: A form with an SWT component created directly with their constructors
What's wrong with this picture? The background of the component we created is directly matched with the system window background. Instead of matching the form's background. In addition, the text box looks good because the screenshot is truncated under Windows XP. On other operating systems, it looks like a blank bar with a 3D border. To solve this problem, We will use the Toolkit factory approach to create these components:
Label label = Toolkit.createlabel (Form.getbody (), "Text field Label:"); Text text = Toolkit.createtext (Form.getbody (), ""); Text.setlayoutdata (New Griddata (Griddata.fill_horizontal)); Button button = Toolkit.createbutton (Form.getbody (), "A checkbox in a form", SWT. CHECK); GD = new Griddata (); Gd.horizontalspan = 2; Button.setlayoutdata (GD);
This view will now look better:
Figure 5: A form with an SWT component created with the factory method of form Toolkit
The factory method provided by Form Toolkit is for convenience. Toolkit has no cover in all cases, even a collection of SWT components, and obviously does not provide this convenience for your own custom-made components. When you need to unify an SWT component with form, you should use a method: Formtoolkit.adapt (Control Control, Boolean Trackfocus,boolean trackkeyboard). All factory methods respectively call this adaptive method.
achieve a "smooth" visual effect
A visible property of Eclipse forms in the Pde editor is the "smooth" visual effect of the component. Components that previously had a 3D border look good in the window, but not in the editor or view. This support is written in the Formtoolkit class. On some systems it is achieved through some customization. For example, take a look at this screenshot from the PDE Editor (version 2.1):
The smooth visual effects of Eclipse forms in picture 6:eclipse 2.1.
Like tables, text classes, check boxes, and so on, are plus a smooth 1 pixel wide border. These borders are not from the component itself (SWT. Border style not used). In addition, if you set it up, Tookit adds a paint listener for each component's parent and borders the component when the paint event occurs. To do so, you need to create an image of the text, the table, Each composite of a component such as a tree calls Paintbordersfor (parent). Each parent only needs to call one method at a time: it does not need to be called for each component.
Form Toolkit knows which component needs a custom border. However, you may have created a new component that is not in the original list, and it also requires a border. You can give toolkit a hint by using code like the following:
Formtoolkit.text_border Formtoolkit.tree_border); Toolkit.paintbordersfor (parent);
You can see in the picture above, "structured (structural)" components like trees and tables have different border styles from the text area and you can choose which one matches your component. Note the components created with the Toolkit factory method do not need to be done.
Because Eclipse 3.0 and on Windows XP, there is no border generated when the Javaw.exe.manifest file is in the Java Virtual Machine Bin folder. (All of my screenshots in this article are truncated under Windows XP). This file--you can
SWT Home PageDownload--tells toolkit to use the XP skin for the local component. XP skins, like text, tables, and trees are already smooth and do not need to be adjusted. To make your code portable, you can always call the Paintbordersfor (composite) method, Let toolkit decide what to do according to the different operating systems.
Custom layout (custom layouts)
Eclipse forms adds two new layout based on SWT layout that inherit SWT layout base classes and can be used on any SWT composite, but are typically used in conjunction with Eclipse forms.
Tablewraplayout
Now we know how to combine a form, let's give a suspense first. We're going to change that hyperlink text to make it longer:
Link.settext ("This is a example of a form, that's much longer" +
"and would need to wrap.");
Let's take a look at the results:
Picture 7: A form using GridLayout
What happened? Remember that we're using GridLayout, and when it asks the hyperlink component to compute its size, the hyperlink tells it the length of the text in a separate line, and although we tell the component to go to the package (wrap), it doesn't do that because GridLayout needs the component to return its length. A hyperlink component--and other SWT components like label--can be used to determine the length and width of a value, but GridLayout does not pass numeric arguments to the component.
What we need is a layout like an HTML table. We want the content to try to match the provided customer space and stack the line. Eclipse forms provides one such layout called Tablewraplayout.gridlayout and tablewraplayout have a lot in common. It's all about using tables to organize parent's children. There's layout. Data to tell layout how to treat each component. All can be prompted to accept components when they need to occupy all the space.
However, they are completely different when they are laid out. Tablewraplayout begins with the column. It calculates the smallest, appropriate, and largest width of each column and uses this information to occupy space. It also tries to separate the columns as equitably as possible so that some components do not have extra space.
Let's use tablewraplayout to change the example (highlighted at the change):
public void Createpartcontrol (composite parent) {toolkit = new Formtoolkit (Parent.getdisplay ()); Form = Toolkit.createform (parent); Form.settext ("Hello, Eclipse Forms");tablewraplayout layout = new Tablewraplayout ();Form.getbody (). setlayout (layout); Hyperlink link = toolkit.createhyperlink (form.getbody (), "Click here", SWT. WRAP); Link.addhyperlinklistener (New Hyperlinkadapter () {public void linkactivated (Hyperlinkevent e) {System.out.println ("Link activated!"); } }); Link.settext ("This is a example of a form", very much longer and would need to wrap. "); Layout.numcolumns = 2;tablewrapdata td = New Tablewrapdata (); Td.colspan = 2; Link.setlayoutdata (TD);Label label = Toolkit.createlabel (Form.getbody (), "Text field Label:"); Text text = Toolkit.createtext (Form.getbody (), "");td = New Tablewrapdata (tablewrapdata.fill_grab); Text.setlayoutdata (TD);Button button = Toolkit.createbutton (Form.getbody (), "A checkbox in a form", SWT. CHECK);td = New Tablewrapdata (); Td.colspan = 2; Button.setlayoutdata (TD);}
We used the same concept as griddata. Some variables have different names (for example, colspan and rowspan,align and valign from HTML table attributes), But you can do the same thing--create a hyperlink and a button to occupy a two-column grid. Because the blanks (margins) and GridLayout are the same, the result will look the same, except that the hyperlinks will now wrap up:
Picture 8: A form using tablewraplayout
One of the main differences between Tablewraplayout and GridLayout is that you should stop to compute the vertical space. In the GridLayout, you typically make "deformable (rigid)" components in natural position and size and make "scalable" (flexible) "Component to occupy horizontal or vertical space. Instead, Tablewraplayout is working from top to bottom, and it holds all the components, and its work is complete. The concept of occupying horizontal space is still present (as shown above). But vertically, you can only be in the cell Select the fill unit when it is higher than the component, or select Top,middle or bottom vertical alignment.
You may notice a place that doesn't match what you just said: Tablewraplayout still has a grabvertical variable. But here's a variable that has a definite purpose here: When a component with a height is set to take up more than one row, Its height gives a condition that the vertical dimension already known, and that the component needs to remove the extra space between the multiple units it resides in.
In order to have a good result with tablewraplayout, determine that the component can approach the style (SWT). WRAP). The component customization components provided by Eclipse froms can be wrapped outside box. This is accomplished by implementing the Ilayoutextension interface:
Public interface Ilayoutextension {
/**
* Computes the minimum width of the parent. All widgets capable of word
* Wrapping should return the width of the longest word this cannot be
* Broken any further.
*
* @param the parent composite
* @param changed <code>true</code> If the cached information should be
* Flushed, <code>false</code> otherwise.
* @return The minimum width of the parent composite
*/
public int Computeminimumwidth (composite parent, Boolean changed);
/**
* Computes the maximum width of the parent. All widgets capable of word
* Wrapping should return the length of the entire text with wrapping
* turned off.
*
* @param the parent composite
* @param changed <code>true</code> if the cached information
* Should be flushed, <code>false</code> otherwise.
* @return The maximum width of the parent composite
*/
public int Computemaximumwidth (composite parent, Boolean changed);
}
The tablewraplayout itself implements this interface, This allows it to deal with the situation when Composites's layout is the children of this composite's parent. Another two methods can compute the two extremes-the apparent minimum width and maximum width when all the components are covered as wide as possible. The difference between the two is to make the extra space between the columns as small as possible To contribute to a fair space.
Let's see what the contribution space is. We'll recommend the code we've written so far and make changes like the following:
Layout.numcolumns = 3; Label label; Tablewrapdata TD; Label = Toolkit.createlabel (Form.getbody (), "Some text to put in", SWT. WRAP); Label = Toolkit.createlabel (Form.getbody (), Some text to put in the second column and make it a bit "+" longer so tha T we can-what happens with column + distribution. This text must is the longest so which it can "+" get more spaces allocated to the columns it belongs to. " WRAP); td = New Tablewrapdata (); Td.colspan = 2; Label.setlayoutdata (TD); Label = Toolkit.createlabel (Form.getbody (), "This text would span two rows and should not grow the column.", SWT. WRAP); td = New Tablewrapdata (); Td.rowspan = 2; Label.setlayoutdata (TD); Label = Toolkit.createlabel (Form.getbody (), "This text goes to column 2 and consumes only one cell", SWT. WRAP); Label.setlayoutdata (New Tablewrapdata (Tablewrapdata.fill_grab)); Label = Toolkit.createlabel (Form.getbody (), "This text goes into column 3 and ConsuMes only one cell too ", SWT. WRAP); Label.setlayoutdata (New Tablewrapdata (Tablewrapdata.fill)); Label = Toolkit.createlabel (Form.getbody (), "This text goes to column 2 and consumes only one cell", SWT. WRAP); Label.setlayoutdata (New Tablewrapdata (Tablewrapdata.fill_grab)); Label = Toolkit.createlabel (Form.getbody (), "This text goes to column 3 and consumes only one cell too", SWT. WRAP); Label.setlayoutdata (New Tablewrapdata (Tablewrapdata.fill)); Form.getbody (). SetBackground (Form.getbody (). Getdisplay (). Getsystemcolor (SWT. Color_widget_background));
This creates a number of labels with different lengths of text. Some labels occupy multiple columns, some occupy multiple lines. To make the test simpler, we set the background of the form to the component background so that the unit can be seen more easily. When we run the example, we get the following look:
The extra space left by the picture 9:tablewraplayout
The key is to compare the minimum width of the component with the maximum width. The greater the difference, the more obvious the result, the larger the gap in the column. The width occupied is the minimum width required by the component. Note that the 3rd column is slightly wider than the 2nd column, This is because the text in column 3rd is longer than the text in column 2nd. If you need to read the relevant theory of the layout, you can go here to the recommendations for HTML table Auto-layout.
Columnlayout
Eclipse forms Another custom layout is a variant of SWT rowlayout. If we place the children on the rowlayout vertically--by columns--and make all the components in the same column the same width, We will get multiple columns from the width of the component. However, the last column is clearly not filled-this is determined by the number of components. If you are in form, we will still see all the components in a column because rowlayout cannot be "vertically" wrapped. If we replace it with GridLayout, The number of columns is up to our own discretion.
In more complex forms, we need the number of columns to change according to the situation. In other words, we want the numbers to change according to the width of the form-it's possible to have more columns, Decreases the number when the width is reduced. And we want all the columns to be the same length as the newspaper layout. These requirements can be achieved through columnlayout.
Compared with tablewraplayout, Columnlayout is simpler. No complex settings are required. The only thing you need to set up is the range of columns, which defaults to 1 to 3. The following example shows a number of paragraphs with columnlayout (sections) form. Initially, you can place all paragraphs with just two columns. If we narrow the editor a little, layout will make them in the same column.
Picture 10: Use Columnlayout for all columns by column. Layout started out as two columns, but changed into a column by changing the space.