diff options
author | Scott Kovatch <skovatch> | 2010-10-29 20:30:06 +0000 |
---|---|---|
committer | Scott Kovatch <skovatch> | 2010-10-29 20:30:06 +0000 |
commit | 2a57b8ad73e3d542a02de871ad2f826671e46cb7 (patch) | |
tree | 41676f803fffff70fcb0bb315abc58d7c20e4c10 | |
parent | 713efae39c9d57849849c3c53d4837d1fcbd0c59 (diff) | |
download | eclipse.platform.swt-2a57b8ad73e3d542a02de871ad2f826671e46cb7.tar.gz eclipse.platform.swt-2a57b8ad73e3d542a02de871ad2f826671e46cb7.tar.xz eclipse.platform.swt-2a57b8ad73e3d542a02de871ad2f826671e46cb7.zip |
More complex example using getAppMenuBar.
-rw-r--r-- | examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet348.java | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet348.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet348.java new file mode 100644 index 0000000000..8e322c134b --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet348.java @@ -0,0 +1,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 getAppMenuBar() (compared to Snippet347) + * Shows how to write a more cross-platform multi-window application using getAppMenuBar(). + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ + +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().getAppMenuBar(); + 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.getAppMenuBar(); + 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(); + } + } +}
\ No newline at end of file |