SWT 给工具栏关联弹出菜单的工具类

SWT 给工具栏关联弹出菜单的工具类

从 SWT Snippets 里提取出来的, 虽然 JFace 里有更好的 MenuManager 之类的, 但是这个对 SWT 就可以用了.

 

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.ToolItem;


public class SWTUtil {
/**
* Assign a popup menu to a widget.
* Whenever the widget is selected, the popup menu will displayed.
* @param parent
* @param popupMenu
*/
public static void assignPopupMenuToWidget(final ToolItem parent, final Menu popupMenu) {
parent.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
widgetSelected(e);
}
public void widgetSelected(SelectionEvent evt) {
// if (evt.detail == SWT.ARROW) {
Rectangle b = parent.getBounds();
Point pt = new Point(b.x, b.y + b.height);
pt = parent.getParent().toDisplay(pt);
popupMenu.setLocation(pt.x, pt.y);
popupMenu.setVisible(true);
// }
}
});
}
}

你可能感兴趣的:(SWT 给工具栏关联弹出菜单的工具类)