SWT forces us to write more lines of code and specify more parameters than necessary.
Here is one simple example:
Label label = new Label(parent, SWT.NONE);
label.setText("labelname");
First questions that come up are:
- Why do I have to specify allways the style parameter when it is in 99.9% of all cases SWT.NONE.
- Why do I have to create a reference 'label' and set the text of the Label in a second line and not in the constructor.
The guys from SWT missed out the chance to make there classes convenient with one or two more constructors for every widget. Next code snippet shows the minimum constructor for the Label class.
new Label(parent, "labelname");
So we have to do something. A convenient method is the answer. Look how efficient the code is. We save more than half of the key strokes.
The idea is actually using the factory method pattern from the Gof (Gang of Four) in a not so narrow sense.
createLabel(parent, "labelname");
Here a more complex example. This is a typical task for the UI. A label and text field, multiline textfield where the label is on top of its column and another multiline textfield with two labels. First could be the name an the second an explanation.
The usual code:
Group group0 = new Group(parent, SWT.NONE);
group0.setText("group0");
group0.setLayout( new GridLayout(2, false));
group0.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Label label0 = new Label(group0, SWT.NONE);
label0.setText("label0");
Text text0 = new Text(group0, SWT.BORDER);
text0.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Label label1 = new Label(group0, SWT.NONE);
label1.setText("label1");
label1.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
Text text1 = new Text(group0, SWT.V_SCROLL | SWT.MULTI | SWT.WRAP | SWT.BORDER);
GridData gd1 = new GridData(SWT.FILL, SWT.CENTER, true, false);
gd1.heightHint = 50;
text1.setLayoutData(gd1);
Label label2 = new Label(group0, SWT.NONE);
label2.setText("label2");
label2.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
Text text2 = new Text(group0, SWT.V_SCROLL | SWT.MULTI | SWT.WRAP | SWT.BORDER);
GridData gd2 = new GridData(SWT.FILL, SWT.CENTER, true, false);
gd2.heightHint = 50;
gd2.verticalSpan = 2;
text2.setLayoutData(gd2);
Label label3 = new Label(group0, SWT.NONE);
label3.setText("label3");
label3.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
The convenient code:
Group group1 = createGroup(parent, "group1", 2);
createLabel(group1, "label0");
createText(group1);
createLabelTop(group1, "label1");
createTextField(group1, 0, 50);
createLabelTop(group1, "label2");
createTextField(group1, 0, 50, 1, 2);
createLabelTop(group1, "label3");
For this purpose I created CompositeBase and GroupBase classes to extend from. If you just want to have the create methods use the WidgetHelper.
The WidgetHelper implements everything as static methods. With a static import for this class, you don't have to prefix every method with the class name.
import static org.strangewt.widgets.WidgetHelper.*;
But be aware, Eclipse breaks down a static import when using 'organise imports' Ctrl + Shift + O.
To fix this problem read this.
No comments:
Post a Comment