package swtbot;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
public class AddDialog extends Dialog {
private Text text1;
private Text text2;
private Label resultLabel;
/**
* Create the dialog.
* @param parentShell
*/
public AddDialog(Shell parentShell) {
super(parentShell);
}
/**
* Create contents of the dialog.
* @param parent
*/
@Override
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
container.setLayout(new GridLayout(5, false));
Label lblAddCalculation = new Label(container, SWT.NONE);
lblAddCalculation.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 5, 1));
lblAddCalculation.setText("Add Calculation");
text1 = new Text(container, SWT.BORDER);
GridData gd_add1Text = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
gd_add1Text.widthHint = 50;
text1.setLayoutData(gd_add1Text);
text1.setData("id", "text1");
text1.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
updateResult();
}
});
Label label = new Label(container, SWT.NONE);
label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
label.setText("+");
text2 = new Text(container, SWT.BORDER);
GridData gd_add2Test = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
gd_add2Test.widthHint = 50;
text2.setLayoutData(gd_add2Test);
text2.setData("id", "text2");
text2.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
updateResult();
}
});
Label label_1 = new Label(container, SWT.NONE);
label_1.setText("=");
resultLabel = new Label(container, SWT.NONE);
resultLabel.setData("id", "result");
resultLabel.setText("No Answer");
return container;
}
protected void updateResult() {
if (text1.getText().isEmpty() || text2.getText().isEmpty()) {
return;
}
int result = Integer.parseInt(text1.getText()) + Integer.parseInt(text2.getText());
resultLabel.setText(Integer.toString(result));
}
/**
* Create contents of the button bar.
* @param parent
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
}
/**
* Return the initial size of the dialog.
*/
@Override
protected Point getInitialSize() {
return new Point(266, 135);
}
}