import java.awt.Dimension;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ApplicationWithPopupWindow {
protected Shell shell;
* Launch the application.
public static void main(String[] args) {
try {
ApplicationWithPopupWindow window = new ApplicationWithPopupWindow();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
* Open the window.
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
* Create contents of the window.
protected void createContents() {
shell = new Shell();
shell.setLayout(new BorderLayout(0, 0));
shell.setSize(450, 300);
shell.setText("SWT Application");
{
Button btnClickToOpen = new Button(shell, SWT.NONE);
btnClickToOpen.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.err.println("button pressed");
PopupWindow popup = new PopupWindow(shell);
Button btn = new Button(popup.getContentComposite(),
SWT.NONE);
btn.setText("Close this modal window");
btn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.err.println();
((Button) e.getSource()).getShell().close();
}
});
popup.openBelow(e.getSource());
}
});
btnClickToOpen.setLayoutData(BorderLayout.CENTER);
btnClickToOpen.setText("click to open a modal window");
}
}
private class PopupWindow {
protected Shell popupShell;
protected Composite contentComposite;
public PopupWindow(Shell parent) {
//
popupShell = new Shell(parent, SWT.RESIZE);
popupShell.addShellListener(new ShellListener() {
@Override
public void shellIconified(ShellEvent e) {
// TODO Auto-generated method stub
}
@Override
public void shellDeiconified(ShellEvent e) {
// TODO Auto-generated method stub
}
@Override
public void shellDeactivated(ShellEvent e) {
// TODO Auto-generated method stub
((Shell) e.getSource()).close();
}
@Override
public void shellClosed(ShellEvent e) {
// TODO Auto-generated method stub
}
@Override
public void shellActivated(ShellEvent e) {
// TODO Auto-generated method stub
}
});
popupShell.setLayout(new BorderLayout(0, 0));
popupShell.setSize(200, 300);
contentComposite = new Composite(popupShell, SWT.NONE);
}
public final Composite getContentComposite() {
return contentComposite;
}
public void open() {
popupShell.open();
}
public void setLocation(Point location) {
popupShell.setLocation(location);
}
public void openAtMousePosition() {
popupShell.setLocation(MouseInfo.getPointerInfo().getLocation().x,
MouseInfo.getPointerInfo().getLocation().y);
}
// FIXME check java-forum.org and or decoration stuff
public void openBelow(Object element) {
Point abs = absolutePosition(element);
popupShell.setLocation(abs);
popupShell.open();
System.err.println("absolutePosition: " + abs.x + "/" + abs.y);
}
private Point absolutePosition(Object element) {
Dimension decorationSize = Windows.getDecorationSize();
int absX = decorationSize.width;
int absY = decorationSize.height;
if (element instanceof Control) {
absY += ((Control) element).getSize().y;
}
while (!(element instanceof Shell)) {
if (element instanceof Control) {
Control con = (Control) element;
Point ownPos = con.getLocation();
element = con.getParent();
absX += ownPos.x;
absY += ownPos.y;
} else {
System.out.println(element.getClass().getSimpleName());
}
}
if (element instanceof Shell) {
Shell sh = (Shell) element;
absX += sh.getLocation().x;
absY += sh.getLocation().y;
}
return new Point(absX, absY);
}
}
private static class Windows {
private static final Dimension DEFAULT = new Dimension(8, 30);
private static final Dimension Vista = new Dimension(8, 30);
private static final Dimension XP = new Dimension(5, 30);
public static Dimension getDecorationSize() {
String OsName = System.getProperty("os.name").toLowerCase();
if (OsName.contains("vista")) {
return Vista;
} else if (OsName.contains("xp")) {
return XP;
} else {
return DEFAULT;
}
}
}
}