Index: src/org/eclipse/swt/snippets/Snippet67.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet67.java,v retrieving revision 1.3 diff -u -r1.3 Snippet67.java --- src/org/eclipse/swt/snippets/Snippet67.java 16 Sep 2005 19:24:37 -0000 1.3 +++ src/org/eclipse/swt/snippets/Snippet67.java 2 Jul 2007 10:42:05 -0000 @@ -32,17 +32,7 @@ item.setText ("Item " + i); } final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN); - item.addListener (SWT.Selection, new Listener () { - public void handleEvent (Event event) { - if (event.detail == SWT.ARROW) { - Rectangle rect = item.getBounds (); - Point pt = new Point (rect.x, rect.y + rect.height); - pt = toolBar.toDisplay (pt); - menu.setLocation (pt.x, pt.y); - menu.setVisible (true); - } - } - }); + item.setMenu(menu); toolBar.pack (); shell.pack (); shell.open (); Index: src/org/eclipse/swt/snippets/Snippet67x.java =================================================================== RCS file: src/org/eclipse/swt/snippets/Snippet67x.java diff -N src/org/eclipse/swt/snippets/Snippet67x.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/swt/snippets/Snippet67x.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,114 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * ToolBar example snippet: place a drop down menu in a tool bar + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import java.util.Date; +import java.util.Random; + +import org.eclipse.swt.*; +import org.eclipse.swt.events.MenuDetectEvent; +import org.eclipse.swt.events.MenuDetectListener; +import org.eclipse.swt.events.MouseAdapter; +import org.eclipse.swt.events.MouseEvent; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet67x { + +public static void main (String [] args) { + final Display display = new Display (); + final Shell shell = new Shell (display); + final ToolBar toolBar = new ToolBar (shell, SWT.FLAT); + toolBar.addMouseListener(new MouseAdapter() { + + @Override + public void mouseDown(MouseEvent e) { + long time = e.time & 0xFFFFFFFFL; + System.out.println("mousedown " + new Date(time)); + } + + @Override + public void mouseDoubleClick(MouseEvent e) { + long time = e.time & 0xFFFFFFFFL; + System.out.println("mousedblclick"); + } + + @Override + public void mouseUp(MouseEvent e) { + long time = e.time & 0xFFFFFFFFL; + System.out.println("mouseup " + new Date(time)); + } + + }); + final Menu menu = new Menu (shell, SWT.POP_UP); + for (int i=0; i<8; i++) { + MenuItem item = new MenuItem (menu, SWT.PUSH); + item.setText ("Item " + i); + } + final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN); + item.setText("My Item"); + item.setMenu(menu); + final ToolItem item2 = new ToolItem (toolBar, SWT.PUSH, 0); + item2.setText("theone"); + item2.addSelectionListener(new SelectionListener() { + + public void widgetSelected(SelectionEvent e) { + System.out.println("sel"); + } + + public void widgetDefaultSelected(SelectionEvent e) { + System.out.println("default sel"); + } + + }); + item2.addMenuDetectListener(new MenuDetectListener(){ + + public void menuDetected(MenuDetectEvent event) { + if (false) { + event.doit = false; + } else { + if (item2.getMenu() != null) { + item2.getMenu().dispose(); + } + final Menu menu = new Menu (shell, SWT.POP_UP); + for (int i=0; i<8; i++) { + MenuItem item = new MenuItem (menu, SWT.PUSH); + item.setText ("Item " + new Random().nextInt(10)); + } + item2.setMenu(menu); + } + } + + }); + toolBar.pack (); + + Button btn = new Button(shell, SWT.PUSH); + btn.setLocation(120, 00); + btn.setText("Hello"); + btn.pack(); + + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + menu.dispose (); + display.dispose (); +} +}