import java.util.ArrayList;
public class Test2 extends Composite {
private Text text1;
private Text text2;
private Button button;
/**
* Create the composite.
*
* @param parent
* @param style
*/
public Test2(Composite parent, int style) {
super(parent, style);
setLayout(new GridLayout(3, false));
Group grpLinks = new Group(this, SWT.NONE);
GridData gd_grpLinks = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
gd_grpLinks.widthHint = 100;
grpLinks.setLayoutData(gd_grpLinks);
grpLinks.setText("links");
Group grpMitte = new Group(this, SWT.NONE);
grpMitte.setLayout(new GridLayout(1, false));
grpMitte.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
grpMitte.setText("mitte");
text1 = new Text(grpMitte, SWT.BORDER);
text1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
text1.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {}
@Override
public void focusGained(FocusEvent arg0) {
button.setVisible(false);
}
});
text1.setFocus();
text2 = new Text(grpMitte, SWT.BORDER);
text2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
text2.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {}
@Override
public void focusGained(FocusEvent arg0) {
button.setVisible(true);
}
});
button = new Button(grpMitte, SWT.NONE);
button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
showDialog(Test2.this);
}
});
button.setText("Sichtbar, bei Fokus auf Textfeld 2");
button.setVisible(false);
Group grpRechts = new Group(this, SWT.NONE);
GridData gd_grpRechts = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
gd_grpRechts.widthHint = 100;
grpRechts.setLayoutData(gd_grpRechts);
grpRechts.setText("rechts");
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
private void showDialog(Composite parent) {
Point controlLocation = button.getLocation();
Point displayLocation = button.toDisplay(controlLocation);
displayLocation.y -= 40;
Shell dialog = new Shell(parent.getDisplay(), SWT.TOOL | SWT.CLOSE);
dialog.setLocation(displayLocation);
dialog.setSize(200, 200);
dialog.setText("Dialog");
dialog.setLayout(new GridLayout(1, false));
ListViewer viewer = new ListViewer(dialog);
viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
viewer.setContentProvider(new ArrayContentProvider());
viewer.setLabelProvider(new LabelProvider());
List<String> variables = new ArrayList<>();
variables.add("Broccoli");
variables.add("Bundt Cake");
variables.add("Cabbage");
variables.add("Candy Canes");
variables.add("Eggs");
variables.add("Potato Chips");
variables.add("Milk");
variables.add("Soda");
variables.add("Chicken");
variables.add("Cinnamon Rolls");
viewer.setInput(variables);
dialog.open();
}
}