michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.widget; michael@0: michael@0: import org.mozilla.gecko.menu.GeckoMenu; michael@0: import org.mozilla.gecko.menu.GeckoMenuInflater; michael@0: import org.mozilla.gecko.menu.MenuPanel; michael@0: import org.mozilla.gecko.menu.MenuPopup; michael@0: michael@0: import android.content.Context; michael@0: import android.view.Menu; michael@0: import android.view.MenuInflater; michael@0: import android.view.MenuItem; michael@0: import android.view.View; michael@0: michael@0: /** michael@0: * A PopupMenu that uses the custom GeckoMenu. This menu is michael@0: * usually tied to an anchor, and show as a dropdrown from the anchor. michael@0: */ michael@0: public class GeckoPopupMenu implements GeckoMenu.Callback, michael@0: GeckoMenu.MenuPresenter { michael@0: michael@0: // An interface for listeners for dismissal. michael@0: public static interface OnDismissListener { michael@0: public boolean onDismiss(GeckoMenu menu); michael@0: } michael@0: michael@0: // An interface for listeners for menu item click events. michael@0: public static interface OnMenuItemClickListener { michael@0: public boolean onMenuItemClick(MenuItem item); michael@0: } michael@0: michael@0: private View mAnchor; michael@0: michael@0: private MenuPopup mMenuPopup; michael@0: private MenuPanel mMenuPanel; michael@0: michael@0: private GeckoMenu mMenu; michael@0: private GeckoMenuInflater mMenuInflater; michael@0: michael@0: private OnDismissListener mDismissListener; michael@0: private OnMenuItemClickListener mClickListener; michael@0: michael@0: public GeckoPopupMenu(Context context) { michael@0: initialize(context, null); michael@0: } michael@0: michael@0: public GeckoPopupMenu(Context context, View anchor) { michael@0: initialize(context, anchor); michael@0: } michael@0: michael@0: /** michael@0: * This method creates an empty menu and attaches the necessary listeners. michael@0: * If an anchor is supplied, it is stored as well. michael@0: */ michael@0: private void initialize(Context context, View anchor) { michael@0: mMenu = new GeckoMenu(context, null); michael@0: mMenu.setCallback(this); michael@0: mMenu.setMenuPresenter(this); michael@0: mMenuInflater = new GeckoMenuInflater(context); michael@0: michael@0: mMenuPopup = new MenuPopup(context); michael@0: mMenuPanel = new MenuPanel(context, null); michael@0: michael@0: mMenuPanel.addView(mMenu); michael@0: mMenuPopup.setPanelView(mMenuPanel); michael@0: michael@0: setAnchor(anchor); michael@0: } michael@0: michael@0: /** michael@0: * Returns the menu that is current being shown. michael@0: * michael@0: * @return The menu being shown. michael@0: */ michael@0: public Menu getMenu() { michael@0: return mMenu; michael@0: } michael@0: michael@0: /** michael@0: * Returns the menu inflater that was used to create the menu. michael@0: * michael@0: * @return The menu inflater used. michael@0: */ michael@0: public MenuInflater getMenuInflater() { michael@0: return mMenuInflater; michael@0: } michael@0: michael@0: /** michael@0: * Inflates a menu resource to the menu using the menu inflater. michael@0: * michael@0: * @param menuRes The menu resource to be inflated. michael@0: */ michael@0: public void inflate(int menuRes) { michael@0: if (menuRes > 0) { michael@0: mMenuInflater.inflate(menuRes, mMenu); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Set a different anchor after the menu is inflated. michael@0: * michael@0: * @param anchor The new anchor for the popup. michael@0: */ michael@0: public void setAnchor(View anchor) { michael@0: mAnchor = anchor; michael@0: } michael@0: michael@0: public void setOnDismissListener(OnDismissListener listener) { michael@0: mDismissListener = listener; michael@0: } michael@0: michael@0: public void setOnMenuItemClickListener(OnMenuItemClickListener listener) { michael@0: mClickListener = listener; michael@0: } michael@0: michael@0: /** michael@0: * Show the inflated menu. michael@0: */ michael@0: public void show() { michael@0: if (!mMenuPopup.isShowing()) michael@0: mMenuPopup.showAsDropDown(mAnchor); michael@0: } michael@0: michael@0: /** michael@0: * Hide the inflated menu. michael@0: */ michael@0: public void dismiss() { michael@0: if (mMenuPopup.isShowing()) { michael@0: mMenuPopup.dismiss(); michael@0: michael@0: if (mDismissListener != null) michael@0: mDismissListener.onDismiss(mMenu); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public boolean onMenuItemSelected(MenuItem item) { michael@0: if (mClickListener != null) michael@0: return mClickListener.onMenuItemClick(item); michael@0: michael@0: return false; michael@0: } michael@0: michael@0: @Override michael@0: public void openMenu() { michael@0: show(); michael@0: } michael@0: michael@0: @Override michael@0: public void showMenu(View menu) { michael@0: mMenuPanel.removeAllViews(); michael@0: mMenuPanel.addView(menu); michael@0: michael@0: openMenu(); michael@0: } michael@0: michael@0: @Override michael@0: public void closeMenu() { michael@0: dismiss(); michael@0: } michael@0: }