aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaphael Moll <ralf@android.com>2013-01-15 19:44:29 -0800
committerRaphael Moll <ralf@android.com>2013-01-15 19:48:19 -0800
commit1fdcf5c83dd676358668b4934b19475af3ab4d6d (patch)
tree6c2e93a6bb1bd6b09bd1833b682bfa1dfbc8df00
parent082b71536f072ea7ab5887ec537a2d4062f1aaaa (diff)
downloadsdk-1fdcf5c83dd676358668b4934b19475af3ab4d6d.zip
sdk-1fdcf5c83dd676358668b4934b19475af3ab4d6d.tar.gz
sdk-1fdcf5c83dd676358668b4934b19475af3ab4d6d.tar.bz2
SDK: use prebuilts/devtools for swtmenubar
Change-Id: I776daa849cac4a277e1eed396e6fb7752d146a75
-rw-r--r--swtmenubar/Android.mk16
-rw-r--r--swtmenubar/src-darwin/com/android/menubar/internal/MenuBarEnhancerCocoa.java341
-rw-r--r--swtmenubar/src/com/android/menubar/IMenuBarCallback.java42
-rw-r--r--swtmenubar/src/com/android/menubar/IMenuBarEnhancer.java73
-rw-r--r--swtmenubar/src/com/android/menubar/MenuBarEnhancer.java241
-rw-r--r--swtmenubar/src/com/android/menubar/MenuBarEnhancer37.java153
6 files changed, 6 insertions, 860 deletions
diff --git a/swtmenubar/Android.mk b/swtmenubar/Android.mk
index 321a78a..865c277 100644
--- a/swtmenubar/Android.mk
+++ b/swtmenubar/Android.mk
@@ -12,25 +12,21 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-#
+
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
-LOCAL_SRC_FILES := $(call all-java-files-under, src)
-LOCAL_JAVA_RESOURCE_DIRS := src
-
-ifeq ($(HOST_OS),darwin)
-LOCAL_SRC_FILES += $(call all-java-files-under, src-$(HOST_OS))
-LOCAL_JAVA_RESOURCE_DIRS += src-$(HOST_OS)
-endif
+# The swtmenubar code has moved to tools/swt/swtmenubar.
+# The rule below uses the prebuilt swtmenubar.jar if found.
LOCAL_MODULE := swtmenubar
LOCAL_MODULE_TAGS := optional
-
LOCAL_JAVA_LIBRARIES := \
swt \
org.eclipse.jface_3.6.2.M20110210-1200
+LOCAL_PREBUILT_JAVA_LIBRARIES := \
+ ../../prebuilts/devtools/$(LOCAL_MODULE)/$(LOCAL_MODULE)$(COMMON_JAVA_PACKAGE_SUFFIX)
-include $(BUILD_HOST_JAVA_LIBRARY)
+include $(BUILD_HOST_PREBUILT)
diff --git a/swtmenubar/src-darwin/com/android/menubar/internal/MenuBarEnhancerCocoa.java b/swtmenubar/src-darwin/com/android/menubar/internal/MenuBarEnhancerCocoa.java
deleted file mode 100644
index 88d230f..0000000
--- a/swtmenubar/src-darwin/com/android/menubar/internal/MenuBarEnhancerCocoa.java
+++ /dev/null
@@ -1,341 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Eclipse Public License, Version 1.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.eclipse.org/org/documents/epl-v10.php
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * History:
- * Original code by the <a href="http://www.simidude.com/blog/2008/macify-a-swt-application-in-a-cross-platform-way/">CarbonUIEnhancer from Agynami</a>
- * with the implementation being modified from the <a href="http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.ui.cocoa/src/org/eclipse/ui/internal/cocoa/CocoaUIEnhancer.java">org.eclipse.ui.internal.cocoa.CocoaUIEnhancer</a>,
- * then modified by http://www.transparentech.com/opensource/cocoauienhancer to use reflection
- * rather than 'link' to SWT cocoa, and finally modified to be usable by the SwtMenuBar project.
- */
-
-package com.android.menubar.internal;
-
-import com.android.menubar.IMenuBarCallback;
-import com.android.menubar.IMenuBarEnhancer;
-
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.internal.C;
-import org.eclipse.swt.internal.Callback;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Event;
-import org.eclipse.swt.widgets.Listener;
-import org.eclipse.swt.widgets.Menu;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-
-public class MenuBarEnhancerCocoa implements IMenuBarEnhancer {
-
- private static final long kAboutMenuItem = 0;
- private static final long kPreferencesMenuItem = 2;
- // private static final long kServicesMenuItem = 4;
- // private static final long kHideApplicationMenuItem = 6;
- private static final long kQuitMenuItem = 10;
-
- static long mSelPreferencesMenuItemSelected;
- static long mSelAboutMenuItemSelected;
- static Callback mProc3Args;
-
- private String mAppName;
-
- /**
- * Class invoked via the Callback object to run the about and preferences
- * actions.
- * <p>
- * If you don't use JFace in your application (SWT only), change the
- * {@link org.eclipse.jface.action.IAction}s to
- * {@link org.eclipse.swt.widgets.Listener}s.
- * </p>
- */
- private static class ActionProctarget {
- private final IMenuBarCallback mCallbacks;
-
- public ActionProctarget(IMenuBarCallback callbacks) {
- mCallbacks = callbacks;
- }
-
- /**
- * Will be called on 32bit SWT.
- */
- @SuppressWarnings("unused")
- public int actionProc(int id, int sel, int arg0) {
- return (int) actionProc((long) id, (long) sel, (long) arg0);
- }
-
- /**
- * Will be called on 64bit SWT.
- */
- public long actionProc(long id, long sel, long arg0) {
- if (sel == mSelAboutMenuItemSelected) {
- mCallbacks.onAboutMenuSelected();
- } else if (sel == mSelPreferencesMenuItemSelected) {
- mCallbacks.onPreferencesMenuSelected();
- } else {
- // Unknown selection!
- }
- // Return value is not used.
- return 0;
- }
- }
-
- /**
- * Construct a new CocoaUIEnhancer.
- *
- * @param mAppName The name of the application. It will be used to customize
- * the About and Quit menu items. If you do not wish to customize
- * the About and Quit menu items, just pass <tt>null</tt> here.
- */
- public MenuBarEnhancerCocoa() {
- }
-
- public MenuBarMode getMenuBarMode() {
- return MenuBarMode.MAC_OS;
- }
-
- /**
- * Setup the About and Preferences native menut items with the
- * given application name and links them to the callback.
- *
- * @param appName The application name.
- * @param display The SWT display. Must not be null.
- * @param callbacks The callbacks invoked by the menus.
- */
- public void setupMenu(
- String appName,
- Display display,
- IMenuBarCallback callbacks) {
-
- mAppName = appName;
-
- // This is our callback object whose 'actionProc' method will be called
- // when the About or Preferences menuItem is invoked.
- ActionProctarget target = new ActionProctarget(callbacks);
-
- try {
- // Initialize the menuItems.
- initialize(target);
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
-
- // Schedule disposal of callback object
- display.disposeExec(new Runnable() {
- public void run() {
- invoke(mProc3Args, "dispose");
- }
- });
- }
-
- private void initialize(Object callbackObject)
- throws Exception {
-
- Class<?> osCls = classForName("org.eclipse.swt.internal.cocoa.OS");
-
- // Register names in objective-c.
- if (mSelAboutMenuItemSelected == 0) {
- mSelPreferencesMenuItemSelected = registerName(osCls, "preferencesMenuItemSelected:"); //$NON-NLS-1$
- mSelAboutMenuItemSelected = registerName(osCls, "aboutMenuItemSelected:"); //$NON-NLS-1$
- }
-
- // Create an SWT Callback object that will invoke the actionProc method
- // of our internal callback Object.
- mProc3Args = new Callback(callbackObject, "actionProc", 3); //$NON-NLS-1$
- Method getAddress = Callback.class.getMethod("getAddress", new Class[0]);
- Object object = getAddress.invoke(mProc3Args, (Object[]) null);
- long proc3 = convertToLong(object);
- if (proc3 == 0) {
- SWT.error(SWT.ERROR_NO_MORE_CALLBACKS);
- }
-
- Class<?> nsMenuCls = classForName("org.eclipse.swt.internal.cocoa.NSMenu");
- Class<?> nsMenuitemCls = classForName("org.eclipse.swt.internal.cocoa.NSMenuItem");
- Class<?> nsStringCls = classForName("org.eclipse.swt.internal.cocoa.NSString");
- Class<?> nsApplicationCls = classForName("org.eclipse.swt.internal.cocoa.NSApplication");
-
- // Instead of creating a new delegate class in objective-c,
- // just use the current SWTApplicationDelegate. An instance of this
- // is a field of the Cocoa Display object and is already the target
- // for the menuItems. So just get this class and add the new methods
- // to it.
- object = invoke(osCls, "objc_lookUpClass", new Object[] {
- "SWTApplicationDelegate"
- });
- long cls = convertToLong(object);
-
- // Add the action callbacks for Preferences and About menu items.
- invoke(osCls, "class_addMethod",
- new Object[] {
- wrapPointer(cls),
- wrapPointer(mSelPreferencesMenuItemSelected),
- wrapPointer(proc3), "@:@"}); //$NON-NLS-1$
- invoke(osCls, "class_addMethod",
- new Object[] {
- wrapPointer(cls),
- wrapPointer(mSelAboutMenuItemSelected),
- wrapPointer(proc3), "@:@"}); //$NON-NLS-1$
-
- // Get the Mac OS X Application menu.
- Object sharedApplication = invoke(nsApplicationCls, "sharedApplication");
- Object mainMenu = invoke(sharedApplication, "mainMenu");
- Object mainMenuItem = invoke(nsMenuCls, mainMenu, "itemAtIndex", new Object[] {
- wrapPointer(0)
- });
- Object appMenu = invoke(mainMenuItem, "submenu");
-
- // Create the About <application-name> menu command
- Object aboutMenuItem =
- invoke(nsMenuCls, appMenu, "itemAtIndex", new Object[] {
- wrapPointer(kAboutMenuItem)
- });
- if (mAppName != null) {
- Object nsStr = invoke(nsStringCls, "stringWith", new Object[] {
- "About " + mAppName
- });
- invoke(nsMenuitemCls, aboutMenuItem, "setTitle", new Object[] {
- nsStr
- });
- }
- // Rename the quit action.
- if (mAppName != null) {
- Object quitMenuItem =
- invoke(nsMenuCls, appMenu, "itemAtIndex", new Object[] {
- wrapPointer(kQuitMenuItem)
- });
- Object nsStr = invoke(nsStringCls, "stringWith", new Object[] {
- "Quit " + mAppName
- });
- invoke(nsMenuitemCls, quitMenuItem, "setTitle", new Object[] {
- nsStr
- });
- }
-
- // Enable the Preferences menuItem.
- Object prefMenuItem =
- invoke(nsMenuCls, appMenu, "itemAtIndex", new Object[] {
- wrapPointer(kPreferencesMenuItem)
- });
- invoke(nsMenuitemCls, prefMenuItem, "setEnabled", new Object[] {
- true
- });
-
- // Set the action to execute when the About or Preferences menuItem is
- // invoked.
- //
- // We don't need to set the target here as the current target is the
- // SWTApplicationDelegate and we have registered the new selectors on
- // it. So just set the new action to invoke the selector.
- invoke(nsMenuitemCls, prefMenuItem, "setAction",
- new Object[] {
- wrapPointer(mSelPreferencesMenuItemSelected)
- });
- invoke(nsMenuitemCls, aboutMenuItem, "setAction",
- new Object[] {
- wrapPointer(mSelAboutMenuItemSelected)
- });
- }
-
- private long registerName(Class<?> osCls, String name)
- throws IllegalArgumentException, SecurityException, IllegalAccessException,
- InvocationTargetException, NoSuchMethodException {
- Object object = invoke(osCls, "sel_registerName", new Object[] {
- name
- });
- return convertToLong(object);
- }
-
- private long convertToLong(Object object) {
- if (object instanceof Integer) {
- Integer i = (Integer) object;
- return i.longValue();
- }
- if (object instanceof Long) {
- Long l = (Long) object;
- return l.longValue();
- }
- return 0;
- }
-
- private static Object wrapPointer(long value) {
- Class<?> PTR_CLASS = C.PTR_SIZEOF == 8 ? long.class : int.class;
- if (PTR_CLASS == long.class) {
- return new Long(value);
- } else {
- return new Integer((int) value);
- }
- }
-
- private static Object invoke(Class<?> clazz, String methodName, Object[] args) {
- return invoke(clazz, null, methodName, args);
- }
-
- private static Object invoke(Class<?> clazz, Object target, String methodName, Object[] args) {
- try {
- Class<?>[] signature = new Class<?>[args.length];
- for (int i = 0; i < args.length; i++) {
- Class<?> thisClass = args[i].getClass();
- if (thisClass == Integer.class)
- signature[i] = int.class;
- else if (thisClass == Long.class)
- signature[i] = long.class;
- else if (thisClass == Byte.class)
- signature[i] = byte.class;
- else if (thisClass == Boolean.class)
- signature[i] = boolean.class;
- else
- signature[i] = thisClass;
- }
- Method method = clazz.getMethod(methodName, signature);
- return method.invoke(target, args);
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
- }
-
- private Class<?> classForName(String classname) {
- try {
- Class<?> cls = Class.forName(classname);
- return cls;
- } catch (ClassNotFoundException e) {
- throw new IllegalStateException(e);
- }
- }
-
- private Object invoke(Class<?> cls, String methodName) {
- return invoke(cls, methodName, (Class<?>[]) null, (Object[]) null);
- }
-
- private Object invoke(Class<?> cls, String methodName, Class<?>[] paramTypes,
- Object... arguments) {
- try {
- Method m = cls.getDeclaredMethod(methodName, paramTypes);
- return m.invoke(null, arguments);
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
- }
-
- private Object invoke(Object obj, String methodName) {
- return invoke(obj, methodName, (Class<?>[]) null, (Object[]) null);
- }
-
- private Object invoke(Object obj, String methodName, Class<?>[] paramTypes, Object... arguments) {
- try {
- Method m = obj.getClass().getDeclaredMethod(methodName, paramTypes);
- return m.invoke(obj, arguments);
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
- }
-}
diff --git a/swtmenubar/src/com/android/menubar/IMenuBarCallback.java b/swtmenubar/src/com/android/menubar/IMenuBarCallback.java
deleted file mode 100644
index b0d6568..0000000
--- a/swtmenubar/src/com/android/menubar/IMenuBarCallback.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Eclipse Public License, Version 1.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.eclipse.org/org/documents/epl-v10.php
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.menubar;
-
-
-
-/**
- * Callbacks used by {@link IMenuBarEnhancer}.
- */
-public interface IMenuBarCallback {
- /**
- * Invoked when the About menu item is selected by the user.
- */
- abstract public void onAboutMenuSelected();
-
- /**
- * Invoked when the Preferences or Options menu item is selected by the user.
- */
- abstract public void onPreferencesMenuSelected();
-
- /**
- * Used by the enhancer implementations to report errors.
- *
- * @param format A printf-like format string.
- * @param args The parameters for the printf-like format string.
- */
- abstract public void printError(String format, Object...args);
-}
diff --git a/swtmenubar/src/com/android/menubar/IMenuBarEnhancer.java b/swtmenubar/src/com/android/menubar/IMenuBarEnhancer.java
deleted file mode 100644
index d835bd6..0000000
--- a/swtmenubar/src/com/android/menubar/IMenuBarEnhancer.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Eclipse Public License, Version 1.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.eclipse.org/org/documents/epl-v10.php
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.menubar;
-
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Menu;
-
-
-/**
- * Interface to the platform-specific MenuBarEnhancer implementation returned by
- * {@link MenuBarEnhancer#setupMenu}.
- */
-public interface IMenuBarEnhancer {
-
- /** Values that indicate how the menu bar is being handlded. */
- public enum MenuBarMode {
- /**
- * The Mac-specific About and Preferences are being used.
- * No File > Exit menu should be provided by the application.
- */
- MAC_OS,
- /**
- * The provided SWT {@link Menu} is being used for About and Options.
- * The application should provide a File > Exit menu.
- */
- GENERIC
- }
-
- /**
- * Returns a {@link MenuBarMode} enum that indicates how the menu bar is going to
- * or has been modified. This is implementation specific and can be called before or
- * after {@link #setupMenu}.
- * <p/>
- * Callers would typically call that to know if they need to hide or display
- * menu items. For example when {@link MenuBarMode#MAC_OS} is used, an app
- * would typically not need to provide any "File > Exit" menu item.
- *
- * @return One of the {@link MenuBarMode} values.
- */
- public MenuBarMode getMenuBarMode();
-
- /**
- * Updates the menu bar to provide an About menu item and a Preferences menu item.
- * Depending on the platform, the menu items might be decorated with the
- * given {@code appName}.
- * <p/>
- * Users should not call this directly.
- * {@link MenuBarEnhancer#setupMenu} should be used instead.
- *
- * @param appName Name used for the About menu item and similar. Must not be null.
- * @param display The SWT display. Must not be null.
- * @param callbacks Callbacks called when "About" and "Preferences" menu items are invoked.
- * Must not be null.
- */
- public void setupMenu(
- String appName,
- Display display,
- IMenuBarCallback callbacks);
-}
diff --git a/swtmenubar/src/com/android/menubar/MenuBarEnhancer.java b/swtmenubar/src/com/android/menubar/MenuBarEnhancer.java
deleted file mode 100644
index 7575ecd..0000000
--- a/swtmenubar/src/com/android/menubar/MenuBarEnhancer.java
+++ /dev/null
@@ -1,241 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Eclipse Public License, Version 1.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.eclipse.org/org/documents/epl-v10.php
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.menubar;
-
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.action.IMenuManager;
-import org.eclipse.jface.action.Separator;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Menu;
-import org.eclipse.swt.widgets.MenuItem;
-
-import com.android.menubar.IMenuBarEnhancer.MenuBarMode;
-
-
-/**
- * On Mac, {@link MenuBarEnhancer#setupMenu} plugs a listener on the About and the
- * Preferences menu items of the standard "application" menu in the menu bar.
- * On Windows or Linux, it adds relevant items to a given {@link Menu} linked to
- * the same listeners.
- */
-public final class MenuBarEnhancer {
-
- private MenuBarEnhancer() {
- }
-
- /**
- * Creates an instance of {@link IMenuBarEnhancer} specific to the current platform
- * and invoke its {@link IMenuBarEnhancer#setupMenu} to updates the menu bar.
- * <p/>
- * Depending on the platform, this will either hook into the existing About menu item
- * and a Preferences or Options menu item or add new ones to the given {@code swtMenu}.
- * Depending on the platform, the menu items might be decorated with the
- * given {@code appName}.
- * <p/>
- * Potential errors are reported through {@link IMenuBarCallback}.
- *
- * @param appName Name used for the About menu item and similar. Must not be null.
- * @param swtMenu For non-mac platform this is the menu where the "About" and
- * the "Options" menu items are created. Typically the menu might be
- * called "Tools". Must not be null.
- * @param callbacks Callbacks called when "About" and "Preferences" menu items are invoked.
- * Must not be null.
- * @return An actual {@link IMenuBarEnhancer} implementation. Can be null on failure.
- * This is currently not of any use for the caller but is left in case
- * we want to expand the functionality later.
- */
- public static IMenuBarEnhancer setupMenu(
- String appName,
- final Menu swtMenu,
- IMenuBarCallback callbacks) {
-
- IMenuBarEnhancer enhancer = getEnhancer(callbacks, swtMenu.getDisplay());
-
- // Default implementation for generic platforms
- if (enhancer == null) {
- enhancer = getGenericEnhancer(swtMenu);
- }
-
- try {
- enhancer.setupMenu(appName, swtMenu.getDisplay(), callbacks);
- } catch (Exception e) {
- // If the enhancer failed, try to fall back on the generic one
- if (enhancer.getMenuBarMode() != MenuBarMode.GENERIC) {
- enhancer = getGenericEnhancer(swtMenu);
- try {
- enhancer.setupMenu(appName, swtMenu.getDisplay(), callbacks);
- } catch (Exception e2) {
- callbacks.printError("SWTMenuBar failed: %s", e2.toString());
- return null;
- }
- }
- }
- return enhancer;
- }
-
- private static IMenuBarEnhancer getGenericEnhancer(final Menu swtMenu) {
- IMenuBarEnhancer enhancer;
- enhancer = new IMenuBarEnhancer() {
-
- public MenuBarMode getMenuBarMode() {
- return MenuBarMode.GENERIC;
- }
-
- public void setupMenu(
- String appName,
- Display display,
- final IMenuBarCallback callbacks) {
- if (swtMenu.getItemCount() > 0) {
- new MenuItem(swtMenu, SWT.SEPARATOR);
- }
-
- // Note: we use "Preferences" on Mac and "Options" on Windows/Linux.
- final MenuItem pref = new MenuItem(swtMenu, SWT.NONE);
- pref.setText("&Options...");
-
- final MenuItem about = new MenuItem(swtMenu, SWT.NONE);
- about.setText("&About...");
-
- pref.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- try {
- pref.setEnabled(false);
- callbacks.onPreferencesMenuSelected();
- super.widgetSelected(e);
- } finally {
- pref.setEnabled(true);
- }
- }
- });
-
- about.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- try {
- about.setEnabled(false);
- callbacks.onAboutMenuSelected();
- super.widgetSelected(e);
- } finally {
- about.setEnabled(true);
- }
- }
- });
- }
- };
- return enhancer;
- }
-
-
- public static IMenuBarEnhancer setupMenuManager(
- String appName,
- Display display,
- final IMenuManager menuManager,
- final IAction aboutAction,
- final IAction preferencesAction,
- final IAction quitAction) {
-
- IMenuBarCallback callbacks = new IMenuBarCallback() {
- public void printError(String format, Object... args) {
- System.err.println(String.format(format, args));
- }
-
- public void onPreferencesMenuSelected() {
- if (preferencesAction != null) {
- preferencesAction.run();
- }
- }
-
- public void onAboutMenuSelected() {
- if (aboutAction != null) {
- aboutAction.run();
- }
- }
- };
-
- IMenuBarEnhancer enhancer = getEnhancer(callbacks, display);
-
- // Default implementation for generic platforms
- if (enhancer == null) {
- enhancer = new IMenuBarEnhancer() {
-
- public MenuBarMode getMenuBarMode() {
- return MenuBarMode.GENERIC;
- }
-
- public void setupMenu(
- String appName,
- Display display,
- final IMenuBarCallback callbacks) {
- if (!menuManager.isEmpty()) {
- menuManager.add(new Separator());
- }
-
- if (aboutAction != null) {
- menuManager.add(aboutAction);
- }
- if (preferencesAction != null) {
- menuManager.add(preferencesAction);
- }
- if (quitAction != null) {
- if (aboutAction != null || preferencesAction != null) {
- menuManager.add(new Separator());
- }
- menuManager.add(quitAction);
- }
- }
- };
- }
-
- enhancer.setupMenu(appName, display, callbacks);
- return enhancer;
- }
-
- private static IMenuBarEnhancer getEnhancer(IMenuBarCallback callbacks, Display display) {
- IMenuBarEnhancer enhancer = null;
- String p = SWT.getPlatform();
- String className = null;
- if ("cocoa".equals(p)) { //$NON-NLS-1$
- className = "com.android.menubar.internal.MenuBarEnhancerCocoa"; //$NON-NLS-1$
-
- if (SWT.getVersion() >= 3700 && MenuBarEnhancer37.isSupported(display)) {
- className = MenuBarEnhancer37.class.getName();
- }
- }
-
- if (System.getenv("DEBUG_SWTMENUBAR") != null) {
- callbacks.printError("DEBUG SwtMenuBar: SWT=%1$s, class=%2$s", p, className);
- }
-
- if (className != null) {
- try {
- Class<?> clazz = Class.forName(className);
- enhancer = (IMenuBarEnhancer) clazz.newInstance();
- } catch (Exception e) {
- // Log an error and fallback on the default implementation.
- callbacks.printError(
- "Failed to instantiate %1$s: %2$s", //$NON-NLS-1$
- className,
- e.toString());
- }
- }
- return enhancer;
- }
-}
diff --git a/swtmenubar/src/com/android/menubar/MenuBarEnhancer37.java b/swtmenubar/src/com/android/menubar/MenuBarEnhancer37.java
deleted file mode 100644
index 8560bfa..0000000
--- a/swtmenubar/src/com/android/menubar/MenuBarEnhancer37.java
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
- * Copyright (C) 2012 The Android Open Source Project
- *
- * Licensed under the Eclipse Public License, Version 1.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.eclipse.org/org/documents/epl-v10.php
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * References:
- * Based on the SWT snippet example at
- * http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet354.java?view=co
- */
-
-package com.android.menubar;
-
-
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Menu;
-import org.eclipse.swt.widgets.MenuItem;
-
-import java.lang.reflect.Method;
-
-public class MenuBarEnhancer37 implements IMenuBarEnhancer {
-
- private static final int kAboutMenuItem = -1; // SWT.ID_ABOUT in SWT 3.7
- private static final int kPreferencesMenuItem = -2; // SWT.ID_PREFERENCES in SWT 3.7
- private static final int kQuitMenuItem = -6; // SWT.ID_QUIT in SWT 3.7
-
- public MenuBarEnhancer37() {
- }
-
- public MenuBarMode getMenuBarMode() {
- return MenuBarMode.MAC_OS;
- }
-
- /**
- * Setup the About and Preferences native menut items with the
- * given application name and links them to the callback.
- *
- * @param appName The application name.
- * @param display The SWT display. Must not be null.
- * @param callbacks The callbacks invoked by the menus.
- */
- public void setupMenu(
- String appName,
- Display display,
- IMenuBarCallback callbacks) {
-
- try {
- // Initialize the menuItems.
- initialize(display, appName, callbacks);
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
-
- // Schedule disposal of callback object
- display.disposeExec(new Runnable() {
- public void run() {
- }
- });
- }
-
- /**
- * Checks whether the required SWT 3.7 APIs are available.
- * <br/>
- * Calling this will load the class, which is OK since this class doesn't
- * directly use any SWT 3.7 API -- instead it uses reflection so that the
- * code can be loaded under SWT 3.6.
- *
- * @param display The current SWT display.
- * @return True if the SWT 3.7 API are available and this enhancer can be used.
- */
- public static boolean isSupported(Display display) {
- try {
- Object sysMenu = call0(display, "getSystemMenu");
- if (sysMenu instanceof Menu) {
- return findMenuById((Menu)sysMenu, kPreferencesMenuItem) != null &&
- findMenuById((Menu)sysMenu, kAboutMenuItem) != null;
- }
- } catch (Exception ignore) {}
- return false;
- }
-
- private void initialize(
- Display display,
- String appName,
- final IMenuBarCallback callbacks)
- throws Exception {
- Object sysMenu = call0(display, "getSystemMenu");
- if (sysMenu instanceof Menu) {
- MenuItem menu = findMenuById((Menu)sysMenu, kPreferencesMenuItem);
- if (menu != null) {
- menu.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent event) {
- callbacks.onPreferencesMenuSelected();
- }
- });
- }
-
- menu = findMenuById((Menu)sysMenu, kAboutMenuItem);
- if (menu != null) {
- menu.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent event) {
- callbacks.onAboutMenuSelected();
- }
- });
- menu.setText("About " + appName);
- }
-
- menu = findMenuById((Menu)sysMenu, kQuitMenuItem);
- if (menu != null) {
- // We already support the "quit" operation, no need for an extra handler here.
- menu.setText("Quit " + appName);
- }
-
- }
- }
-
- private static Object call0(Object obj, String method) {
- try {
- Method m = obj.getClass().getMethod(method, (Class<?>[])null);
- if (m != null) {
- return m.invoke(obj, (Object[])null);
- }
- } catch (Exception ignore) {}
- return null;
- }
-
- private static MenuItem findMenuById(Menu menu, int id) {
- MenuItem[] items = menu.getItems();
- for (int i = items.length - 1; i >= 0; i--) {
- MenuItem item = items[i];
- Object menuId = call0(item, "getID");
- if (menuId instanceof Integer) {
- if (((Integer) menuId).intValue() == id) {
- return item;
- }
- }
- }
- return null;
- }
-}