/******************************************************************************* * Copyright (c) 2007, 2009 compeople AG and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * compeople AG - initial API and implementation *******************************************************************************/ import org.eclipse.jface.action.Action; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.CoolBar; import org.eclipse.swt.widgets.CoolItem; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.ToolBar; import org.eclipse.swt.widgets.ToolItem; /** * Snippet for discussing Bug 281433. */ public class Snippet281433 { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout(SWT.VERTICAL)); Label logo = new Label(shell, SWT.NONE); logo.setBackground(logo.getDisplay().getSystemColor(SWT.COLOR_CYAN)); logo.setText("Something else like a logo does here.\nMenu goes beneath"); CoolBar coolBar = new CoolBar(shell, SWT.FLAT); coolBar.setLocked(true); CoolItem coolItem = new CoolItem(coolBar, SWT.NONE); ToolBar toolBar = new ToolBar(coolBar, SWT.FLAT); coolItem.setControl(toolBar); String[] menus = { "&File", "&Edit", "&Source" }; for (String s : menus) { ToolItem toolItem = new ToolItem(toolBar, SWT.RADIO); toolItem.setText(s); Menu menu = createMenu(toolItem, s + ": "); new TopItemListener(menu, toolItem); } display.addFilter(SWT.KeyUp, new Listener() { public void handleEvent(Event event) { System.out.println("KeyUp: " + event); } }); coolItem.setSize(coolItem.computeSize(SWT.DEFAULT, SWT.DEFAULT)); shell.setSize(300, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } // helping methods ////////////////// private static Action createAction(final String caption) { Action result = new Action() { public void run() { System.out.println(caption); }; }; result.setText(caption); return result; } private static Menu createMenu(ToolItem toolItem, String prefix) { Menu result = new Menu(toolItem.getParent()); String[] items = { "Item 1", "Item 2", "Item 3", "Item 4" }; for (String s : items) { MenuItem item = new MenuItem(result, SWT.NONE); item.setText(prefix + s); } result.setVisible(false); return result; } // helping classes ////////////////// private static class TopItemListener extends SelectionAdapter { private final ToolItem toolItem; private final Menu menu; public TopItemListener(Menu menu, ToolItem toolItem) { this.menu = menu; this.toolItem = toolItem; this.toolItem.addSelectionListener(this); } public void widgetSelected(SelectionEvent e) { System.out.println(e); if (e.getSource() == toolItem) { if (toolItem.getSelection()) { Rectangle bounds = toolItem.getBounds(); Point loc = toolItem.getParent().toDisplay(bounds.x, bounds.height + bounds.y); menu.setLocation(loc); menu.setVisible(true); } else { menu.setVisible(false); } } } } }