View | Details | Raw Unified | Return to bug 193318 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/swt/snippets/Snippet67.java (-11 / +1 lines)
Lines 32-48 Link Here
32
		item.setText ("Item " + i);
32
		item.setText ("Item " + i);
33
	}
33
	}
34
	final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN);
34
	final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN);
35
	item.addListener (SWT.Selection, new Listener () {
35
	item.setMenu(menu);
36
		public void handleEvent (Event event) {
37
			if (event.detail == SWT.ARROW) {
38
				Rectangle rect = item.getBounds ();
39
				Point pt = new Point (rect.x, rect.y + rect.height);
40
				pt = toolBar.toDisplay (pt);
41
				menu.setLocation (pt.x, pt.y);
42
				menu.setVisible (true);
43
			}
44
		}
45
	});
46
	toolBar.pack ();
36
	toolBar.pack ();
47
	shell.pack ();
37
	shell.pack ();
48
	shell.open ();
38
	shell.open ();
(-)src/org/eclipse/swt/snippets/Snippet67x.java (+114 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.swt.snippets;
12
13
/*
14
 * ToolBar example snippet: place a drop down menu in a tool bar
15
 *
16
 * For a list of all SWT example snippets see
17
 * http://www.eclipse.org/swt/snippets/
18
 */
19
import java.util.Date;
20
import java.util.Random;
21
22
import org.eclipse.swt.*;
23
import org.eclipse.swt.events.MenuDetectEvent;
24
import org.eclipse.swt.events.MenuDetectListener;
25
import org.eclipse.swt.events.MouseAdapter;
26
import org.eclipse.swt.events.MouseEvent;
27
import org.eclipse.swt.events.SelectionEvent;
28
import org.eclipse.swt.events.SelectionListener;
29
import org.eclipse.swt.graphics.*;
30
import org.eclipse.swt.widgets.*;
31
32
public class Snippet67x {
33
34
public static void main (String [] args) {
35
	final Display display = new Display ();
36
	final Shell shell = new Shell (display);
37
	final ToolBar toolBar = new ToolBar (shell, SWT.FLAT);
38
	toolBar.addMouseListener(new MouseAdapter() {
39
	
40
		@Override
41
		public void mouseDown(MouseEvent e) {
42
			long time = e.time & 0xFFFFFFFFL;
43
			System.out.println("mousedown " + new Date(time));
44
		}
45
46
		@Override
47
		public void mouseDoubleClick(MouseEvent e) {
48
			long time = e.time & 0xFFFFFFFFL;
49
			System.out.println("mousedblclick");
50
		}
51
52
		@Override
53
		public void mouseUp(MouseEvent e) {
54
			long time = e.time & 0xFFFFFFFFL;
55
			System.out.println("mouseup " + new Date(time));
56
		}
57
	
58
	});
59
	final Menu menu = new Menu (shell, SWT.POP_UP);
60
	for (int i=0; i<8; i++) {
61
		MenuItem item = new MenuItem (menu, SWT.PUSH);
62
		item.setText ("Item " + i);
63
	}
64
	final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN);
65
	item.setText("My Item");
66
	item.setMenu(menu);
67
	final ToolItem item2 = new ToolItem (toolBar, SWT.PUSH, 0);
68
	item2.setText("theone");
69
	item2.addSelectionListener(new SelectionListener() {
70
	
71
		public void widgetSelected(SelectionEvent e) {
72
			System.out.println("sel");
73
		}
74
	
75
		public void widgetDefaultSelected(SelectionEvent e) {
76
			System.out.println("default sel");
77
		}
78
	
79
	});
80
	item2.addMenuDetectListener(new MenuDetectListener(){
81
82
		public void menuDetected(MenuDetectEvent event) {
83
			if (false) {
84
				event.doit = false;
85
			} else {
86
				if (item2.getMenu() != null) {
87
					item2.getMenu().dispose();
88
				}
89
				final Menu menu = new Menu (shell, SWT.POP_UP);
90
				for (int i=0; i<8; i++) {
91
					MenuItem item = new MenuItem (menu, SWT.PUSH);
92
					item.setText ("Item " + new Random().nextInt(10));
93
				}
94
				item2.setMenu(menu);
95
			}
96
		}
97
98
	});
99
	toolBar.pack ();
100
	
101
	Button btn = new Button(shell, SWT.PUSH);
102
	btn.setLocation(120, 00);
103
	btn.setText("Hello");
104
	btn.pack();
105
106
	shell.pack ();
107
	shell.open ();
108
	while (!shell.isDisposed ()) {
109
		if (!display.readAndDispatch ()) display.sleep ();
110
	}
111
	menu.dispose ();
112
	display.dispose ();
113
}
114
} 

Return to bug 193318