summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet348.java
blob: 7267f3821260324a454c7815bd331c513d30a091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************************
 * Copyright (c) 2010 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;

/*
 * Display snippet: slightly more complex example for Display.getMenuBar() (compared to Snippet347)
 * Shows how to write a more cross-platform multi-window application using Display.getMenuBar().
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.7
 */
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;

public class Snippet348 {
	
	static boolean createdScreenBar = false;
	
	static void createMenuBar(Shell s) {
		Menu bar = Display.getCurrent().getMenuBar();
		boolean hasAppMenuBar = (bar != null);
		if (bar == null) {
			bar = new Menu(s, SWT.BAR);
		} 
		
		// Populate the menu bar once if this is a screen menu bar.
		// Otherwise, we need to make a new menu bar for each shell.
		if (!createdScreenBar || !hasAppMenuBar) {
			MenuItem item = new MenuItem(bar, SWT.CASCADE);
			item.setText("File");
			Menu menu = new Menu(item);
			item.setMenu(menu);
			menu.addMenuListener(new MenuListener() {

				public void menuHidden(MenuEvent e) {
					System.out.println("Menu closed: " + e);
				}

				public void menuShown(MenuEvent e) {
					System.out.println("Menu open: " + e);
				}
				
			});
			MenuItem newWindow = new MenuItem(menu, SWT.PUSH);
			newWindow.setText("New Window");
			newWindow.setAccelerator(SWT.MOD1 | 'N');
			newWindow.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					Shell s = createShell();
					s.open();
				}
			});
			if (!SWT.getPlatform().equals("cocoa")) {
				MenuItem exit = new MenuItem(menu, SWT.PUSH);
				exit.setText("Exit");
				exit.addListener(SWT.Selection, new Listener() {
					public void handleEvent(Event event) {
						Display d = Display.getCurrent();
						Shell[] shells = d.getShells();
						for (Shell shell : shells) {
							shell.close();
						}
					}
				});
			}
			if (!hasAppMenuBar) s.setMenuBar(bar);
			createdScreenBar = true;
		}
	}
	
	static Shell createShell() {
		final Shell shell = new Shell(SWT.SHELL_TRIM);
		createMenuBar(shell);
		
		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				Display d = Display.getCurrent();
				Menu bar = d.getMenuBar();
				boolean hasAppMenuBar = (bar != null);
				if (!hasAppMenuBar) {
					shell.getMenuBar().dispose();
					Shell[] shells = d.getShells();
					if ((shells.length == 1) && (shells[0] == shell)) {
						if (!d.isDisposed()) d.dispose();
					}
				}					
			}
		});
		
		return shell;
	}
	
	public static void main(String[] args) {
		final Display display = new Display();
		
		Shell shell = createShell();
		shell.open();
		
		while (!display.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}